Share
Explore

Java Objects: Make an OBJECT Oriented tik tak toe game in which user plays with computer

Learning Outcome: Introduction to Java Objects and Object-Oriented Tic Tac Toe Game

Introduction to Java Objects

Learning Outcomes:

Understand the concepts of classes, objects, and object-oriented programming.
Learn how to create classes and instantiate objects in Java.
Understand the basics of encapsulation, inheritance, and polymorphism.
Activities and Exercises:
Introduction to object-oriented programming (OOP) concepts (15 minutes)
Creating classes and objects in Java (15 minutes)
Encapsulation and data hiding (15 minutes)
Inheritance and polymorphism (15 minutes)

Designing the Object-Oriented Tic Tac Toe Game

Learning Outcomes:
Understand the structure of a basic Tic Tac Toe game.
Learn how to design classes and objects for the game.
Implement game logic using OOP principles.
Activities and Exercises:
Analyzing the Tic Tac Toe game requirements (15 minutes)
Designing classes and objects for the game (15 minutes)
Implementing the game logic (30 minutes)

Adding a Computer Player to the Tic Tac Toe Game

Learning Outcomes:
Learn how to create a simple AI for the computer player.
Understand how to implement game strategies for the computer player.
Integrate the computer player into the existing game.
Activities and Exercises:
Introduction to simple AI concepts for Tic Tac Toe (15 minutes)
Implementing a basic strategy for the computer player (15 minutes)
Integrating the computer player into the game (30 minutes)
Here's an outline of the classes and methods for the Object-Oriented Tic Tac Toe game:
Player class
name: String
symbol: char
getName(): String
getSymbol(): char
makeMove(Board, int, int): boolean
HumanPlayer class (extends Player)
makeMove(Board, int, int): boolean
ComputerPlayer class (extends Player)
makeMove(Board): boolean
findBestMove(Board): int[]
Board class
board: char[][]
displayBoard(): void
isCellEmpty(int, int): boolean
markCell(int, int, char): boolean
isFull(): boolean
checkWin(char): boolean
TicTacToe class
board: Board
player1: Player
player2: Player
startGame(): void
The TicTacToe class will have the main method that initializes the game objects and starts the game loop. The ComputerPlayer will implement a basic strategy (e.g., random moves or minimax algorithm) to make its moves.


Here's the complete code for a simple Tic Tac Toe game in Java, where the user plays against the computer. This code uses the basic strategy provided earlier.
import java.util.Scanner;
import java.util.Random;

public class TicTacToe {
private char[][] board;
private final char USER = 'X';
private final char COMPUTER = 'O';
private final char EMPTY = '.';

public TicTacToe() {
board = new char[3][3];
initializeBoard();
}

private void initializeBoard() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
board[row][col] = EMPTY;
}
}
}

public void printBoard() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
System.out.print(board[row][col]);
}
System.out.println();
}
}

public boolean isBoardFull() {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (board[row][col] == EMPTY) {
return false;
}
}
}
return true;
}

public boolean hasWon(char player) {
// Check rows and columns
for (int i = 0; i < 3; i++) {
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player)
|| (board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true;
}
}

// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player)
|| (board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true;
}

return false;
}

public void makeMove(int row, int col, char player) {
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == EMPTY) {
board[row][col] = player;
}
}

public int[] computerMove() {
int[] move = new int[2];
Random random = new Random();

do {
move[0] = random.nextInt(3);
move[1] = random.nextInt(3);
} while (board[move[0]][move[1]] != EMPTY);

return move;
}

public static void main(String[] args) {
TicTacToe game = new TicTacToe();
Scanner scanner = new Scanner(System.in);
int userRow, userCol;
int[] computerMove;

while (!game.isBoardFull()) {
game.printBoard();

System.out.print("Enter row and column (0-2): ");
userRow = scanner.nextInt();
userCol = scanner.nextInt();
game.makeMove(userRow, userCol, game.USER);

if (game.hasWon(game.USER)) {
game.printBoard();
System.out.println("You won!");
break;
}

if (!game.isBoardFull()) {
computerMove = game.computerMove();
game.makeMove(computerMove[0], computerMove[1], game.COMPUTER);
System.out.println("Computer move: " + computerMove[0] + " " + computerMove[1]);

if (game.hasWon(game.COMPUTER)) {
game.printBoard();
System.out.println("Computer won!");
break;
}
}
}

if (game.isBoardFull() && !game.hasWon(game.USER) && !game.hasWon(game.COMPUTER)) {
game.printBoard();
System.out.println("It's a draw!");
}
}
}
This code consists of a TicTacToe class with methods for initializing, printing, and updating the game board. It also checks for a win or draw and makes moves for both the user and the computer.
The main method serves as the game loop, prompting the user for input and making the computer move.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.