Question

In Java, I need help adding a print statement if the user chooses an already used...

In Java, I need help adding a print statement if the user chooses an already used slot. Everything I have tried has failed.

// Tic Tac Toe program that plays the game with user
// Renee Waggoner
// July 26, 2019
// Special Requirements: None
package tic_tac_toe;
import java.util.*;
class TicTacToe
{
   char ttt[][] = new char[3][3];
   static final char player1 = 'O';
   static final char player2 = 'X';
   Scanner scan =new Scanner(System.in);

   String playerID(char player)
   {
       // Prints the identity of the player
       // For example:
       // player2: X
       if (player == player1)
           return "player1: " + player;
       else
           return "player2: " + player;
   }
  
   void getPlayerInput(char player)
   {
       // ******* Details to fill in ************

       // Prompt the user for a cell input. Make sure its a legal
       // cell designator. Also make sure the cell doesn't already
       // have a player in it. Prompt the user again in the case
       // of any problem. Once a valid spot has been found,
       // you will issue a statement like:
       char choice;
       System.out.println("Entering Input For:" + playerID(player));

       System.out.println("enter in cell [a, b, ... i]");
       choice = scan.next().charAt(0);

       switch(choice)
       {
       case 'a':ttt[0][0] = player;break;
       case 'b':ttt[0][1] = player;break;
       case 'c':ttt[0][2] = player;break;
       case 'd':ttt[1][0] = player;break;
       case 'e':ttt[1][1] = player;break;
       case 'f':ttt[1][2] = player;break;
       case 'g':ttt[2][0] = player;break;
       case 'h':ttt[2][1] = player;break;
       case 'i':ttt[2][2] = player;break;
       }

   }
  
   boolean gameIsDraw()
   {
       // ******* Details to fill in ************
       // If all ttt entries have been taken return true
       // otherwise return false
       for(int i = 0; i < ttt.length; i++)
           for(int j = 0; j < ttt [i].length; j++)
               if( ttt [i][j] == ' ')
                   return false;
       return true;
   }
  
   boolean winner(char player)
   {
       // ******* Details to fill in ************
       // Check to see if the parameter player has won
       // Winning means that player shows up in a line of
       // three. The line can be a column, row or a diagonal
       // Return true if player is a winner, otherwise return false.
       for(int i = 0; i < 3; i++)
          

           if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
               return true;

       for(int i = 0; i < 3; i++)

           if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
               return true;

       if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
           return true;

       if(ttt[2][0] == player && ttt[1][1] == player && ttt[0][2] == player)
           return true;

       return false;
   }
  
   void displayBoard()
   {
   // ******* Details to fill in ************
       // game board spacing
   System.out.println("===============================e");
   System.out.println("---a-----b-----c---");
System.out.println("| "+ttt[0][0]+" | "+ttt[0][1]+" | "+ttt[0][2]+" |\n");
System.out.println("---d-----e-----f---");
System.out.println("| "+ttt[1][0]+" | "+ttt[1][1]+" | "+ttt[1][2]+" |\n");
System.out.println("---g-----h-----i---");
System.out.println("| "+ttt[2][0]+" | "+ttt[2][1]+" | "+ttt[2][2]+" |\n");
System.out.println("-------------------");
     
   }
  
   void newgame()
   {
       char currPlayer = player1;
       for(int i = 0; i < 3; i++)
           for(int j = 0; j < 3; j++)
               ttt [i][j] =' ';
       boolean continueOn = true;
       while (continueOn)
       {
           displayBoard();
           if (gameIsDraw())
           {
               System.out.println("******** Game Ends in Draw");
               continueOn = false;
           }
           else
           {
               getPlayerInput(currPlayer);
               if (winner(currPlayer))
               {
                   System.out.println("******** We have a winner: " + playerID(currPlayer));
                   // calling displayBoard method
                   displayBoard();   
                   continueOn = false;
               }
               else
               {
                   if (currPlayer == player1) currPlayer = player2;
                   else currPlayer = player1;
               }
           }
       }
   }
   public static void main(String[] args)
   {
       TicTacToe game = new TicTacToe();
       String str;
       do
       {
           game.newgame();
           System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
           str = game.scan.next();
       } while ("y".equals(str));
       // otherwise prints Bye
       System.out.println("Bye");
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
class TicTacToe
{
char ttt[][] = new char[3][3];
static final char player1 = 'O';
static final char player2 = 'X';
Scanner scan =new Scanner(System.in);

String playerID(char player)
{
// Prints the identity of the player
// For example:
// player2: X
if (player == player1)
return "player1: " + player;
else
return "player2: " + player;
}
  
void getPlayerInput(char player)
{
// ******* Details to fill in ************

// Prompt the user for a cell input. Make sure its a legal
// cell designator. Also make sure the cell doesn't already
// have a player in it. Prompt the user again in the case
// of any problem. Once a valid spot has been found,
// you will issue a statement like:
char choice;
System.out.println("Entering Input For:" + playerID(player));

while(true) {
   System.out.println("enter in cell [a, b, ... i]");
choice = scan.next().charAt(0);

int flag=0;

switch(choice)
{
case 'a':
   if(ttt[0][0] != player1 || ttt[0][0] != player2) {
       ttt[0][0] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[0][0]));
   }
   break;
case 'b':
   if(ttt[0][1] != player1 || ttt[0][1] != player2) {
       ttt[0][1] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[0][1]));
   }
   break;
case 'c':
   if(ttt[0][2] != player1 || ttt[0][2] != player2) {
       ttt[0][2] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[0][2]));
   }
   break;
