Skip to content

Commit 72f6181

Browse files
author
Vishal M Yadav
committed
finished tic tac toe
1 parent a824015 commit 72f6181

File tree

6 files changed

+82
-46
lines changed

6 files changed

+82
-46
lines changed

src/main/java/com/gatomalvado/tictactoe/Main.java

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public static void main(String[] args) {
1919
Scanner scanner = new Scanner(System.in);
2020
int numberOfUsers = scanner.nextInt();
2121
System.out.println("Total number of users -> "+numberOfUsers);
22+
if(numberOfUsers <= -1){
23+
System.out.println("Number of users cannot be less than 2");
24+
System.exit(0);
25+
}
2226
List<User> users = new ArrayList<>();
2327
List<Symbol> symbols = getSymbols(numberOfUsers);
2428
for(int i=1; i<=numberOfUsers; i++){
@@ -34,34 +38,43 @@ public static void main(String[] args) {
3438

3539
// users and their symbols have been taken as input
3640
// declare the board
37-
TicTacToeGame game = new TicTacToeGame(symbols, users);
38-
game.printGameCurrentStatus();
39-
int totalMoves = numberOfUsers*numberOfUsers;
40-
for(int i=0; i<totalMoves; i++){
41-
String moveString = scanner.next();
42-
String[] strings = moveString.split(" ");
43-
if(strings.length != 3){
44-
System.out.println("Invalid Move");
45-
i--;
46-
} else{
47-
User user = game.getUser(strings[0]);
48-
if(user == null){
41+
TicTacToeGame game;
42+
try{
43+
game = new TicTacToeGame(symbols, users);
44+
game.printGameCurrentStatus();
45+
int totalMoves = game.getTotalMoves();
46+
for(int i=0; i<totalMoves; i++){
47+
String moveString = scanner.nextLine();
48+
String[] strings = moveString.split(" ");
49+
if(strings.length != 3){
50+
System.out.println("Invalid Move");
4951
i--;
50-
continue;
51-
}
52-
try{
53-
Move move = new Move(Integer.parseInt(strings[0]), Integer.parseInt(strings[1]), user);
54-
if(!game.takeMove(move)){
52+
} else{
53+
User user = game.getUser(strings[0]);
54+
if(user == null){
55+
i--;
56+
continue;
57+
}
58+
try{
59+
Move move = new Move(Integer.parseInt(strings[1]), Integer.parseInt(strings[2]), user);
60+
if(!game.takeMove(move)){
61+
i--;
62+
} else if(game.checkAnyWinner()){
63+
System.out.println(game.getWinner().getName()+" won the game");
64+
break;
65+
}
66+
game.printGameCurrentStatus();
67+
}catch(Exception exc){
68+
exc.printStackTrace();
69+
System.out.println("Invalid Move");
5570
i--;
56-
} else if(game.checkAnyWinner()){
57-
System.out.println(game.getWinner().getName()+" won the game");
58-
break;
5971
}
60-
}catch(Exception e){
61-
System.out.println("Invalid Move");
62-
i--;
6372
}
6473
}
74+
}catch(UnsupportedOperationException exc){
75+
exc.printStackTrace();
76+
System.out.println(exc.getMessage());
77+
System.exit(0);
6578
}
6679
}
6780

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
1. -> X 1 1
2+
O 2 1
3+
X 2 1
4+
X 1 3
5+
O 1 2
6+
X 2 2
7+
O 3 1

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@ public class Board {
66

77
public Board(int size) {
88
this.size = size;
9-
cells = new Symbol[size][size];
9+
this.cells = new Symbol[size][size];
10+
initBoard();
11+
}
12+
13+
private void initBoard() {
14+
for (int i = 0; i < size; i++) {
15+
for (int j = 0; j < size; j++) {
16+
cells[i][j] = new Symbol("_");
17+
}
18+
System.out.println();
19+
}
1020
}
1121

1222
public void printBoard() {
@@ -27,11 +37,11 @@ public int getSize() {
2737
}
2838

2939
public boolean isCellAlreadyOccupied(int x, int y) {
30-
return cells[x][y] != null;
40+
return !cells[x][y].getSign().equals("_");
3141
}
3242

3343
public void updateCell(Move move){
34-
this.cells[move.getXPos()][move.getYPos()] = move.getUser().getSymbol();
44+
this.cells[move.getXPos()-1][move.getYPos()-1] = move.getUser().getSymbol();
3545
}
3646

3747
public Symbol getCell(int x, int y){

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
public class User {
1111
private String name;
1212
private Symbol symbol;
13-
1413
}

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ public boolean takeMove(Move move) {
5858
if (!validateMove(move)) {
5959
return false;
6060
}
61-
moves.add(move);
62-
board.updateCell(move);
61+
this.moves.add(move);
62+
this.board.updateCell(move);
63+
User currUser = this.turnQueue.remove();
64+
this.turnQueue.add(currUser);
6365
return true;
6466
}
6567

@@ -76,15 +78,15 @@ public boolean checkAnyWinner() {
7678
private boolean validateMove(Move move) {
7779
User user = turnQueue.peek();
7880
if (move.getUser() != user) {
79-
System.out.print("Invalid Move");
81+
System.out.println("Invalid Move");
8082
return false;
8183
}
82-
if (move.getXPos() >= board.getSize() || move.getYPos() >= board.getSize() || move.getYPos() < 0 || move.getXPos() < 0) {
83-
System.out.print("Invalid Move");
84+
if (move.getXPos() > board.getSize() || move.getYPos() > board.getSize() || move.getYPos() <= 0 || move.getXPos() <= 0) {
85+
System.out.println("Invalid Move");
8486
return false;
8587
}
86-
if (board.isCellAlreadyOccupied(move.getXPos(), move.getYPos())) {
87-
System.out.print("Invalid Move");
88+
if (board.isCellAlreadyOccupied(move.getXPos()-1, move.getYPos()-1)) {
89+
System.out.println("Invalid Move");
8890
return false;
8991
}
9092
return true;
@@ -101,4 +103,8 @@ public User getUser(String symbol) {
101103
public User getWinner(){
102104
return this.winner;
103105
}
106+
107+
public int getTotalMoves(){
108+
return board.getSize()*board.getSize();
109+
}
104110
}

src/main/java/com/gatomalvado/tictactoe/service/impl/TwoWinnerStrategy.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public boolean isThereIsAnyWinner(Board board) {
1313
// validate vertical rows
1414
for (int j = 0; j < size; j++) {
1515
// scroll through one column
16-
Symbol firstSymbol = board.getCell(0, j);
1716
boolean isAllCrossed = true;
1817
for (int i = 1; i < size; i++) {
19-
Symbol currSymbol = board.getCell(i, j);
20-
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
18+
String currSymbolSign = board.getCell(i, j).getSign();
19+
String prevSymbolSign = board.getCell(0, j).getSign();
20+
if ("_".equals(prevSymbolSign) || !currSymbolSign.equals(prevSymbolSign)) {
2121
// ignore this column and move forward
2222
isAllCrossed = false;
2323
continue;
@@ -34,8 +34,9 @@ public boolean isThereIsAnyWinner(Board board) {
3434
Symbol firstSymbol = board.getCell(i, 0);
3535
boolean isAllCrossed = true;
3636
for (int j = 1; j < size; j++) {
37-
Symbol currSymbol = board.getCell(i, j);
38-
if (currSymbol == null || currSymbol.getSign().equals(firstSymbol.getSign())) {
37+
String prevSymbolSign = board.getCell(i, j-1).getSign();
38+
String currSymbolSign = board.getCell(i, j).getSign();
39+
if (!currSymbolSign.equals(prevSymbolSign) || currSymbolSign.equals("_")) {
3940
// ignore this column and move forward
4041
isAllCrossed = false;
4142
continue;
@@ -47,11 +48,11 @@ public boolean isThereIsAnyWinner(Board board) {
4748
}
4849

4950
// validate top left to bottom right diagonal
50-
Symbol firstSymbol = board.getCell(0, 0);
5151
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())) {
52+
for (int i = 1; i < size; i++) {
53+
String prevSymbolSign = board.getCell(i-1, i-1).getSign();
54+
String currSymbolSign = board.getCell(i, i).getSign();
55+
if (!prevSymbolSign.equals(currSymbolSign) || currSymbolSign.equals("_")) {
5556
// ignore this column and move forward
5657
isAllCrossed = false;
5758
break;
@@ -63,11 +64,11 @@ public boolean isThereIsAnyWinner(Board board) {
6364
}
6465

6566
// validate top right to bottom left diagonal
66-
firstSymbol = board.getCell(0, size - 1);
6767
isAllCrossed = true;
6868
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())) {
69+
String currSymbolSign = board.getCell(i, size - 1 - i).getSign();
70+
String prevSymbolSign = board.getCell(i, size - 1 - (i-1)).getSign();
71+
if (!currSymbolSign.equals(prevSymbolSign) || currSymbolSign.equals("_")) {
7172
// ignore this column and move forward
7273
isAllCrossed = false;
7374
break;

0 commit comments

Comments
 (0)