The Game of Nim - Step 3
◀ The Game of Nim - Step 2▶ The Game of Nim - Step 4 Amazon
Let’s apply the 3rd step of the Four-Step Programming Model to solve the game of Nim!
Four Step Programming Model: Step 3 Interrelation: All variables are independent of one another.
State: max and min are determined by the programmer and shouldn’t change throughout the program; size is the current number of marbles and is always decreasing; whoseTurn should switch back and forth between player and computer; compTakeOff and youTakeOff should be legal number of marbles to draw; mode should not change once it is determined.
Scope: Out of these variables, we can make max and min global constants because their values do not change throughout the program.
Here’s the updated skeleton:
bool deadNum(int);
int compute(int);
const int MAX = 91;
const int MIN = 13;
int main() {
int size, whoseTurn, compTakeOff, youTakeOff, mode;
- same
}
/*
precondition: n should 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 should be a positive integer
postcondition: return the number of marbles smart computer should draw
*/
int compute(int n) {
- same
}
Let's look at the final step next!
◀ The Game of Nim - Step 2▶ The Game of Nim - Step 4