From 849d4f60e965729b014c2663481faf51d15d5ea6 Mon Sep 17 00:00:00 2001 From: vaasu Date: Wed, 1 Apr 2020 03:35:04 +0530 Subject: [PATCH] added template service --- .../TruecallerAssignmentController.java | 22 +++++ .../backend/entities/Tile.java | 97 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/main/java/com/truecaller/truecallerassignment/backend/controller/TruecallerAssignmentController.java create mode 100644 src/main/java/com/truecaller/truecallerassignment/backend/entities/Tile.java diff --git a/src/main/java/com/truecaller/truecallerassignment/backend/controller/TruecallerAssignmentController.java b/src/main/java/com/truecaller/truecallerassignment/backend/controller/TruecallerAssignmentController.java new file mode 100644 index 0000000..50d7ab0 --- /dev/null +++ b/src/main/java/com/truecaller/truecallerassignment/backend/controller/TruecallerAssignmentController.java @@ -0,0 +1,22 @@ +package com.truecaller.truecallerassignment.backend.controller; + +import java.util.concurrent.atomic.AtomicLong; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.truecaller.truecallerassignment.backend.entities.Tile; + +@RestController +public class TruecallerAssignmentController { + private static final String template = "Hello, %s!"; + private final AtomicLong counter = new AtomicLong(); + + @GetMapping("/hello-world") + @ResponseBody + public Tile sayHello(@RequestParam(name="name", required=false, defaultValue="Stranger") String name) { + return new Tile(4, 5, false); + } +} \ No newline at end of file diff --git a/src/main/java/com/truecaller/truecallerassignment/backend/entities/Tile.java b/src/main/java/com/truecaller/truecallerassignment/backend/entities/Tile.java new file mode 100644 index 0000000..a530366 --- /dev/null +++ b/src/main/java/com/truecaller/truecallerassignment/backend/entities/Tile.java @@ -0,0 +1,97 @@ +package com.truecaller.truecallerassignment.backend.entities; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +public class Tile { + private int row; + private int column; + private boolean visited; + private List allowedMoves; + + public Tile(int row, int column, boolean visited) { + super(); + this.row = row; + this.column = column; + this.visited = visited; + } + + + public int getRow() { + return row; + } + + public void setRow(int row) { + this.row = row; + } + + public int getColumn() { + return column; + } + + public void setColumn(int column) { + this.column = column; + } + + public boolean isVisited() { + return visited; + } + + public void setVisited(boolean visited) { + this.visited = visited; + } + + public String getAllowedMoves() { + String moves = null; + if (allowedMoves != null) { + moves = allowedMoves.stream().map(tile -> tile.row + "-" + tile.column).collect(Collectors.joining(", ")); + } + + return moves; + } + + public void setAllowedMoves(List allowedMoves) { + this.allowedMoves = allowedMoves; + } + + /** + * Function to generate all possible moves from a tile based on it's current position. + * + * @return List list of all positions possible from given tile + * + */ + public void generateAllowedMoves() { + List allowedMoves = new ArrayList(); + if (row > 3) + allowedMoves.add(getTile(row - 3, column)); + if (row < 8) + allowedMoves.add(getTile(row + 3, column)); + if (column > 3) + allowedMoves.add(getTile(row, column - 3)); + if (column < 8) + allowedMoves.add(getTile(row, column + 3)); + if (row > 2 && column > 2) + allowedMoves.add(getTile(row - 2, column - 2)); + if (row > 2 && column < 9) + allowedMoves.add(getTile(row - 2, column + 2)); + if (row < 9 && column > 2) + allowedMoves.add(getTile(row + 2, column - 2)); + if (row < 9 && column < 9) + allowedMoves.add(getTile(row + 2, column + 2)); + + this.allowedMoves = allowedMoves.stream().filter(tile -> !tile.visited).collect(Collectors.toList()); + } + + @Override + public String toString() { + String moves = null; + if (allowedMoves != null) { + moves = allowedMoves.stream().map(tile -> tile.row + "-" + tile.column).collect(Collectors.joining(", ")); + } + return "Tile [row=" + row + ", column=" + column + ", visited=" + visited + ", allowedMoves=" + moves + + "]"; + } + + +}