Converting number outputs and inputs into words - java

i had a quick question, so i finished up writing my program which is a simple Rock, Paper Scissors game but at the moment it only displays 0,1,2 which 0 is scissors, 1 is rock, and 2 is paper. The problem im having is that im not quite sure how to give the users input as well as what the computer outputs go from displaying numbers to displaying scissors, rock, paper.
import java.util.Scanner;
import java.lang.Math;
public class Lab3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Hello user! lets play ");
System.out.println("Rock, Paper, Scissors.");
System.out.println();
System.out.print("Type in 0 for Scissors, 1 for Rock, or 2 for Paper ");
int userInput = in.nextInt();
int opponentsHand = (int)(Math.random()*3);
if (userInput == opponentsHand)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" Despite your efforts of playing " + userInput + ", this battle has ended in a draw!");
}
if (userInput < opponentsHand && opponentsHand != 2)
{
System.out.print("Darth vader has played "+ opponentsHand);
System.out.println(", You played " + userInput + "You have Lost");
}
else if (userInput < opponentsHand && opponentsHand == 2)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You played " + userInput + " You have won");
}
if (userInput > opponentsHand && opponentsHand != 0)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You have played " + userInput + " You have won");
}
else if (userInput > opponentsHand && opponentsHand == 0)
{
System.out.print("Darth Vader has played " + opponentsHand);
System.out.println(" You have played " + userInput + " You have lost");
}
}
}
thank you

One way is to simply use an array to store the names of the hands:
String[] hands = {"scissors", "rock", "paper"};
When you print, you do the following:
System.out.print("Darth Vader has played " + hands[opponentsHand]);

Related

Debugging my simple java game(Rock Paper Scissor) [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I created this program to run infinite times asking the user to enter a choice(rock or paper or scissor), which seems to work fine. The problem is no else ....if or if ....else statements are satisfied. Whatever input i give it just prints out the else statement in the else statement, (Enter a valid choice). I cant find the mistake i made so ill link my code below..... Thanks in advance.
import java.util.Scanner;
import java.util.Random;
public class apples {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
Random number = new Random();
String rps[] = {"rock", "paper", "scissor"};
String player;
String ai;
int rand, pscore=0, aiscore=0;
while(1 < 2){
System.out.println("Take your pick \nrock \npaper \nscissor");
player = input.nextLine();
rand = number.nextInt(3);
ai = rps[rand];
System.out.println(ai);
if(player == ai){
pscore+=0;
aiscore+=0;
System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
System.out.print("\n");
continue;
}else{
if(player == "rock" && ai == "paper"){
aiscore+=1;
pscore+=0;
System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
System.out.print("\n");
continue;
}else if(player == "rock" && ai == "scissor"){
pscore+=1;
aiscore+=0;
System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
System.out.print("\n");
continue;
}else if(player == "paper" && ai == "rock"){
pscore+=1;
aiscore+=0;
System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
System.out.print("\n");
continue;
}else if(player == "paper" && ai == "scissor"){
aiscore+=1;
pscore+=0;
System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
System.out.print("\n");
continue;
}else if(player == "scissor" && ai == "rock"){
aiscore+=1;
pscore+=0;
System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
System.out.print("\n");
continue;
}else if(player == "scissor" && ai == "paper"){
aiscore+=0;
pscore+=1;
System.out.println("Your opponent chose " + ai + "\tYour score= "+ pscore + "\tOpponents score = "+aiscore);
System.out.print("\n");
continue;
}else{
System.out.println("Enter a valid choice");
System.out.print("\n");
continue;
}
}
}
}
}
Instead of :
if(player == "rock" && ai == "paper"){
...
}
Use
if(player.equals("rock") && ai.equals("paper")){
...
}
This will apply to all of your pieces of code which compare Strings.
In java, == tests for reference equality: if the two objects are actually the same object. Two strings holding the same characters will fail this check, which is what's happening in your logic.
See How do I compare strings in Java?

Inputted string also assigned to an integer?