case 'd':
   if(ttt[1][0] != player1 || ttt[1][0] != player2) {
       ttt[1][0] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[1][0]));
   }
   break;
case 'e':
   if(ttt[1][1] != player1 || ttt[1][1] != player2) {
       ttt[1][1] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[1][1]));
   }
   break;
case 'f':
   if(ttt[1][2] != player1 || ttt[1][2] != player2) {
       ttt[1][2] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[1][2]));
   }
   break;
case 'g':
   if(ttt[2][0] != player1 || ttt[2][0] != player2) {
       ttt[2][0] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[2][0]));
   }
   break;
case 'h':
   if(ttt[2][1] != player1 || ttt[2][1] != player2) {
       ttt[2][1] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[2][1]));
   }
   break;
case 'i':
   if(ttt[2][2] != player1 || ttt[2][2] != player2) {
       ttt[2][2] = player;
       flag=1;
   }else {
       System.out.println("Slot is already chosen by "+playerID(ttt[2][2]));
   }
   break;
default:
   System.out.println("Please enter a valid slot");
}

if(flag == 1) {
   break;
}
}

}
  
boolean gameIsDraw()
{
// ******* Details to fill in ************
// If all ttt entries have been taken return true
// otherwise return false
for(int i = 0; i < ttt.length; i++)
for(int j = 0; j < ttt [i].length; j++)
if( ttt [i][j] == ' ')
return false;
return true;
}
  
boolean winner(char player)
{
// ******* Details to fill in ************
// Check to see if the parameter player has won
// Winning means that player shows up in a line of
// three. The line can be a column, row or a diagonal
// Return true if player is a winner, otherwise return false.
for(int i = 0; i < 3; i++)
  

if(ttt[i][0] == player && ttt[i][1] == player && ttt[i][2] == player)
return true;

for(int i = 0; i < 3; i++)

if(ttt[0][i] == player && ttt[1][i] == player && ttt[2][i] == player)
return true;

if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player)
return true;

if(ttt[2][0] == player && ttt[1][1] == player && ttt[0][2] == player)
return true;

return false;
}
  
void displayBoard()
{
// ******* Details to fill in ************
// game board spacing
System.out.println("===============================e");
System.out.println("---a-----b-----c---");
System.out.println("| "+ttt[0][0]+" | "+ttt[0][1]+" | "+ttt[0][2]+" |\n");
System.out.println("---d-----e-----f---");
System.out.println("| "+ttt[1][0]+" | "+ttt[1][1]+" | "+ttt[1][2]+" |\n");
System.out.println("---g-----h-----i---");
System.out.println("| "+ttt[2][0]+" | "+ttt[2][1]+" | "+ttt[2][2]+" |\n");
System.out.println("-------------------");

}
  
void newgame()
{
char currPlayer = player1;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
ttt [i][j] =' ';
boolean continueOn = true;
while (continueOn)
{
displayBoard();
if (gameIsDraw())
{
System.out.println("******** Game Ends in Draw");
continueOn = false;
}
else
{
getPlayerInput(currPlayer);
if (winner(currPlayer))
{
System.out.println("******** We have a winner: " + playerID(currPlayer));
// calling displayBoard method
displayBoard();   
continueOn = false;
}
else
{
if (currPlayer == player1) currPlayer = player2;
else currPlayer = player1;
}
}
}
}
public static void main(String[] args)
{
TicTacToe game = new TicTacToe();
String str;
do
{
game.newgame();
System.out.println("Do you want to play Tic-Tac-Toe (y/n)?");
str = game.scan.next();
} while ("y".equals(str));
// otherwise prints Bye
System.out.println("Bye");
}
}

Add a comment
Know the answer?
Add Answer to:
In Java, I need help adding a print statement if the user chooses an already used...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT