diff --git a/src/main/java/tictactoe/Client.java b/src/main/java/tictactoe/Client.java index 95e7009..2dfb687 100644 --- a/src/main/java/tictactoe/Client.java +++ b/src/main/java/tictactoe/Client.java @@ -9,6 +9,7 @@ import tictactoe.model.types.GameState; import tictactoe.model.types.PlayerType; import tictactoe.strategy.winning.ColumnWinningRule; +import tictactoe.strategy.winning.DiagonalWinningRule; import tictactoe.strategy.winning.GameWinningRule; import tictactoe.strategy.winning.RowWinningRule; @@ -35,6 +36,7 @@ public static void main(String[] args) throws Exception { List rules = new ArrayList<>(); rules.add(new RowWinningRule()); rules.add(new ColumnWinningRule()); + rules.add(new DiagonalWinningRule()); Game game = gameController.startGame(players, rules, 3); diff --git a/src/main/java/tictactoe/model/Game.java b/src/main/java/tictactoe/model/Game.java index d931f6a..8b871ea 100644 --- a/src/main/java/tictactoe/model/Game.java +++ b/src/main/java/tictactoe/model/Game.java @@ -109,12 +109,17 @@ private boolean checkWinner(Board board, Move currMove) { } private boolean validateMove(Move currMove) { - if(currMove.getCell().getCellState().equals(CellState.FILLED)){ - return false; - } + // if(currMove.getCell().getCellState().equals(CellState.FILLED)){ + // return false; + // } + // the above Condition is wrong since we are initializing the cellstate to empty for currMove + // We need to check the Board current row and col int row = currMove.getCell().getRow(); int col = currMove.getCell().getCol(); + if(board.getCells().get(row).get(col).cellState.equals(CellState.FILLED)) { + return false; + } if (row >= board.getSize()) { return false; diff --git a/src/main/java/tictactoe/strategy/winning/DiagonalWinningRule.java b/src/main/java/tictactoe/strategy/winning/DiagonalWinningRule.java index 8a734b3..a008671 100644 --- a/src/main/java/tictactoe/strategy/winning/DiagonalWinningRule.java +++ b/src/main/java/tictactoe/strategy/winning/DiagonalWinningRule.java @@ -2,10 +2,50 @@ import tictactoe.model.Board; import tictactoe.model.Move; +import tictactoe.model.Symbol; + +import java.util.HashMap; +import java.util.Map; public class DiagonalWinningRule implements GameWinningRule{ + private final Map diagCount = new HashMap<>(); + + private final Map antiDiagCount = new HashMap<>(); @Override public boolean checkWinner(Board board, Move move) { + //S1: take the row and col and playerSymbol + //S2: check if the row and col comes under diag or antiDiag + //s3: check whether playerSymbol is existed other add the player symbol and count + + int row = move.getCell().getRow(); + int col = move.getCell().getCol(); + Symbol playerSymbol = move.getPlayer().getSymbol(); + + //condition to check diagonal + if(row == col) { + if(!diagCount.containsKey(playerSymbol)) { + diagCount.put(playerSymbol, 0); + } + diagCount.put(playerSymbol, diagCount.get(playerSymbol)+1); + + if(diagCount.get(playerSymbol).equals(board.getSize())) { + return true; + } + } + + + //condition to check antiDiagonal + if(row+col == board.getSize()-1) { + if(!antiDiagCount.containsKey(playerSymbol)) { + antiDiagCount.put(playerSymbol, 0); + } + antiDiagCount.put(playerSymbol, antiDiagCount.get(playerSymbol)+1); + + if(antiDiagCount.get(playerSymbol).equals(board.getSize())) { + return true; + } + } + return false; //HW }