|
| 1 | +package com.baeldung.algorithms.mcts.tictactoe; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public class Board { |
| 8 | + int[][] boardValues; |
| 9 | + int totalMoves; |
| 10 | + |
| 11 | + public static final int DEFAULT_BOARD_SIZE = 3; |
| 12 | + |
| 13 | + public static final int IN_PROGRESS = -1; |
| 14 | + public static final int DRAW = 0; |
| 15 | + public static final int P1 = 1; |
| 16 | + public static final int P2 = 2; |
| 17 | + |
| 18 | + public Board() { |
| 19 | + boardValues = new int[DEFAULT_BOARD_SIZE][DEFAULT_BOARD_SIZE]; |
| 20 | + } |
| 21 | + |
| 22 | + public Board(int boardSize) { |
| 23 | + boardValues = new int[boardSize][boardSize]; |
| 24 | + } |
| 25 | + |
| 26 | + public Board(int[][] boardValues) { |
| 27 | + this.boardValues = boardValues; |
| 28 | + } |
| 29 | + |
| 30 | + public Board(int[][] boardValues, int totalMoves) { |
| 31 | + this.boardValues = boardValues; |
| 32 | + this.totalMoves = totalMoves; |
| 33 | + } |
| 34 | + |
| 35 | + public Board(Board board) { |
| 36 | + int boardLength = board.getBoardValues().length; |
| 37 | + this.boardValues = new int[boardLength][boardLength]; |
| 38 | + int[][] boardValues = board.getBoardValues(); |
| 39 | + int n = boardValues.length; |
| 40 | + for (int i = 0; i < n; i++) { |
| 41 | + int m = boardValues[i].length; |
| 42 | + for (int j = 0; j < m; j++) { |
| 43 | + this.boardValues[i][j] = boardValues[i][j]; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public void performMove(int player, Position p) { |
| 49 | + this.totalMoves++; |
| 50 | + boardValues[p.getX()][p.getY()] = player; |
| 51 | + } |
| 52 | + |
| 53 | + public int[][] getBoardValues() { |
| 54 | + return boardValues; |
| 55 | + } |
| 56 | + |
| 57 | + public void setBoardValues(int[][] boardValues) { |
| 58 | + this.boardValues = boardValues; |
| 59 | + } |
| 60 | + |
| 61 | + public int checkStatus() { |
| 62 | + int boardSize = boardValues.length; |
| 63 | + int maxIndex = boardSize - 1; |
| 64 | + int[] diag1 = new int[boardSize]; |
| 65 | + int[] diag2 = new int[boardSize]; |
| 66 | + |
| 67 | + for (int i = 0; i < boardSize; i++) { |
| 68 | + int[] row = boardValues[i]; |
| 69 | + int[] col = new int[boardSize]; |
| 70 | + for (int j = 0; j < boardSize; j++) { |
| 71 | + col[j] = boardValues[j][i]; |
| 72 | + } |
| 73 | + |
| 74 | + int checkRowForWin = checkForWin(row); |
| 75 | + if(checkRowForWin!=0) |
| 76 | + return checkRowForWin; |
| 77 | + |
| 78 | + int checkColForWin = checkForWin(col); |
| 79 | + if(checkColForWin!=0) |
| 80 | + return checkColForWin; |
| 81 | + |
| 82 | + diag1[i] = boardValues[i][i]; |
| 83 | + diag2[i] = boardValues[maxIndex - i][i]; |
| 84 | + } |
| 85 | + |
| 86 | + int checkDia1gForWin = checkForWin(diag1); |
| 87 | + if(checkDia1gForWin!=0) |
| 88 | + return checkDia1gForWin; |
| 89 | + |
| 90 | + int checkDiag2ForWin = checkForWin(diag2); |
| 91 | + if(checkDiag2ForWin!=0) |
| 92 | + return checkDiag2ForWin; |
| 93 | + |
| 94 | + if (getEmptyPositions().size() > 0) |
| 95 | + return IN_PROGRESS; |
| 96 | + else |
| 97 | + return DRAW; |
| 98 | + } |
| 99 | + |
| 100 | + private int checkForWin(int[] row) { |
| 101 | + boolean isEqual = true; |
| 102 | + int size = row.length; |
| 103 | + int previous = row[0]; |
| 104 | + for (int i = 0; i < size; i++) { |
| 105 | + if (previous != row[i]) { |
| 106 | + isEqual = false; |
| 107 | + break; |
| 108 | + } |
| 109 | + previous = row[i]; |
| 110 | + } |
| 111 | + if(isEqual) |
| 112 | + return previous; |
| 113 | + else |
| 114 | + return 0; |
| 115 | + } |
| 116 | + |
| 117 | + public void printBoard() { |
| 118 | + int size = this.boardValues.length; |
| 119 | + for (int i = 0; i < size; i++) { |
| 120 | + for (int j = 0; j < size; j++) { |
| 121 | + System.out.print(boardValues[i][j] + " "); |
| 122 | + } |
| 123 | + System.out.println(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public List<Position> getEmptyPositions() { |
| 128 | + int size = this.boardValues.length; |
| 129 | + List<Position> emptyPositions = new ArrayList<>(); |
| 130 | + for (int i = 0; i < size; i++) { |
| 131 | + for (int j = 0; j < size; j++) { |
| 132 | + if (boardValues[i][j] == 0) |
| 133 | + emptyPositions.add(new Position(i, j)); |
| 134 | + } |
| 135 | + } |
| 136 | + return emptyPositions; |
| 137 | + } |
| 138 | + |
| 139 | + public void printStatus() { |
| 140 | + switch (this.checkStatus()) { |
| 141 | + case P1: |
| 142 | + System.out.println("Player 1 wins"); |
| 143 | + break; |
| 144 | + case P2: |
| 145 | + System.out.println("Player 2 wins"); |
| 146 | + break; |
| 147 | + case DRAW: |
| 148 | + System.out.println("Game Draw"); |
| 149 | + break; |
| 150 | + case IN_PROGRESS: |
| 151 | + System.out.println("Game In rogress"); |
| 152 | + break; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments