|
-
Nov 9th, 2000, 11:21 PM
#1
Thread Starter
New Member
Hi everybody! I have an assignment due for Monday and I really can't get it (I'm just a beginner, so please don't mock) Um, well, you've all played the game MasterMind, right? With the hidden color scheme and the other person has to guess, and you tell them how many colors they've got right and how many positions they've got right? Right? Well, for this assignment, I have to write a program that generates three hidden random colors from a set of four colors (red, blue, green, yellow) at run-time and then allows the user enter three guesses in three text boxes. The form then displays how many colors they have right and how many positions they have right. Any suggestions?
-
Nov 10th, 2000, 06:19 AM
#2
Fanatic Member
Basically, you'll do this:
1) Pick 3 random numbers, between 1 and 4. These are your 'hidden colors'
2) Have the user guess the numbers.
If the first guess matches the first hidden number, add 1 to the 'CorrectPosition' counter, but if it matches a hidden number in a different slot (like guess 1 = hidden 2) then add 1 to the 'CorrectColor' Counter.
Note: Remember to set CorrectPosition and CorrectColor = 0 before each round's check.
3) If the 'CorrectPosition' counter = 3 then we have a winner.
more...
This method will produce the following:
[code]
hidden - 1 3 2
---------------------- CorrectPosition CorrectColor
guess 1 - 4 4 4 0 0
guess 2 - 3 3 3 1 3
Notice that CorrectColor = 3 because all three matches 1 hidden color.
I'll leave that to you to fix
r0ach™
Don't forget to rate the post
-
Jan 2nd, 2003, 02:56 AM
#3
New Member
Originally posted by r0ach
Basically, you'll do this:
1) Pick 3 random numbers, between 1 and 4. These are your 'hidden colors'
2) Have the user guess the numbers.
If the first guess matches the first hidden number, add 1 to the 'CorrectPosition' counter, but if it matches a hidden number in a different slot (like guess 1 = hidden 2) then add 1 to the 'CorrectColor' Counter.
Note: Remember to set CorrectPosition and CorrectColor = 0 before each round's check.
3) If the 'CorrectPosition' counter = 3 then we have a winner.
more...
This method will produce the following:
[code]
hidden - 1 3 2
---------------------- CorrectPosition CorrectColor
guess 1 - 4 4 4 0 0
guess 2 - 3 3 3 1 3
Notice that CorrectColor = 3 because all three matches 1 hidden color.
I'll leave that to you to fix
Anyone knows how to fix the "CorrectColor" ? Been thinking of it for some time but can't figure it out. Sorry for being stupid.
-
Jan 2nd, 2003, 03:31 AM
#4
KING BODWAD XXI
Hmmm i think you mean the line
---------------------- CorrectPosition CorrectColor
in which case you need a comment mark or ' so you need the line
'---------------------- CorrectPosition CorrectColor
it should turn green and prevent the compiler from reading it when compiling
-
Jan 2nd, 2003, 08:17 AM
#5
New Member
Thankz BodwadUK for your input. But you get me wrong.
What I mean is....
Lets say the "Hidden Code" is 1234.
If the player input "1 1 1 1"
It will Prompt the number "1" is the Correct Number and in the Correct Position while the other three "1"s is the Correct Number but different position.
The code i'm using...
If InputBox1.Text = number1 Then
Shape1.BackColor = vbGreen
ElseIf InputBox2.Text = number2 Then
Shape1.BackColour = vbYellow
And so on....
-------------------------------------------------
If InputBox2.Text = number2 Then
Shape1.BackColor = vbGreen
ElseIf InputBox2.Text = number1 Then <--------- Thats The Part!
Shape1.BackColour = vbYellow
-------------------------------------------------
Clear enough everyone? Thankz
Edit: The colour of the shapes will changed to "GREEN" if the position and number is right. And the colour of the shapes will be changed to "YELLOW" if the number is correct and position is wrong
Last edited by TikoWeide; Jan 2nd, 2003 at 08:32 AM.
-
Jan 2nd, 2003, 08:45 AM
#6
Use 2 booleans per hidden number. 1 boolean should be true is the number is found, not at the correct place, the other should be true if found at the correct place. Then loop again, is all 4 the "correct place" booleans are true, the code is found,
-
Jan 2nd, 2003, 08:55 AM
#7
Frenzied Member
Check each number if it is on the rigth position.
If it is set its backcolor to green.
Then check the others where the backcolor isn't green .
if backcolor of shape 2 isn't green then number 2 isn't on the right position.
Check each one (no green or yellow backcolor ) against each number where the backolor of referenced number isn't colored yet.
This way you should get round the problem.
Did you really think two years over this prob. ???
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Jan 2nd, 2003, 10:14 PM
#8
New Member
Originally posted by swatty
Check each number if it is on the rigth position.
If it is set its backcolor to green.
Then check the others where the backcolor isn't green .
if backcolor of shape 2 isn't green then number 2 isn't on the right position.
Check each one (no green or yellow backcolor ) against each number where the backolor of referenced number isn't colored yet.
This way you should get round the problem.
Did you really think two years over this prob. ???
Thankz Lightning. But I'm not sure how to use Boolean.
Thankz swatty. Ya I spend this years plus on this problem but still can't solve it.
I don't really understand you... or may i say iI don't understand you at all. Could you explain a little clearer?
-
Jan 3rd, 2003, 03:16 AM
#9
KING BODWAD XXI
To Use Boolean
Dim BlnTest as Boolean
BlnTest = true
BlnTest = False
Its That Simple, Default is False
You Could Use An Array Though
Dim strArray(3) as byte 'Remember Arrays Start at 0
0 = Incorrect
1 = Right Color Wrong Place
2 = Correct
Check Each Time For All Elements to be 2
-
Jan 3rd, 2003, 05:06 AM
#10
Frenzied Member
Originally posted by TikoWeide
Thankz swatty. Ya I spend this years plus on this problem but still can't solve it.
I don't really understand you... or may i say iI don't understand you at all. Could you explain a little clearer?
this is how i've done it.
VB Code:
Public table(11, 4) As Long
' table array 0 is filled with hidden colors
' 1 with colors of first row
Public Function checkpoint() As Integer
Dim i As Integer
Dim j As Integer
Dim strControl As String
Dim geteld(4) As String
checkpoint = 0
strControl = "Result" & (iShape - 1)
For i = 0 To 3
If table(iShape - 1, i) = table(0, i) Then
geteld(i) = "X-1" ' keep this one counted on correct place
frmMastermind.Controls([strControl])(checkpoint).FillColor = vbWhite
checkpoint = checkpoint + 1 ' keep number of correct ones
End If
Next
For i = 0 To 3
If geteld(i) <> "X-1" Then
For j = 0 To 3
If table(iShape - 1, i) = table(0, j) Then ' check if exists
If (geteld(j) = "") Then
geteld(j) = "X-2" ' keep this one as counted
j = 4
End If
End If
Next
End If
Next
i = checkpoint
For j = 0 To 3
If geteld(j) = "X-2" Then
frmMastermind.Controls([strControl])(i).FillColor = vbYellow
i = i + 1
End If
Next
Do Until i > 3
frmMastermind.Controls([strControl])(i).FillColor = vbBlack
i = i + 1
Loop
End Function
' i made 11 shape control arrays from 0-3
'so i can run throug them after all four shapes are filed
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Jan 6th, 2003, 03:09 AM
#11
New Member
Originally posted by swatty
this is how i've done it.
Your code is way too difficult for me to understand. Mind taking a look at mine?
VB Code:
Private Sub Go_Click()
If Text1.Text = Num1 Then
Shape1.BackColor = vbGreen
ElseIf Text1.Text = Num2 Then
Shape1.BackColor = vbYellow
ElseIf Text1.Text = Num3 Then
Shape1.BackColor = vbYellow
ElseIf Text1.Text = Num4 Then
Shape1.BackColor = vbYellow
Else: Shape1.BackColor = vbRed
End If
If Text2.Text = Num2 Then
Shape2.BackColor = vbGreen
ElseIf Text2.Text = Num1 Then
Shape2.BackColor = vbYellow
ElseIf Text2.Text = Num3 Then
Shape2.BackColor = vbYellow
ElseIf Text2.Text = Num4 Then
Shape2.BackColor = vbYellow
Else: Shape2.BackColor = vbRed
End If
If Text3.Text = Num3 Then
Shape3.BackColor = vbGreen
ElseIf Text3.Text = Num1 Then
Shape3.BackColor = vbYellow
ElseIf Text3.Text = Num2 Then
Shape3.BackColor = vbYellow
ElseIf Text3.Text = Num4 Then
Shape3.BackColor = vbYellow
Else: Shape3.BackColor = vbRed
End If
If Text4.Text = Num4 Then
Shape4.BackColor = vbGreen
ElseIf Text4.Text = Num1 Then
Shape4.BackColor = vbYellow
ElseIf Text4.Text = Num2 Then
Shape4.BackColor = vbYellow
ElseIf Text4.Text = Num3 Then
Shape4.BackColor = vbYellow
Else: Shape4.BackColor = vbRed
End If
Label6.Caption = Text1.Text 'Display Previous Input
Label7.Caption = Text2.Text
Label8.Caption = Text3.Text
Label9.Caption = Text4.Text
TriesAttempted = TriesAttempted + 1
If (TriesAttempted = 10) And ((Text1.Text <> Num1) Or (Text2.Text <> Num2) Or (Text3.Text <> Num3) Or (Text4.Text <> Num4)) Then
MsgBox "I'm So Surprise That You Couldn't Break The Code Within 10 Tries", , ""
MsgBox "Well, Try Again. Better Luck Next Time! =)", vbExclamation, "Game Over"
End If
If (Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4) Then
MsgBox "Congratulations! You Won!", vbExclamation, "Game Over"
End If
If ((Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4)) Or (TriesAttempted = 10) Then
Go.Enabled = False
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command8.Enabled = False
Command9.Enabled = False
Command10.Enabled = False
Delete.Enabled = False
End If
Text1.Text = "" 'Clear Textboxes
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
-
Jan 8th, 2003, 03:15 AM
#12
Frenzied Member
If you tried it out you'd see it won't work quite well.
If numbers are 1,4,3,2
user puts in 4,4,4,4
then colors will be yellow, green,yellow,yellow
I'll try to set it right when i get a bit time left.
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
-
Jan 8th, 2003, 07:25 AM
#13
Frenzied Member
VB Code:
Private Sub Go_Click()
'*********MODIFICATIONS MADE BY S
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim c As Control
'for better use i made a control array of the shapes
'Use the tag property to know it is already used, reset them to 0
Shape(0).Tag = 0
Shape(1).Tag = 0
Shape(2).Tag = 0
Shape(3).Tag = 0
'first test if they are in the right plase
If Text1.Text = Num1 Then Shape(0).Tag = 1
If Text2.Text = Num2 Then Shape(1).Tag = 1
If Text3.Text = Num3 Then Shape(2).Tag = 1
If Text4.Text = Num4 Then Shape(3).Tag = 1
'test if is there but not on right place
For i = 0 To 3
If Shape(i).Tag <> 1 Then
Select Case i
Case 0: Set c = Text1
Case 1: Set c = Text2
Case 2: Set c = Text3
Case 3: Set c = Text4
End Select
For j = 0 To 3
Select Case j
Case 0: n = Num1
Case 1: n = Num2
Case 2: n = Num3
Case 3: n = Num4
End Select
If c.Text = n Then ' check if exists
If Shape(j).Tag = 0 Then
Shape(j).Tag = 2 ' keep this one as counted
j = 4
End If
End If
Next
End If
Next
For j = 0 To 3
If Shape(j).Tag = 1 Then Shape(j).BackColor = vbGreen
If Shape(j).Tag = 2 Then Shape(j).BackColor = vbYellow
If Shape(j).Tag = 0 Then Shape(j).BackColor = vbRed
Next
'END OF MODIFICATIONS MADE BY S
Label6.Caption = Text1.Text 'Display Previous Input
Label7.Caption = Text2.Text
Label8.Caption = Text3.Text
Label9.Caption = Text4.Text
TriesAttempted = TriesAttempted + 1
If (TriesAttempted = 10) And ((Text1.Text <> Num1) Or (Text2.Text <> Num2) Or (Text3.Text <> Num3) Or (Text4.Text <> Num4)) Then
MsgBox "I'm So Surprise That You Couldn't Break The Code Within 10 Tries", , ""
MsgBox "Well, Try Again. Better Luck Next Time! =)", vbExclamation, "Game Over"
End If
If (Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4) Then
MsgBox "Congratulations! You Won!", vbExclamation, "Game Over"
End If
If ((Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4)) Or (TriesAttempted = 10) Then
Go.Enabled = False
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = False
Command7.Enabled = False
Command8.Enabled = False
Command9.Enabled = False
Command10.Enabled = False
Delete.Enabled = False
End If
Text1.Text = "" 'Clear Textboxes
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
End Sub
Last edited by swatty; Jan 8th, 2003 at 07:32 AM.
Code:
If Question = Incomplete Then
AnswerNextOne
Else
ReplyIfKnown
End If
cu Swatty
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|