Reputation: 53
I'm having issues with my loop. The loop i think works fine in theory its just that when I try to you pile it says it needs to be static. I change it to static then generator for the Random number doesnt work anymore any suggestions would be appreciated.
Question 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.
For Example. If the initial pile starts at 100 then player 1 can take anywhere between 1 - 50 marbles. Let's assume player 1 takes 20 marbles cutting the pile down to 80 marbles. Player 2 can take anywhere between 1 - 40 marbles. This continues until all the marbles are gone.
Write a program in which the computer players 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 (a value between 1 and n / 2).
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.
NOTE: You do not have to worry about rounding when the numbers you are working with are odd. Simply use integer division which will truncate (remove with no rounding) the decimal places of the division.
MY CODE
import java.util.Random;
import java.util.Scanner;
public class Nim
{ Random generator= new Random();
int turn= generator.nextInt(2);
int pile= generator.nextInt(91) + 10;
int smartDumb= generator.nextInt(2);
Scanner in= new Scanner(System.in);
public static void main(String [] args){
String Comp;
int currentPile= pile;
while (pile>0){
int take=0;
if(smartDumb==1)
Comp="dumb";
else Comp= "smart";
if (turn == 1)
turn = 0;
else
turn = 1;
{
if (turn==0){
System.out.println("Please take some marbles.");
currentPile=pile-in.nextInt();
turn=1;
}
else
if (comp="dumb")
take= generator.nextInt(1,currentPile);
Currentpile= currentPile-take;
if (comp=="smart")
if (currentPile > 64)
currentPile=63;
if (currentPile > 32 && currentPile < 63)
currentPile=31;
if (currentPile > 16 && currentPile < 31)
currentPile=15;
if (currentPile > 8 && currentPile < 15)
currentPile=7;
if (currentPile > 4 && currentPile < 7)
currentPile=3;
if (currentPile == 2)
currentPile = 1;
if (currentPile==1)
currentPile=0;
System.out.println("The game is over!");
break;
}
}
}}
Upvotes: 0
Views: 3499
Reputation: 5585
While vandale is correct that you should use {}
around the body of your if statements, your immediate problem is that you have a static
method (main
) trying to access non-static fields of the class Nim
. That's not allowed. In order to access a non-static field, you need to have an instance of the class, and you don't. Since you're obviously a beginner, I won't delve too deeply into what all of that means (we'll leave that for your professor). For now, what you need to know is that since main
is static
the only variables it can access are its own (the ones defined inside it), or other static
variables defined outside it.
The reason you're getting the compile error right now is because main
is trying to use
Random generator= new Random();
Which isn't static. So that needs to be changed to
static Random generator = new Random();
Of course, the very next line repeats the same mistake.
int turn= generator.nextInt(2);
should be
static int turn = generator.nextInt(2);
And so on. Static is sticky. Static things can only access other static things.
Another, probably better alternative is to move those declarations inside the main
method. Since you're not using any other methods, there's no reason to declare them at the top level, and you won't have to declare them static
.
Upvotes: 1
Reputation: 3650
I think you need braces following your if's:
if (comp="dumb")
and
if (comp=="smart")
It is also very beneficial to breakdown your code into smaller subroutines so you can test/debug smaller chunks of code at a time
Upvotes: 0