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);
}
}