Question & Answer: CS210-Machine Problem 3 The Ancient Game of Nim 20 Points Asigned: September 14, 2017 Due: September 21, 2017 The…..

Make the program as small as possible
Make sure its matches the sample run
CS210-Machine Problem 3 The Ancient Game of Nim 20 Points Asigned: September 14, 2017 Due: September 21, 2017 The game of Nim has existed in various forms since at least the 16th century (according to Wikipedia, so it must be true). It is fairly simple, yet still provides a challenge. Here are the rules that we are going to implement: 1. The game board consists of three heaps of stones initially containing 3, 4, and 5 stones each 2. Players alternate turns removing stones from a single pile each turn. 3. A Player make remove as many stones as they wish in a single move, but they must all come from the same pile. 4. The player that removes the last stone wins Our program will labels the heaps as A, B, and C. A move will be entered as the heap letter followed by the number of stones to be removed. For a move to be legal, the letter must be A, B, or C, the number that follows must be greater than 0 and less than or equal to the number of stones remaining in that heap. See the sample run for some illustrated games. Like last time, your task is to create a program to allow a human to play this game against a computer opponent. Unlike last time, this computer opponent will use a perfect strategy, (e.g., if it goes first, it will always win. If it goes second, and the human makes a mistake, it will win). If you want more information, the wikipedia article provides more information than you will ever need. Your program should allow the player to play as many games as he or she likes. For each new game, the loser of the previous game goes irst. Therefore, if both players play perfectly, the games will be split evenly Program Design Like MP2, this program is going to have many tasks that it needs to accomplish. We will develop one function at a time, solving each smaller task, and then combine them all at the end. I will give you the specification of cach function. You should write the function and then test it with a main program that ensures that the function works by itself. Once a function is working, move on the to the next function, which will require a different sain function to test it. Once we have all the individual functions working, we will finally write the final main that will actually play the game. Print Greeting n Name: print greeting None nct puts: eturns: one
media%2Fe03%2Fe03b99f7-1ff8-4483-9b8e-a9
media%2Fec0%2Fec0e9434-647d-4915-b086-b3
media%2F016%2F0167f684-10c7-4e9b-9830-cc

CS210-Machine Problem 3 The Ancient Game of Nim 20 Points Asigned: September 14, 2017 Due: September 21, 2017 The game of Nim has existed in various forms since at least the 16th century (according to Wikipedia, so it must be true). It is fairly simple, yet still provides a challenge. Here are the rules that we are going to implement: 1. The game “board” consists of three heaps of stones initially containing 3, 4, and 5 stones each 2. Players alternate turns removing stones from a single pile each turn. 3. A Player make remove as many stones as they wish in a single move, but they must all come from the same pile. 4. The player that removes the last stone wins Our program will labels the heaps as ‘A’, ‘B, and ‘C. A move will be entered as the heap letter followed by the number of stones to be removed. For a move to be legal, the letter must be ‘A’, ‘B’, or ‘C’, the number that follows must be greater than 0 and less than or equal to the number of stones remaining in that heap. See the sample run for some illustrated games. Like last time, your task is to create a program to allow a human to play this game against a computer opponent. Unlike last time, this computer opponent will use a perfect strategy, (e.g., if it goes first, it will always win. If it goes second, and the human makes a mistake, it will win). If you want more information, the wikipedia article provides more information than you will ever need. Your program should allow the player to play as many games as he or she likes. For each new game, the loser of the previous game goes irst. Therefore, if both players play perfectly, the games will be split evenly Program Design Like MP2, this program is going to have many tasks that it needs to accomplish. We will develop one function at a time, solving each smaller task, and then combine them all at the end. I will give you the specification of cach function. You should write the function and then test it with a main program that ensures that the function works by itself. Once a function is working, move on the to the next function, which will require a different sain function to test it. Once we have all the individual functions working, we will finally write the final main that will actually play the game. Print Greeting n Name: print greeting None nct puts: eturns: one

Expert Answer

 

//main.c

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>

void print_greeting();
void print_scoreboard(int human_score, int computer_score);
int play_nim(int starting_player);
void get_computer_move(int heap1, int heap2, int heap3, char* chosen_heap, int* number_removed);
bool user_wishes_to_continue();

int main()
{
int human_score = 0;
int computer_score = 0;
int starting_player = 1;
bool wishes_to_continue = true;
int winner;

print_greeting();
while(wishes_to_continue)
{
winner = play_nim(starting_player);
if(winner == 1)
{
human_score++;
printf(“Player 1 wins!”);
}
else if(winner == 2)
{
computer_score++;
printf(“Player 2 wins!”);
}
print_scoreboard(human_score, computer_score);
wishes_to_continue = user_wishes_to_continue();
}
return 0;
}

void print_greeting()
{
printf(“Welcome to the Ancient Game of Nimn”);
printf(“n”);
printf(“Player 1 is you (the human)n”);
printf(“Player 2 is me (the computer)n”);
}

void print_scoreboard(int human_score, int computer_score)
{
printf(“n***********n”);
printf(“Current Score:n”);
printf(“Player 1 (Human): %dn”, human_score);
printf(“Player 2 (Computer): %dn”, computer_score);
}

