// JohnsJava.webs.com import java.util.*; public class Maze { public static void main(String[] args) { startGame(); //--------Your Code Goes Here-----------// //Methods: //moveRight(#); //moveLeft(#); //moveUp(#); //moveDown(#); moveRight(5); //--------------------------------------// } public static void startGame() { createMaze(); System.out.println(getMazeString()); try { Thread.sleep(2000); } catch (Exception ex) { } } public static void moveRight(int num) { status = "Moving Right"; for(int i = 0; i < num; i++) { if((maze[x+1][y] == ' ') || (maze[x+1][y] == 'E')) { x = x+1; System.out.println(getMazeString()); } else { status = "Blocked"; System.out.println(getMazeString()); } testForWin(); try { Thread.sleep(DELAY); } catch (Exception ex) { } } } public static void moveLeft(int num) { status = "Moving Left"; for(int i = 0; i < num; i++) { if((maze[x-1][y] == ' ') || (maze[x-1][y] == 'E')) { x = x-1; System.out.println(getMazeString()); } else { status = "Blocked"; System.out.println(getMazeString()); } testForWin(); try { Thread.sleep(DELAY); } catch (Exception ex) { } } } public static void moveDown(int num) { status = "Moving Down"; for(int i = 0; i < num; i++) { if((maze[x][y+1] == ' ') || (maze[x][y+1] == 'E')) { y = y+1; System.out.println(getMazeString()); } else { status = "Blocked"; System.out.println(getMazeString()); } testForWin(); try { Thread.sleep(DELAY); } catch (Exception ex) { } } } public static void moveUp(int num) { status = "Moving Up"; for(int i = 0; i < num; i++) { if((maze[x][y-1] == ' ') || (maze[x][y-1] == 'E')) { y = y-1; System.out.println(getMazeString()); } else { status = "Blocked"; System.out.println(getMazeString()); } testForWin(); try { Thread.sleep(DELAY); } catch (Exception ex) { } } } public static void testForWin() { if(maze[x][y] == 'E') { status = ("--------YOU WIN!---------"); System.out.println(getMazeString()); System.exit(0); } } public static char[][] maze = new char[25][12]; public static int x = 1; public static int y = 1; public static final int DELAY = 300; public static String status = ""; public static void createMaze() { /* 0123456789012345678901234 0************************* 1* *************** 2********* *************** 3********* ********* E 4********* ********* ***** 5*** ********* ***** 6*** *************** ***** 7*** *************** ***** 8*** ***** 9************************* 0************************* 1************************* */ // create all the *'s for(int a = 0; a < 25; a++) { for(int b = 0; b < 12; b++) { maze[a][b] = '*'; } } // create all the spaces for(int i = 1; i < 10; i++) { maze[i][1] = ' '; } for(int i = 2; i < 6; i++) { maze[9][i] = ' '; } for(int i = 3; i < 9; i++) { maze[i][5] = ' '; } for(int i = 6; i < 9; i++) { maze[3][i] = ' '; } for(int i = 4; i < 20; i++) { maze[i][8] = ' '; } for(int i = 7; i > 2; i--) { maze[19][i] = ' '; } for(int i = 19; i < 25; i++) { maze[i][3] = ' '; } maze[24][3] = 'E'; } public static String getMazeString() { String s = ""; for(int b = 0; b < 12; b++) { if(b != 0) s = s + "\n"; for(int a = 0; a < 25; a++) { if((a == x) && (b == y)) s = s + '@'; else s = s + maze[a][b]; } } s = s + "\n" + status; for(int i = 14; i < 25; i++) { s = s + "\n"; } return s; } /*moveRight(8); moveDown(4); moveLeft(6); moveDown(3); moveRight(16); moveUp(5); moveRight(7);*/ }