Skip to content

Instantly share code, notes, and snippets.

@ShlomiRex
Created February 26, 2017 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShlomiRex/f7ccea31f2a336129172b3bfb0996ff2 to your computer and use it in GitHub Desktop.
Save ShlomiRex/f7ccea31f2a336129172b3bfb0996ff2 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class Solution {
static Random rnd;
//True - if not wall. False - if wall.
static boolean[] directions = {false,false,false,false};
static String line;
static Scanner s = new Scanner(System.in);
static int currentDirectionn = 0;
public static void main(String[] args) throws IOException {
//Direction pointer.
//0 = up, 1 = left, 2 = right, 3 = down
rnd = new Random();
s.nextLine(); //dump player id input
while(true) {
//Initialize up,down,left,right (Directions array)
readInput();
//Get random direction to go to
currentDirectionn = getRandomValidDirection();
moveToCurrentDirection();
//After we move, our last direction is relative to our new view, so now its UP.
currentDirectionn = 0;
resetArray();
}
}
public static void moveToCurrentDirection() {
if(currentDirectionn == 0)
System.out.println("UP");
else if(currentDirectionn == 1)
System.out.println("LEFT");
else if(currentDirectionn == 2)
System.out.println("RIGHT");
else
System.out.println("DOWN");
}
public static void resetArray() {
directions[0] = false;
directions[1] = false;
directions[2] = false;
directions[3] = false;
}
public static int getRandomValidDirection() {
if(directions[currentDirectionn] == true)
return currentDirectionn;
while(true) {
int r = rnd.nextInt(4);
if(directions[r] == true)
return r;
}
}
public static void readInput() {
// 35 = #
// 45 = -
// 101 = e
//First line
while(s.hasNextLine() == false) { } // wait for line
line = s.nextLine();
if(line.charAt(1) == 35)
directions[0] = false;
else
directions[0] = true;
isDoor(line);
//Second line
line = s.nextLine();
if(line.charAt(0) == 35)
directions[1] = false;
else
directions[1] = true;
if(line.charAt(2) == 35)
directions[2] = false;
else
directions[2] = true;
isDoor(line);
//Third line
line = s.nextLine();
if(line.charAt(1) == 35)
directions[3] = false;
else
directions[3] = true;
isDoor(line);
}
public static void isDoor(String line) {
if(line.indexOf(101) != -1) {
//found door
System.out.println("Door at " + line.indexOf(101));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment