I've run into an issue that doesn't seem to be a prevelant one. I'm creating a program that plays a game with the user in which the each rolls virtual dice to go first and then after setting stones (between 10 and 50) each player removes 1-3 stones from the pile and the player to remove the last stone wins.
The issue that I'm running into is that of the turn counter. In the second displayed while loop, all four of the conditions are returning an error:
Nimatron.java:108: error: unexpected type
if (counter%2 = aiRoll)
^
required: variable
found: value
I'm unsure of why the compiler doesn't like the modulo being in the condition, but, in short, my question is: Why doesn't the compiler like this format and; is there a better way to code turns in a java game?
`
//turn assignment
while (true)
{
if (aiRoll > userRoll)
{
aiRoll = 1;
userRoll = 0;
stoneCount = (int)(Math.random()*40)+11;
System.out.println("I will be going first as well as choosing the number of stones in the pile.");
System.out.println("I choose to place " + stoneCount + " stones in the pile.");
break;
}
if (aiRoll < userRoll)
{
aiRoll = 0;
userRoll = 1;
System.out.println("Since you rolled a higher number than me, you will choose the number of stones in the pile.");
System.out.println("How many stones would you like to put in the pile?");
stoneCount = input.nextInt();
input.nextLine();
if (stoneCount > 50 || stoneCount < 10)
{
System.out.println("You have made an invalid selection in the number of stones to place in the pile.");
System.out.println("Please select a number between 10 and 50.");
continue;
}
if (stoneCount <= 50 && stoneCount >= 10)
{
System.out.println("Alright, I will place " + stoneCount + " stones in the pile.");
break;
}
}
if (aiRoll == userRoll)
{
System.out.println("The rolls are the same, so we're going to roll again for a tie-breaker.");
continue;
}
}
//gameplay
while ((int) stoneCount > 0)
{
if (counter%2 = aiRoll)
{
int takeStones = (int)(Math.random()*3)+1;
System.out.println("Since it is my turn, I will remove " + takeStones + " from the pile.");
stoneCount -= takeStones;
continue;
}
if (counter%2 = userRoll)
{
System.out.println("Since it is your turn, you may take stone(s) (between 1 and 3)");
System.out.println("How many stones would you like to take from the pile?");
int takeStones = input.nextInt();
input.nextLine();
stoneCount -= takeStones;
continue;
}
if (stoneCount > 0)
{
counter++;
}
if (stoneCount <= 0)
{
if (counter%2 = aiRoll)
{
System.out.println("I win! Better luck next time...");
break;
}
if (counter%2 = userRoll)
{
System.out.println("Congrats! You have won this round!");
}
}
}
If I left any part of the code out that y'all would need to help, please just let me know and I'll update it.
Aucun commentaire:
Enregistrer un commentaire