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
30 changes: 29 additions & 1 deletion tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,35 @@ public void nextAttempt(int box) {
* @return p1 or p2 whoever has won; or null if no winner yet
*/
public Player checkVictory() {
// TODO
//Diagonal
if(gameState[0][0] !=null && gameState[0][0] == gameState[1][1] && gameState[2][2]==gameState[1][1]){
return gameState[0][0] == p1.getCharacter() ? p1:p2;
}
if(gameState[0][2] !=null && gameState[0][2] == gameState[1][1] && gameState[2][0]==gameState[1][1]){
return gameState[0][0] == p1.getCharacter() ? p1:p2;
}
//Row
if(gameState[0][0] !=null && gameState[0][0] == gameState[0][1] && gameState[0][1] == gameState[0][2])
return gameState[0][0] == p1.getCharacter() ? p1:p2;

if(gameState[1][0] !=null && gameState[1][0] == gameState[1][1] && gameState[1][1] == gameState[1][2])
return gameState[1][0] == p1.getCharacter() ? p1:p2;

if(gameState[2][0] !=null && gameState[2][0] == gameState[2][1] && gameState[2][1] == gameState[2][2])
return gameState[2][0] == p1.getCharacter() ? p1:p2;

//Column
if(gameState[0][0] !=null && gameState[0][0] == gameState[1][0] && gameState[2][0]==gameState[1][0]){
return gameState[0][0] == p1.getCharacter() ? p1:p2;
}

if(gameState[0][1] !=null && gameState[0][1] == gameState[1][1] && gameState[2][1]==gameState[1][1]){
return gameState[0][1] == p1.getCharacter() ? p1:p2;
}

if(gameState[0][2] !=null && gameState[0][2] == gameState[1][2] && gameState[2][2]==gameState[1][2]){
return gameState[0][2] == p1.getCharacter() ? p1:p2;
}
return null;
}

Expand Down
31 changes: 28 additions & 3 deletions tic-tac-toe-cli/src/main/java/com/scaler/tictactoe/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.scaler.tictactoe;

import java.util.Scanner;

public class Main {
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
Expand All @@ -17,5 +16,31 @@ public static void main(String[] args) {
5.1 checkVictory function shows any player has won
5.2 all boxes have been marked
*/

Game game = new Game("X", "O");

Scanner scan = new Scanner(System.in);
int plays = 0;
while (game.checkVictory() == null && plays < 9) {
try {
System.out.println("Player - " + game.getNextTurn().getCharacter());
System.out.println(game.printGameState());
int num = scan.nextInt();
game.nextAttempt(num);
plays++;
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
continue;
} catch (IllegalStateException e) {
System.out.println(e.getMessage());
continue;
}
}
var result = game.checkVictory();
if (result == null) {
System.out.println("Its a Tie!");
} else {
System.out.println("Congratulation " + result.getCharacter());
}
}
}
41 changes: 41 additions & 0 deletions tic-tac-toe-cli/src/test/java/com/scaler/tictactoe/GameTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,45 @@ void throwsExceptionForInvalidBoxAttempt() {
});

}

@Test
void isVictoryDiagonal() {
Game g = new Game("❌", "⭕️");
g.nextAttempt(1);
g.nextAttempt(2);
g.nextAttempt(5);
g.nextAttempt(3);
g.nextAttempt(9);

assertEquals(g.getP1(), g.checkVictory());

}

@Test
void isVictoryFirstRow() {
Game g = new Game("❌", "⭕️");
g.nextAttempt(1);
g.nextAttempt(4);
g.nextAttempt(2);
g.nextAttempt(5);
assertNull(g.checkVictory());

g.nextAttempt(3);
assertEquals(g.getP1(), g.checkVictory());

}

@Test
void isVictoryFirstColumn() {

Game g = new Game("⭕️", "❌");
g.nextAttempt(1);
g.nextAttempt(2);
g.nextAttempt(4);
g.nextAttempt(5);
assertNull(g.checkVictory());

g.nextAttempt(7);
assertEquals(g.getP1(), g.checkVictory());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@RequestMapping("/tasks")
@RestController
Expand Down Expand Up @@ -38,6 +39,26 @@ ResponseEntity<Task> addNewTask(@RequestBody Task task) {
* delete task no 5 (response with correct HTTP code)
* if task 5 does not exist, send 404
*/
@GetMapping("/{id}")
ResponseEntity<Task> getTask(@PathVariable("id") int id){
if(id<0 || id>=taskList.size())
return (ResponseEntity<Task>) ResponseEntity.notFound();
return ResponseEntity.ok(taskList.get(id));
}

@PatchMapping("/{id}")
ResponseEntity<Task> updateTask(@PathVariable("id") int id){
if(id<0 || id>=taskList.size())
return (ResponseEntity<Task>) ResponseEntity.notFound();
taskList.get(id).setDone(true);
return ResponseEntity.ok(taskList.get(id));
}

@RequestMapping(value = "/{id}",method = {RequestMethod.DELETE})
void deleteTask(@PathVariable("id") int id){
if(id<0 || id>=taskList.size())
return;
taskList.remove(id);
}

}