I am creating a program that allows you to play a game similar to paper, scissors rock.
The game works, but I am trying to incorporate a loop into the code that will ask the user if they wish to continue playing.
If yes, it will ask them for another input.
If no, the program will simply state "Thanks for playing"
Here's the code:
import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
public static void main(String[]args)
{
String yourMove;
String compMove;
int compInt;
String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};
Scanner input = new Scanner(System.in);
Random rand = new Random(6);
compInt = rand.nextInt(5)+1;
if (compInt == 1)
compMove = "Monkey";
else if (compInt == 2)
compMove = "Robot";
else if (compInt == 3)
compMove = "Pirate";
else if (compInt == 4)
compMove = "Ninja";
else if (compInt == 5)
compMove = "Zombie";
System.out.println("What do you choose?: ");
yourMove = input.next();
//MONKEY
if(yourMove == "Monkey" || compInt == 1)
System.out.println("Tie");
else if (yourMove== "Monkey" || compInt == 2)
System.out.println("You Win! Monkey Unplugs Robot!");
else if (yourMove=="Monkey" || compInt == 3)
System.out.println("You Lose! Pirate Skewers Monkey!");
else if (yourMove == "Monkey" || compInt==4)
System.out.println("You Win! Monkey fools Ninja!");
else if (yourMove== "Monkey" || compInt==5)
System.out.println("You Lose! Zombie savages monkey!");
//RoBOT
else if(yourMove == "Robot" || compInt == 2)
System.out.println("Tie");
else if (yourMove== "Robot" || compInt == 1)
System.out.println("You Lose! Monkey Unplugs Robot!");
else if (yourMove=="Robot" || compInt == 3)
System.out.println("You Lose! Pirate Drowns Robot!!");
else if (yourMove == "Robot" || compInt==4)
System.out.println("You Win! Robot Chokes Ninja");
else if (yourMove== "Robot" || compInt==5)
System.out.println("You win! Robot Crushes Zombie!");
//PIRATE
else if(yourMove == "Pirate" || compInt == 3)
System.out.println("Tie");
else if (yourMove== "Pirate" || compInt == 1)
System.out.println("You Win! Pirate Skewers Monkey!");
else if (yourMove=="Pirate" || compInt == 2)
System.out.println("You Win! Pirate Drowns Robot!");
else if (yourMove == "Pirate" || compInt==4)
System.out.println("You Lose! Ninja Karate Chops Pirate!");
else if (yourMove== "Pirate" || compInt==5)
System.out.println("You Lose! Zombie Eats Pirate!");
//NINJA
else if(yourMove == "Ninja" || compInt == 4)
System.out.println("Tie");
else if (yourMove== "Ninja" || compInt == 1)
System.out.println("You Lose! Monkey Fools Ninja!");
else if (yourMove=="Ninja" || compInt == 2)
System.out.println("You Lose! Robot Chokes Ninja!");
else if (yourMove == "Ninja" || compInt==3)
System.out.println("You Win! Ninja Karate Chops Pirate!");
else if (yourMove== "Ninja" || compInt==5)
System.out.println("You Win! Ninja Decapitates Zombie!");
//ZOMBIE
else if(yourMove == "Zombie" || compInt == 5)
System.out.println("Tie");
else if (yourMove== "Zombie" || compInt == 1)
System.out.println("You Win! Zombie Savages Monkey!");
else if (yourMove=="Zombie" || compInt == 2)
System.out.println("You Lose! Robot Crushes Zombie!");
else if (yourMove == "Zombie" || compInt==3)
System.out.println("You Win! Zombie Eats Pirate!");
else if (yourMove== "Zombie" || compInt==4)
System.out.println("You Lose! Ninja Decapitates Zombie!");
}
}
You could put the entire logic inside a do-while loop. The condition will be if the character entered is=='y' or 'Y'. The pseudo code should be:
char choice='n';
do
{
< Insert Game logic here >
System.out.println("Do you wanna continue? Enter y or Y for Yes")'
choice = <obtain input using Scanner here>;
}
while(choice=='y'||choice=='Y');
System.out.println("Thanks for Playing");
Well...let's be honest here. Your code actually doesn't work. I mean, yes it runs but it will definitely not carry out the game play properly.
Your random generator for computer game character selection will not work properly. You will need to do it this way:
Random rand = new Random();
compInt = rand.nextInt(5)+1;
Leave the 6 out of the initialization of rand.
The logic within the conditions for all your IF and IF/ELSE statements is set up so that at no time will any User input check past the MONKEY game character and this is because you use the OR (||) operator instead of the AND (&&) operator. The way you have it set up, if the computer chooses ROBOT and a User chooses ZOMBIE then the condition contained within:
else if (yourMove == "Monkey" || compInt == 2) {
System.out.println("You Win! Monkey Unplugs Robot!");
}
will always fall true and display the message:
You Win! Monkey Unplugs Robot!
even though the User entered Zombie. Remember...the condition is basically set to be either or which is exactly what you don't want. You want both conditions to be true so you need to use the AND (&&) operator.
Besides that:
yourMove == "Monkey"
will always give unexpected results and here's why. Use the String.equals() method instead, something like:
yourMove.equals("Monkey")
You declare the characters variable as a string array yet nowhere do you utilize that Array. Why even bother. Just make the characters variable a space delimited String and utilize it to see if the User actually has supplied a character within the game play, for example:
String characters = "Monkey Robot Pirate Ninja Zombie";
yourMove = "";
while (yourMove.equals("")) {
System.out.println("What do you choose? --> ");
yourMove = input.nextLine().toLowerCase();
if (yourMove.equals("quit")) {
System.out.println("Thanks for playing. Bye Bye");
System.exit(0);
}
if (!characters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
System.out.println("You entered an invalid Game Character! Try again...\n");
continue;
}
}
In any case, here is a complete working game:
package okekpejmoropinzee;
import java.util.Random;
import java.util.Scanner;
public class OkekpeJMoropinzee {
static boolean playAgain = true;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (playAgain == true) {
playGame(input);
}
System.out.println("Thanks for playing");
input.close();
}
private static void playGame(Scanner input) {
String yourMove = "", compMove = "";
int compInt = 0;
String gameCharacters = "Monkey Robot Pirate Ninja Zombie";
Random rand = new Random();
compInt = rand.nextInt(5)+1;
if (compInt == 1) { compMove = "Monkey"; }
else if (compInt == 2) { compMove = "Robot"; }
else if (compInt == 3) { compMove = "Pirate"; }
else if (compInt == 4) { compMove = "Ninja"; }
else if (compInt == 5) { compMove = "Zombie"; }
System.out.println(compMove + " --- " + compInt);
yourMove = "";
while (yourMove.equals("")) {
System.out.println("What do you choose? --> ");
yourMove = input.nextLine().toLowerCase();
if (yourMove.equals("quit")) {
System.out.println("Thanks for playing. Bye Bye");
System.exit(0);
}
if (!gameCharacters.toLowerCase().contains(yourMove) || yourMove.equals("")) {
System.out.println("You entered an invalid Game Character! Try again...\n");
continue;
}
}
//MONKEY
if (yourMove.equals("monkey") && compInt == 1) {
System.out.println("Tie");
}
else if (yourMove.equals("monkey") && compInt == 2) {
System.out.println("You Win! Monkey Unplugs Robot!");
}
else if (yourMove.equals("monkey") && compInt == 3) {
System.out.println("You Lose! Pirate Skewers Monkey!");
}
else if (yourMove.equals("monkey") && compInt == 4) {
System.out.println("You Win! Monkey fools Ninja!");
}
else if (yourMove.equals("monkey") && compInt == 5) {
System.out.println("You Lose! Zombie savages monkey!");
}
//ROBOT
else if (yourMove.equals("robot") && compInt == 2) {
System.out.println("Tie");
}
else if (yourMove.equals("robot") && compInt == 1) {
System.out.println("You Lose! Monkey Unplugs Robot!");
}
else if (yourMove.equals("robot") && compInt == 3) {
System.out.println("You Lose! Pirate Drowns Robot!!");
}
else if (yourMove.equals("robot") && compInt == 4) {
System.out.println("You Win! Robot Chokes Ninja");
}
else if (yourMove.equals("robot") && compInt == 5) {
System.out.println("You win! Robot Crushes Zombie!");
}
//PIRATE
else if (yourMove.equals("pirate") && compInt == 3) {
System.out.println("Tie");
}
else if (yourMove.equals("pirate") && compInt == 1) {
System.out.println("You Win! Pirate Skewers Monkey!");
}
else if (yourMove.equals("pirate") && compInt == 2) {
System.out.println("You Win! Pirate Drowns Robot!");
}
else if (yourMove.equals("pirate") && compInt == 4) {
System.out.println("You Lose! Ninja Karate Chops Pirate!");
}
else if (yourMove.equals("pirate") && compInt == 5) {
System.out.println("You Lose! Zombie Eats Pirate!");
}
//NINJA
else if(yourMove.equals("ninja") && compInt == 4) {
System.out.println("Tie");
}
else if (yourMove.equals("ninja") && compInt == 1) {
System.out.println("You Lose! Monkey Fools Ninja!");
}
else if (yourMove.equals("ninja") && compInt == 2) {
System.out.println("You Lose! Robot Chokes Ninja!");
}
else if (yourMove.equals("ninja") && compInt == 3) {
System.out.println("You Win! Ninja Karate Chops Pirate!");
}
else if (yourMove.equals("ninja") && compInt == 5) {
System.out.println("You Win! Ninja Decapitates Zombie!");
}
//ZOMBIE
else if(yourMove.equals("zombie") && compInt == 5) {
System.out.println("Tie");
}
else if (yourMove.equals("zombie") && compInt == 1) {
System.out.println("You Win! Zombie Savages Monkey!");
}
else if (yourMove.equals("zombie") && compInt == 2) {
System.out.println("You Lose! Robot Crushes Zombie!");
}
else if (yourMove.equals("zombie") && compInt==3) {
System.out.println("You Win! Zombie Eats Pirate!");
}
else if (yourMove.equals("zombie") && compInt==4) {
System.out.println("You Lose! Ninja Decapitates Zombie!");
}
//Ask if User wants to play the game again...
String playMore = "";
while (!playMore.equals("y") && !playMore.equals("n")) {
System.out.println("\nDo you want to play another game? (y/n) ");
playMore = input.nextLine().toLowerCase();
}
if (playMore.equals("n")) { playAgain = false; }
}
}
You could do something like this:
import java.util.Scanner;
import java.util.Random;
public class OkekpeJMoropinzee
{
public static void main(String[]args)
{
String playAgain;
do{
String yourMove;
String compMove;
int compInt;
String[] characters = {"Monkey","Robot","Pirate","Ninja","Zombie"};
Scanner input = new Scanner(System.in);
Random rand = new Random(6);
compInt = rand.nextInt(5)+1;
if (compInt == 1)
compMove = "Monkey";
else if (compInt == 2)
compMove = "Robot";
else if (compInt == 3)
compMove = "Pirate";
else if (compInt == 4)
compMove = "Ninja";
else if (compInt == 5)
compMove = "Zombie";
System.out.println("What do you choose?: ");
yourMove = input.next();
//MONKEY
if(yourMove == "Monkey" || compInt == 1)
System.out.println("Tie");
else if (yourMove== "Monkey" || compInt == 2)
System.out.println("You Win! Monkey Unplugs Robot!");
else if (yourMove=="Monkey" || compInt == 3)
System.out.println("You Lose! Pirate Skewers Monkey!");
else if (yourMove == "Monkey" || compInt==4)
System.out.println("You Win! Monkey fools Ninja!");
else if (yourMove== "Monkey" || compInt==5)
System.out.println("You Lose! Zombie savages monkey!");
//RoBOT
else if(yourMove == "Robot" || compInt == 2)
System.out.println("Tie");
else if (yourMove== "Robot" || compInt == 1)
System.out.println("You Lose! Monkey Unplugs Robot!");
else if (yourMove=="Robot" || compInt == 3)
System.out.println("You Lose! Pirate Drowns Robot!!");
else if (yourMove == "Robot" || compInt==4)
System.out.println("You Win! Robot Chokes Ninja");
else if (yourMove== "Robot" || compInt==5)
System.out.println("You win! Robot Crushes Zombie!");
//PIRATE
else if(yourMove == "Pirate" || compInt == 3)
System.out.println("Tie");
else if (yourMove== "Pirate" || compInt == 1)
System.out.println("You Win! Pirate Skewers Monkey!");
else if (yourMove=="Pirate" || compInt == 2)
System.out.println("You Win! Pirate Drowns Robot!");
else if (yourMove == "Pirate" || compInt==4)
System.out.println("You Lose! Ninja Karate Chops Pirate!");
else if (yourMove== "Pirate" || compInt==5)
System.out.println("You Lose! Zombie Eats Pirate!");
//NINJA
else if(yourMove == "Ninja" || compInt == 4)
System.out.println("Tie");
else if (yourMove== "Ninja" || compInt == 1)
System.out.println("You Lose! Monkey Fools Ninja!");
else if (yourMove=="Ninja" || compInt == 2)
System.out.println("You Lose! Robot Chokes Ninja!");
else if (yourMove == "Ninja" || compInt==3)
System.out.println("You Win! Ninja Karate Chops Pirate!");
else if (yourMove== "Ninja" || compInt==5)
System.out.println("You Win! Ninja Decapitates Zombie!");
//ZOMBIE
else if(yourMove == "Zombie" || compInt == 5)
System.out.println("Tie");
else if (yourMove== "Zombie" || compInt == 1)
System.out.println("You Win! Zombie Savages Monkey!");
else if (yourMove=="Zombie" || compInt == 2)
System.out.println("You Lose! Robot Crushes Zombie!");
else if (yourMove == "Zombie" || compInt==3)
System.out.println("You Win! Zombie Eats Pirate!");
else if (yourMove== "Zombie" || compInt==4)
System.out.println("You Lose! Ninja Decapitates Zombie!");
System.out.println("Would you like to play again? Type yes to play again.");
playAgain = input.next();
} while(playAgain.equals("yes"));
}
}
Related
So i'm working on a card game where it's almost like rock paper scissors. here's the rules, emperor beats citizen, citizen beats slave, and slave beats emperor. there's a side with 4 citizens and a slave and the other side plays with 4 citizens and an emperor. I have set number values equal to each of the cards but for some reason I cannot seem to make the program continue without using 1 to play the emperor.
public static void emperorsTurn() {
Random cards = new Random();
int numberx = 0;
for (int counter = 1; counter <= 3; counter++) {
numberx = 1 + cards.nextInt(5);
}
Scanner sc = new Scanner(System.in);
System.out.println("Please pick the card you are playing. \n
if you are playing the Emperor press 1,
if you are playing the citizen press 2");
int eOS = sc.nextInt(); //fix the input
if (eOS == 1 && numberx == 2) {
System.out.println("you have played the emperor! \n
the emperor defeats the citizen");
}
if (eOS == 1 && numberx == 1) {
System.out.println("you have played the emperor! \n
the emperor is defeated by the slave");
if (eOS == 2 && numberx == 1) {
System.out.println("you have played the citizen, this defeats the slave");
if (eOS == 2 && numberx == 2) {
System.out.println("you have played the citizen, this ties with the citizen");
if (eOS == 2 && numberx == 3) {
System.out.println("you have played the citizen, this defeats the slave");
}
}
}
}
}
That is because this part of the code (commented) will never be executed since eOs must always be 1. The if condition will always fail if eOs is any other number:
if (eOS == 1 && numberx == 1) {
System.out.println("you have played the emperor! \n the emperor
is defeated by the slave");
/*if (eOS == 2 && numberx == 1) {
System.out.println("you have played the citizen, this
defeats the slave");
if (eOS == 2 && numberx == 2) {
System.out.println("you have played the citizen, this
ties with the citizen");
if (eOS == 2 && numberx == 3) {
System.out.println("you have played the citizen,
this defeats the slave");*/
}}}}}
To achieve what you want, you must rewrite your code to be:
if (eOS == 1 && numberx == 1) {
System.out.println("you have played the emperor! \n the emperor
is defeated by the slave");
}
else if (eOS == 2)
{
if (numberx == 1) {
System.out.println("you have played the citizen, this
defeats the slave");
}
else if (numberx == 2) {
System.out.println("you have played the citizen, this
ties with the citizen");
}
else if (numberx == 3) {
System.out.println("you have played the citizen,
this defeats the slave");
}
else
{
//print out something else if number is not 1,2 or 3
}
}
OR you can do it as:
switch(eOs)
{
case 1:
if(numberx == 1) {
System.out.println("you have played the emperor! \n the emperor
is defeated by the slave");
}
break;
case 2:
if (numberx == 1) {
System.out.println("you have played the citizen, this
defeats the slave");
}
else if (numberx == 2) {
System.out.println("you have played the citizen, this
ties with the citizen");
}
else if (numberx == 3) {
System.out.println("you have played the citizen,
this defeats the slave");
}
else
{
//print out something else if number is not 1,2 or 3
}
break;
}
EDIT
Also, I would suggest that if you are going to have a combination of
if(eOS ==1 && numberx== //some other values apart from 1)
{
}
you break it down since numberx is randomized and can have other values other than 1, to be like:
if (eOS == 1) {
if(numberx==1)
{
//print out something;
}
else if(numberx==//another value e.g. 2)
{
//print out something else;
}
else{
//
}
}
else if(eOS==2)//the rest of the code
I am trying to get the "press q to quit" function to work properly, but I'm having some trouble. The "invalid Character" function is also not working properly. How would I go about fixing this? My professor suggested putting the "press q to quit" function in the beginning of my code, but hasn't given me any further instruction.
public static void main(String[] args) {
char userChar = 0;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");
// If player chooses to quit
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
}
// Start of loop
while (userChar != 'q' && userChar != 'Q') {
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
}
// If computer generates 2 (Paper)
else if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
}
// If computer generates 3 (Scissors)
else if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
}
// If player types an invalid character
else {
System.out.println("Invalid input! Please enter a valid character.");
}
userChar = sc.next().charAt(0);
}
}
}
In order to really take care of invalid input you need to do it first thing in the loop. Moreover you can't do it in the else block of your if-else statements because in those if-else you check the randomNumber, not the input, so it doesn't make sense and it will never work. In addition you check the input as what you get from sc.next().charAt(0) but for example if the user insert a string like "R_invalid_something_without_space" you only look at the first letter which is R and you take it as a valid input. I modified your code so that you read a line in each loop iteration and if this line is not q / r / p / s it will consider as invalid input and if its q the program will finish.
public static void main(String[] args) {
char userChar = 0;
Scanner sc = new Scanner(System.in);
Random rnd = new Random();
// Intro/directions/prompting for user input
System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");
// Start of loop
while (true) {
// what I changed
// **********************************************************
String line = sc.nextLine().toLowerCase();
if(line.equals("q")){
System.out.println("Player chose to quit. Goodbye!");
break;
}
if(!line.equals("r") && !line.equals("s") && !line.equals("p")) {
System.out.println("Invalid input! Please enter a valid character.");
continue;
}
userChar = line.charAt(0);
// **********************************************************
// Prompting computer to generate a random number
int randomNumber = rnd.nextInt(3) + 1;
// If computer generates 1 (Rock)
if (randomNumber == 1) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock vs. Rock! It's a tie!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper covers Rock, you win!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Rock breaks Scissors, you lose!");
}
}
// If computer generates 2 (Paper)
else if (randomNumber == 2) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Paper covers Rock, you lose!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Paper vs. Paper! It's a tie!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors cuts Paper, you win!");
}
}
// If computer generates 3 (Scissors)
else if (randomNumber == 3) {
if (userChar == 'r' || userChar == 'R') {
System.out.println("Rock breaks Scissors, you win!");
} else if (userChar == 'p' || userChar == 'P') {
System.out.println("Scissors cuts Paper, you lose!");
} else if (userChar == 's' || userChar == 'S') {
System.out.println("Scissors vs. Scissors! It's a tie!");
}
}
}
}
Well, firstly your condition to quit is outside of scope of while loop which means it will never execute in this given context.
Secondly, condition in your while loop will not be true unless userChar is not q and Q simultaneously, which doesn't make sense.
What I suggest is to refactor your while loop little bit, as such:
while (true) {
//Your logic here
if (userChar == 'q' || userChar == 'Q') {
System.out.println("Player chose to quit. Goodbye!");
break;
}
//Your logic here
}
I created a simple Tic Tac Toe game. Everything runs perfect except that at the end of the game, when the program decides the outcome it prints the last line twice. For example if player 1 wins it prints:
Player 1 has won
Player 1 has won
I've tried to write input.nextLine(); after the user enters the integer but with no luck.
Thanks in advance.
Here is my code:
public static void main(String[] args) {
//The board
char board[][] = new char [3][3];
//Players' characters
char player1 = 'X';
char player2 = 'O';
char turn = 0;
int rowNumber;
int columnNumber;
int count = 0;
//Welcome mesage
System.out.println("This is a tic tac toe game! \n\n");
System.out.println("Player1 - X \nPlayer 2 - O \n\n");
Scanner input = new Scanner(System.in);
//We display the board
board(board);
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
count = count % 2;
if (count == 0) {
System.out.println("Player 1 turn! \n");
turn = player1;
count++;
} else if (count == 1) {
System.out.println("Player 2 turn!");
turn = player2;
count++;
}
while (true) {
System.out.println("Enter a row(1-3):");
rowNumber = input.nextInt();
System.out.println("Enter a column(1-3):");
columnNumber = input.nextInt();
if (board[rowNumber - 1][columnNumber - 1] != 'X'
&& board[rowNumber - 1][columnNumber - 1] != 'O') {
board[rowNumber - 1][columnNumber - 1] = turn;
break;
} else {
System.out.println("Place already taken. Enter again!");
}
}
board(board);
checkWin(board, player1);
checkWin(board, player2);
fullBoard(board);
}
System.out.println("Just checking;");
}
public static void board(char board[][]) {
System.out.println(board[0][0] + " | " + board[0][1] + " | "
+ board[0][2]);
System.out.println(board[1][0] + " | " + board[1][1] + " | "
+ board[1][2]);
System.out.println(board[2][0] + " | " + board[2][1] + " | "
+ board[2][2]);
}
public static boolean checkWin(char board[][], char player) {
if (board[0][0] == player && board[0][1] == player && board[0][2] == player ||
board[1][0] == player && board[1][1] == player && board[1][2] == player ||
board[2][0] == player && board[2][1] == player && board[2][2] == player ||
board[0][0] == player && board[1][0] == player && board[2][0] == player ||
board[0][1] == player && board[1][1] == player && board[2][1] == player ||
board[0][2] == player && board[1][2] == player && board[2][2] == player ||
board[0][0] == player && board[1][1] == player && board[2][2] == player ||
board[2][0] == player && board[1][1] == player && board[0][2] == player){
if(player == 'X'){
System.out.println("Player 1 has won!");
}
else if(player == 'O'){
System.out.println("Player 2 has won!");
}
return false;
}else {
return true;
}
}
public static boolean fullBoard(char board[][]) {
if ((board[0][0] == 'X' || board[0][0] == 'O')
&& (board[0][1] == 'X' || board[0][1] == 'O')
&& (board[0][2] == 'X' || board[0][2] == 'O')
&& (board[1][0] == 'X' || board[1][0] == 'O')
&& (board[1][1] == 'X' || board[1][1] == 'O')
&& (board[1][2] == 'X' || board[1][2] == 'O')
&& (board[2][0] == 'X' || board[2][0] == 'O')
&& (board[2][1] == 'X' || board[2][1] == 'O')
&& (board[2][2] == 'X' || board[2][2] == 'O')){
System.out.println("It's a tie!");
return false;
}else {
return true;
}
}
}
Because you are calling method twice, once in loop condition and second inside loop.
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
....
checkWin(board, player1);
checkWin(board, player2);
}
You don't need to call it inside loop, in condition it is sufficient.
Change it to following...
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)) {
....
// Remove method call from here.
}
This is due to your while loop here :
while (checkWin(board, player1) && checkWin(board, player2)
&& fullBoard(board)){
// stuff
// and then again...
checkWin(board, player1);
checkWin(board, player2);
fullBoard(board);
}
My code results in an exception .. can you help me spot my problem?
import java.util.Scanner;
import java.util.Random;
class apples {
public static void main(String[] args){
Scanner SPS = new Scanner(System.in);
System.out.println("Choose:\tstone,paper or scissors");
SPS.hasNext();
Random rand = new Random(2);
int scissors = rand.nextInt(0);
int stone = rand.nextInt(1);
int paper = rand.nextInt(2);
System.out.println((SPS.hasNext("scissors")||SPS.hasNext("stone")||SPS.hasNext("paper"))? null:"go play away from here");
if(rand.nextInt() == 0 && SPS.hasNext("scissors")){
System.out.println("scissors");
System.out.println("You tie");
}else if(rand.nextInt() == 0 && SPS.hasNext("stone")){
System.out.println("scissors");
System.out.println("You lose");
}else if(rand.nextInt() == 0 && SPS.hasNext("paper")){
System.out.println("scissors");
System.out.println("You won");
}else if(rand.nextInt() == 1 && SPS.hasNext("stone")){
System.out.println("stone");
System.out.println("You tie");
}else if(rand.nextInt() == 1 && SPS.hasNext("scissors")){
System.out.println("stone");
System.out.println("You won");
}else if(rand.nextInt() == 1 && SPS.hasNext("paper")){
System.out.println("stone");
System.out.println("You lose");
}else if(rand.nextInt() == 2 && SPS.hasNext("paper")){
System.out.println("paper");
System.out.println("You tie");
}else if(rand.nextInt() == 2 && SPS.hasNext("stone")){
System.out.println("paper");
System.out.println("You won");
}else if(rand.nextInt() == 2 && SPS.hasNext("scissors"))
System.out.println("paper");
System.out.println("You lose");
}
}
and the result
Choose: stone,paper or scissors
stone
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at stone.paper.scissors.apples.main(apples.java:20)
Well, your problem is here:
int scissors = rand.nextInt(0);
because, as said in the exception, 0 is not positive.
What you wanted to do is:
int scissors = 0;
int stone = 1;
int paper = 2;
and use the variables after when you are comparing:
if (!SPS.hasNext("(scissors|stone|paper)")) {
System.out.println("go play away from here");
} else {
int randomChoice = rand.nextInt(3);
String userChoice = SPS.next();
if(randomChoice == scissors && userChoice.equals("scissors")){
System.out.println("scissors");
System.out.println("You tie");
}else if(randomChoice == scissors && userChoice.equals("stone")){
System.out.println("scissors");
System.out.println("You lose");
}else if(randomChoice == scissors && userChoice.equals("paper")){
System.out.println("scissors");
System.out.println("You won");
}else if(randomChoice == stone && userChoice.equals("stone")){
System.out.println("stone");
System.out.println("You tie");
}else if(randomChoice == stone && userChoice.equals("scissors")){
System.out.println("stone");
System.out.println("You won");
}else if(randomChoice == stone && userChoice.equals("paper")){
System.out.println("stone");
System.out.println("You lose");
}else if(randomChoice == paper && userChoice.equals("paper")){
System.out.println("paper");
System.out.println("You tie");
}else if(randomChoice == paper && userChoice.equals("stone")){
System.out.println("paper");
System.out.println("You won");
}else if(randomChoice == paper && userChoice.equals("scissors")) {
System.out.println("paper");
System.out.println("You lose");
}
}
Note: I also corrected:
your incorrect use of rand
your incorrect use of the Scanner
your missing brackets for the last else if.
When I run this, it displays all System.out.println("") statements in the case. How do I make it choose the correct if statement and nothing else?
And if I could go back to the beginning after it finds an outcome, that would also be helpful.
import java.util.Scanner;
import java.util.Random;
public class TEST {
private static Scanner myScanner;
public static void main(String[] args) {
myScanner = new Scanner(System.in);
Random myRandom = new Random();
int randomNumber;
char reply;
System.out.print("Rock(R) Paper(P) or Scissors(S)? ");
reply = myScanner.findWithinHorizon(".", 0).charAt(0);
randomNumber = myRandom.nextInt(3) + 1;
switch (randomNumber) {
case 1:
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Rock. You lost!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Rock. You tied!");
}
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Rock. You won!");
break;
}
case 2:
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Paper. You tied!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Paper. You won!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Paper. You lost!");
break;
}
case 3:
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Scissor. You won!");
}
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Scissor. You lost!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Scissor. You tied!");
break;
}
}
}
}
You have two problems:
1) Your if statements don't run because they end in semi-colon. This means the code in {} forms a block and runs either way.
2) Your breaks are in the wrong place. The first only breaks if 'R' (or 'r') is chosen. The later breaks if the case is run.
if (reply == 'P' || reply == 'p'); {
System.out.println("Computer: Paper. You tied!");
}
if (reply == 'S' || reply == 's'); {
System.out.println("Computer: Paper. You won!");
}
if (reply == 'R' || reply == 'r'); {
System.out.println("Computer: Paper. You lost!");
break;
}
vs
if (reply == 'P' || reply == 'p') {
System.out.println("Computer: Paper. You tied!");
}
if (reply == 'S' || reply == 's') {
System.out.println("Computer: Paper. You won!");
}
if (reply == 'R' || reply == 'r') {
System.out.println("Computer: Paper. You lost!");
}
break;
Also, as suggested in the comments, you can use a loop to prompt for input. For example:
while (! done) {
System.out.print("Rock(R) Paper(P) or Scissors(S)? (or Quit(Q) ");
reply = myScanner.findWithinHorizon(".", 0).charAt(0);
// more code here
}
You'll need another case here so you can set done to true and end the loop.
You followed the if's with an extra ;
The syntax for an if statements is:
if(condition){
Expressions;
}
the semicolon makes it:
if(condition)
; //empty line, effectively ignores the if
{
Expressions;
}