Created
September 11, 2023 08:29
-
-
Save ShivangiM/b1035b18f4405183449c594f2b76e3f2 to your computer and use it in GitHub Desktop.
Play Tic Tack Toe with running this Java program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TicTacToe { | |
private char[][] board; | |
private char currentPlayer; | |
public TicTacToe() { | |
board = new char[3][3]; | |
currentPlayer = 'X'; | |
initializeBoard(); | |
} | |
public void initializeBoard() { | |
for (int row = 0; row < 3; row++) { | |
for (int col = 0; col < 3; col++) { | |
board[row][col] = ' '; | |
} | |
} | |
} | |
public void printBoard() { | |
System.out.println("-------------"); | |
for (int row = 0; row < 3; row++) { | |
System.out.print("| "); | |
for (int col = 0; col < 3; col++) { | |
System.out.print(board[row][col] + " | "); | |
} | |
System.out.println("\n-------------"); | |
} | |
} | |
public boolean isBoardFull() { | |
for (int row = 0; row < 3; row++) { | |
for (int col = 0; col < 3; col++) { | |
if (board[row][col] == ' ') { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
public boolean makeMove(int row, int col) { | |
if (row < 0 || row >= 3 || col < 0 || col >= 3 || board[row][col] != ' ') { | |
return false; | |
} | |
board[row][col] = currentPlayer; | |
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; | |
return true; | |
} | |
public boolean checkWin() { | |
// Check rows | |
for (int row = 0; row < 3; row++) { | |
if (board[row][0] == currentPlayer && board[row][1] == currentPlayer && board[row][2] == currentPlayer) { | |
return true; | |
} | |
} | |
// Check columns | |
for (int col = 0; col < 3; col++) { | |
if (board[0][col] == currentPlayer && board[1][col] == currentPlayer && board[2][col] == currentPlayer) { | |
return true; | |
} | |
} | |
// Check diagonals | |
if (board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) { | |
return true; | |
} | |
if (board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer) { | |
return true; | |
} | |
return false; | |
} | |
public static void main(String[] args) { | |
TicTacToe game = new TicTacToe(); | |
Scanner scanner = new Scanner(System.in); | |
System.out.println("Welcome to Tic-Tac-Toe!"); | |
System.out.println("Commands:"); | |
System.out.println("- To make a move, enter row and column separated by a space ' ' (e.g., 1 2)."); | |
System.out.println("- To quit the game, enter 'quit' or 'exit'."); | |
boolean gameRunning = true; | |
while (gameRunning) { | |
game.printBoard(); | |
System.out.print("Player " + game.currentPlayer + ", enter your move: "); | |
String input = scanner.nextLine().trim(); | |
if (input.equalsIgnoreCase("quit") || input.equalsIgnoreCase("exit")) { | |
gameRunning = false; | |
System.out.println("Game has been terminated."); | |
continue; | |
} | |
try { | |
String[] inputParts = input.split(" "); | |
int row = Integer.parseInt(inputParts[0]) - 1; | |
int col = Integer.parseInt(inputParts[1]) - 1; | |
if (game.makeMove(row, col)) { | |
if (game.checkWin()) { | |
game.printBoard(); | |
System.out.println("Player " + game.currentPlayer + " wins! Congratulations!"); | |
gameRunning = false; | |
} else if (game.isBoardFull()) { | |
game.printBoard(); | |
System.out.println("It's a draw!"); | |
gameRunning = false; | |
} | |
} else { | |
System.out.println("Invalid move. Try again."); | |
} | |
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { | |
System.out.println("Invalid input. Please enter row and column numbers separated by space (e.g., 1 2), or enter 'quit' to exit."); | |
} | |
} | |
scanner.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment