Skip to content
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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Ignore all the idea files
.idea/*
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/tic-tac-toe-fork.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 84 additions & 14 deletions tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Game.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.scaler.tictactoe;


import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
public class Game {
@Getter
private Player p1;
Expand All @@ -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) {
Expand All @@ -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();
}

/**
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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! :(.");
}

}
}
38 changes: 24 additions & 14 deletions tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Main.java
Original file line number Diff line number Diff line change
@@ -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!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
@Getter
public class Player {
private String character;
private String name;
}