i am making a rock paper scissors game in java for collage and was wondering how to subtract 1 from a for loop.
the full code works but if some one enters a invalid number (lower then 1 or higher then 3) my code asks them to reenter a number(1, 2, 3) but the for loop counts it as a loop so i end up with less moves.
i need to change something in the last "else if" but i cant figure it out
could some one point me in the right direction?
thanks.
the full code is this:
import java.io.*;
import java.util.Random;
public class RockPaperScissors {
static int loss = 0;
static int win = 0;
static int tie = 0;
int draw;
static int playerHand;
static int compHand;
int gameLoop;
public void playerMoves() {
if ( playerHand == compHand ){ //if both hands (player and computer) are the same
System.out.println("Draw, your picked " + playerHand + " and the computer picked " + compHand );
tie++; // add 1 to tie score
}
else if (playerHand == 1 && compHand == 2){ // if player picks Rock and computer picks paper
System.out.println("the computer picks " + compHand + "! " + "Paper beats rock, You lose");
loss++; // add 1 to loss score
}
else if (playerHand == 1 && compHand == 3){ // if player picks rock and computer scissors
System.out.println("the computer picks " + compHand + "! " + "Rock beats Scissors, You win!");
win++; // add 1 to win score
}
else if (playerHand == 2 && compHand == 1){ //if player picks paper and computer picks rock
System.out.println("the computer picks " + compHand + "! " + "Paper beats rock, you win!");
win++; // add 1 to win score
}
else if (playerHand == 2 && compHand == 3){ // if player picks paper and computer scissors
System.out.println("the computer picks " + compHand + "! " + "Scissors beats Paper, you lose!");
loss++; // add 1 to loss score
}
else if (playerHand == 3 && compHand == 1){ // if player picks scissors and computer rock
System.out.println("the computer picks " + compHand + "! " + "Rock beats Scissors, you lose!");
loss++; // add 1 to loss score
}
else if (playerHand == 3 && compHand == 2){ // if player picks scissors and computer paper
System.out.println("the computer picks " + compHand + "! " + "Scissors beats Paper, you win!");
win++; // add 1 to win score
}
else if (playerHand < 1 || playerHand > 3) {
System.out.println(playerHand + " is not a valid number. Try again...");// if not valid number ask again.
gameLoop = gameLoop - 1; // subtract 1 from gameLoop
}
else {
System.out.println("Great job, you broke it...");
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.println("Welcome to Rock Paper Scissors");
System.out.println("Lets play ten games and see if you can outsmart the computer!");
for (int gameLoop = 0; gameLoop < 10; gameLoop++) { // a for loop to keep the game running 10 times
Random randomNumber = new Random(); // create a new random number everytime
compHand = (int) randomNumber.nextInt(3); // generate a random number for the computer (compHand)
compHand++;
// while (playerHand < 1 || playerHand > 3) {
// System.out.println(playerHand + " is not a valid move. Try again...");
System.out.println("Rock(1), Paper(2), or Scissors(3) Please enter the number");
RockPaperScissors draw = new RockPaperScissors();
RockPaperScissors.playerHand = Integer.parseInt(br.readLine());
draw.playerMoves(); // go to public void playerMoves and use that.
System.out.println("the score is: " + win + " Games won. " + loss + " Games lost. " + tie + " Games tie."); // print out the game score at the end of every game
System.out.println("");
}
}
}