Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added strategies #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/TicTacToe/strategies/ColumnWinningStratergies.java
Original file line number Diff line number Diff line change
@@ -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<Integer, HashMap<Symbol, Integer>> 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<Symbol, Integer> 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();

}
}



28 changes: 26 additions & 2 deletions src/TicTacToe/strategies/RowWinningStrategy.java
Original file line number Diff line number Diff line change
@@ -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<Integer, HashMap<Symbol, Integer>> 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<Symbol, Integer> 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();
}
}
5 changes: 4 additions & 1 deletion src/TicTacToe/strategies/WinningStrategy.java
Original file line number Diff line number Diff line change
@@ -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);
}