This is my Main Method
package game1;
public class game1 {
public static int c;
public static void main(String args[])
{
System.out.print("Welcome to my Game of Nim\n\n");
toss TOSS = new toss();
toothpicks TOOTHPICKS = new toothpicks();
letsPlay START = new letsPlay();
c = TOSS.WhoGoesFirst();
toothpicks.number = TOOTHPICKS.HowMany();
while(toothpicks.number > 1)
{
if(c==0 || c==2)
{
toothpicks.number = START.yourTurn();
}
else if(c==1 || c==2)
{
toothpicks.number = START.myTurn();
}
c=2;
}
}
}
And this is a class which i built to play the game
package game1;
import java.util.Scanner;
public class letsPlay {
Scanner input = new Scanner(System.in);
int remove;
public int yourTurn()
{
if(toothpicks.number == 1)
{
System.out.print("YOU LOSE");
return 0;
}
System.out.print("Your turn. There are " +toothpicks.number+" toothPicks left. ");
System.out.print("How many You want to remove?");
remove = input.nextInt();
toothpicks.number -= remove;
return toothpicks.number;
}
public int myTurn()
{
if(toothpicks.number == 1)
{
System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. \n");
System.out.print("I,the Computer, will remove it\n");
System.out.print("YOU WIN");
remove=1;
toothpicks.number -= remove;
}
else if(toothpicks.number==2)
{
System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
remove = 1;
System.out.print("I,the Computer, will remove "+ remove +" toothPicks\n");
toothpicks.number -= remove;
}
else if(toothpicks.number==3)
{
System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
remove = 2;
System.out.print("I,the Computer, will remove "+remove +" toothPicks\n");
toothpicks.number -= remove;
}
else
{
System.out.print("My turn. There are " +toothpicks.number+" toothPicks left. ");
remove =1 + (int)(Math.random()*4);
System.out.print("I,the Computer, will remove "+remove +" toothPicks.\n");
toothpicks.number-=remove;
}
input.close();
return toothpicks.number;
}
}
EDIT :This is toothpicks Class
public class toothpicks {
public static int number;
public int HowMany()
{
Scanner input =new Scanner(System.in);
do
{
if(game1.c==1)
{
System.out.print("I choose the no. of toothPicks to be " );
number = input.nextInt();
if(number<20 || number>30)
{
System.out.print("ToothPicks Should be between 20-30\n");
}
}
else
{
number = 20 + (int)(Math.random()*10);
System.out.print("computer has choosed "+ number +" Toothpicks \n");
}
}while(number<20 || number>30);
input.close();
return number;
}
}
I also made other class toss for toss. When I run this program it cannot work something going wrong in letsPlay class which i cannot figure out yet as I'm very new to JAVA
, Program stops before it will ask me to input how many toothpicks i want to remove. Is anything is going wrong with the scanner input or what is it?Any help would be appreciated.
OUTPUT Welcome to my Game of Nim
This time Computer are going to go first
I choose the no. of toothPicks to be 23
My turn. There are 23 toothPicks left. I,the Computer, will remove 4 toothPicks.
Your turn. There are 19 toothPicks left. How many You want to remove? Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at game1.letsPlay.yourTurn(letsPlay.java:18)
at game1.game1.main(game1.java:21)
Please Note number
is a static variable which I declare in toothpicks
class.