Question

In: Computer Science

The game of Nim. This is a well-known game with a number of variants. The following...

The game of Nim. This is a well-known game with a number of variants. The following variant has an interesting winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. Then the other player takes a turn. The player who takes the last marble loses.

Write a program in which the computer plays against a human opponent. Generate a random integer between 10 and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1 to decide whether the computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the computer plays smart or stupid. In stupid mode, the computer simply takes a random legal value (between 1 and n/2) from the pile whenever it has a turn. In smart mode the computer takes off enough marbles to make the size of the pile a power of two minus 1 - that is, 3, 7, 15, 31, or 63. That is always a legal move, except when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move.

You will note that the computer cannot be beaten in smart mode when it has the first move unless the pile size happens to be 15, 31, or 63. Of course, a human player who has the first turn and knows the winning strategy can win against the computer.

Make sure your program tells the user whether the computer played in smart or stupid mode.

Your program will need to use both decisions (if statements) and loops.

method returns a double value >= 0.0 and < 1.0. To get a random integer between two integer values (Min and Max) use:

Min + (int)(Math.random() * ((Max - Min) + 1))

Write pseudocode and/or a flow chart before you try to write this program. As you write your program, a good practice would be to write and test it in parts. For example, generate the initial size of the pile, which player goes first, and what mode the computer plays in and output without actually playing the game. Then play one round and end the game, etc.

Submit:
Pseudocode for your solution.
Your Java code as a .java file.

Solutions

Expert Solution

Pseudocode

1. n = a random integer between 10 and 100

2.. mode = a random integer between 0 and 1

3. Repeat While True:

4. Print: n + " marbles left"

5. IF n==0 Then

6 Print "You won!"

7. Else

8. Print : "How many marbles would you like to remove from pile?"

9. Read m

10. If m<=0 Or  m>n/2 Then

Goto Step 8.

11. n = n - m;

12. Print: n + " marbles left"

13. If n==0 Then

14. Print "Computer won!"

15. Else

16. If n==1  Then m=1
17. Else If mode==0 Then

18.  m = a random integer between 1 and n/2

19. Else If mode ==1 And n>=6 Then

20. m = a random integer between 2 and 5

21.  m = Power(m, 2) -1

22. If m<=0 Or m>n/2 Then

23. Goto 20

24. Else

25. m = a random integer between 1 and n/2

26. If m<=0 Or  m>n/2 Then

27, Goto 25

28. n = n - m

29. Print "The computer has taken " + m + " marbles from pile"

[End of loop]

30.End

Program

import java.util.Scanner;

//class Nim
class Nim
{
   //main method
   public static void main (String[] args)
   {
       //create instance of Scanner class and Random class
       Scanner sc = new Scanner(System.in);
      
       //Generate a random integer between 10 and 100 (initial size of the pile)
       int n = (int)(Math.random() * 91) + 10;
      
       //Generate a random integer between 0 and 1 (computer plays mode: smart or stupid)
       int mode = (int)(Math.random() * 2);
      
       if(mode==0)
           System.out.println ("Stupid mode");
       else
           System.out.println ("Smart mode");
          
       int m;
      
       //start play game
       while(true)
       {
           //display current state of the board
           System.out.println (n + " marbles left\n");
      
           //check for end of game
           if(n==0)
           {
               System.out.println ("You won!");
               break;
           }
          
           //prompt and read stone number from user
           do
           {
               System.out.print ("How many marbles would you like to remove from pile?");
               m = sc.nextInt();
           }while(m<=0 || m>n/2&&n!=1);
          
           //decrease number of stones
           n = n - m;
          
           //display current state of the board
           System.out.println (n + " marbles left\n");
          
           //check for end of game
           if(n==0)
           {
               System.out.println ("Computer won!");
               break;
           }
          
           //Computer's turn
      
           //select stone numbers randomly
          
           if(n==1) m=1;
          
           else if(mode==0)
           {
               do
               {
                   m = (int)(Math.random() * n/2) + 1;
               }while(m<=0|| m>n/2);
           }
           else
           {
           if(n>=6)
           {
               do
                   {
                       m = (int)(Math.random() * 5) + 2;
                       m = (int)Math.pow(m,2) - 1;
                   }while(m<=0 || m>n/2);
           }
           else
           {
               do
                   {
                       m = (int)(Math.random() * n/2) + 1;
                   }while(m<=0|| m>n/2);
           }
           }

           //decrease number of stones
           n = n - m;
          
           //display computer selections
           System.out.println ("The computer has taken " + m + " marbles from pile.\n");
       }
   }
}

