diff --git a/src/TicTacToe/strategies/ColumnWinningStratergies.java b/src/TicTacToe/strategies/ColumnWinningStratergies.java new file mode 100644 index 0000000..4be08bf --- /dev/null +++ b/src/TicTacToe/strategies/ColumnWinningStratergies.java @@ -0,0 +1,38 @@ +package TicTacToe.strategies; + +import TicTacToe.models.Board; +import TicTacToe.models.Move; +import TicTacToe.models.Symbol; + +import java.util.HashMap; + +public class ColumnWinningStratergies implements WinningStrategy { + + HashMap> counts = new HashMap<>(); + @Override + public boolean checkWinner(Board board , Move move) { + + // O(1) + // 0 -> {"X" , 2} + // 0 -> {"O" , 0} + int c = move.getCell().getCol(); + Symbol symbol = move.getCell().getSymbol(); + + if(!counts.containsKey(c)){ + counts.put(c, new HashMap<>()); + } + + HashMap countCol = counts.get(c); + + if(!countCol.containsKey(symbol)){ + countCol.put(symbol, 0); + } + countCol.put(symbol, countCol.get(symbol) + 1); + + return countCol.get(symbol) == board.getSize(); + + } +} + + + diff --git a/src/TicTacToe/strategies/RowWinningStrategy.java b/src/TicTacToe/strategies/RowWinningStrategy.java index e055e3a..fe5db85 100644 --- a/src/TicTacToe/strategies/RowWinningStrategy.java +++ b/src/TicTacToe/strategies/RowWinningStrategy.java @@ -1,7 +1,31 @@ package TicTacToe.strategies; +import TicTacToe.models.Board; +import TicTacToe.models.Move; +import TicTacToe.models.Symbol; + +import java.util.HashMap; + public class RowWinningStrategy implements WinningStrategy { - public void checkWinner() { - System.out.println("Checking for row win"); + + HashMap> counts = new HashMap<>(); + public boolean checkWinner(Board board , Move move) { + // O(1) + // 0 -> {{"X" , 2}, {"O" , 1}} + int r = move.getCell().getRow(); + Symbol symbol = move.getCell().getSymbol(); + + if(!counts.containsKey(r)){ + counts.put(r, new HashMap<>()); + } + + HashMap countRow = counts.get(r); + + if(!countRow.containsKey(symbol)){ + countRow.put(symbol, 0); + } + countRow.put(symbol, countRow.get(symbol) + 1); + + return countRow.get(symbol) == board.getSize(); } } diff --git a/src/TicTacToe/strategies/WinningStrategy.java b/src/TicTacToe/strategies/WinningStrategy.java index e496224..f8087af 100644 --- a/src/TicTacToe/strategies/WinningStrategy.java +++ b/src/TicTacToe/strategies/WinningStrategy.java @@ -1,6 +1,9 @@ package TicTacToe.strategies; +import TicTacToe.models.Board; +import TicTacToe.models.Move; + public interface WinningStrategy { - public void checkWinner(); + public boolean checkWinner(Board board , Move move); }