diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..70bcae1
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+#Ignore all the idea files
+.idea/*
\ No newline at end of file
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..af773d4
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
new file mode 100644
index 0000000..7755d50
--- /dev/null
+++ b/.idea/gradle.xml
@@ -0,0 +1,63 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..fdc392f
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..22f1312
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..6a3dc1c
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/tic-tac-toe-fork.iml b/.idea/tic-tac-toe-fork.iml
new file mode 100644
index 0000000..d6ebd48
--- /dev/null
+++ b/.idea/tic-tac-toe-fork.iml
@@ -0,0 +1,9 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Game.java b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Game.java
index eec2ba8..60c3ee7 100644
--- a/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Game.java
+++ b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Game.java
@@ -1,8 +1,10 @@
package com.scaler.tictactoe;
+import lombok.AllArgsConstructor;
import lombok.Getter;
+@AllArgsConstructor
public class Game {
@Getter
private Player p1;
@@ -13,13 +15,27 @@ public class Game {
private Player nextTurn;
private String[][] gameState = new String[3][3];
+ private int numTurns;
- public Game(String p1Char, String p2Char) {
- p1 = new Player(p1Char);
- p2 = new Player(p2Char);
+ public Game(Player p1, Player p2) {
+ this.p1 = p1;
+ this.p2 = p2;
+ this.numTurns = 0;
// init next turn for player 1
- nextTurn = p1;
+ nextTurn = this.p1;
+
+ // printing initial status
+ printStatus();
+ }
+
+ private void printStatus() {
+ printGameState();
+ printNextTurn();
+ }
+
+ private void printNextTurn() {
+ System.out.println("Next Turn: " + nextTurn.getName());
}
public String getCharInBox(int box) {
@@ -39,14 +55,19 @@ public void nextAttempt(int box) {
int row = (box - 1) / 3;
int col = (box - 1) % 3;
- if (box < 1 || box > 9) throw new IllegalArgumentException("Box no. must be between 1 and 9");
- if (gameState[row][col] != null) throw new IllegalStateException("This box is not empty");
+ if (box < 1 || box > 9)
+ throw new IllegalArgumentException("Box no. must be between 1 and 9");
+ if (gameState[row][col] != null)
+ throw new IllegalStateException("This box is not empty");
gameState[row][col] = nextTurn.getCharacter();
+ numTurns++;
// switch turn of players
- if (nextTurn == p1) nextTurn = p2;
- else nextTurn = p1;
+ nextTurn = (nextTurn == p1) ? p2 : p1;
+
+ // Print new state of board
+ this.printStatus();
}
/**
@@ -56,14 +77,63 @@ public void nextAttempt(int box) {
*/
public Player checkVictory() {
// TODO
+ Player rowWinner = checkRows();
+ Player colWinner = checkCols();
+ if (rowWinner != null) {
+ return rowWinner;
+ } else if (colWinner != null) {
+ return colWinner;
+ } else {
+ return checkDiag();
+ }
+ }
+
+ private Player checkDiag() {
+
+ if (gameState[0][0] != null && gameState[0][0] == gameState[1][1] && gameState[1][1] == gameState[2][2]) {
+ return getPlayer(gameState[0][0]);
+ } else if (gameState[2][0] != null && gameState[2][0] == gameState[1][1] && gameState[1][1] == gameState[0][2]) {
+ return getPlayer(gameState[2][0]);
+ } else {
+ return null;
+ }
+
+ }
+
+
+ private Player checkCols() {
+ for (int i = 0; i < 3; i++) {
+ if (gameState[0][i] != null && gameState[0][i] == gameState[1][i] && gameState[1][i] == gameState[2][i])
+ return getPlayer(gameState[0][i]);
+ }
+
return null;
}
- public String printGameState() {
- return " " + gameState[0][0] + " | " + gameState[0][1] + " | " + gameState[0][2] + "\n" +
- "------------\n" +
- " " + gameState[1][0] + " | " + gameState[1][1] + " | " + gameState[1][2] + "\n" +
- "------------\n" +
- " " + gameState[2][0] + " | " + gameState[2][1] + " | " + gameState[2][2];
+ private Player checkRows() {
+ for (int i = 0; i < 3; i++) {
+ if (gameState[i][0] != null && gameState[i][0] == gameState[i][1] && gameState[i][1] == gameState[i][2])
+ return getPlayer(gameState[i][0]);
+ }
+
+ return null;
+ }
+
+ private Player getPlayer(String s) {
+ return s.equals(p1.getCharacter()) ? p1 : p2;
+ }
+
+ public void printGameState() {
+ System.out.println(
+ "Current State of Board: \n" +
+ " " + gameState[0][0] + " | " + gameState[0][1] + " | " + gameState[0][2] + "\n" +
+ "------------\n" +
+ " " + gameState[1][0] + " | " + gameState[1][1] + " | " + gameState[1][2] + "\n" +
+ "------------\n" +
+ " " + gameState[2][0] + " | " + gameState[2][1] + " | " + gameState[2][2] + "\n");
+ }
+
+ public int getTurns() {
+ return numTurns;
}
}
diff --git a/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/GameController.java b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/GameController.java
new file mode 100644
index 0000000..2b83f2f
--- /dev/null
+++ b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/GameController.java
@@ -0,0 +1,42 @@
+package com.scaler.tictactoe;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+import java.security.InvalidParameterException;
+import java.util.Scanner;
+
+@AllArgsConstructor
+@Getter
+public class GameController {
+
+ Game game;
+
+ public void start() {
+
+ Scanner scanner = new Scanner(System.in);
+ // TODO 2. For every turn, print whose turn it is, and state of game(3x3 box)
+ while (game.checkVictory() == null && game.getTurns() < 9) {
+ System.out.println("Please enter the box number you'd like to place your character in: ");
+ int x = scanner.nextInt();
+ try {
+ game.nextAttempt(x);
+ } catch (InvalidParameterException e) {
+ System.out.println(e.getMessage());
+ } catch (IllegalStateException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+
+
+ Player victor = game.checkVictory();
+
+ if (victor != null) {
+ System.out.println(victor.getName() + " has won. Congratulations!");
+ }
+ else {
+ System.out.println("The game was a draw! :(.");
+ }
+
+ }
+}
diff --git a/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Main.java b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Main.java
index 55a0864..d6ea42e 100644
--- a/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Main.java
+++ b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Main.java
@@ -1,21 +1,31 @@
package com.scaler.tictactoe;
+import java.security.InvalidParameterException;
+import java.util.Scanner;
+
public class Main {
+
+ public static final String p1Name = "Parag";
+ public static final String p1Char = "X";
+ public static final String p2Char = "O";
+ public static final String p2Name = "Sheetal";
+
public static void main(String[] args) {
- Game game = new Game("X", "O");
- System.out.println(game.printGameState());
-
- /*
- TODO: Create the entire game; steps are:
- 1. Construct game object with 2 player characters
- 2. For every turn, print whose turn it is, and state of game (3x3 box)
- 3. Read input (between 1-9) as the box to be marked by next player
- 4. Validate input and mark the box
- 4.1 If input invalid, make player enter box no again
- 5. Repeat steps 2-4 until either;
- 5.1 checkVictory function shows any player has won
- 5.2 all boxes have been marked
- */
+ // TODO: Create the entire game; steps are:
+ // TODO 1. Construct game object with 2 player characters
+ Game game = new Game(new Player(p1Char, p1Name), new Player(p2Char, p2Name));
+ GameController controller = new GameController(game);
+
+ controller.start();
+
+ // TODO 3. Read input (between 1 - 9)as the box to be marked by next player
+ // TODO 4. Validate input and mark the box
+ // TODO 4.1 If input invalid, make player enter box no again
+ // TODO 5. Repeat steps 2 - 4 until either;
+ // TODO 5.1 checkVictory function shows any player has won
+ // TODO 5.2 all boxes have been marked
+
+ System.out.println("Break point sout!");
}
}
diff --git a/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Player.java b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Player.java
index fe02aff..fc6c026 100644
--- a/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Player.java
+++ b/tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Player.java
@@ -7,4 +7,5 @@
@Getter
public class Player {
private String character;
+ private String name;
}