Output:


Stupid mode
99 marbles left

How many marbles would you like to remove from pile?31
68 marbles left

The computer has taken 28 marbles from pile.

40 marbles left

How many marbles would you like to remove from pile?15
25 marbles left

The computer has taken 10 marbles from pile.

15 marbles left

How many marbles would you like to remove from pile?7
8 marbles left

The computer has taken 4 marbles from pile.

4 marbles left

How many marbles would you like to remove from pile?1
3 marbles left

The computer has taken 1 marbles from pile.

2 marbles left

How many marbles would you like to remove from pile?1
1 marbles left

The computer has taken 1 marbles from pile.

0 marbles left

You won!


Related Solutions

Nim Game Java, PegClass One variation of the game of Nim, described in Chapter 5 Exercise...
Nim Game Java, PegClass One variation of the game of Nim, described in Chapter 5 Exercise 12, is played with four piles of stones. Initially, the piles contain 1, 2, 3, and 4 stones. On each turn, a player may take 1, 2, or 3 stones from a single pile. The player who takes the last stone loses. a) Write a program that allows two players to play Nim. Be sure to allow only legal moves. The program should be...
How to prove that player I can always win a Nim game in which hte number...
How to prove that player I can always win a Nim game in which hte number of heaps with an odd number of coins is odd. I understand the objective, having trouble formuating a more formula argument for the problem.
Using induction: Show that player 1 can always win a Nim game in which the number...
Using induction: Show that player 1 can always win a Nim game in which the number of heaps with an odd number of coins is odd.
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
Write a program in Basic to play the game of Nim with acom­puter.
Write a program in Basic to play the game of Nim with acom­puter.
The Brain Game The purpose of this assignment is to introduce you to the well-known scientific...
The Brain Game The purpose of this assignment is to introduce you to the well-known scientific fact that humans do not make decisions rationally. Most of the time, decisions are made without people even realizing the “why” behind their decisions. Our decisions are often driven by our own biases instead of logic or good judgement. These numerous “poor instincts” are called cognitive biases. It is important to identify cognitive biases in Personal Finance because they cause so many of the...
Prove the following statement: Suppose it's your turn and the Nim sum of the number of...
Prove the following statement: Suppose it's your turn and the Nim sum of the number of coins in the heaps is equal to 0. Then whatever you do, the Nim sum of the number of coins after your move will not be equal to 0.
A and B play the following game: A writes down either number 1 or number 2,...
A and B play the following game: A writes down either number 1 or number 2, and B must guess which one. If the number that A has written down is i and B has guessed correctly, B receives i units from A. If B makes a wrong guess, B pays i unit to A. If B randomizes his decision by guessing 1 with probability p and 2 with probability 1 - p, determine his expected gain if (a) A...
discuss five well known microfinance approaches
discuss five well known microfinance approaches
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm...
Consider the following variants of the Towers of Hanoi. For each of variant, describe an algorithm to solve it in as few moves as possible. Prove that your algorithm is correct. Initially, all the n disks are on peg 1, and you need to move the disks to peg 2. In all the following variants, you are not allowed to put a bigger disk on top of a smaller disk. Consider the disappearing Tower of Hanoi puzzle where the largest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT