added template service

This commit is contained in:
2020-04-01 03:35:04 +05:30
parent 02ecfd315e
commit 849d4f60e9
2 changed files with 119 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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<Tile> 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<Tile> allowedMoves) {
this.allowedMoves = allowedMoves;
}
/**
* Function to generate all possible moves from a tile based on it's current position.
*
* @return List<Tile> list of all positions possible from given tile
*
*/
public void generateAllowedMoves() {
List<Tile> allowedMoves = new ArrayList<Tile>();
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
+ "]";
}
}