TicTacToe.java (3937B) download
1package nl.isygameclient.models.games.tictactoe;
2
3import nl.isygameclient.models.Game;
4import nl.isygameclient.models.Player;
5import nl.isygameclient.models.PlayerManager;
6import nl.isygameclient.models.board.HistoryBoard;
7import nl.isygameclient.util.Vector2D;
8
9import java.util.*;
10
11public class TicTacToe extends Game {
12
13 public TicTacToe(PlayerManager playerManager) {
14 super(playerManager, new HistoryBoard<>(3,3){});
15 }
16
17 @Override
18 public void restart() {
19 playerManager.restart();
20 board = new HistoryBoard<>(3,3){};
21 }
22
23 @Override
24 public boolean move(Player player, Vector2D<Integer, Integer> pos) {
25 if (!isMoveValid(player, pos)) {
26 return false;
27 }
28
29 var currentPlayer = playerManager.getCurrentPlayer();
30 Map<Vector2D<Integer, Integer>, Player> changes = new HashMap<>();
31 changes.put(pos, currentPlayer);
32 board.add(currentPlayer, changes);
33
34 playerManager.nextPlayer();
35 return true;
36 }
37
38 @Override
39 public boolean isMoveValid(Player player, Vector2D<Integer, Integer> pos) {
40 boolean positionOnBoard = (pos.getX() >= 0 && pos.getX() < board.getWidth()) && (pos.getY() >= 0 && pos.getY() < board.getHeight());
41 boolean positionEmpty = (board.get(pos) == null);
42 return positionOnBoard && positionEmpty;
43 }
44
45 @Override
46 public boolean isDraw() {
47 return !Objects.equals(board, null);
48 }
49
50 @Override
51 public boolean isGameOver() {
52 return getWinners().size() < 1 || isDraw();
53 }
54
55
56 private boolean isPlayer(Player player, Vector2D<Integer, Integer> pos) {
57 return Objects.equals(player, board.get(pos));
58 }
59 @Override
60 public boolean isWinner(Player player) {
61 boolean topRow = isPlayer(player, new Vector2D<>(0,0)) && isPlayer(player, new Vector2D<>(0,1)) && isPlayer(player, new Vector2D<>(0,2));
62 boolean middleRow = isPlayer(player, new Vector2D<>(1,0)) && isPlayer(player, new Vector2D<>(1,1)) && isPlayer(player, new Vector2D<>(1,2));
63 boolean bottomRow = isPlayer(player, new Vector2D<>(2,0)) && isPlayer(player, new Vector2D<>(2,1)) && isPlayer(player, new Vector2D<>(2,2));
64
65 boolean leftColumn = isPlayer(player, new Vector2D<>(0,0)) && isPlayer(player, new Vector2D<>(0,1)) && isPlayer(player, new Vector2D<>(0,2));
66 boolean midColumn = isPlayer(player, new Vector2D<>(1,0)) && isPlayer(player, new Vector2D<>(1,1)) && isPlayer(player, new Vector2D<>(1,2));
67 boolean rightColumn = isPlayer(player, new Vector2D<>(2,0)) && isPlayer(player, new Vector2D<>(2,1)) && isPlayer(player, new Vector2D<>(2,2));
68
69 boolean lrCross = isPlayer(player, new Vector2D<>(0,0)) && isPlayer(player, new Vector2D<>(1,1)) && isPlayer(player, new Vector2D<>(2,2));
70 boolean rlCross = isPlayer(player, new Vector2D<>(0,2)) && isPlayer(player, new Vector2D<>(1,1)) && isPlayer(player, new Vector2D<>(2,0));
71
72 return topRow || middleRow || bottomRow || leftColumn || midColumn || rightColumn || lrCross || rlCross;
73 }
74
75 @Override
76 public List<Vector2D<Integer, Integer>> getValidMoves(Player player) {
77 List<Vector2D<Integer, Integer>> valid = new ArrayList<>();
78 for (int y = 0; y < board.getHeight(); y++) {
79 for (int x = 0; x < board.getWidth(); x++) {
80 if (isMoveValid(player, new Vector2D<>(x, y))) valid.add(new Vector2D<>(x,y));
81 }
82 }
83 return valid;
84 }
85
86 public List<Player> getWinners(){
87 List<Player> players = new ArrayList<>();
88 for (Player player: playerManager.getPlayers()) {
89 if (isWinner(player)) {
90 players.add(player);
91 }
92 }
93 return players;
94 }
95
96 @Override
97 public int getPlayerScore(Player player){
98 if (isGameOver() && isWinner(player)) {
99 return 1;
100 }
101 return 0;
102 }
103}