ID:1799865
 
What's the story behind writing this? It's for my Visual Basic 2012 class in college. The book asked me to make a pick up sticks game. Which I was glad to do. But, then I read further that it was asking me to make it so the bot always wins. That's when I wasn't so interested and the code started getting nasty. I'll probably get a bad grade for this because I didn't make the bot unbeatable completely. But, it'll think for itself a bit. I'm not going to be a cheater. You can't do this to me, school system. I program for the gamers! WHOSE WITH ME!?

P.S. Ignore the crappy comments. They're to please my professor. I tried. But, comments are annoying to place on things that are nearly self-explanatory. Eventually, I just stop caring.

Public Class Form1
Dim matchsticks As Integer
Dim matchsticksLabel As String
Dim playing As Boolean = False
Dim playerTurn As Boolean = False

'Give the Rules of the Game button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Declare a constant so it doesn't fill up the line MsgBox() is on.
Const rules As String = "The object of this game is to pick up sticks. But, that's not all. Here is how the game is played. You choose the number of matchsticks (from 5 to 40) to place in a pile. Then, the computer will choose who goes first. When your turn comes, you can remove 1 - 3 matchsticks from the pile. The contestant who removes the last matchstick loses."

'Show the rules to the user
MsgBox(rules, , "Da Rules")
End Sub

'Begin a New Game button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If playing Then
If MsgBox("Do you really want to start a new game?", vbQuestion + vbYesNo) = vbNo Then
Return
End If
End If
'Constants for min/max sticks
Const minSticks As Integer = 5
Const maxSticks As Integer = 40

'Reset the matchsticks label
matchsticksLabel = ""

'Display input box and prompt the user properly.
If Integer.TryParse(InputBox("How many matchsticks do you want to place in a pile? (A number:" & minSticks.ToString() & " - " & maxSticks.ToString() & ")", "Starting Matchsticks", minSticks), matchsticks) Then
'The user has entered a valid entry.
If matchsticks >= minSticks And matchsticks <= maxSticks Then
playing = True
For index As Integer = 1 To matchsticks
matchsticksLabel += "| "
Next
TextBox1.Text = matchsticksLabel

If (matchsticks Mod 4) = 1 Then
playerTurn = True
MsgBox("The computer has chosen for you to go first.", , "Player Turn")
Else
playerTurn = False
MsgBox("The computer has chosen the be the first to go.", , "Player Turn")
CheckIfWon(CInt(Int((3 * Rnd()) + 1)))

End If
Else
'The user has entered a number out of range. Reset the program.
playing = False
matchsticks = 0
TextBox1.Text = matchsticksLabel
Return
End If
End If
End Sub

'Remove 1 matchstick
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim draw As Integer = 1
If draw > matchsticks Then
draw = matchsticks
End If
matchsticks -= 1
If Not CheckIfWon(draw) Then
playerTurn = Not playerTurn
TextBox1.Text = matchsticksLabel.Substring(0, matchsticks * 2)
CheckIfWon(CInt(Int((3 * Rnd()) + 1)))
End If
End Sub

'Remove 2 matchstick
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim draw As Integer = 2
If draw > matchsticks Then
draw = matchsticks
End If
matchsticks -= draw
If Not CheckIfWon(draw) Then
playerTurn = Not playerTurn
TextBox1.Text = matchsticksLabel.Substring(0, matchsticks * 2)
CheckIfWon(Int((3 * Rnd()) + 1))
End If
End Sub

'Remove 3 matchstick
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim draw As Integer = 3
If draw > matchsticks Then
draw = matchsticks
End If
matchsticks -= draw
If Not CheckIfWon(draw) Then
playerTurn = Not playerTurn
TextBox1.Text = matchsticksLabel.Substring(0, matchsticks * 2)
CheckIfWon(Int((3 * Rnd()) + 1))
End If
End Sub

