First off let me announce that I am a noob at Java. I am taking Computer Science and I need help with this code. It is a game, but the problem is that my nStones, computerMove and humanMove methods do not work because of some non static field thing. I have asked everyone in my class, my teacher, and now I am trying the internet. I've looked all over stackoverflow I am a noob and some stuff doesn't make sense. Here it is:
import java.util.*;
public class GameforCompSci
{ /*
To win you must take the last variables so when stone count is zero you lose and the other player wins
you see how many are left there for you determine a strategy to get to be the last one to take it
*/
public static boolean endgame = false;
private int winner = 0;
private int nStones;
public static void main(String[] args)
{
nStones = (int)(Math.random() * (51 - 10) + 10);
System.out.println("Welcome to the game!");
System.out.println("Stones: " + nStones);
int whostarts = 0;
whostarts = (int)(Math.random() * (0 - 2) + 2);
if (whostarts == 1)
{
System.out.println("You start.");
while (nStones > 0){
humanMove(nStones);
System.out.println("Stones: " + nStones);
computerMove(nStones);
System.out.println("Stones: " + nStones);
}
}
else
{ System.out.println("Computer starts.");
while (nStones > 0){
computerMove(nStones);
System.out.println("Stones: " + nStones);
humanMove(nStones);
System.out.println("Stones: " + nStones);
}
}
//endgame logic
if (endgame = true)
{
switch(winner){
case 1:
System.out.println("You win!");
break;
case 2:
System.out.println("You lose, computer wins!");
break;
default:
System.out.println("Something went wront please try again!");
}
System.exit(0);
}
}
public int computerMove(int nStones)
{
int picked = 0;
if (nStones <= 0)
{
winner = 1;
endgame = true;
}
if (nStones > 10){
picked = (int)(Math.random() * (4 - 1) + 1);
nStones = nStones - picked;
}
else
{
switch(nStones)
{
case 10:
picked = 3;
break;
case 9:
picked = 3;
break;
case 8:
picked = 2;
break;
case 7:
picked = 1;
break;
case 6:
picked = 2;
break;
case 5:
picked = 1;
break;
case 4:
picked = 1;
break;
case 3:
picked = 3;
break;
case 2:
picked = 2;
break;
case 1:
picked = 1;
break;
default:
endgame=true;
break;
}
nStones = nStones - picked;
}
return nStones;
}
public int humanMove(int nStones)
{
if (nStones <= 0)
{
winner = 2;
endgame = true;
}
Scanner in = new Scanner(System.in);
System.out.println("How many stones do you take? (Only 1 to 3)");
int n = in.nextInt();
nStones = nStones - n;
if (n == 1 || n == 2 || n == 3)
{
System.out.println("You picked: " + n);
nStones = nStones - n;
}
else
{
System.out.println("Invalid input");
System.out.println("No stones taken");
n=0;
}
return nStones;
}
}
/*
2 players take turns taking stones from a pile. On each move a player must take one, two or three stones. The player who takes the last stone wins.
b) Write a method computerMove:
/*
* Returns the correct number of stones to take
* (according to the winning strategy)
* when nStones remain in the pile.
* If such a move is not possible, returns a random number of stones to take
* Precondition: nStones > 0
*/
//c) Finish the method humanMove.
/*
* Prompts the user to take a number of stones.
* If the move is valid returns the number of stones taken;
* otherwise displays the correct error message –-
* "You are allowed to take 1, 2, or 3 stones, only."
* Or "Can't take that many; only <nStones> stones are left in the pile."
* -- and returns -1;
* Precondition: nStones > 0
d) Write a main method to:
/*
* Generate a random number of stones between 10 and 50
* Store the number of stones in nStones and keep track of it.
* Alternate calling the players, "Computer" and "Human"
* Determine when there is a winner, announce it and end the program.
* You may use more methods to do these steps. */