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

HW - Handling diagonal Winning Rule #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
2 changes: 2 additions & 0 deletions src/main/java/tictactoe/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,6 +36,7 @@ public static void main(String[] args) throws Exception {
List<GameWinningRule> rules = new ArrayList<>();
rules.add(new RowWinningRule());
rules.add(new ColumnWinningRule());
rules.add(new DiagonalWinningRule());

Game game = gameController.startGame(players, rules, 3);

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/tictactoe/model/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/tictactoe/strategy/winning/DiagonalWinningRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol, Integer> diagCount = new HashMap<>();

private final Map<Symbol, Integer> 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
}
Expand Down