Results 1 to 13 of 13

Thread: Help with Mastermind application

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2000
    Location
    Canada
    Posts
    2

    Question

    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?

  2. #2
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722
    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

  3. #3
    New Member
    Join Date
    Jan 2003
    Posts
    4
    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.

  4. #4
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  5. #5
    New Member
    Join Date
    Jan 2003
    Posts
    4
    Originally posted by BodwadUK
    Hmmm i think you mean the line 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
    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.

  6. #6
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 2002
    Location
    Eygelshoven
    Posts
    1,611
    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,

  7. #7
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    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

  8. #8
    New Member
    Join Date
    Jan 2003
    Posts
    4
    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?

  9. #9
    KING BODWAD XXI BodwadUK's Avatar
    Join Date
    Aug 2002
    Location
    Nottingham
    Posts
    2,176
    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
    If you dribble then you are as mad as me

    Lost World Creations Website (XBOX Indie games)
    Lene Marlin

  10. #10
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    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:
    1. Public table(11, 4) As Long
    2. ' table array 0 is filled with hidden colors
    3. ' 1 with colors of first row
    4. Public Function checkpoint() As Integer
    5.    Dim i As Integer
    6.    Dim j As Integer
    7.    Dim strControl As String
    8.    Dim geteld(4) As String
    9.    
    10.    checkpoint = 0
    11.    strControl = "Result" & (iShape - 1)
    12.    For i = 0 To 3
    13.      If table(iShape - 1, i) = table(0, i) Then
    14.          geteld(i) = "X-1" ' keep this one counted on correct place
    15.          frmMastermind.Controls([strControl])(checkpoint).FillColor = vbWhite
    16.          checkpoint = checkpoint + 1   ' keep number of correct  ones
    17.       End If
    18.    Next
    19.    
    20.    For i = 0 To 3
    21.       If geteld(i) <> "X-1" Then
    22.          For j = 0 To 3
    23.             If table(iShape - 1, i) = table(0, j) Then   ' check if exists
    24.                If (geteld(j) = "") Then
    25.                   geteld(j) = "X-2" ' keep this one as counted
    26.                    j = 4
    27.                End If
    28.             End If
    29.          Next
    30.       End If
    31.    Next
    32.    
    33.    i = checkpoint
    34.    For j = 0 To 3
    35.       If geteld(j) = "X-2" Then
    36.          frmMastermind.Controls([strControl])(i).FillColor = vbYellow
    37.          i = i + 1
    38.       End If
    39.    Next
    40.    
    41.    Do Until i > 3
    42.       frmMastermind.Controls([strControl])(i).FillColor = vbBlack
    43.       i = i + 1
    44.    Loop
    45. End Function
    46. ' i made 11 shape control arrays from 0-3
    47. 'so i can run throug them after all four shapes are filed
    Code:
    If Question = Incomplete Then
       AnswerNextOne
    Else
       ReplyIfKnown
    End If
    cu Swatty

  11. #11
    New Member
    Join Date
    Jan 2003
    Posts
    4
    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:
    1. Private Sub Go_Click()
    2.  
    3. If Text1.Text = Num1 Then
    4.     Shape1.BackColor = vbGreen
    5. ElseIf Text1.Text = Num2 Then
    6.     Shape1.BackColor = vbYellow
    7. ElseIf Text1.Text = Num3 Then
    8.     Shape1.BackColor = vbYellow
    9. ElseIf Text1.Text = Num4 Then
    10.     Shape1.BackColor = vbYellow
    11. Else: Shape1.BackColor = vbRed
    12. End If
    13.  
    14. If Text2.Text = Num2 Then
    15.     Shape2.BackColor = vbGreen
    16. ElseIf Text2.Text = Num1 Then
    17.     Shape2.BackColor = vbYellow
    18. ElseIf Text2.Text = Num3 Then
    19.     Shape2.BackColor = vbYellow
    20. ElseIf Text2.Text = Num4 Then
    21.     Shape2.BackColor = vbYellow
    22. Else: Shape2.BackColor = vbRed
    23. End If
    24.  
    25. If Text3.Text = Num3 Then
    26.     Shape3.BackColor = vbGreen
    27. ElseIf Text3.Text = Num1 Then
    28.     Shape3.BackColor = vbYellow
    29. ElseIf Text3.Text = Num2 Then
    30.     Shape3.BackColor = vbYellow
    31. ElseIf Text3.Text = Num4 Then
    32.     Shape3.BackColor = vbYellow
    33. Else: Shape3.BackColor = vbRed
    34. End If
    35.  
    36. If Text4.Text = Num4 Then
    37.     Shape4.BackColor = vbGreen
    38. ElseIf Text4.Text = Num1 Then
    39.     Shape4.BackColor = vbYellow
    40. ElseIf Text4.Text = Num2 Then
    41.     Shape4.BackColor = vbYellow
    42. ElseIf Text4.Text = Num3 Then
    43.     Shape4.BackColor = vbYellow
    44. Else: Shape4.BackColor = vbRed
    45. End If
    46.  
    47. Label6.Caption = Text1.Text 'Display Previous Input
    48. Label7.Caption = Text2.Text
    49. Label8.Caption = Text3.Text
    50. Label9.Caption = Text4.Text
    51.  
    52. TriesAttempted = TriesAttempted + 1
    53. If (TriesAttempted = 10) And ((Text1.Text <> Num1) Or (Text2.Text <> Num2) Or (Text3.Text <> Num3) Or (Text4.Text <> Num4)) Then
    54.     MsgBox "I'm So Surprise That You Couldn't Break The Code Within 10 Tries", , ""
    55.     MsgBox "Well, Try Again. Better Luck Next Time! =)", vbExclamation, "Game Over"
    56. End If
    57.  
    58. If (Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4) Then
    59.     MsgBox "Congratulations! You Won!", vbExclamation, "Game Over"
    60. End If
    61.  
    62. If ((Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4)) Or (TriesAttempted = 10) Then
    63.     Go.Enabled = False
    64.     Command1.Enabled = False
    65.     Command2.Enabled = False
    66.     Command3.Enabled = False
    67.     Command4.Enabled = False
    68.     Command5.Enabled = False
    69.     Command6.Enabled = False
    70.     Command7.Enabled = False
    71.     Command8.Enabled = False
    72.     Command9.Enabled = False
    73.     Command10.Enabled = False
    74.     Delete.Enabled = False
    75. End If
    76.  
    77. Text1.Text = "" 'Clear Textboxes
    78. Text2.Text = ""
    79. Text3.Text = ""
    80. Text4.Text = ""
    81. End Sub

  12. #12
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    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

  13. #13
    Frenzied Member swatty's Avatar
    Join Date
    Aug 2002
    Location
    somewhere on earth
    Posts
    1,478
    VB Code:
    1. Private Sub Go_Click()
    2.    '*********MODIFICATIONS MADE BY S
    3.    Dim i As Integer
    4.    Dim j As Integer
    5.    Dim n As Integer
    6.    Dim c As Control
    7.    'for better use i made a control array of the shapes
    8.    'Use the tag property to know it is already used, reset them to 0
    9.    Shape(0).Tag = 0
    10.    Shape(1).Tag = 0
    11.    Shape(2).Tag = 0
    12.    Shape(3).Tag = 0
    13.    
    14.    'first test if they are in the right plase
    15.    If Text1.Text = Num1 Then Shape(0).Tag = 1
    16.    If Text2.Text = Num2 Then Shape(1).Tag = 1
    17.    If Text3.Text = Num3 Then Shape(2).Tag = 1
    18.    If Text4.Text = Num4 Then Shape(3).Tag = 1
    19.    
    20.    'test if is there but not on right place
    21.    For i = 0 To 3
    22.       If Shape(i).Tag <> 1 Then
    23.          Select Case i
    24.             Case 0: Set c = Text1
    25.             Case 1: Set c = Text2
    26.             Case 2: Set c = Text3
    27.             Case 3: Set c = Text4
    28.          End Select
    29.            
    30.          For j = 0 To 3
    31.             Select Case j
    32.                Case 0: n = Num1
    33.                Case 1: n = Num2
    34.                Case 2: n = Num3
    35.                Case 3: n = Num4
    36.             End Select
    37.             If c.Text = n Then   ' check if exists
    38.                If Shape(j).Tag = 0 Then
    39.                   Shape(j).Tag = 2 ' keep this one as counted
    40.                   j = 4
    41.                End If
    42.             End If
    43.          Next
    44.       End If
    45.    Next
    46.    
    47.    For j = 0 To 3
    48.       If Shape(j).Tag = 1 Then Shape(j).BackColor = vbGreen
    49.       If Shape(j).Tag = 2 Then Shape(j).BackColor = vbYellow
    50.       If Shape(j).Tag = 0 Then Shape(j).BackColor = vbRed
    51.    Next
    52.    'END OF MODIFICATIONS MADE BY S
    53.    Label6.Caption = Text1.Text 'Display Previous Input
    54.    Label7.Caption = Text2.Text
    55.    Label8.Caption = Text3.Text
    56.    Label9.Caption = Text4.Text
    57.    
    58.    TriesAttempted = TriesAttempted + 1
    59.    If (TriesAttempted = 10) And ((Text1.Text <> Num1) Or (Text2.Text <> Num2) Or (Text3.Text <> Num3) Or (Text4.Text <> Num4)) Then
    60.        MsgBox "I'm So Surprise That You Couldn't Break The Code Within 10 Tries", , ""
    61.        MsgBox "Well, Try Again. Better Luck Next Time! =)", vbExclamation, "Game Over"
    62.    End If
    63.    
    64.    If (Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4) Then
    65.        MsgBox "Congratulations! You Won!", vbExclamation, "Game Over"
    66.    End If
    67.    
    68.    If ((Text1.Text = Num1) And (Text2.Text = Num2) And (Text3.Text = Num3) And (Text4.Text = Num4)) Or (TriesAttempted = 10) Then
    69.        Go.Enabled = False
    70.        Command1.Enabled = False
    71.        Command2.Enabled = False
    72.        Command3.Enabled = False
    73.        Command4.Enabled = False
    74.        Command5.Enabled = False
    75.        Command6.Enabled = False
    76.        Command7.Enabled = False
    77.        Command8.Enabled = False
    78.        Command9.Enabled = False
    79.        Command10.Enabled = False
    80.        Delete.Enabled = False
    81.    End If
    82.    
    83.    Text1.Text = "" 'Clear Textboxes
    84.    Text2.Text = ""
    85.    Text3.Text = ""
    86.    Text4.Text = ""
    87. 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
  •  



Click Here to Expand Forum to Full Width