The Game of Nim - Step 2

◀ The Game of Nim - Step 1▶ The Game of Nim - Step 3
Amazon Let’s apply the 2nd step of the Four-Step Programming Model to solve the game of Nim!

Four Step Programming Model: Step 2
Now let’s determine what variables we need. We need 7 integers to store the highest possible number of marbles initially, the lowest possible number of marbles initially, the current number of marbles, the current turn, the number of marbles computer draws at a particular turn, the number of marbles the user draws at a particular turn, and the mode the computer adopts.

By the way, there is something wrong with the skeleton; the game should be played until one side loses, so we need a loop. Here is my refined skeleton:
bool deadNum(int);
int compute(int);

int main() {  
int max, min, size, whoseTurn, compTakeOff, youTakeOff, mode;

- initialize random number generator
- introduce the game of Nim and display its rules
- determine who goes first and the initial number of marbles
- deal with the case when computer plays stupid
while the number of marbles is >= 1
		if it’s user’s turn, prompt user to enter a number
			if the number is invalid, prompt user to enter another one
		if it’s computer’s turn, draw a random legal number of marbles
		switch turn
- deal with the case when computer plays smart
	while the number of marbles is >= 1
		if it’s user’s turn, prompt user to enter a number
			if the number is invalid, prompt user to enter another one
if it’s computer’s turn, draw a number of marbles based on the strategy 
 		switch turn
}

/*
precondition: n must be a positive integer
postcondition: return true if n is 1 less than any power of 2; return false otherwise
*/
bool deadNum(int n) {
- same
}

/*
precondition: n must be a positive integer 
postcondition: return the number of marbles smart computer should draw
*/
int compute(int n) {
- same
}
Let's look at the next step!
◀ The Game of Nim - Step 1▶ The Game of Nim - Step 3

fShare
Questions? Let me know!