Create an interactive JAVA program (A human player vs computer) Tic Tac Toe game
ID: 3766069 • Letter: C
Question
Create an interactive JAVA program
(A human player vs computer) Tic Tac Toe game.
Follow these instructions for the entire program
Must keep the program short and explain with comments
Ask the user to play again until user answers “NO”
The file “game_log.txt does not have to be created first. Let the program create this file.
The game board does not have to print the Tic TAC Toe table
Use: do-while loop, nest loops, enhanced for loops, Boolean Operators
Player should be able to pick between an ‘’x’’ or ‘’o’’
The program should print an empty board, with numbers representing each playable position:
1 2 3
4 5 6
7 8 9
The user should provide the number representative of the position where the user would like to place their letter of choice. Handle scenarios where the user chooses a position that is not available.
Once the user input is received, then the program should place the opposing letter in an available position.
The game should continue until
The player wins by connecting three in a row either horizontally, vertically or diagonally (Print “You win!”)
The player loses if the program connects three in a row either horizontally, vertically or diagonally. (Print “Better luck next time.”)
The game is a draw if all playable spaces are filled but neither connects three in a row. (Print “draw!”)
After each move the game should be printed so that the player knows what the game board looks like after each play.
After each move the game board should be written to a file named “game_log.txt”, which records all game activity
After the game ends, the program should ask the user to play again.
If “yes” then a new game begins
If “no” then print:
The number of games played.
The number of wins, losses and draws.
The player’s winning percentage. [Winning percentage= (#of games won/# of games played)*100]
Print “thanks for playing!”
Close file.
The program should handle potential exceptions that may be thrown by using try/catch.
Explanation / Answer
Answer :
import java.util.Scanner;
public class TicTacToeGame
{
private int counter;
private char posn[]=new char[10];
private char player;
public static void main(String args[])
{
String ch;
TicTacToeGame Toe=new TicTacToeGame();
do{
Toe.beginBoard();
Toe.play_game();
System.out.println ("Would you like to play again (Enter 'Y')? ");
Scanner in =new Scanner(System.in);
ch=in.nextLine();
System.out.println("ch value is "+ch);
}while (ch.equals("Y"));
}
public void beginBoard()
{
char posndef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
int i;
counter = 0;
player = 'X';
for (i=1; i<10; i++) posn[i]=posndef[i];
presentBoard();
}
public String presentBoard()
{
System.out.println( " " );
System.out.println( " " );
System.out.println( " " + posn [1] + " | " +posn [2]+ " | " +posn [3]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +posn [4]+ " | " +posn [5]+ " | " +posn [6]);
System.out.println( " | | " );
System.out.println( " ___|____|___ " );
System.out.println( " " +posn [7]+ " | " +posn [8]+ " | " +posn [9]);
System.out.println( " | | " );
System.out.println( " | | " );
System.out.println( " " );
return "presentBoard";
}
public void play_game()
{
int spot;
char blank = ' ';
System.out.println( "Player " + getPlayer() +" will go first and be the letter 'X'" );
do {
presentBoard();
System.out.println( " Player " + getPlayer() +" choose a posn." );
boolean posTaken = true;
while (posTaken) {
Scanner in =new Scanner (System.in);
spot=in.nextInt();
posTaken = checkPosn(spot);
if(posTaken==false)
posn[spot]=getPlayer();
}
System.out.println( "Nice move." );
presentBoard();
nextPlayer();
}while ( getWinner() == blank );
}
public char getWinner()
{
char Winner = ' ';
// Check if X wins
if (posn[1] == 'X' && posn[2] == 'X' && posn[3] == 'X') Winner = 'X';
if (posn[4] == 'X' && posn[5] == 'X' && posn[6] == 'X') Winner = 'X';
if (posn[7] == 'X' && posn[8] == 'X' && posn[9] == 'X') Winner = 'X';
if (posn[1] == 'X' && posn[4] == 'X' && posn[7] == 'X') Winner = 'X';
if (posn[2] == 'X' && posn[5] == 'X' && posn[8] == 'X') Winner = 'X';
if (posn[3] == 'X' && posn[6] == 'X' && posn[9] == 'X') Winner = 'X';
if (posn[1] == 'X' && posn[5] == 'X' && posn[9] == 'X') Winner = 'X';
if (posn[3] == 'X' && posn[5] == 'X' && posn[7] == 'X') Winner = 'X';
if (Winner == 'X' )
{System.out.println("Player1 wins the game." );
return Winner;
}
// Check if O wins
if (posn[1] == 'O' && posn[2] == 'O' && posn[3] == 'O') Winner = 'O';
if (posn[4] == 'O' && posn[5] == 'O' && posn[6] == 'O') Winner = 'O';
if (posn[7] == 'O' && posn[8] == 'O' && posn[9] == 'O') Winner = 'O';
if (posn[1] == 'O' && posn[4] == 'O' && posn[7] == 'O') Winner = 'O';
if (posn[2] == 'O' && posn[5] == 'O' && posn[8] == 'O') Winner = 'O';
if (posn[3] == 'O' && posn[6] == 'O' && posn[9] == 'O') Winner = 'O';
if (posn[1] == 'O' && posn[5] == 'O' && posn[9] == 'O') Winner = 'O';
if (posn[3] == 'O' && posn[5] == 'O' && posn[7] == 'O') Winner = 'O';
if (Winner == 'O' )
{
System.out.println( "Player2 wins the game." );
return Winner; }
for(int i=1;i<10;i++)
{
if(posn[i]=='X' || posn[i]=='O')
{
if(i==9)
{
char Draw='D';
System.out.println(" Game is stalemate ");
return Draw;
}
continue;
}
else
break;
}
return Winner;
}
public boolean checkPosn(int spot)
{
if (posn[spot] == 'X' || posn[spot] == 'O')
{
System.out.println("That posn is already taken, please choose another");
return true;
}
else {
return false;
}
// counter++;
}
public void nextPlayer()
{
if (player == 'X')
player = 'O';
else player = 'X';
}
public String getTitle()
{
return "Tic Tac Toe" ;
}
public char getPlayer()
{
return player;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.