int play_nim(int starting_player)
{
int heap_a = 5;
int heap_b = 4;
int heap_c = 3;
char chosen_heap;
int number_removed;
int player_turn;
int bad;

player_turn = starting_player;

if(starting_player == 1)
{
printf(“Player 1 goes first this time!n”);
}
else
{
printf(“Player 2 goes first this time!n”);
}
// This should be AND not OR. You could add up the heap counts and see if the total = 0.
while((heap_a != 0 || heap_b != 0 || heap_c != 0) == true)
{
if(player_turn == 1)
{
printf(“Player 1n”);
printf(“Heaps:n”);
printf(“A: %dn”, heap_a);
printf(“B: %dn”, heap_b);
printf(“C: %dn”, heap_c);
printf(“Enter the letter of the heap and number of objects to remove:”);
scanf(” %c%d”, &chosen_heap, &number_removed);
chosen_heap = tolower(chosen_heap);

if (number_removed <= 0){ // check the number removed first for > 0.
bad = 1;
}
else if((chosen_heap == ‘a’) && (number_removed <= heap_a))
{
bad = 0;
}else if((chosen_heap == ‘b’) && (number_removed <= heap_b))
{
bad = 0;
}
else if((chosen_heap == ‘c’) && (number_removed <= heap_c))
{
bad = 0;
}else{
bad = 1;
}

while(bad == 1)
{
printf(“Illegal move! Try again.n”);
printf(“Player 1n”);
printf(“Heaps:n”);
printf(“A: %dn”, heap_a);
printf(“B: %dn”, heap_b);
printf(“C: %dn”, heap_c);
printf(“Enter the letter of the heap and number of objects to remove:”);
scanf(” %c%d”, &chosen_heap, &number_removed);
chosen_heap = tolower(chosen_heap);

if (number_removed <= 0){ // check the number removed first for > 0.
bad = 1;
}
else if((chosen_heap == ‘a’) && (number_removed <= heap_a))
{
bad = 0;
}else if((chosen_heap == ‘b’) && (number_removed <= heap_b))
{
bad = 0;
}
else if((chosen_heap == ‘c’) && (number_removed <= heap_c))
{
bad = 0;
}else{
bad = 1;
}

}
if(chosen_heap == ‘a’)
{
heap_a = heap_a – number_removed;
}
else if(chosen_heap == ‘b’)
{
heap_b = heap_b – number_removed;
}
else if(chosen_heap == ‘c’)
{
heap_c = heap_c – number_removed;
}
}
else
{
printf(“Player 2n”);
printf(“Heaps:n”);
printf(“A: %dn”, heap_a);
printf(“B: %dn”, heap_b);
printf(“C: %dn”, heap_c);
get_computer_move(heap_a, heap_b, heap_c, &chosen_heap, &number_removed);
printf(“COMPUTER moves %c%dn”, chosen_heap, number_removed);
if(chosen_heap == ‘a’)
{
heap_a = heap_a – number_removed;
}
if(chosen_heap == ‘b’)
{
heap_b = heap_b – number_removed;
}
if(chosen_heap == ‘c’)
{
heap_c = heap_c – number_removed;
}
}
if(heap_a != 0 || heap_b != 0 || heap_c != 0)
{
if(player_turn == 1)
{
player_turn = 2;
}
else
{
player_turn = 1;
}
}
}
return
player_turn;
}

void get_computer_move(int heap1, int heap2, int heap3, char* chosen_heap, int* number_removed) /* gets the move the computer has chosen */
{
int nim_number = heap1 ^ heap2 ^ heap3;
if(nim_number == 0)
{
if(heap1 != 0)
{
heap1–;
*number_removed = 1;
*chosen_heap = ‘a’;
}
else if(heap2 != 0)
{
heap2–;
*number_removed = 1;
*chosen_heap = ‘b’;
}
else
{
heap3–;
*number_removed = 1;
*chosen_heap = ‘c’;
}
}
else
{
if((heap1 ^ nim_number) < heap1)
{
*number_removed = heap1 – (heap1 ^ nim_number);
*chosen_heap = ‘a’;
}
else if((heap2 ^ nim_number) < heap2)
{
*number_removed = heap2 – (heap2 ^ nim_number);
*chosen_heap = ‘b’;
}
else if((heap3 ^ nim_number) < heap3)
{
*number_removed = heap3 – (heap3 ^ nim_number);
*chosen_heap = ‘c’;
}
}
}
bool user_wishes_to_continue() /* prompts the user if they wish to continue playing the game */
{
char play_again;

printf(“Do you wish to play again? (y/n)”);
scanf(” %c”, &play_again);
play_again = tolower(play_again);

if(play_again != ‘y’ && play_again != ‘n’)
{
printf(“Bad input! Try again.n”);
printf(“Do you wish to play again? (y/n)”);
scanf(” %c”, &play_again);
play_again = tolower(play_again);
}
if(play_again == ‘y’)
{
return true;
}
else
{
return false;
}
}

================================================================================

sample output

Still stressed from student homework?
Get quality assistance from academic writers!