Added files for generating route
This commit is contained in:
@@ -4,10 +4,10 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AssignmentApplication {
|
||||
public class TruecallerAssignmentApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AssignmentApplication.class, args);
|
||||
SpringApplication.run(TruecallerAssignmentApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,17 +1,37 @@
|
||||
package com.truecaller.truecallerassignment.backend.controller;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.truecaller.truecallerassignment.backend.entities.Tile;
|
||||
import com.truecaller.truecallerassignment.backend.services.ChessBoardService;
|
||||
import com.truecaller.truecallerassignment.backend.utilities.ChessBoardUtility;
|
||||
|
||||
@RestController
|
||||
public class TruecallerAssignmentController {
|
||||
private static final String template = "Hello, %s!";
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
|
||||
@Autowired
|
||||
private ChessBoardService service;
|
||||
|
||||
@Autowired
|
||||
private ChessBoardUtility utility;
|
||||
|
||||
@GetMapping("/")
|
||||
public String index() {
|
||||
return "Hello there! I'm running.";
|
||||
public List<Tile> findPath(@RequestParam int row, @RequestParam int column) {
|
||||
Set<Tile> allTiles = utility.populateAllTiles();
|
||||
allTiles.stream().forEach(tile -> utility.generateAllowedMoves(tile, allTiles));
|
||||
Tile tile = utility.getTile(row, column, allTiles);
|
||||
List<Tile> traversedTiles = new ArrayList<Tile>();
|
||||
service.findPath(tile, traversedTiles);
|
||||
utility.transformResponse(traversedTiles);
|
||||
return traversedTiles;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.truecaller.truecallerassignment.backend.entities;
|
||||
|
||||
public class CBRouteResponse {
|
||||
private int row;
|
||||
private int column;
|
||||
private int order;
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.truecaller.truecallerassignment.backend.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -9,6 +8,7 @@ public class Tile {
|
||||
private int column;
|
||||
private boolean visited;
|
||||
private List<Tile> allowedMoves;
|
||||
private String allowedMovesUI;
|
||||
|
||||
public Tile(int row, int column, boolean visited) {
|
||||
super();
|
||||
@@ -41,47 +41,22 @@ public class Tile {
|
||||
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 List<Tile> getAllowedMoves() {
|
||||
return allowedMoves;
|
||||
}
|
||||
|
||||
|
||||
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());
|
||||
// }
|
||||
public String getAllowedMovesUI() {
|
||||
return allowedMovesUI;
|
||||
}
|
||||
|
||||
public void setAllowedMovesUI(String allowedMovesUI) {
|
||||
this.allowedMovesUI = allowedMovesUI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.truecaller.truecallerassignment.backend.services;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.truecaller.truecallerassignment.backend.entities.Tile;
|
||||
|
||||
@Service
|
||||
public class ChessBoardService {
|
||||
|
||||
public static void findPath(Tile currentTile, List<Tile> traversedTiles) {
|
||||
if (currentTile.isVisited()) {
|
||||
return;
|
||||
}
|
||||
if (currentTile.isVisited()) {
|
||||
return;
|
||||
}
|
||||
currentTile.setVisited(true);
|
||||
traversedTiles.add(currentTile);
|
||||
if (traversedTiles.size() == 100) {
|
||||
return;
|
||||
}
|
||||
currentTile.getAllowedMoves().stream().forEach(tile -> findPath(tile, traversedTiles));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package com.truecaller.truecallerassignment.backend.utilities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.truecaller.truecallerassignment.backend.entities.Tile;
|
||||
|
||||
@Component
|
||||
public class ChessBoardUtility {
|
||||
|
||||
public static Set<Tile> populateAllTiles() {
|
||||
return IntStream.rangeClosed(1, 10).boxed().flatMap(
|
||||
row -> IntStream.rangeClosed(1, 10)
|
||||
.boxed().map(column -> new Tile(row, column, false))).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public static Tile getTile(int x, int y, Set<Tile> allTiles) {
|
||||
if (allTiles == null) {
|
||||
allTiles = new HashSet<Tile>();
|
||||
}
|
||||
List<Tile> pList = allTiles.stream().filter(tile -> tile.getRow() == x && tile.getColumn() == y).collect(Collectors.toList());
|
||||
if (pList.size() != 0) {
|
||||
return pList.get(0);
|
||||
} else {
|
||||
Tile p = new Tile(x, y, false);
|
||||
allTiles.add(p);
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
public static void displayPath(List<Tile> traversedTiles) {
|
||||
Function<Tile, String> tileToPosition = tile -> tile.getRow() + "," + tile.getColumn();
|
||||
if (traversedTiles.size() == 100) {
|
||||
String path = traversedTiles.stream().map(tileToPosition).collect(Collectors.joining(" => "));
|
||||
System.out.println("Found a tour from the specified position:\n" + path);
|
||||
} else {
|
||||
System.out.println("No path could be generated with given initial position and rules of movement!");
|
||||
}
|
||||
}
|
||||
|
||||
public static void displayAdvanced(List<Tile> traversedTiles) {
|
||||
Function<Tile, String> tileToPosition = tile -> tile.getRow() + "," + tile.getColumn();
|
||||
int count = 1;
|
||||
String [][] chessBoard = new String[10][10];
|
||||
if (traversedTiles.size() == 100) {
|
||||
for (Tile tile : traversedTiles) {
|
||||
String displayValue = count + ": " + tile.getRow() + "-" + tile.getColumn();
|
||||
count++;
|
||||
chessBoard[tile.getRow() - 1][tile.getColumn()-1] = displayValue;
|
||||
}
|
||||
System.out.println("Found a tour from the specified position:\n");
|
||||
// Stream.of(chessBoard)
|
||||
// .flatMap(Stream::of)
|
||||
//// .map(t -> Integer.parseInt(t.substring(0, t.indexOf(":"))))
|
||||
//// .sorted((t1, t2) -> Integer.compare(Integer.parseInt(t1.substring(0, t1.indexOf(":"))), Integer.parseInt(t2.substring(0, t2.indexOf(":")))))
|
||||
// .forEach(System.out::print);
|
||||
for (int i = 0; i < chessBoard.length; i++) {
|
||||
System.out.println();
|
||||
if (i == 9) {
|
||||
for (int j = 0; j < chessBoard[0].length; j++) {
|
||||
System.out.print(chessBoard[i][j] + "\t");
|
||||
}
|
||||
} else {
|
||||
for (int j = 0; j < chessBoard[0].length; j++) {
|
||||
System.out.print(chessBoard[i][j] + "\t\t");
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
System.out.println("No path could be generated with given initial position and rules of movement!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(Tile tile, Set<Tile> allTiles) {
|
||||
List<Tile> allowedMoves = new ArrayList<Tile>();
|
||||
int row = tile.getRow();
|
||||
int column = tile.getColumn();
|
||||
if (row > 3)
|
||||
allowedMoves.add(getTile(row - 3, column, allTiles));
|
||||
if (row < 8)
|
||||
allowedMoves.add(getTile(row + 3, column, allTiles));
|
||||
if (column > 3)
|
||||
allowedMoves.add(getTile(row, column - 3, allTiles));
|
||||
if (column < 8)
|
||||
allowedMoves.add(getTile(row, column + 3, allTiles));
|
||||
if (row > 2 && column > 2)
|
||||
allowedMoves.add(getTile(row - 2, column - 2, allTiles));
|
||||
if (row > 2 && column < 9)
|
||||
allowedMoves.add(getTile(row - 2, column + 2, allTiles));
|
||||
if (row < 9 && column > 2)
|
||||
allowedMoves.add(getTile(row + 2, column - 2, allTiles));
|
||||
if (row < 9 && column < 9)
|
||||
allowedMoves.add(getTile(row + 2, column + 2, allTiles));
|
||||
|
||||
tile.setAllowedMoves(allowedMoves.stream().filter(tile1 -> !tile1.isVisited()).collect(Collectors.toList()));
|
||||
}
|
||||
|
||||
public void transformResponse(List<Tile> traversedTiles) {
|
||||
traversedTiles = traversedTiles.stream().map(tile -> {
|
||||
List<Tile> tiles = tile.getAllowedMoves();
|
||||
String moves = null;
|
||||
if (tiles != null) {
|
||||
moves = tiles.stream().map(tile1 -> tile1.getRow() + "-" + tile1.getColumn()).collect(Collectors.joining(", "));
|
||||
}
|
||||
tile.setAllowedMoves(null);
|
||||
tile.setAllowedMovesUI(moves);
|
||||
return tile;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user