/* Jason M. Snouffer Friday, December 13, 2002 Computer Science & Programming Programming Project This program is a text-based game based on the well-known game Nim. The game is with a number of stones, in which each player takes a turn removing either 1 or 2 of the stones. The player who takes the last stone has lost the game.programemd built-in strategies to make the game more challenging. */ #include #include using namespace std; /**************************************************************************************** *********************************Global Variables**************************************** ****************************************************************************************/ enum Valid_Players {computer, human}; /**************************************************************************************** ********************************Function Prototypes************************************** ****************************************************************************************/ /****** get_number_of_stones ****** This function prompts the user for the number of stones they want to play with, the function utilizes a do while loop to verify that this number is positive. precondition: none postcondition: This function prints a message, prompting the user to enter data, and a positive integer will be returned, which represents the number of stones */ int get_number_of_stones(void); /****** get_whose_turn ****** This function allows the user to choose who will take the first turn, the computer or the user. The function prompts the user to either enter a 0 for the computer or 1 for the user. The function also verifies that the user has entered a valid entry. precondition: Valid_Player is an enum postcondition: This function prints a message, prompting the user to enter data, and returns the enum, Valid_Players which represents whose turn it is. */ Valid_Players get_whose_turn(void); /****** computer_take_turn ****** This function recieves the number of stones remaining and utilizing built-in optimal strategies, the computer will remove either 0 or 1 stones from the number of stones. precondition: number_of_stones must be greater than zero postcondtion: This function will print a message notifying the user how many stones the computer is removing. Then it modifies number_of_stones through pass_by_reference so that the number of stones the computer has taken is subtracted from it. */ void computer_take_turn (int & stones_remaining); /****** human_take_turn ****** This function recieves the number of stones remaining and then prompts the user to enter the number of stones he would like to remove. The function verifies that the number entered is either 1 or 2 and is less than the number of stones remaining. precondition: number_of_stones must be greater than zero postcondtion: This function prints a message prompting the user to enter the number of stones to remove, and subtracts this from the number_of_stones through pass-by- reference. */ void human_take_turn (int & stones_remaining); /****** switch_whose_turn ****** This function changes whose turn it is to remove stones. If it is currently the computer's turn, then it will become the user's turn, and vice versa. precondition: whose_turn must be initialized postcondition: whose_turn will be changed through pass_by_reference, if whose_turn is equal to computer then it will be changed to equal human, and vice versa. */ void switch_whose_turn (Valid_Players & current_whose_turn); /****** nim_winner ****** This function recieves the value of whose_turn, and displays a message stating who has won the game. The winner is the value of whose_turn. precondtion: whose_turn should represent the winner. postcondition: This function displays a victory message notifying the user who the winner is. */ void nim_winner (Valid_Players winner); /**************************************************************************************** *************************************int main()****************************************** ****************************************************************************************/ int main() { int number_of_stones; Valid_Players whose_turn; cout<<"Welcome to Computer Nim\n\n"; number_of_stones = get_number_of_stones(); cout<<"OK - let's play with " <>number_of_stones; if (number_of_stones <= 0) { cout<>whose_turn_number; if ( (whose_turn_number != 0) && (whose_turn_number != 1) ) { cout<= 2) ) { stones_to_take = 2; } else // If (stones_remaining % 3) is either 1 or 2; or there is only 1 stone left. { stones_to_take = 1; } cout<<"I'm going to take " < 1) cout<<'s'; cout<<".\n"; stones_remaining -= stones_to_take; //Subtracts the number of stones taken from the number of remaining stones. } /****** human_take_turn ****** This function recieves the number of stones remaining and then prompts the user to enter the number of stones he would like to remove. The function verifies that the number entered is either 1 or 2 and is less than the number of stones remaining. precondition: number_of_stones must be greater than zero postcondtion: This function prints a message prompting the user to enter the number of stones to remove, and subtracts this from the number_of_stones through pass-by- reference. */ void human_take_turn (int & stones_remaining) { int stones_to_take; bool invalid_number_of_stones = false; cout<<"How many stones would you like to remove (1"; if (stones_remaining != 1) cout<<" or 2"; cout<<")? "; do //This loop verifies that user's entered number of stones to take is valid. { cin>>stones_to_take; if ( ( (stones_to_take != 1) && (stones_to_take != 2) ) || (stones_to_take > stones_remaining) ) { invalid_number_of_stones = true; cout<<"That is not a legal number of stones, how many would you like? "; } } while (invalid_number_of_stones); stones_remaining -= stones_to_take; //Subtracts the number of stones taken from the number of remaining stones. } /****** switch_whose_turn ****** This function changes whose turn it is to remove stones. If it is currently the computer's turn, then it will become the user's turn, and vice versa. precondition: whose_turn must be initialized postcondition: whose_turn will be changed through pass_by_reference, if whose_turn is equal to computer then it will be changed to equal human, and vice versa. */ void switch_whose_turn (Valid_Players & current_whose_turn) { if (current_whose_turn == computer) current_whose_turn = human; else current_whose_turn = computer; } /****** nim_winner ****** This function recieves the value of whose_turn, and displays a message stating who has won the game. The winner is the value of whose_turn. precondtion: whose_turn should represent the winner. postcondition: This function displays a victory message notifying the user who the winner is. */ void nim_winner (Valid_Players winner) { if (winner == computer) cout<<"Ha! Ha! I win!!\n\n"; else cout<<"Congratulations, you win!\n\n"; }