-1

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. */
user3186044
  • 1
  • 1
  • 1
  • 1
    What error are you seeing? What is the expected behavior? What have you tried doing? What makes you suspect it is an issue with statics? – einnocent Jan 11 '14 at 21:39
  • I guess what I am asking is how do I make it work or how do I make the moves and nstones work for the instance. Do I need to not use main method and if so how do I make it run? – user3186044 Jan 11 '14 at 22:08

3 Answers3

2

You cannot use a non-static member in a static method, it makes no sense: non-static members (like your variable winner or your method humanMove) belong to an instance of a class (in this case an instance of GameforCompSci). As for static members, they do not belong to a particular instance of GameforCompSci.

This way, you can't reference e.g. winner (non-static variable) in your main method (which is static) because it doesn't exist in this context actually: you're not in an instance of GameforCompSci.

GuiTeK
  • 1,561
  • 5
  • 20
  • 39
  • I guess what I am asking is how do I make it work or how do I make the moves and nstones work for the instance. Do I need to not use main method and if so how do I make it run? – user3186044 Jan 11 '14 at 22:08
1

Think of statics as "one thing". When something is static, there's only ever one of it. Every time you reference it, your pointing to the same object.

When something isnt static, that means theres a separate copy of it for each instance of the object.

So static methods can only ever access other static variables, or things passed to it as parameters. It cant access non-static ones, as it wouldn't know which instance of it you were talking about.

Understanding static/non-static is pretty critical if you want to learn Java. It will save you a lot of work when you understand when to use each.

darkflame
  • 998
  • 1
  • 9
  • 23
0

A static method can only reference other static methods and static fields. The main method must be a static method, but your humanMove and computerMove methods are not static, and while your endgame field is static, winner and nStones are not static.

There are two ways you can fix this. You could add a static keyword to those other fields and methods, or you could create a new, non-static method called, for example, play, and put the current contents of your main method in it. Then change endgame to not be static, and use the following main method to start your program:

public static void main(String[] args) {
    GameforCompSci game = new GameforCompSci();
    game.play();
}

This would create an instance of your class, and then call the instance method play, from which you could reference the other instance methods and instance variables.

David Conrad
  • 15,432
  • 2
  • 42
  • 54