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 Java that emulates a Tic Tac Toe game.
When you are done, a typical session will look like this:
Welcome to tic-tac-toe. Enter coordinates for your move following the X and O prompts. 1 2 3 A | | ----- B | | ----- C | | X:A2 1 2 3 A |X| ----- B | | ----- C | | O:B3 1 2 3 A |X| ----- B | |O ----- C | |
And so on. Illegal moves will prompt the user again for a new move. A win or a stalemate will be announced, mentioning the winning side if any. The program will terminate whenever a single game is complete.
This file has the general framework of the TicTacToe class. An object of this class will represent a tic-tac-toe "board". The board will be represented internally by a two dimensional array of characters (3 x 3), and by a character indicating who's turn it is ('X' or 'O'). These are stored in the class instance variables as follows.
private char[][] board; private char player; // 'X' or 'O'
You will need to define the following methods:
1. A constructor:
public TicTacToe()
to create an empty board, with initial value of a space (' ')
2. play method
public play(String position)
if position represents a valid move (e.g., A1, B3), add the current player's symbol to the board and return true. Otherwise, return false.
3. switchTurn method
public void switchTurn()
switches the current player from X to O, or vice versa.
4. won method
public boolean won()
Returns true if the current player has filled three in a row, column or either diagonal. Otherwise, return false.
5. stalemate method
public boolean stalemate()
Returns true if there are no places left to move;
6. printBoard method
public void printBoard()
prints the current board
7. Use the following test code in your main method to create a TicTacToe object and print it using the printBoard method given, so as to test your code. Your printBoard method should produce the first board in the example above.
public static void main(String[] args) { Scanner in = new Scanner(System.in); TicTacToe game = new TicTacToe(); System.out.println("Welcome to Tic-tac-toe"); System.out.println("Enter coordinates for your move following the X and O prompts"); while(!game.stalemate()) { game.printBoard(); System.out.print(game.getPlayer() + ":"); //Loop while the method play does not return true when given their move. //Body of loop should ask for a different move while(!game.play(in.next())) { System.out.println("Illegal move. Enter your move."); System.out.print(game.getPlayer() + ":"); } //If the game is won, call break; if(game.won()) break; game.printBoard(); //Switch the turn game.switchTurn(); } game.printBoard(); if(game.won()) { System.out.println("Player "+game.getPlayer()+" Wins!!!!"); } else { System.out.println("Stalemate"); } }
JAVA Program For Tic Tac Toe
TIC TAC TOE class :-
public class TicTacToe {
private char[][] board:
private char player;
public TicTacToe() {
board = new char [3][3];
player = 'x';
initializeBoard();
}
public char getplayer()
{
return player;
}
public void initializeBoard() {
for (int i = 0; i<3; i++) {
for (int j = 0; j<3; j++) {
board[i][j] ='-';
}
}
}
public void printBoard() {
System.out.println("---------");
for (int i=0; i<3; i++) {
System.out.println("|");
for(int j=0; j<3; j++) {
System.out.println(board[i][j]+"|");
}
System.out.println();
System.out.println("----------");
}
}
public boolean isBoardFull() {
boolean isFull= true;
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
if(board[i][j] == '-') {
isFull= false;
}
}
}
return isFull;
}
public boolean won() {
return(won() || checkColumnsForwon() || checkDiagonalsForwon());
}
private boolean checkRowsForwon() {
for (int i=0; i<3; i++) {
if(checkRowCol(board[i][0], board[i][j], board[i][2])== true) {
return true;
}
}
return false;
}
private boolean checkColumnsForwon() {
for(int i=0; i<3; i++) {
if(checkRowCol(board[0][i], board[1][i], board[2][i])== true) {
return true;
}
}
return false;
}
private boolean checkDiagonalsForwon() {
return ((checkRowCol(board[0][0], board[1][1], board[2][2]) == true)|| (checkRowCol(board[0][2], board[1][1], board[2][0])== true));
}
private boolean checkRowCol(char c1, char c2, char c3) {
return ((c1 != '-') && (c1 == c2) && (c2 == c3));
}
public void changePlayer() {
if(player == 'x') {
player = 'o';
}
else {
player = 'x';
}
}
public boolean placeMark(int row, int col) {
if ((row >= 0) && (row < 3)) {
if((col >= 0) && (col<3)) {
if(board[row][col] == '-') {
board[row][col] = player;
return true;
}
}
}
return false;
}
}
Main Class is same as given above in the question
If Program will not run then comment in the comment section there is a change in one function Called Stalemate since I'm unable to understand the use of this function.
In a game of Tic Tac Toe, two players take turns making an available cell in...
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...
(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...
In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...
Java project In a game of tic-tac-toe, two players take turns marking 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 draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...
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...
Hi, I am a student in college who is trying to make a tic tac toe game on Java. Basically, we have to make our own game, even ones that already exist, on Java. We also have to implement an interface, that I have no idea exactly. The professor also stated we need at least 2 different data structures and algorithms for the code. The professor said we could use code online, we'd just have to make some of our...
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...
(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...
You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...