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 me compile!

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: ");

{
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;
}
}
}

Recommended Answers

All 4 Replies

Arrays and Matrices in C are zero based not 1 based!

foo[8] uses indexes {0...7}

fob[8][3] uses index [0...7][0...2]

You're overwriting your array! Recheck your indexing!

A cool project is to build a large maze using the computer, display it on a graphics screen, then watch a bot traverse it!

How do I fix that? I guess I just don't understand where I'm supposed to go from there or what I need to redo. Could you help me?

You already have an earlier thread on this subject. Have you read the responses?

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: ");

You need to indent your code. You have a switch statement that goes from lines 14 - 28. You have a for-loop that starts at line 11 and ends I'm not sure where, but line 30 is inside that for-loop, and I assume it's not supposed to be, since it discusses what the "final" location is, which I assume is supposed to only display once. The for-loop is going to have problems when i is greater than 0, since you only have robot_instructions initialized for index 0. When i is greater than 0, that's going to cause problems on line 14.

I think you probably need to start over from scratch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.