Question
I need help writing a code for a game of NIM with 0-2 players in C( 0 meaning two computers play, 1 meaning 1 human vs 1 computer, and 2 meaning 1 human vs 1 human). It must give the human players the ability to input their names, display the board after each move, make the loser go first if they decide to play again, and keep a count of the number of wins each player has, the computer players dont need to win a certain number of times. The program also must use functions. Thank you very much for your time.
Explanation / Answer
* ----------- * This program simulates a simple variant of the game of nim. * In this version, the game starts with a pile of 13 coins * on a table. Players then take turns removing 1, 2, or 3 * coins from the pile. The player who takes the last coin * loses. This simulation allows a human player to compete * against the computer. */ #include #include "genlib.h" #include "simpio.h" #include "strlib.h" /* * Constants * --------- * InitialCoins -- Initial number of coins * MaxTake -- The maximum number of coins a player may take * NoGoodMove -- Sentinel indicating no good move is available */ #define InitialCoins 13 #define MaxTake 3 #define NoGoodMove -1 /* * Type: playerT * ------------- * This enumeration type distinguishes the turns for the human * player from those for the computer. */ typedef enum { Human, Computer } playerT; /* Private function prototypes */ static void GiveInstructions(void); static void AnnounceWinner(int nCoins, playerT whoseTurn); static int GetUserMove(int nCoins); static bool MoveIsLegal(int nTaken, int nCoins); static int ChooseComputerMove(int nCoins); static int FindGoodMove(int nCoins); static bool IsBadPosition(int nCoins); /* * Main program * ------------ * This program plays the game of nim. In this implementation, * the human player always goes first. */ main() { int nCoins, nTaken; playerT whoseTurn; GiveInstructions(); nCoins = InitialCoins; whoseTurn = Human; while (nCoins > 1) { printf("There are %d coins in the pile. ", nCoins); switch (whoseTurn) { case Human: nTaken = GetUserMove(nCoins); whoseTurn = Computer; break; case Computer: nTaken = ChooseComputerMove(nCoins); printf("I'll take %d. ", nTaken); whoseTurn = Human; break; } nCoins -= nTaken; } AnnounceWinner(nCoins, whoseTurn); } /* * Function: GiveInstructions * Usage: GiveInstructions(); * -------------------------- * This function explains the rules of the game to the user. */ static void GiveInstructions(void) { printf("Hello. Welcome to the game of nim. "); printf("In this game, we will start with a pile of "); printf("%d coins on the table. ", InitialCoins); printf("On each turn, you "); printf("and I will alternately take between 1 and "); printf("%d coins from the table. ", MaxTake); printf("The player who "); printf("takes the last coin loses. "); printf(" "); } /* * Function: AnnounceWinner * Usage: AnnounceWinner(nCoins, whoseTurn); * ----------------------------------------- * This function announces the final result of the game. */ static void AnnounceWinner(int nCoins, playerT whoseTurn) { if (nCoins == 0) { printf("You took the last coin. You lose. "); } else { printf("There is only one coin left. "); switch (whoseTurn) { case Human: printf("I win. "); break; case Computer: printf("I lose. "); break; } } } /* * Function: GetUserMove * Usage: nTaken = GetUserMove(nCoins); * ------------------------------------ * This function is responsible for the human player's turn. * It takes the number of coins left in the pile as an argument, * and returns the number of coins that the player removes * from the pile. The function checks the move for legality * and gives the player repeated chances to enter a legal move. */ static int GetUserMove(int nCoins) { int nTaken, limit; while (TRUE) { printf("How many would you like? "); nTaken = GetInteger(); if (MoveIsLegal(nTaken, nCoins)) break; limit = (nCoins 0 && nTaken