I have to create a maze in which a robot has to follow the set of directions given and I'm so stuck on how to make the robot go through the maze. I have most all of it done but I'm still getting errors. The maze includes 1.safe place 2.chasm 3.trap 4. destination. The robot can 1. move forward once 2. jump 3. Turn left 4. Turn right
Here's what I have so far.Please help!

import java.util.Scanner;

public class GameMachine
{
    public static void main (String args [])
    { 
    int robot_x = 0;
    int robot_y = 0;
    int robot_m_x = 0;
    int robot_m_y = 0;

    int[] robot_instructions = new int[64];
    int i = 0;
       int robot_state = 0; 
    int[][] maze = new int[8][8]; 

    robot_instructions[0] = 1;
       maze[4][5] = 2;
    maze[6][7] = 3;
    maze[1][1] = 1;
    maze[8][8] = 4;
    maze[3][2] = 3;
    maze[4][1] = 2;

    robot_x = 1;
    robot_y = 1; 
    for(i = 0; i < 64; i++)
    { 
    robot_state = 1;
    switch(robot_instructions[i])
    {
    case 1:robot_x = robot_m_x;
    robot_y = robot_m_y + 1;
    break;
    case 2:robot_x = robot_m_x;
    robot_y =  robot_m_y + 2 ; 
    break;
    case 3:       
    robot_x = robot_m_x - 1;
    break;
    case 4:
       robot_x = robot_m_x + 1;
       break;
    }

       System.out.println("Robot final location X: " + robot_x + " Y: " + robot_y);
    System.out.println ("Robot State: ");

    switch( maze[(robot_y*10) + robot_x])
    {
    case 1:robot_state = 1;
    System.out.println ( "Robot is at a safe place");
    break;
    case 2:robot_state = 2;
    System.out.println ("Robot falls into Chasm");
    break;
    case 3:robot_state = 3;
    System.out.println("Robot falls into the trap");
    break;
    case 4:robot_state = 4;
    System.out.println( "Robot reaches Destination");
    break;
    case 5:robot_state = 5;
    System.out.println ( "Robot fails to reach Destination");
    break;
    } 
   }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

ok, first of all you need to post that in code tags so it doesn't break my eyes trying to read.

from what i gather, are you even getting a user input?

maze[(robot_y*10) + robot_x]

what is this supposed to do?

Sorry this is my first time on this site.
Well I dont know if I need that, or if i'm fine with the rest of the code. I just need to add the robot going through the maze and its frustrating cause I know its prob. something simple.

Sorry this is my first time on this site.
Well I dont know if I need that, or if i'm fine with the rest of the code. I just need to add the robot going through the maze and its frustrating cause I know its prob. something simple.

You're not to the point yet where you can add a robot, I'd say. You don't have a maze and you don't have a way to display the maze:

int[] robot_instructions = new int[64];
int i = 0;
int robot_state = 0;
int[][] maze = new int[8][8];

robot_instructions[0] = 1;
maze[4][5] = 2;
maze[6][7] = 3;
maze[1][1] = 1;
maze[8][8] = 4;
maze[3][2] = 3;
maze[4][1] = 2;

Your maze[][] array isn't filled in all the way and you have an index problem here:

maze[8][8] = 4;

Valid maze indexes are 0 through 7. 8 is out of bounds since you defined it here:

int[][] maze = new int[8][8];
switch( maze[(robot_y*10) + robot_x])

Isn't maze a 2-D array, not a 1-D array? I think you need to go back to the drawing board on this one.

This article has been dead for over six months. Start a new discussion instead.