I need some help on this exact program Write a program for

/// I need some help on this exact program!! //// Write a program for playing a variation of the Chinese game ”Tsyan-shizi” called the game of NIM. Our version of the game starts with up to 20 rods and up to 10 stones in each rod. Two players take turns removing stones from the rods. On a player’s turn, she chooses a rod and removes one or more stones from that rod. She can remove all stones from the rod. The player who takes the last stone from the rods (so that all rods are empty) wins. You will implement the following algorithm in the main function. Each line in the algorithm will correspond to a function call. Important! Write your code incrementally. This means you should write each function one at a time, check that it compiles and runs properly, test it, and then implement the next function. i. Prompt and read the number of rods ii. Prompt and read the number of stones in each rod; iii. Draw the rods with percentages; iv. Display statistics; v. while (some rod is NOT empty) do vi. Prompt and read the next player’s move (Prompt and read the rod to modify and the number of stones to remove from the rod); vii. Remove the specified number of stones from the specified rod; vii. if (all rods are empty) then ix. Print a message congratulating the winning player. x. else xi. Redraw the rods with percentages; xii. Display statistics; xiii. Change to the other player; xiv. end xv. end ///// Here is a template //// #include #include using namespace std; // FUNCTION PROTOTYPES HERE int main () { // Define variables // algorithm: // prompt and read number of rods // prompt and read the number of objects in each rod\\// draw the rods with percentages // display statistics // WHILE some rod is NOT empty DO // prompt and read the nect player\'s move // remove the specified number of objects from the specified rod // IF all the heaps are empty, THEN // Print a message congratulating the winning player // ELSE // Redraw the rods with percentages // Display statistics // change to the other player // END IF // END WHILE return 0; } // FUNCTION DEFINITIONS GO HERE: //////// Here is a sample output /////// How many rods are in this game? 2 [space line] How many stones are on rod1: 2 How many rods are on rod 2: 3 [space line] Rod 1: ** (40.000%) Rod 2: *** (60.000%) [space line] Rod 1 has the smallest number of stones with 2 object(s). Rod 2 has the largest number of stones with 3 object(s). The average number of stones per rod (i.e., rods with stones) is 2.50 stones. [space line] Player (1) : Which rod would you like to play? 2 Enter number of stones to remove (3 or less) from rod 2: 3 [space line] Rod 1: ** (100.000%) Rod 2: (0.000%) [space line] Rod 1 has the smallest amount of stones with 2 object(s). Rod 1 has the largest amount of stones with 2 object(s). The average number of stones per rod (i.e., rods with stones) is 2.00 stones. [space line] Player (2) : Which rod would you like to play? 1 Enter the number of stones to remove (2 or less) from rod 1: 2 [space line] Congratulations! Player 2 wins.

Solution

Below is the Code for NIM game:

public class NimClass
   {

        //Declare varaiables

        private int [] _board;//the board for the game -> just an array
   
        public NimClass()

        {
           
            NimGame nimGame;//ADDED THIS SO WE CAN DETERMINE WHAT PLAYER IT CURRENTLY IS
            //Determing # of stones in each pile at the beginning.
            _board = new int [4];
            _board[0] = 3;//pile 1 - 3 stones
            _board[1] = 5;//pile 2 - 5 stones
            _board[2] = 7;//pile 3 - 7 stones
            _board[3] = 9;//pile 4 - 9 stones
            System.out.println(\"Welcome to the Game of Nim\ \" +
                               \"You\'ll need to first pick a pile\ \" +
                               \"Then select how many stones you wish to remove\ \");
        }

       //Display the game board
        public void displayBoard()//just setting up the board with default values
        {

            for(int row = 0; row < 4; row++)

            {

                if(_board[row] == 0)

                    System.out.print(\"    0    \");
                else

                    System.out.print(\"     \" + _board[row] + \"   \");

            }

                //Print out the piles

                System.out.println(\"\ Pile 1   Pile 2   Pile 3   Pile 4\ \");

        }
        //Determine the player\'s moves
        public boolean playerMove (int pile, int stones)
        {
          if ((pile < 1) || (pile > _board.length))
          {
              return false;//user specified wrong pile

          }
  
            if (_board[pile - 1] >= stones)
            {
              _board[pile - 1] -= stones;
              return true;
            }

         else
            {
              return false;
            }
        }
           //Determine the winner of the game
        public int determineWinner()
        {
            boolean complete = true;
            int winner = -1;
  
            //Checking the piles to see if they\'re empty.
            if((_board[0] == 0) && ( _board[1]== 0) && ( _board[2]==0) && ( _board[3]==0))
            {
                winner = NimGame.player;//FIXED THIS HERE
                complete = false;//MADE CHANGE HERE!
            }
  
            if(complete == false && winner == 1)
            {
                //Display the results of player 1 winning.
                System.out.println(\"Player 1 Wins!\");
                winner = 0;
            }
            if(complete == false && winner == 2)
            {
                //Display the results of player 2 winning.
                System.out.println(\"Player 2 Wins!\");
                winner = 0;
            }
            return winner;
        }
   }

/// I need some help on this exact program!! //// Write a program for playing a variation of the Chinese game ”Tsyan-shizi” called the game of NIM. Our version
/// I need some help on this exact program!! //// Write a program for playing a variation of the Chinese game ”Tsyan-shizi” called the game of NIM. Our version

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site