EDIT: Holy crap I use that word a lot, especially given how little I understand. Fuck. I guess if I
say it enough I hope it'll come true?
I am frankly sick of multithreading at this point, so, lets relax for today. I looked up someone's 21 sticks game, then modified it to make it prettier. 21 sticks is a game with 2 players, where, oddly enough, there are 21 sticks. Each player then takes turns removing either 1 or 2 sticks from the pile until none are left. The player to take the last stick(s) is the loser.
Didn't want to spend too much time on it, so there's no GUI, and no error checking, but I guess that gives me something to do next time I want to wind down and take a breather :P As it stands, you'll need to run it from your command line, or just through your IDE
Let's start off with the source code, which you can find here.
Have a look through it, there's no multithreading, since it's a turn based game, so it's relatively not-as-hard to write, read and understand! If there are any problems, let me know!
First off, we need a few initial variables:
Gotta hold the number of sticks remaining, then we need a scanner for player input (using this is really simple, if you don't remember how). Then we have a string to say if the player goes first or not. This is a string because it'll be based on player input!
The last 2 are going to be used often. take determines how many sticks are taken, and end is there as a boolean to allow the program to tell when the game is over.
There are conditionals for this, but I found, based on how I executed the turns, without an end variable, if the player went first in a turn, and lost, the computer would afterwards attempt to take his turn. Also losing. 'Twas weird.
TL;DR: Lines 7-11 are required variables. Go through 'em in your head.
Game() : We have a constructor here, just to initialize the scanner, set the starting sticks and making sure we're not ending before we've begun :P
startGame() : This is just to determine if the player wants to start or not. You'll notice, as I mentioned before, that there's no real error checking. If you play this game in an IDE or console, I expect you to get it right!
This one's wide, had to limit its size! It goes through the computer's move, implementing super-basic AI in lines 25-29.
See, the first part of the if-statement is that "if the remainder of (sticks - 2)/3 is 0..." I honestly don't have a head for logic at this point in the day, so I couldn't tell you why this condition is so special, I just jacked it off the example code I found.
The second part of the if-statement is, however, easy enough to get. Should've just written "sticks == 2", but that'll suffice. If there's 2 sticks, don't take them both! Savvy?
If none of the above are true, just take 2!
At this point, you want the program to tell you what action the computer took, so we print it out on line 31, then modify the stick-pile on the very next line.
Lines 34-36 are what to do in the case of a loss on the computer's part. Print off a winning message to the player, and set the end variable to true!
This one's a little more complex, but not really. First step is to tell you how many sticks there are, so you can make an informed decision, then use the scanner to get your input. The scanner is great, isn't it? One scanner can just be used to get anything!
As commented, I'm not using real error checking, but it's a bum deal to just take 20 sticks on turn 1 and screw over the AI, so as long as you put in an integer, I've closed the range off in lines 46-50
Now it's pretty much the same as the computer's move. Modify the stick pile, and if you lose, let you know.
Ah, the part where everything comes together.
Line 62: Start the game off by determining who goes first.
Lines 65-69: If the player goes first in turn order, let them move. As long as the game hasn't ended because of the player losing, the AI can now take it's turn. Repeat until loss.
Lines 70-74: Same as above, but in reverse order. Easy.
And there you have it! Ta-dah, voila and et cetera.
See you tomorrow, when I get back to the grueling task of trying to understand wtf is going on with goddamn multithreading! Comments/questions welcome, as always!