Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The purpose of this lab assignment is Problem Statement: 1. In Project 1, you will write a program to simulate a simple game named
The purpose of this lab assignment is Problem Statement: 1. In Project 1, you will write a program to simulate a simple game named Nim: two players alternately take marbles from a pile. In each turn, a player must make a legal move taking at least one but at most half of the marbles from the pile and then let the other player take a turn. The player who is to take the last marble loses the game. to continue practicing how to define your own functions to help you make big progress with Project 1 This Lab assignment will help you solve two sub-problems of Project 1: complete the definitions of TWO functions, named computer_play and printMarbles, shown as follows: /* * It represents the legal move when it is the computer's turn to play The computer takes "smart strategy" trying to win the game: (1) if the current pile size is not a Power-of-Two-minus-One, it * takes * it enough marbles to make the remaining size a Power-of-Two- (2) if the current pile size is already a Power-of-Two-minus-One, minus-One makes a random move taking at least one but at most half *@param pile int&: the pile size before and after the computer's turn, call by reference *@return: void function */ void computer_play (int& pile) It represents the legal move when it is the computer's turn to play Nim game. The computer always takes "smart strategy" trying to win the game. Under "smart strategy", the computer needs to make the following legal move to win the game: (1) take off enough marbles to make the size of the remaining pile a Power-of-Two-minus-One (1, 3, 7, 15, 31, and so on, are some examples of the Power-of-Two-minus-One) if the current pile size is not one of those Power-of-Two-minus-One; (2) When the current pile size is already a Power-of-Two-minus-One, make a random move taking at least one but at most half of the marbles. *** void computer_play (int& pile) It represents the legal move when it is the computer's turn to play Nim game. The computer always takes "smart strategy" trying to win the game. Under "smart strategy", the computer needs to make the following legal move to win the game: (1) take off enough marbles to make the size of the remaining pile a Power-of-Two-minus-One (1, 3, 7, 15, 31, and so on, are some examples of the Power-of-Two-minus-One) if the current pile size is not one of those Power-of-Two-minus-One; (2) When the current pile size is already a Power-of-Two-minus-One, make a random move taking at least one but at most half of the marbles. After each legal move either from the computer or the human player, your program should display the number of marbles on current pile (you are required to use constant named MARBLE to display on screen: cout < < MARBLE; and it represents a single marble on the pile). Complete the definition of the following function: /* * It displays the marbles on the pile: the number of marbles depends on passing in parameter *@param pile int: representing how many marbles are on the pile to display *@return: void function void printMarbles (int pile) #include #include #include #include #include using namespace std; // define some constants const int PILE MAX= 50; const int PILE MIN = 10; const char MARBLE static castechar(232); //use code 232 in ASCII Table to represent sarble const int WIDTH= 10; // for Layout purpose const int PILE SIZE TEST 50; //the pile size for testing purpose in Labs only // void printfarbles(int pile) \\ It displays the sarbles on the pile: the number of marbles depends on passing in parameter paran pile int: representing how many marbles are on the pile to display @return: void function [/ // display marbles on the pile void printfarblegint pile); // void computer.play(int& pile) \\ It represents the legal move when it is the computer's turn to play The computer takes "snart strategy" trying to win the game: (1) if the current pile size is not a Poser-of-Teo-sinus-One, it takes enough marbles to make the remaining size a Power-of-Two- minus-One I (2) if the current pile size is already a Power-of-Two-minus-One, it makes a randon move taking at least one but at most half @param pile int&: the pile size before and after the computer's turn, call by reference @return: void function */ // represent the legal move when it is the computer's turn to play the game void computer play (int& pile); int pilesize= PILE_MAX; while (pilesize >= 1) if (pilesize == ) Bint main() int seed static_castint>(time(0)); srand(seed); // Generate the initial size of pile (of marbles) // at the range of [PILE MIN, PILE_MAX] // for testing purpose only: we "hard-code" the initial size of pile to a constant PILE_SIZE_TEST int pilesize PILE_SIZE_TEST; // comment out the testing purpose at line number 40, enable the following statement, which is the general case //int pileSize = PILE MIN + rand() (PILE MAX - PILE MIN + 1); cout < < *************** cout < cout < < cout < < " In combinatorial game theory: a two-player deterministic perfect information turn-based game cout < < is a first-player-win, if with perfect play the first player to move, always force a win cout < < I will show you (not prove to you) that cout Phot " the game of Nin is a CONDITIONAL first-player-win game :) The name of Min 2 A **** < < endl; < < endl; < < endl; < < endl; < < en < < endl; "
Step by Step Solution
★★★★★
3.59 Rating (156 Votes )
There are 3 Steps involved in it
Step: 1
c include stdio h include stdlib h include time h Function to check if a number is a poweroftwominus...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started