View Single Post
Old Feb 25th, 2007, 8:03 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You can't have the computer remove a random number of sticks in the range 1-3, always. You have remove a number in the range 1 to the lesser of 3 or the number of sticks left (if there are two left, you can't take 3).

You need a variable, sticksLeft. You need to compare sticksLeft to 3 and generate a random number in the range 1-3 or 1-sticksLeft, accordingly. When sticks are removed, you subtract that number from sticksLeft. When sticksLeft is zero, the game is over.

Obviously, after you've subtracted the player's number, it's the computer's turn. When the computer is through, it's the player's turn. That cycle gets broken by "game over".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote