From c0c21abce4b9bca0e419cedbdb4da92ef1715e80 Mon Sep 17 00:00:00 2001 From: Brandon Dsouza Date: Mon, 7 Mar 2022 11:24:27 +0530 Subject: [PATCH 1/2] Game implementation --- .../main/java/com/scaler/tictactoe/Game.java | 30 +++++++++++++- .../main/java/com/scaler/tictactoe/Main.java | 31 ++++++++++++-- .../java/com/scaler/tictactoe/GameTests.java | 41 +++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) 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..130b814 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 @@ -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; } 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..ac2338a 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,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 @@ -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()); + } } } diff --git a/tic-tac-toe-cli/src/test/java/com/scaler/tictactoe/GameTests.java b/tic-tac-toe-cli/src/test/java/com/scaler/tictactoe/GameTests.java index 56fce40..86183a3 100644 --- a/tic-tac-toe-cli/src/test/java/com/scaler/tictactoe/GameTests.java +++ b/tic-tac-toe-cli/src/test/java/com/scaler/tictactoe/GameTests.java @@ -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()); + } } From d56d55c9cbd76a9e6889e9bb2f0ebb44fd589ab7 Mon Sep 17 00:00:00 2001 From: Brandon Dsouza Date: Mon, 7 Mar 2022 18:15:04 +0530 Subject: [PATCH 2/2] Tasks implementations --- .../todolist/controllers/TasksController.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java b/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java index a39e36c..dbf29a9 100644 --- a/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java +++ b/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java @@ -7,6 +7,7 @@ import java.net.http.HttpResponse; import java.util.ArrayList; import java.util.List; +import java.util.ResourceBundle; @RequestMapping("/tasks") @RestController @@ -38,6 +39,26 @@ ResponseEntity addNewTask(@RequestBody Task task) { * delete task no 5 (response with correct HTTP code) * if task 5 does not exist, send 404 */ + @GetMapping("/{id}") + ResponseEntity getTask(@PathVariable("id") int id){ + if(id<0 || id>=taskList.size()) + return (ResponseEntity) ResponseEntity.notFound(); + return ResponseEntity.ok(taskList.get(id)); + } + + @PatchMapping("/{id}") + ResponseEntity updateTask(@PathVariable("id") int id){ + if(id<0 || id>=taskList.size()) + return (ResponseEntity) 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); + } }