Write a game that allows the user to challenge the computer to a game of Pick-Up
ID: 3679646 • Letter: W
Question
Write a game 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 25) 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 contestat who removes the last matchsticks loses. 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 remaider when n is divided by 4 is (n Mod 4). After writing the porogram, play a few games witht he computer and observe that the computer always wins. Everytime buttons one two or three is pressed and the computer picks the text box should reflect the amount of sticks left.
In addition: Make sure that buttons one two and three will be disabled before the game begins.
Five, Six, Pick-Up Sticks Give the Rules of the Game Begin a New Game How many matchsicks would you like to pick up? ne Two Three Current Status Summary COwit UIExplanation / Answer
Public Class Stick
' Declare global variables
Dim num_stick 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 Game_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
First()
If computerTurn = True Then
Turn()
End If
End Sub
Function GetNumber() As Integer
' Obtains the number of matchsticks and validates the input
num_stick = CInt(InputBox("Enter a number between 5 and 40", "Number of Sticks"))
If (num_stick < 5) Or (num_stick > 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 num_stick
End Function
Sub OutputSticks()
txtCurrentPile.Text = " "
Select Case num_stick
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 = ("| | | | | | | | | | | | | | | | | | | | | | | | |")
End Select
End Sub
Sub First()
If num_stick Mod 4 = 1 Then
computerTurn = False
MessageBox.Show("Player Goes First")
Else
computerTurn = True
MessageBox.Show("Computer Goes First")
End If
End Sub
Sub Turn()
If num_stick Mod 4 = 3 Then
num_stick = num_stick - 2
MessageBox.Show("Computer takes 2 sticks.")
ElseIf (num_stick Mod 4 = 2) Then
num_stick = num_stick - 1
MessageBox.Show("Computer takes 1 stick.")
ElseIf (num_stick Mod 4 = 0) Then
num_stick = num_stick - 3
MessageBox.Show("Computer takes 3 sticks.")
End If
If num_stick = 1 Then
MessageBox.Show("Only 1 stick left. You lose!")
num_stick = 0
End If
OutputSticks()
End Sub
Private Sub one_click(sender As Object, e As EventArgs) Handles btnOne.Click
If num_stick = 0 Then
MessageBox.Show("Please start a new game.")
Else
MessageBox.Show("Player takes 1 stick.")
num_stick = num_stick - 1
If num_stick = 1 Then
MessageBox.Show("Only 1 stick left. You win!")
num_stick = 0
End If
OutputSticks()
Turn()
End If
End Sub
Private Sub Two_click(sender As Object, e As EventArgs) Handles btnTwo.Click
If num_stick = 0 Then
MessageBox.Show("Please start a new game.")
Else
MessageBox.Show("Player takes 2 sticks.")
num_stick = num_stick - 2
OutputSticks()
If num_stick = 1 Then
MessageBox.Show("Only 1 stick left. You win!")
num_stick = 0
End If
Turn()
End If
End Sub
Private Sub Three_click(sender As Object, e As EventArgs) Handles btnThree.Click
If num_stick = 0 Then
MessageBox.Show("Please start a new game.")
Else
MessageBox.Show("Player takes 3 sticks.")
num_stick = num_stick - 3
OutputSticks()
If num_stick = 1 Then
MessageBox.Show("Only 1 stick left. You win!")
num_stick = 0
End If
Turn()
End If
End Sub
End Class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.