0% found this document useful (0 votes)
6 views

Tic Tac Toe Game

Uploaded by

eeshu.soma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Tic Tac Toe Game

Uploaded by

eeshu.soma
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

//Eeshita Soma

//Computer Science II K - 4th

import java.util.Scanner;

public class TicTacToeGame {

public static void main (String[] args) {


Scanner keyboard = new Scanner(System.in);
String[][] grid = new String[3][3];
TicTacToeMethods.setGrid(grid);
boolean gameOn = true;
String currentPlayer = "X";

System.out.println("Welcome to Tic Tac Toe!");


TicTacToeMethods.printGrid(grid);

while (gameOn) {
System.out.println(currentPlayer + "'s turn. Enter row and column (1-
3): ");
int row = keyboard.nextInt() - 1;
int col = keyboard.nextInt() - 1;

if (TicTacToeMethods.checkValid(grid, row, col)) {


grid[row][col] = currentPlayer;
TicTacToeMethods.printGrid(grid);

if (TicTacToeMethods.checkWin(grid)) {
System.out.println("Game over! " + currentPlayer + " wins!");
gameOn = false;
} else if (isDraw(grid)) {
System.out.println("Game over! It's a draw!");
gameOn = false;
} else {
currentPlayer = currentPlayer.equals("X") ? "O" : "X";
}
} else {
System.out.println("Invalid move. Try again.");
}
}
}

// Helper method to check for a draw


public static boolean isDraw(String[][] grid) {
for (String[] row : grid) {
for (String cell : row) {
if (cell.equals("*")) {
return false;
}
}
}
return true;
}
}

You might also like