Block of Code

Practical Examples for Programmers

  • Home
  • C++
  • Java
  • Visual Basic
  • Discrete Math
  • Ask a Question

Pick-Up Sticks

Using Visual Basic, write a program that allows the user to challenge the computer to a game of Pick-Up Sticks.  Here is how the game is played.  The user chooses the number of matchsticks (from 5 to 40) to place in a pile.  Then, the computer chooses who will go first.  At each turn, the contestant can remove one, two or three matchsticks from the pile.  The contestant who removes the last matchstick loses.  See the figure below for an example of the output.

Pickup Sticks

The computer should make the user always select from a pile where the number of matchsticks has a remainder of 1 when divided by 4.  For instance, if the user initially chooses a number of matchsticks that has a remainder of 1 when divided by 4, then the computer should have the user go first.  Otherwise, the computer should go first and remove the proper number of matchsticks.  [Note: The remainder when n is divided by 4 is (n Mod 4).]  After writing the program, play a few games with the computer and observe that the computer always wins.

Suggested Control Names and Attributes:

Name Property Text Property Control Type Notes
 frmPickUpSticks Five, Six, Pick-Up Sticks Form Holds Controls
 btnGiveRules Give the Rules of the Game Button Triggers event to show rules
 btnNewGame Begin a New Game Button Triggers event to start a new game
 btnOne One Button Triggers event to take 1 stick and display new pile
 btnTwo Two Button Triggers event to take 2 sticks and display new pile
 btnThree Three Button Triggers event to take 3 sticks and display new pile
 txtCurrentPile TextBox Display current contents of pile.  Read only set to true.

Comments:

I could have used loops to make some sections of the code easier. For example, displaying the number of sticks in the pile. I chose not to because at this point, we haven’t learned how to code loops.

Write the Code:

' Project: Pick Up Sticks
' Description: Play a game of pick up sticks with the computer

Public Class PickUpSticks
    ' Declare global variables
    Dim numberOfSticks As Integer = 0
    Dim valid As Boolean = True
    Dim computerTurn As Boolean = True

    Private Sub btnGiveRules_Click(sender As Object, e As EventArgs) Handles btnGiveRules.Click
        MessageBox.Show("Choose the number of matchsticks (between 5 and 40). The computer will decide who goes first. Remove one, two or three matchsticks from the pile. The contestant who removes the last matchstick loses.", "Rules of the Game")
    End Sub

    Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles btnNewGame.Click
        ' Ask for number of matchsticks
        GetNumber()
        ' Output number of matchsticks
        OutputSticks()
        ' Decide who goes first
        WhoGoesFirst()

        If computerTurn = True Then
            ComputersTurn()
        End If
    End Sub

    Function GetNumber() As Integer
        ' Obtains the number of matchsticks and validates the input
        numberOfSticks = CInt(InputBox("Enter a number between 5 and 40", "Number of Sticks"))
        If (numberOfSticks < 5) Or (numberOfSticks > 40) Then
            MessageBox.Show("You entered an invalid number. Please try again")
            valid = False
        Else
            valid = True
        End If
        If valid = False Then
            GetNumber()
        End If
        Return numberOfSticks
    End Function

    Sub OutputSticks()
        txtCurrentPile.Text = " "
        Select Case numberOfSticks
            Case 5
                txtCurrentPile.Text = ("| | | | |")
            Case 6
                txtCurrentPile.Text = ("| | | | | |")
            Case 7
                txtCurrentPile.Text = ("| | | | | | |")
            Case 8
                txtCurrentPile.Text = ("| | | | | | | |")
            Case 9
                txtCurrentPile.Text = ("| | | | | | | | |")
            Case 10
                txtCurrentPile.Text = ("| | | | | | | | | |")
            Case 11
                txtCurrentPile.Text = ("| | | | | | | | | | |")
            Case 12
                txtCurrentPile.Text = ("| | | | | | | | | | | |")
            Case 13
                txtCurrentPile.Text = ("| | | | | | | | | | | | |")
            Case 14
                txtCurrentPile.Text = ("| | | | | | | | | | | | | |")
            Case 15
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | |")
            Case 16
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | |")
            Case 17
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | |")
            Case 18
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | |")
            Case 19
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | |")
            Case 20
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | |")
            Case 21
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | |")
            Case 22
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | |")
            Case 23
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | |")
            Case 24
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | |")
            Case 25
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 26
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 27
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 28
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 29
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 30
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 31
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 32
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 33
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 34
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 35
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 36
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 37
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 38
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 39
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
            Case 40
                txtCurrentPile.Text = ("| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |")
        End Select
    End Sub

    Sub WhoGoesFirst()
        If numberOfSticks Mod 4 = 1 Then
            computerTurn = False
            MessageBox.Show("Player Goes First")
        Else
            computerTurn = True
            MessageBox.Show("Computer Goes First")
        End If
    End Sub

    Sub ComputersTurn()
        If numberOfSticks Mod 4 = 3 Then
            numberOfSticks = numberOfSticks - 2
            MessageBox.Show("Computer takes 2 sticks.")
        ElseIf (numberOfSticks Mod 4 = 2) Then
            numberOfSticks = numberOfSticks - 1
            MessageBox.Show("Computer takes 1 stick.")
        ElseIf (numberOfSticks Mod 4 = 0) Then
            numberOfSticks = numberOfSticks - 3
            MessageBox.Show("Computer takes 3 sticks.")
        End If
        If numberOfSticks = 1 Then
            MessageBox.Show("Only 1 stick left. You lose!")
            numberOfSticks = 0
        End If
        OutputSticks()
    End Sub

    Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click
        If numberOfSticks = 0 Then
            MessageBox.Show("Please start a new game.")
        Else
            MessageBox.Show("Player takes 1 stick.")
            numberOfSticks = numberOfSticks - 1
            If numberOfSticks = 1 Then
                MessageBox.Show("Only 1 stick left. You win!")
                numberOfSticks = 0
            End If
            OutputSticks()
            ComputersTurn()
        End If
    End Sub

    Private Sub btnTwo_Click(sender As Object, e As EventArgs) Handles btnTwo.Click
        If numberOfSticks = 0 Then
            MessageBox.Show("Please start a new game.")
        Else
            MessageBox.Show("Player takes 2 sticks.")
            numberOfSticks = numberOfSticks - 2
            OutputSticks()
            If numberOfSticks = 1 Then
                MessageBox.Show("Only 1 stick left. You win!")
                numberOfSticks = 0
            End If
            ComputersTurn()
        End If
    End Sub

    Private Sub btnThree_Click(sender As Object, e As EventArgs) Handles btnThree.Click
        If numberOfSticks = 0 Then
            MessageBox.Show("Please start a new game.")
        Else
            MessageBox.Show("Player takes 3 sticks.")
            numberOfSticks = numberOfSticks - 3
            OutputSticks()
            If numberOfSticks = 1 Then
                MessageBox.Show("Only 1 stick left. You win!")
                numberOfSticks = 0
            End If
            ComputersTurn()
        End If
    End Sub
End Class

Secondary Sidebar

This is the secondary sidebar

Copyright © 2025 · Metro Pro Theme on Genesis Framework · WordPress · Log in