Skip to content

Commit a824015

Browse files
author
Vishal M Yadav
committed
in progress added strategy layer for tic tac toe
1 parent 139a196 commit a824015

File tree

6 files changed

+133
-82
lines changed

6 files changed

+133
-82
lines changed

src/main/java/com/gatomalvado/tictactoe/model/Board.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
package com.gatomalvado.tictactoe.model;
2-
3-
4-
5-
62
public class Board {
73

84
private Symbol[][] cells;
95
private int size;
106

117
public Board(int size) {
128
this.size = size;
13-
if(size==2){
14-
cells = new Symbol[size+1][size+1];
15-
}else if(size > 2){
16-
cells = new Symbol[size+][]
17-
}
9+
cells = new Symbol[size][size];
1810
}
1911

2012
public void printBoard() {

src/main/java/com/gatomalvado/tictactoe/orchestrator/TicTacToeGame.java

Lines changed: 10 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import com.gatomalvado.tictactoe.model.Move;
1212
import com.gatomalvado.tictactoe.model.Symbol;
1313
import com.gatomalvado.tictactoe.model.User;
14+
import com.gatomalvado.tictactoe.service.BoardFactory;
15+
import com.gatomalvado.tictactoe.service.StrategyFactory;
1416

1517

1618
public class TicTacToeGame {
@@ -21,14 +23,17 @@ public class TicTacToeGame {
2123
private Map<String, User> symbolUserMap;
2224
private Queue<User> turnQueue;
2325
private List<Move> moves;
24-
26+
private StrategyFactory strategyFactory;
27+
private BoardFactory boardFactory;
2528
private User winner;
2629

2730
public TicTacToeGame(List<Symbol> symbolList, List<User> users) {
2831
this.symbolList = symbolList;
2932
this.users = users;
3033
this.moves = new ArrayList<>();
31-
this.board = new Board(users.size());
34+
this.boardFactory = new BoardFactory();
35+
this.strategyFactory = new StrategyFactory();
36+
this.board = this.boardFactory.createBoard(users.size());
3237
generateSymbolUserMap();
3338
createTurns();
3439
}
@@ -60,80 +65,12 @@ public boolean takeMove(Move move) {
6065

6166

6267
public boolean checkAnyWinner() {
63-
int size = board.getSize();
64-
65-
// validate vertical rows
66-
for (int j = 0; j < size; j++) {
67-
// scroll through one column
68-
Symbol firstSymbol = board.getCell(0, j);
69-
boolean isAllCrossed = true;
70-
for (int i = 1; i < size; i++) {
71-
Symbol currSymbol = board.getCell(i, j);
72-
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
73-
// ignore this column and move forward
74-
isAllCrossed = false;
75-
continue;
76-
}
77-
}
78-
if (isAllCrossed) {
79-
this.winner = moves.getLast().getUser();
80-
return true;
81-
}
82-
}
83-
84-
// validate horizontal rows
85-
for (int i = 0; i < size; i++) {
86-
// scroll throw one row
87-
Symbol firstSymbol = board.getCell(i, 0);
88-
boolean isAllCrossed = true;
89-
for (int j = 1; j < size; j++) {
90-
Symbol currSymbol = board.getCell(i, j);
91-
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
92-
// ignore this column and move forward
93-
isAllCrossed = false;
94-
continue;
95-
}
96-
}
97-
if (isAllCrossed) {
98-
this.winner = moves.getLast().getUser();
99-
return true;
100-
}
101-
}
102-
103-
// validate top left to bottom right diagonal
104-
Symbol firstSymbol = board.getCell(0, 0);
105-
boolean isAllCrossed = true;
106-
for (int i = 0; i < size; i++) {
107-
Symbol currSymbol = board.getCell(i, i);
108-
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
109-
// ignore this column and move forward
110-
isAllCrossed = false;
111-
break;
112-
}
113-
}
114-
115-
if (isAllCrossed) {
68+
boolean isThereAnyWinner = this.strategyFactory.getWinnerStrategy(this.board).isThereIsAnyWinner(board);
69+
if(isThereAnyWinner){
11670
this.winner = moves.getLast().getUser();
11771
return true;
11872
}
119-
120-
// validate top right to bottom left diagonal
121-
firstSymbol = board.getCell(0, size - 1);
122-
isAllCrossed = true;
123-
for (int i = 1; i < size; i++) {
124-
Symbol currSymbol = board.getCell(i, size - 1 - i);
125-
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
126-
// ignore this column and move forward
127-
isAllCrossed = false;
128-
break;
129-
}
130-
}
131-
132-
if(isAllCrossed){
133-
this.winner = moves.getLast().getUser();
134-
}
135-
136-
return isAllCrossed;
73+
return false;
13774
}
13875

13976
private boolean validateMove(Move move) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.gatomalvado.tictactoe.service;
2+
3+
import com.gatomalvado.tictactoe.model.Board;
4+
5+
public class BoardFactory {
6+
public Board createBoard(int noOfUsers){
7+
switch(noOfUsers){
8+
case 2:
9+
return new Board(3);
10+
default:
11+
throw new UnsupportedOperationException("This is not supported yet");
12+
}
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.gatomalvado.tictactoe.service;
2+
3+
import com.gatomalvado.tictactoe.model.Board;
4+
5+
public interface IWinnerDeciderStrategy {
6+
7+
boolean isThereIsAnyWinner(Board board);
8+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.gatomalvado.tictactoe.service;
2+
3+
import com.gatomalvado.tictactoe.model.Board;
4+
import com.gatomalvado.tictactoe.service.impl.TwoWinnerStrategy;
5+
6+
public class StrategyFactory {
7+
8+
private IWinnerDeciderStrategy twoWinnerStrategy;
9+
10+
public StrategyFactory(){
11+
this.twoWinnerStrategy = new TwoWinnerStrategy();
12+
}
13+
14+
public IWinnerDeciderStrategy getWinnerStrategy(Board board){
15+
switch(board.getSize()){
16+
case 3:
17+
return twoWinnerStrategy;
18+
default:
19+
throw new UnsupportedOperationException("This is not supported yet");
20+
}
21+
}
22+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.gatomalvado.tictactoe.service.impl;
2+
3+
import com.gatomalvado.tictactoe.model.Board;
4+
import com.gatomalvado.tictactoe.model.Symbol;
5+
import com.gatomalvado.tictactoe.service.IWinnerDeciderStrategy;
6+
7+
public class TwoWinnerStrategy implements IWinnerDeciderStrategy {
8+
9+
@Override
10+
public boolean isThereIsAnyWinner(Board board) {
11+
int size = board.getSize();
12+
13+
// validate vertical rows
14+
for (int j = 0; j < size; j++) {
15+
// scroll through one column
16+
Symbol firstSymbol = board.getCell(0, j);
17+
boolean isAllCrossed = true;
18+
for (int i = 1; i < size; i++) {
19+
Symbol currSymbol = board.getCell(i, j);
20+
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
21+
// ignore this column and move forward
22+
isAllCrossed = false;
23+
continue;
24+
}
25+
}
26+
if (isAllCrossed) {
27+
return true;
28+
}
29+
}
30+
31+
// validate horizontal rows
32+
for (int i = 0; i < size; i++) {
33+
// scroll throw one row
34+
Symbol firstSymbol = board.getCell(i, 0);
35+
boolean isAllCrossed = true;
36+
for (int j = 1; j < size; j++) {
37+
Symbol currSymbol = board.getCell(i, j);
38+
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
39+
// ignore this column and move forward
40+
isAllCrossed = false;
41+
continue;
42+
}
43+
}
44+
if (isAllCrossed) {
45+
return true;
46+
}
47+
}
48+
49+
// validate top left to bottom right diagonal
50+
Symbol firstSymbol = board.getCell(0, 0);
51+
boolean isAllCrossed = true;
52+
for (int i = 0; i < size; i++) {
53+
Symbol currSymbol = board.getCell(i, i);
54+
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
55+
// ignore this column and move forward
56+
isAllCrossed = false;
57+
break;
58+
}
59+
}
60+
61+
if (isAllCrossed) {
62+
return true;
63+
}
64+
65+
// validate top right to bottom left diagonal
66+
firstSymbol = board.getCell(0, size - 1);
67+
isAllCrossed = true;
68+
for (int i = 1; i < size; i++) {
69+
Symbol currSymbol = board.getCell(i, size - 1 - i);
70+
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
71+
// ignore this column and move forward
72+
isAllCrossed = false;
73+
break;
74+
}
75+
}
76+
return isAllCrossed;
77+
}
78+
}

0 commit comments

Comments
 (0)