game-client

Play TicTacToe and Reversi
Log | Files | Refs

TicTacToe.java (3992B)


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