Environment: Eclipse IDE • Topic: IP Networking? (No) • Topic: Java Console Game
Create a text-based Tic Tac Toe game using a 3x3 grid. The game runs in the console and supports either:
lastname with your real last name everywhere.
Example: PX_TicTacToe_Cusack.java and class name PX_TicTacToe_Cusack.
main)Players enter row and col (1–3).
Example: 2 3 means row 2, column 3.
Create 3x3 board filled with spaces
Ask user to choose game mode (PvP or PvC)
Set current player to X
LOOP until game over:
Print board
IF computer turn:
pick an empty spot and place O
ELSE:
ask for row/col
validate (range + empty)
place current player's mark
IF current player has 3 in a row:
announce winner, end loop
ELSE IF board full:
announce tie, end loop
ELSE:
switch player
Print final board and end message
IMPORTANT: Rename the class and file using your last name.
Example: PX_TicTacToe_Cusack.
import java.util.Random;
import java.util.Scanner;
/*
* File: PX_TicTacToe_lastname.java
* Console Tic Tac Toe
* Skills: 2D arrays, methods, game state checking, input validation
*
* Turn in:
* 1) PX_TicTacToe_lastname.java
* 2) PX_TicTacToe_lastname.png (screenshot of completed game in console)
* 3) PX_TicTacToe_lastname.mp4 (video of you running and playing the game)
*/
public class PX_TicTacToe_lastname {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char[][] board = createBoard();
Random rand = new Random();
System.out.println("=== TIC TAC TOE (Console) ===");
System.out.println("1) Player vs Player");
System.out.println("2) Player vs Computer");
int mode = getMode(input);
char currentPlayer = 'X';
boolean gameOver = false;
while (!gameOver) {
printBoard(board);
if (mode == 2 && currentPlayer == 'O') {
System.out.println("Computer (O) is choosing a move...");
computerMove(board, rand);
} else {
System.out.println("Player " + currentPlayer + ", enter your move (row col) from 1 to 3:");
int[] move = getValidMove(board, input);
placeMove(board, move[0], move[1], currentPlayer);
}
// Check win or tie
if (isWinner(board, currentPlayer)) {
printBoard(board);
System.out.println("✅ Player " + currentPlayer + " WINS!");
gameOver = true;
} else if (isTie(board)) {
printBoard(board);
System.out.println("🤝 It's a TIE!");
gameOver = true;
} else {
currentPlayer = switchPlayer(currentPlayer);
}
}
input.close();
}
// =========================
// Board Setup + Display
// =========================
public static char[][] createBoard() {
char[][] b = new char[3][3];
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
b[r][c] = ' ';
}
}
return b;
}
public static void printBoard(char[][] b) {
System.out.println();
System.out.println(" 1 2 3");
System.out.println(" -------------");
for (int r = 0; r < 3; r++) {
System.out.print((r + 1) + " |");
for (int c = 0; c < 3; c++) {
System.out.print(" " + b[r][c] + " |");
}
System.out.println();
System.out.println(" -------------");
}
System.out.println();
}
// =========================
// Game Mode
// =========================
public static int getMode(Scanner input) {
int mode;
while (true) {
System.out.print("Choose mode (1 or 2): ");
if (input.hasNextInt()) {
mode = input.nextInt();
if (mode == 1 || mode == 2) {
return mode;
}
} else {
input.next(); // clear bad input
}
System.out.println("Invalid. Enter 1 or 2.");
}
}
// =========================
// Human Move + Validation
// =========================
public static int[] getValidMove(char[][] board, Scanner input) {
while (true) {
int row, col;
// Ensure we get two integers
if (!input.hasNextInt()) {
input.next();
System.out.println("Invalid input. Enter two numbers like: 2 3");
continue;
}
row = input.nextInt();
if (!input.hasNextInt()) {
input.next();
System.out.println("Invalid input. Enter two numbers like: 2 3");
continue;
}
col = input.nextInt();
// Convert 1-3 input into 0-2 indexes
row--;
col--;
if (row < 0 || row > 2 || col < 0 || col > 2) {
System.out.println("Row and col must be between 1 and 3.");
continue;
}
if (board[row][col] != ' ') {
System.out.println("That spot is already taken. Choose another.");
continue;
}
return new int[]{row, col};
}
}
public static void placeMove(char[][] board, int row, int col, char player) {
board[row][col] = player;
}
// =========================
// Computer Move
// =========================
public static void computerMove(char[][] board, Random rand) {
// Simple AI: pick a random empty spot
while (true) {
int row = rand.nextInt(3);
int col = rand.nextInt(3);
if (board[row][col] == ' ') {
board[row][col] = 'O';
System.out.println("Computer chose: " + (row + 1) + " " + (col + 1));
break;
}
}
}
// =========================
// Win/Tie Checking
// =========================
public static boolean isWinner(char[][] b, char p) {
// Rows
for (int r = 0; r < 3; r++) {
if (b[r][0] == p && b[r][1] == p && b[r][2] == p) return true;
}
// Columns
for (int c = 0; c < 3; c++) {
if (b[0][c] == p && b[1][c] == p && b[2][c] == p) return true;
}
// Diagonals
if (b[0][0] == p && b[1][1] == p && b[2][2] == p) return true;
if (b[0][2] == p && b[1][1] == p && b[2][0] == p) return true;
return false;
}
public static boolean isTie(char[][] b) {
for (int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
if (b[r][c] == ' ') return false; // still moves available
}
}
return true;
}
public static char switchPlayer(char p) {
return (p == 'X') ? 'O' : 'X';
}
}
PX_TicTacToe_lastname.pngPX_TicTacToe_lastname.mp4| Category | Points | What I’m Looking For |
|---|---|---|
| 2D Array Board | 20 | Uses a 3x3 char[][] board and updates it correctly. |
| Board Display | 15 | Clean console display with row/column labels, updates after each turn. |
| Input Validation | 20 | Rejects invalid entries, out-of-range moves, and occupied spots. |
| Win + Tie Detection | 25 | Correctly detects wins (rows/cols/diagonals) and ties. |
| Methods + Organization | 10 | Uses multiple methods; code is readable and well-commented. |
| Turn-In Files | 10 | All 3 files submitted with correct naming: .java, .png, .mp4 |