Welcome to Dream.In.Code
Become a VB Expert!

Join 149,989 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,352 people online right now. Registration is fast and FREE... Join Now!




Nim game

 
Reply to this topicStart new topic

Nim game, 2 player match game

hardley98
16 Nov, 2006 - 09:48 AM
Post #1

New D.I.C Head
*

Joined: 30 Oct, 2006
Posts: 7


My Contributions
Designing a game called "Nim" for a project. It's a two player game and starts with 23 matches. Player 1 & 2 select 1, 2, or 3 matches in turn until whoever has to select the last match looses. The problem I'm have is getting the variables to set values correctly when each player clicks their respective button. Variables update on player 1 turn but are wrong for player 2. Variables are set initially in a Form1_Load.

CODE

Public Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Player1, Player2 As String
        Dim Matches As Integer
        Matches = 23
        MatchesLeftLabel.Text = "There are " & Matches & " matches."
        Player1 = InputBox("Enter your name Player 1.", "Name of Player 1.", "Type your name here")
        Player2 = InputBox("Enter your name Player 2.", "Name of Player 2.", "Type your name here")
        Player1Button.Text = Player1
        Player2Button.Text = Player2
        Player2Button.Enabled = False
        RulesLabel.Text = ("1. Each game starts with 23 matches.")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("2. Each player will take turn and must remove")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("    1, 2, or 3 matches; otherwise 1 match is removed.")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("3. The player that removes the last match or")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("    matches looses the game.")

        InstructionLabel.Text = "Player " & Player1 & " please enter the number of matches to be removed."

        MatchesLabel.Text = "ooooo ooooo ooooo ooooo ooo" + vbCrLf + "||||| ||||| ||||| ||||| |||"

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Close()
    End Sub

    Private Sub Player1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Player1Button.Click
        Dim Number, Count As Byte
        Dim Matches, NumberLeft As Integer
        Dim Player1, Player2 As String
        Player1Button.Text = Player1
        If Count = 0 Then Matches = 23
        Matches = Matches - Number
        MatchesLabel.Text = ""
        Number = Val(NumberTextBox.Text)
        If Number > 3 Or 0 Then
            Number = 1
        End If
        Matches = Matches - Number
        MatchesLeftLabel.Text = "There are " & Matches & " matches."
        Player1Button.Enabled = False
        Player2Button.Enabled = True
        Player2Button.Focus()
        NumberTextBox.Focus()
        NumberTextBox.Text = ""
        InstructionLabel.Text = "Player " & player2 & " please enter the number of matches to be removed."
        For NumberLeft = 1 To Matches
            MatchesLabel.Text = MatchesLabel.Text + "o"
        Next
    End Sub

    Private Sub Player2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Player2Button.Click
        Dim Number, Count As Byte
        Dim Matches, NumberLeft As Integer
        Dim Player1, Player2 As String
        Player2Button.Text = Player2
        InstructionLabel.Text = "Player " & Player1 & " please enter the number of matches to be removed."
        'If Count = 0 Then Matches = 23
        'Matches = Matches - Number
        MatchesLabel.Text = ""
        Number = Val(NumberTextBox.Text)
        If Number > 3 Or 0 Then
            Number = 1
        End If
        Matches = Matches - Number
        MatchesLeftLabel.Text = "There are " & Matches & " matches."
        Player1Button.Enabled = True
        Player2Button.Enabled = False
        Player1Button.Focus()
        NumberTextBox.Focus()
        NumberTextBox.Text = ""
        For NumberLeft = 1 To Matches
            MatchesLabel.Text = MatchesLabel.Text + "o"
        Next
    End Sub

User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Nim Game
16 Nov, 2006 - 09:16 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
Wow, there are a lot of little things wrong here. I REWOTE YOUR CODE AND BROKE IT UP.
So don't get upset. There is a method called Hungarian Format which is
used by a lot of programmers but not all. But if you get into the habbit when you are just
starting it is easier. Basicly you precede your varible name with the type of object it is in
a three letter code.
Button ------------------ btnMyButton or cmdMyButton (Command Button VB6 and Prior)
TextBox ---------------- txtMyTextBox
Label ------------------- lblMyLabel
ComboBox ------------ cmbMyCombo
ListBox ----------------- lstMyListBox
DataGrid --------------- dgdMyDataGrid
PictureBox ------------- picMyPictureBox
and so on

