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
135 changes: 131 additions & 4 deletions src/main/java/com/agileoracleseval/Main.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,136 @@
package com.agileoracleseval;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
import java.io.*;
import java.util.*;

public class Main {
public static void main(String[] args) {
System.out.print("Hello and welcome Team Agile Oracles!");
public static void main(String[] args) throws IOException {

String filePath = "src/main/java/com/agileoracleseval/board.txt";
String statePath = "src/main/java/com/agileoracleseval/snakeState.txt";

// Read grid
List<String> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
lines.add(line.replace(" ", ""));
}
}

int rows = lines.size();
int cols = lines.get(0).length();

char[][] grid = new char[rows][cols];
for (int r = 0; r < rows; r++) {
grid[r] = lines.get(r).toCharArray();
}

List<int[]> snakeList = new LinkedList<>();

File stateFile = new File(statePath);

if (stateFile.exists()) {
try (BufferedReader br = new BufferedReader(new FileReader(stateFile))) {
String line;
while ((line = br.readLine()) != null) {
String[] p = line.split(",");
int r = Integer.parseInt(p[0]);
int c = Integer.parseInt(p[1]);
snakeList.add(new int[]{r, c});
}
}

// clear grid and rebuild snake
for (int r = 0; r < rows; r++) {
Arrays.fill(grid[r], '-');
}
for (int[] part : snakeList) {
grid[part[0]][part[1]] = 'o';
}

}
else {
// first run
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 'o') {
snakeList.add(new int[]{r, c});
}
}
}
}

// directions
String[] dirs = {"up", "down", "left", "right"};
int[][] moves = {
{-1, 0},
{1, 0},
{0, -1},
{0, 1}
};

String direction = args[0];
int steps = Integer.parseInt(args[1]);

for (int step = 0; step < steps; step++) {
int[] head = ((LinkedList<int[]>) snakeList).getLast();
int newRow = head[0];
int newCol = head[1];

for (int i = 0; i < dirs.length; i++) {
if (direction.equals(dirs[i])) {
newRow += moves[i][0];
newCol += moves[i][1];
}
}

// Invalid move
if (snakeList.size() > 1) {
int[] neck = snakeList.get(snakeList.size() - 2);
if (newRow == neck[0] && newCol == neck[1]) {
System.out.println("Reverse movement is Invalid!.");
break;
}
}

// Hit the wall (Error Solution)
if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols) {
System.out.println("Hit the wall!");
break;
}

// move
snakeList.add(new int[]{newRow, newCol});
grid[newRow][newCol] = 'o';

int[] tail = ((LinkedList<int[]>) snakeList).removeFirst();
grid[tail[0]][tail[1]] = '-';
}

// print
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
System.out.print(grid[r][c] + " ");
}
System.out.println();
}

try (BufferedWriter bw = new BufferedWriter(new FileWriter(filePath))) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
bw.write(grid[r][c]);
if (c < cols - 1) bw.write(" ");
}
bw.newLine();
}
}

try (BufferedWriter bw = new BufferedWriter(new FileWriter(statePath))) {
for (int[] part : snakeList) {
bw.write(part[0] + "," + part[1]);
bw.newLine();
}
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/agileoracleseval/board.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - o - - - - -
- - - - - - - - - o - - - - -
- - - - - - - o o o - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
- - - - - - - - - - - - - - -
5 changes: 5 additions & 0 deletions src/main/java/com/agileoracleseval/snakeState.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
7,7
7,8
7,9
6,9
5,9