I'm trying to make a rock-paper-scissors program that is a best two out of three where the computer randomly rolls a 0-2 and each of those are assigned to rock, paper, or scissors, and then it compares the userInput and counts a win for computer or player then adds it up.
BUT, I can't figure out how to make it that if user were to enter "scissors" the program would know that it's also assigned to 2 (For comparison purposes).
public static void main(String[] args) {
Random r = new Random();
int gameCount = 0;
int computerWins = 0;
int playerWins = 0;
int rock = 0;
int paper = 1;
int scissors = 2;
int playerChoice;
int computerChoice = r.nextInt(3);
System.out.println("Welcome to Rock Paper Scissors! Best 2 out of 3!");
while (gameCount >= 0 && gameCount < 3)
{
System.out.println("Enter \"Rock\", \"Paper\", or \"Scissors\"");
break;
}
playerChoice = userInput.nextInt()
//If player enters anything besides rock, paper, or scissors
if (playerChoice < 0 || playerChoice >= 3) {
System.out.println("That wasn't an option");
computerWins++;
gameCount++;
//The game goes on, and the winners are added up!
} else if (playerChoice == 0 && computerChoice == 1) {
computerWins++;
gameCount++;
System.out.println("Rock v Paper! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 0) {
playerWins++;
gameCount++;
System.out.println("Paper v Rock! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 2) {
computerWins++;
gameCount++;
System.out.println("Paper v Scissors! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 1) {
playerWins++;
gameCount++;
System.out.println("Scissors v Paper! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 0) {
computerWins++;
gameCount++;
System.out.println("Scissors v Rock! Computer Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 0 && computerChoice == 2) {
playerWins++;
gameCount++;
System.out.println("Rock v Scissors! Player Wins!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 0 && computerChoice == 0) {
gameCount++;
System.out.println("Rock v Rock! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 1 && computerChoice == 1) {
gameCount++;
System.out.println("Paper v Paper! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
} else if (playerChoice == 2 && computerChoice == 2) {
gameCount++;
System.out.println("Paper v Paper! Tie!\n" +
"Player has won " + playerWins + " times and the computer " +
"has won " + computerWins + " times");
}
//Check if game count reaches max games then chooses a winner
if (gameCount == 3 && computerWins > playerWins) {
System.out.println("The Computer Wins!");
} else if (gameCount == 3 && computerWins < playerWins) {
System.out.println("The Player Wins!");
} else if (gameCount == 3 && computerWins == playerWins) {
System.out.println("The game is a tie!");
}
}
}
So instead of playerChoice = userInput.nextInt(); try this:
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
try {
playerChoice = Integer.parseInt(input);
} catch (NumberFormatException e) {
if (input.equalsIgnoreCase("rock")) {
playerChoice = rock;
} else if (input.equalsIgnoreCase("paper")) {
playerChoice = paper;
} else if (input.equalsIgnoreCase("scissors")) {
playerChoice = scissors;
} else {
// if input is invalid
playerChoice = -1;
}
}
Since you're using userInput.nextInt() and playerChoice is an int and can only hold ints, you need to parse your user's input. In this case, Integer.parseInt(input) will try to find an int in the user input. If it can't it will return an exception; that's why there's a try-catch block. If it isn't an int, it will look for each string and assign the associated int value to playerChoice or -1 if the input is invalid. Then the rest of your code should be able to appropriately handle the playerChoice after that.

Rock Paper Scissors Game Best 2 out of 3 with Loops

The code is below. My program runs smoothly except for two issues. It does not play for just three rounds and I need it to play for just 3 rounds and determine a winner based off of whoever won at least once during the three rounds despite ties (if it comes down to that scenario randomly). It plays until there is a 2 to 1 winning ratio for either the player or the computer. I do not want it to run continuously like this. The other issue is that once a final winner is declared, the line “Enter rock, paper, or scissors” appears again, and it is not needed there. How do I go about fixing these two issues? Thank you.
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Prompt user to enter rock, paper, or scissors
//Compare random value
while(true){
int wins = 0;
int losses = 0;
int rnd = 0;
int USER = 0;
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n"
+ "Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
// Plays 3 rounds before terminating
while(rnd<3) {
Random GAME = new Random();
int PC = 1+GAME.nextInt(3);
Scanner keyboard = new Scanner (System.in);
int SCISSOR, ROCK, PAPER;
ROCK = 1;
PAPER = 2;
SCISSOR= 3;
String USER_Input = keyboard.next();
if(USER_Input.equals("Rock")) {
USER = 1;
}
if(USER_Input.equals("Paper")) {
USER = 2;
}
if(USER_Input.equals("Scissors")) {
USER = 3;
}
//If the user enters a value greater then 3 or less than 1 it will terminate the program
//and display an error message
while (USER > 3 || USER < 1) {
System.err.println("Incorrect value entered.");
System.exit(0);
break;
}
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == SCISSOR)
if(PC == PAPER){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if(PC == ROCK){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player wins
if(USER == ROCK)
if(PC == SCISSOR ){
System.out.println("Rock v Scissor! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer wins
else if (PC == PAPER){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Player Wins
if(USER == PAPER)
if(PC == ROCK){
System.out.println("Paper v Rock! Player Wins!");
wins++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
//Computer Wins
else if (PC == SCISSOR){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
rnd++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
}
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YN = new Scanner(System.in);
String YN_String = YN.next();
if(YN_String.equals("Yes") || YN_String.equals("yes")){
}if(YN_String.equals("No") || YN_String.equals("no")) {
System.out.println ("Goodbye!");
break;
}
}
}
}
The code looks like it should work the way you want it to, aside from the fact that in the case of ties, you aren't incrementing the round counter:
if(USER == PC){
if(USER == SCISSOR){
System.out.println("Scissors v Scissors! Tie!");
rnd++;
}
if(USER == ROCK){
System.out.println("Rock v Rock! Tie!");
rnd++;
}
if(USER == PAPER){
System.out.println("Paper v Paper! Tie!");
rnd++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
System.out.print("Enter \"Rock\", \"Paper\" or \"Scissors\" \n");
}
As a side note, you could take out the rnd++; from each individual result, and have it increase every time you get input from the user, since you want it to increase no matter what.
Your second issue stems from the fact that you're prompting for new input every time you receive input, after you tell the user the result, including the ending result. You could fix this by removing the prompt command from the individual results and placing it at the beginning of your while loop instead. Note that if you do this, you may also want to remove the prompt from your welcome message as well, or it will prompt twice at the beginning: once in the welcome message, and again when the while loop starts immediately after.
import java.util.Scanner;
import java.util.Random;
/**
*
* #author Chloe Harris
*
*/
public class RockPaperScissorsGame {
public static void main(String[] args) {
// TODO code application logic here
//Set integers for wins, losses, rounds, and user
while(true){
int wins = 0;
int losses = 0;
int round = 0;
int Player = 0;
//Plays 3 rounds before terminating
//Prompt user to input Rock Paper Scissors
System.out.print("Welcome to Rock Paper Scissors! Best 2 out of 3! \n");
Scanner keyboard = new Scanner (System.in);
while(round<3) {
System.out.println("Enter \"Rock\", \"Paper\" or \"Scissors\"");
Random Game = new Random();
int Computer = 1+Game.nextInt(3);
int Scissors, Rock, Paper;
Rock = 1;
Paper = 2;
Scissors= 3;
String UserInput = keyboard.next();
if(UserInput.equals("Rock")) {
Player = 1;
}
if(UserInput.equals("Paper")) {
Player = 2;
}
if(UserInput.equals("Scissors")) {
Player = 3;
}
//If the user enters a value greater then 3 (Scissors) or less than 1 (Rock)
//it will terminate the program and display an error message
while (Player > 3 || Player < 1) {
losses++;
round++;
System.out.println("Not a valid input! Computer wins");
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish tie scenarios using if statements
if(Player == Computer){
if(Player == Scissors){
System.out.println("Scissors v Scissors! Tie!");
round++;
}
if(Player == Rock){
System.out.println("Rock v Rock! Tie!");
round++;
}
if(Player == Paper){
System.out.println("Paper v Paper! Tie!");
round++;
}
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Establish the various winning scenarios using if and else if statements
//Player wins
if(Player == Scissors)
if(Computer == Paper){
System.out.println("Scissors v Paper! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if(Computer == Rock){
System.out.println("Scissors v Rock! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player wins
if(Player == Rock)
if(Computer == Scissors ){
System.out.println("Rock v Scissors! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer wins
else if (Computer == Paper){
System.out.println("Rock v Paper! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Player Wins
if(Player == Paper)
if(Computer == Rock){
System.out.println("Paper v Rock! Player Wins!");
wins++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
//Computer Wins
else if (Computer == Scissors){
System.out.println("Paper v Scissors! Computer Wins!");
losses++;
round++;
System.out.println("Player has won " + wins + " times and the computer has won " + losses + " times");
}
}
//Determine final winner using if statements
//Ask if player would like to play again by setting up string
if(wins>losses){
System.out.println("The Player Wins!");
}if(losses>wins){
System.out.println("The Computer Wins!");
}
System.out.println("Play again? \"Yes\" or \"No\"");
Scanner YesNo = new Scanner(System.in);
String YesNo_String = YesNo.next();
if(YesNo_String.equalsIgnoreCase("yes")) {
}if(YesNo_String.equalsIgnoreCase("No")) {
System.out.println ("Goodbye!");
}
}
}
}

Better Algorithm? Better Array?

This is my code:
/* Name: Steven Royster
* Date: Jan. 15, 2015
* Rock, Paper, Scissors Program
* This program simulates a game of rock, paper, scissors with the user until someone has one a total of five times.
*/
import java.util.Random;
import java.util.Scanner;
public class RPS {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Let's play rock, paper, scissors! First to five wins!");
String[] choice = { "zero" , "rock" , "paper" , "scissors" };
Random rander = new Random();
Scanner input = new Scanner(System.in);
int userScore = 0, compScore = 0,
userChoice, compChoice;
while (compScore < 5 && userScore < 5)
{
compChoice = rander.nextInt(3) + 1;
System.out.println("\nEnter: 1 for ROCK | 2 for PAPER | 3 for SCISSORS.");
userChoice = input.nextInt();
if (compChoice == userChoice) // tie
{
System.out.println("I chose " + choice[compChoice] + " too, so we tied!");
}
else if ( ( compChoice == 1 && userChoice == 3 ) //computer wins
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
{
System.out.println("I win! I chose " + choice[compChoice] + ". " +
choice[compChoice] + " beats " + choice[userChoice] + "." );
compScore += 1;
}
else //human wins
{
System.out.println("You win! I chose " + choice[compChoice] + ". " +
choice[userChoice] + " beats " + choice[compChoice] + ".");
userScore += 1;
}
}//end while
if (userScore == 5)
{
System.out.println("\nCongrats! You're the winner! You got "
+ userScore + " points. I only got " + compScore + " points." );
}
}//end main
}//end class
Is there a better algorithm I can implement rather than using the three separate conditions within my else-if statement? Also, instead of having the 'zero' string in my array, how could I check through my array using only numbers 1, 2, and 3?
Just use
String[] choice = { "rock" , "paper" , "scissors" };
Then instead of choice[userChoice] you can do choice[userChoice - 1].
You don't need to write
if ( ( compChoice == 1 && userChoice == 3 )
|| ( compChoice == 2 && userChoice == 1 )
|| ( compChoice == 3 && userChoice == 2) )
because it is the same as
if (compChoice == 1 + (userChoice % 3))

Programming a tic tac toe game in JAVA. Using many if statements how would i get the game to read a tie game?

PROMPT
Welcome to play the game of guessing who is winning!
Please enter your game 1 board (* to exit) > XXOOOXXOO
Your game 1 is as follows:
XXO
OOX
XOO
Your game 1: Tie
Please enter your game 2 board (* to exit) > XXXOOXXOO
Your game 2 is as follows:
XXX
OOX
XOO
Your game 2: X won the game by row 1.
etc.
How could I make it read a tie game?
package PA6;
import java.util.Scanner;
public class PA6 {
private static char [ ] [ ] ttt = new char [4] [4] ;
private static int gameNum = 1;
public static void main(String[] args)
{//MAIN OPEN
System.out.println("Welcome to play the game of guessing who is winning!");
Scanner scan = new Scanner (System.in);
String gameString;
System.out.println("Please enter your game " + gameNum + " board (* to exit) > ");
gameString = scan.nextLine();
int n;
while (!gameString.equals("*"))
{//WHILE LOOP OPEN
System.out.println("Your game " + gameNum + " is as follows: ");
n = 0;
ttt = new char [4][4];
for(int i = 1 ; i < 4; i++)
{//FOR LOOP OPEN
for(int j = 1; j < 4; j++)
{//NESTED FOR LOOP OPEN
ttt [i][j]= gameString.charAt(n);
n++;
}//NESTED FOR LOOP CLOSED
}//FOR LOOP CLOSED
System.out.println(ttt[1][1] + "" + ttt[1][2] + "" + ttt[1][3]);
System.out.println(ttt[2][1] + "" + ttt[2][2] + "" + ttt[2][3]);
System.out.println(ttt[3][1] + "" + ttt[3][2] + "" + ttt[3][3]);
if(winRow1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 1");
if(winRow2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 2");
if(winRow3('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 3");
if(winRow1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 1");
if(winRow2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 2");
if(winRow3('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by row 3");
if(winColumn1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 1");
if(winColumn2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 2");
if(winColumn3('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by Column 3");
if(winColumn1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 1");
if(winColumn2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 2");
if(winColumn3('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by Column 3");
if(winDiagonal1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by diagonal 1");
if(winDiagonal1('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by diagonal 1");
if(winDiagonal2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by diagonal 2");
if(winDiagonal2('X' , ttt))
System.out.println("Your game " + gameNum + ": X won the game by diagonal 2");
gameNum++;
System.out.print("Please enter your game " + gameNum + " board (* to exit) > ");
gameString = scan.nextLine();
}//WHILE LOOP CLOSED
}//MAIN CLOSED
public static boolean winDiagonal1( char player, char a [][])
{
if ( ttt[1][1] == player && ttt[2][2] == player && ttt[3][3] == player )
return true;
return false;
}
public static boolean winDiagonal2 (char player, char a [][])
{
if( ttt[1][3] == player && ttt[2][2] == player && ttt[3][1] == player )
return true;
return false;
}
public static boolean winRow1 (char player, char a [][])
{
if (ttt[1][1] == player && ttt[1][2] == player && ttt[1][3] == player)
return true;
return false;
}
public static boolean winRow2 (char player, char a [][])
{
if (ttt[2][1] == player && ttt[2][2] == player && ttt[2][3] == player)
return true;
return false;
}
public static boolean winRow3 (char player, char a [][])
{
if (ttt[3][1] == player && ttt[3][2] == player && ttt[3][3] == player)
return true;
return false;
}
public static boolean winColumn1 (char player, char a [][])
{
if (ttt[1][1] == player && ttt[2][1] == player && ttt [3][1] == player)
return true;
return false;
}
public static boolean winColumn2 (char player, char a [][])
{
if (ttt[1][2] == player && ttt[2][2] == player && ttt [3][2] == player)
return true;
return false;
}
public static boolean winColumn3 (char player, char a [][])
{
if (ttt[1][3] == player && ttt[2][3] == player && ttt [3][3] == player)
return true;
return false;
}
}
I you convert your if statements to combined if else if statements than what is left in your else case will be the tie.
if(winRow1('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 1");
else if(winRow2('O' , ttt))
System.out.println("Your game " + gameNum + ": O won the game by row 2");
.
.
.
else
System.out.println("Your game " + gameNum + ": It is a tie");
By the way,this way you could avoid multiple messages like when there are one row and one column fulticked at the same time ;).

Categories