0

I generate a random start number of straws. The computer goes first and picks 1,2 or 3 straws then the player goes. The person to pick up the last straw loses. I think I almost have the code right but I am getting an error that non-static c cannot be referenced from static context. And I'm not sure what that means. Also, to start the game I am supposed to generate a number between 10 and 20 inclusive. Did I do that correctly? The cup class was given to me.

    import java.util.Random;
        import java.util.Scanner;
        import java.util.ArrayList;
        import java.lang.Math;
        class MyNim
        {
        public static void main(String[] args)
        {
           int humanScore=0;
           int computerScore=0;
           Scanner kb=new Scanner(System.in);
           int n, a;
           String x;
           x="null";
           int straws;
           Random r=new Random();  
           do
           {
              straws=r.nextInt(11)+10;
              System.out.println("Straws = " +straws);
              while(straws>=0)
              {
                 a=Cup.c.select();
                 straws=straws-a;
                 System.out.println("computer chose "+a+" straws.");
                 if(straws<=1)
                 {
                    System.out.println("Human Wins!!!");
                    humanScore++;
                 }

                 System.out.println("Straws left = " +straws);
                 System.out.println("Your turn. Pick 1,2,or 3 straws.");
                 n=kb.nextInt();

                 straws=straws-n;
                 System.out.println("Straws left = " +straws);
                 if(straws<=1)
                 {
                    System.out.println("Human Loses!!!");
                    computerScore++;
                 }
              }
              System.out.println("Scores: Human = "+humanScore+" Computer =      "+computerScore);
              System.out.println("Game Over! Want to play again? Hit enter to start new game or type in quit to exit game.");
              x=kb.nextLine();  

          }while(!x.equals("quit"));
          System.out.println("Thanks for playing!");
          System.exit(1);
          }
        }
        class Cup
        {
           ArrayList<Integer> c = new ArrayList<Integer>();

           public Cup()
           {
              c.add(1);
              c.add(2);
              c.add(3);
           }
           public int count()
           {
              return c.size();
           }
           public int select()
           {
              int index = (int)(c.size()*Math.random());
              return c.get(index);
           }
           public void remove(Integer move)
           {
              c.remove(move);
           }
}
bluenote10
  • 23,414
  • 14
  • 122
  • 178

2 Answers2

1

c is a non-static variable that belongs to the Cup class.

Non-static variables can't be referenced from a static context.

This means that you can't reference this variable in a static method, which main happens to be.


See these answers for more information:

What is the reason behind "non-static method cannot be referenced from a static context"?

Non-static variable cannot be referenced from a static context

Community
  • 1
  • 1
Martin M J
  • 830
  • 1
  • 5
  • 12
  • so if I wanted to access the select method how would I go about that? – meghan my Oct 30 '15 at 02:24
  • A solution is to take all the code you now have in ``main``, put it into a new non-static method called for example ``run``, and write this in main: ``new MyNim().run();`` Because ``run`` is a non-static method, it can access variables like ``c``. – Martin M J Oct 30 '15 at 02:33
  • I still got the same error when I tried using a non-static method. – meghan my Oct 30 '15 at 02:47
0

If you want to use an instance of the Cup class, you have to instantiate it first in the main- method, i.e.

final Cup cup = new Cup();

Then you use the cup, e.g.

final int selected = cup.select();

You should probably not access the member variable c of cup directly. To enforce this, make the List c a private varibale.

static means, that there's only one of the static variable/method per class. "non-static" (a.k.a member) variables/methods exist per instance. For example the list c is a member variable - it is bound to a specific Cup instance. If you have several Cups like so:

Cup cupOne = new Cup();
Cup cupTwo = new Cup();

each will have it's own separate List c.

Accessing member variables/methods works only by specifying the instance they are bound to:

cupOne.select();

If you try to access them through their class, you'll get the error that you saw.

HTH

Nudelsuppe
  • 157
  • 1
  • 12