well here is your hacked code, I tried to document most of the changes
CODE


Public Class Form1
    'Declare Variables that need to maintain a changing value at the beginning of the form,
    ' this gives the variable scope thoughout the form

    Dim Number, Count As Integer   ' use Integer not Byte
    Dim Matches, NumberLeft As Integer
    Dim Player1, Player2 As String
    Dim thisPlayer As String


    Public Sub Form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'initialise variables
        Count = 0
        Matches = 23
        ' Create UI   '  I added the second Instruction label so the number of matches left is
        '                always visible
        InstructionLabel2.Text = "There are " & Matches & " matches."
        Player1 = InputBox("Enter your name Player 1.", "Name of Player 1.", "Type your name here")
        Player2 = InputBox("Enter your name Player 2.", "Name of Player 2.", "Type your name here")
        Player1Button.Text = Player1
        Player2Button.Text = Player2
        Player2Button.Enabled = False
        RulesLabel.Text = ("1. Each game starts with 23 matches.")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("2. Each player will take turn and must remove")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("    1, 2, or 3 matches; otherwise 1 match is removed.")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("3. The player that removes the last match or")
        RulesLabel.Text = RulesLabel.Text + vbCrLf + ("    matches looses the game.")

        InstructionLabel.Text = "Player " & Player1 & " please enter the number of matches to be removed."
                                'You need to work out your graphics better  I left this part alone for you to do
        MatchesLabel.Text = "ooooo ooooo ooooo ooooo ooo" + vbCrLf + "||||| ||||| ||||| ||||| |||"

    End Sub


    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        Close()
    End Sub


    Private Sub Player1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Player1Button.Click
        ' When two processes are exactly the same, create a sub for just that portion
        ' this allows you to call the same routine from multiple actions
        thisPlayer = Player2 ' for displaing name for next player in RemoveMatch()
        RemoveMatch()  ' I created a routine for removing matches
        Player1Button.Enabled = False
        Player2Button.Enabled = True
        NumberTextBox.Text = ""
        NumberTextBox.Focus()

    End Sub


    Private Sub Player2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Player2Button.Click
        ' When two processes are exactly the same, create a sub for just that portion
        ' this allows you to call the same routine from multiple actions      
        thisPlayer = Player1 ' for displaing name for next player in RemoveMatch()
        RemoveMatch()
        Player1Button.Enabled = True
        Player2Button.Enabled = False
        NumberTextBox.Text = ""
        NumberTextBox.Focus()

    End Sub


    Private Sub RemoveMatch()
        ' you were declaring your variables here which reset them to 0, I moved them to
        ' the Class Form1 General Declarations
        'try to make your comments about what is happening, not just a copy of the code
        ' ie instead of
        'If Count = 0 Then Matches = 23
        'Matches = Matches - Number
        ' use something more like

        ' if game is over reset variables
        If Matches = 0 Then Matches = 23
        'Remove a number of matches
        Number = Val(NumberTextBox.Text)
        If (Number > 3) Or (Number < 1) Then 'A Comparitor is required for each side of the Boolean Operation
            Number = 1
        End If
        Matches = Matches - Number
        InstructionLabel2.Text = "There are " & Matches & " matches."

        InstructionLabel.Text = "Player " & thisPlayer & " please enter the number of matches to be removed."
        ' clear matches graphic
        MatchesLabel.Text = ""
        'Create MatchHeads
        For NumberLeft = 1 To Matches
            MatchesLabel.Text = MatchesLabel.Text + "o"
        Next
    End Sub
End Class


I tried to comment all my changes but I may have missed one or two.

This post has been edited by KeyWiz: 16 Nov, 2006 - 09:22 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 07:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month