Please make this into one class.
JAVA Please:
Tic Tac Toe class -
Write a fifth game program that can play Tic Tac Toe.
This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move.
The move is recorded on the screen and the computer picks his move from those that are left.
The player then picks his next move.
The program should allow only legal moves.
The program should indicate when there is a winner and tell who won.
The player is then asked if he would like to play again.
A sample output is shown in the attached file.
Answer :
import java.util.*;
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( "\n\n" );
System.out.println( "\n\n" );
System.out.println( "\n\n\t\t" + posn [1] + " | " +posn [2]+ " | "
+posn [3]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\n\n\t\t" +posn [4]+ " | " +posn [5]+ " | "
+posn [6]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t ___|____|___ " );
System.out.println( "\n\n\t\t" +posn [7]+ " | " +posn [8]+ " | "
+posn [9]);
System.out.println( " \t\t | | " );
System.out.println( " \t\t | | " );
System.out.println( "\n\n" );
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( "\n\n 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;
}
}
Please make this into one class. JAVA Please: Tic Tac Toe class - Write a fifth...
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner. Details: The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
Write a c program that will allow two users to play a tic-tac-toe game. You should write the program such that two people can play the game without any special instructions. (assue they know how to play the game). Prompt first player(X) to enter their first move. .Then draw the gameboard showing the move. .Prompt the second player(O) to enter their first move. . Then draw the gameboard showing both moves. And so on...through 9 moves. You will need to:...
Please do this in Java (Java FX) Tic-Tac-Toe game (aka X's and O's) in which the player, who is "X", plays against the application, which is "O". The game should be built using the basic window-with-canvas code used for the various tutorials and course assignments. The user plays by dragging "X" tokens onto the Tic-Tac-Toe board area after which the application will automatically play by moving an "O" onto the board area. When a game is over the app should...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...