Bài Thực Hành Số 8

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Bài thực hành số 8

1. Sơ đồ lớp

2. Sơ đồ tuần tự

Bài thực hành số 8 1


3. Code
import java.util.Random;

public class TicTacToeGame {


private final TicTacToeBoard board;
private Player currentPlayer;

public TicTacToeGame() {
this.currentPlayer = getRandomPlayer();
this.board = new TicTacToeBoard();

Bài thực hành số 8 2


private Player getRandomPlayer() {
Random random = new Random();
return (random.nextInt(2) == 1) ? Player.HUMAN : Playe
}

public void startGame() {


System.out.println("Welcome to the game");
displayBoard();

while (!board.isGameOver()) {
Move temp;
if (currentPlayer == Player.COMPUTER) {
temp = board.getRandomValidMove();
} else {
temp = TicTacToeScanner.readMove();
}
board.updateBoard(temp, currentPlayer);
displayBoard();
switchPlayer();
}
announceWinner();
}

private void announceWinner() {


Player winner = board.getWinner();
if (winner == null) {
System.out.println("It's a draw");
} else {
System.out.println(STR."The winner is: \{winner}"
}
}

private void switchPlayer() {


currentPlayer = (currentPlayer == Player.HUMAN) ? Play
}

private void displayBoard() {


System.out.println(board);

Bài thực hành số 8 3


}

public class Move {


private int row;
private int column;

public Move(int row, int column) {


this.row = row;
this.column = column;
}

public int getRow() {


return this.row;
}

public int getColumn() {


return this.column;
}
}

import java.util.Scanner;

public class TicTacToeScanner {


private static final Scanner scanner = new Scanner(System

public static Move readMove() {


System.out.println("Enter row and column (e.g., 1 1):
int row = scanner.nextInt();
int column = scanner.nextInt();
return new Move(row, column);
}
}

public enum Player {


HUMAN("human"),

Bài thực hành số 8 4


COMPUTER("computer");

String value;

private Player (String value) {


this.value = value;
}

public String getPlayer() {


return value;
}

import java.util.Random;

public class TicTacToeBoard {


private static final int SIZE = 3;
private char[][] board;

public TicTacToeBoard() {
initializeBoard();
}

private void initializeBoard() {


board = new char[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = '-';
}
}
}

public boolean isMoveValid(Move move) {


int row = move.getRow();
int col = move.getColumn();
return row >= 0 && row < SIZE && col >= 0 && col < SIZ
}

Bài thực hành số 8 5


public void updateBoard(Move move, Player player) {
board[move.getRow()][move.getColumn()] = player == Pla
}

public boolean isGameOver() {


return getWinner() != null || isBoardFull();
}

public Player getWinner() {


for (int i = 0; i < SIZE; i++) {
// Check rows
if (board[i][0] == board[i][1] && board[i][1] ==
return board[i][0] == 'X' ? Player.HUMAN : Pla
}
// Check columns
if (board[0][i] == board[1][i] && board[1][i] ==
return board[0][i] == 'X' ? Player.HUMAN : Pla
}
}
// Check diagonals
if (board[0][0] == board[1][1] && board[1][1] == board
return board[0][0] == 'X' ? Player.HUMAN : Player
}
if (board[0][2] == board[1][1] && board[1][1] == board
return board[0][2] == 'X' ? Player.HUMAN : Player
}
return null; // No winner
}

private boolean isBoardFull() {


for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i][j] == '-') {
return false; // Found an empty cell
}
}
}

Bài thực hành số 8 6


return true; // Board is full
}

public Move getRandomValidMove() {


Random random = new Random();
int row, col;
do {
row = random.nextInt(SIZE);
col = random.nextInt(SIZE);
} while (!isMoveValid(new Move(row, col)));
return new Move(row, col);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
sb.append(board[i][j]).append(' ');
}
sb.append('\n');
}
return sb.toString();
}
}

Bài thực hành số 8 7

You might also like