'Check if the user has one or whether or not they are playing.
Private Function CheckIfWon(draw As Integer) As Boolean
'If there are no matchsticks to begin with or they are not playing... do nothing.
If (draw <= 0) Or (Not playing) Then
Return True
End If

'Whether or not the computer is drawing
If playerTurn = False Then
If draw >= matchsticks Then
If matchsticks <> 1 Then
draw = matchsticks - 1
Else
draw = matchsticks
End If
End If
matchsticks -= draw
TextBox1.Text = matchsticksLabel.Substring(0, matchsticks * 2)
End If

'End game results.
If matchsticks <= 0 Then
If playerTurn = False Then
MsgBox("The computer selects " & draw.ToString() & " matchsticks. I win.", , "Hooray")
Return True
Else
MsgBox("I select " & draw.ToString() & " matchsticks. I lose.", , "Boo")
Return True
End If
End If

'Post game results, whenever any contestant draws.
If playerTurn = True Then
MsgBox("I select " & draw.ToString() & "matchsticks. There are " & matchsticks.ToString() & " matchsticks left.", , "Drawing")
Else
MsgBox("The computer selects " & draw.ToString() & "matchsticks. There are " & matchsticks.ToString() & " matchsticks left.", , "Drawing")
playerTurn = Not playerTurn
End If
Return False
End Function
End Class
Oh, god. Why are you still writing in Basic in 2015? :P


That language looks like Python and Assembly had a baby.
A lot of colleges require basic if your going for software development or other degree that requires programming. That is why he is writing in basic.
As long as it gets that A.
But, then I read further that it was asking me to make it so the bot always wins.

I wonder if that's a tarp ..
Basic is a really elegant language which favors aesthetics. Some variants have close to the same functionality as modern languages like C++.

Visual Basic, I've never used, but for the most part I understand what the code is doing. For people who don't like writing out "Function / EndFunction" and "If / Else / EndIf" then it's definitely not for you. It's a convention used in Basic that's stuck around since its arrival.

People underestimate Visual Basic and its variants. I use BlitzMax which is a flavor of Basic, compiles to Windows/Mac/Linux executables, and is a managed code. It has flaws like every other language, but I absolutely love it.
I despise Basic, VB, and all variants of "human readable" programming languages.

The only reason VB hasn't been allowed to die is because Microsoft continues to hamfist it into their premium office products.

C master race.
In response to Ter13
Ter13 wrote:
C master race.

YES! OHHH GODD YESS! AAAHHHH!

That is how I feel about this course. I hate the language. However, it is a requirement if I want to move on to bigger and better things. I am, however, taking C# as well. It's online.
Visual Basic makes my eyes bleed. It's funny that they call that "human readable" when trying to read that confuses me more than C#, which is supposed to be the harder alternative, correct? I picked up 2 books a few weeks ago and skimmed through the code examples for VB and C# and C# looked far more understandable than VB.


Ruby, Python and C# are the only languages I like to read. Lua is a bit readable too but who even uses Lua? Shots fired.
In response to Chumble93
Chumble93 wrote:
A lot of colleges require basic if your going for software development or other degree that requires programming. That is why he is writing in basic.

Hell, I went to school for an Naval Engineering/Systems Design degree in the mid '00s, and we needed to take VB.
In response to PixelScientist
Yeah, I had to take basic when I was going to school for information technology. How is that by the way? Naval Engineering?
Chumble93 wrote:
How is that by the way? Naval Engineering?

I'm loathe to go too off topic, but it was fairly enjoyable. I sailed for a handful of years as an engineer and then went shore-side to work in power plants. My education mostly focused on systems engineering, which is a holistic approach to engineering and design that examines how systems interact with one another. In application, I actually ended up being involved in predictive maintenance engineering, which is diagnosing equipment using a variety of nondestructive testing methods.

I've since left the industry to work in web development. I mostly deal with websites, but I had the opportunity to work on the Xbox One and its subsequent release in China. That was pretty awesome.

Edit: Part of my post disappeared into the ether. Blech! Edited to correct.