In: Computer Science
Objective:
Write a program that simulates a robot running a queue of commands to move around a board with obstacles.
Requirements:
The board is composed of spaces that are either empty (“_”) or have an obstacle (“X”). Also the board is assumed to be 10x10 spaces. The robot (“O”) has an x and y position corresponding to its location on the board, and four commands: move up, move down, move left, and move right. Both the board and the robot’s commands are assumed to be read in via a file and should use the format given. Assume the robot starts in the top left corner of the board. The robot and an obstacle cannot exist in the same space, and if that happens the robot will “crash”, or in other words the simulation will stop. Also if the robot goes outside of the bounds of the board, then the robot will “crash” as well. The robot’s commands must be enqueued and dequeued from a Generic Queue of your own making. You may not use the built in Java Queue.
Please use a generic queue using enqueue and dequeue.
Files Used:
board.txt
_____X____ _______X__ _______X__ X____X____ _X________ __X___X___ _________X ___X__X___ ___X______ __XX______
robotcommands.txt
Move Right Move Down Move Down Move Down Move Right Move Right Move Right Move Down Move Right Move Right Move Right Move Right Move Down Move Down Move Down Move Down Move Down Move Right
Example Dialog:
Welcome to the Robot Simulator
Enter file for the Board
board.txt
Enter file for the Robot Commands
robotCommands.txt
O____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Simulation begin
Command 0
_O___X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 1
_____X____
_O_____X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 2
_____X____
_______X__
_O_____X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 3
_____X____
_______X__
_______X__
XO___X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 4
_____X____
_______X__
_______X__
X_O__X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 5
_____X____
_______X__
_______X__
X__O_X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 6
_____X____
_______X__
_______X__
X___OX____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 7
_____X____
_______X__
_______X__
X____X____
_X__O_____
__X___X___
_________X
___X__X___
___X______
__XX______
Command 8
_____X____
_______X__
_______X__
X____X____
_X___O____
__X___X___
_________X
___X__X___
___X______
__XX______
Command 9
_____X____
_______X__
_______X__
X____X____
_X____O___
__X___X___
_________X
___X__X___
___X______
__XX______
Command 10
_____X____
_______X__
_______X__
X____X____
_X_____O__
__X___X___
_________X
___X__X___
___X______
__XX______
Command 11
_____X____
_______X__
_______X__
X____X____
_X______O_
__X___X___
_________X
___X__X___
___X______
__XX______
Command 12
_____X____
_______X__
_______X__
X____X____
_X________
__X___X_O_
_________X
___X__X___
___X______
__XX______
Command 13
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
________OX
___X__X___
___X______
__XX______
Command 14
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X_O_
___X______
__XX______
Command 15
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X____O_
__XX______
Command 16
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX____O_
Command 17
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX_____O
Simulation end
Quit? Enter "true" to quit or hit enter to run another simulation
True
Example Dialog 2:
Welcome to the Robot Simulator
Enter file for the Board
board2.txt
Enter file for the Robot Commands
robotCommandsBad.txt
OX___XXX__
___X___X__
XXXXX__X__
X____X_X__
_X_X______
__X__XX___
______XXXX
___X__XXXX
___X__XXXX
__XX______
Simulation begin
Command 0
_X___XXX__
O__X___X__
XXXXX__X__
X____X_X__
_X_X______
__X__XX___
______XXXX
___X__XXXX
___X__XXXX
__XX______
Command 1
_X___XXX__
_O_X___X__
XXXXX__X__
X____X_X__
_X_X______
__X__XX___
______XXXX
___X__XXXX
___X__XXXX
__XX______
Command 2
_X___XXX__
__OX___X__
XXXXX__X__
X____X_X__
_X_X______
__X__XX___
______XXXX
___X__XXXX
___X__XXXX
__XX______
Command 3
CRASH!
Simulation end
Quit? Enter "true" to quit or hit enter to run another simulation
True
////////////////////// Queue.java
public class Queue<T> {
  
   // node class
   public class Node<T>{
       private T data;// data
       private Node<T> next;// next
link
      
       // constructors
       public Node(){
           this.setData((T)
new Object());
          
this.setNext(null);
       }
       public Node(T d){
          
this.setData(d);
          
this.setNext(null);
          
       }
      
       // getter and setter
       public Node<T> getNext()
{
           return
next;
       }
       public void setNext(Node<T>
next) {
           this.next =
next;
       }
       public T getData() {
           return
data;
       }
       public void setData(T data) {
           this.data =
data;
       }  
   
   }
  
   // root for queue
   private Node<T> root;
  
   // constructor
   public Queue(){
       this.root=null;
   }
  
   // add data in queue
   void enqueue(T data){
       Node<T> node=new
Node<T>(data);
       // set root
       if(this.root==null){
          
this.root=node;
       }
       else{
           // add data at
last
           Node<T>
cur=this.root;
          
while(cur!=null){
          
    if(cur.getNext()==null){
          
        cur.setNext(node);
          
        break;
          
    }
          
    cur=cur.getNext();
           }
       }
      
   }
  
   // return root data
   T denqueue(){
       if(this.isEmpty()==false){
           T
data=this.root.getData();
           // set root as
next root element
          
this.root=this.root.getNext();
           return
data;
       }
       return null;
   }
  
   // top of the element
   T top(){
       if(this.isEmpty()==false){
           T
data=this.root.getData();
           return
data;
       }
       return null;
   }
  
   // empty queue
   boolean isEmpty(){
       return this.root==null;
   }
  
   // clear queue
   void clear(){
       this.root=null;
      
   }
  
}
////////////////////////////////// Driver.java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Driver {
   public static void main(String [] args){
       // for user input
       Scanner sc=new
Scanner(System.in);
      
       // queue object
       Queue<String> queue=new
Queue<String>();
          
   
      
       while(true){
          
System.out.println("Welcome to the Robot Simulator");
          
System.out.println("Enter file for the Board");
           // board file
name
           String
filename1=sc.next();
          
          
System.out.println("Enter file for the Robot Commands");
           // robot
Commands file name
           String
filename2=sc.next();
          
           // 10X10
board
           char [][]
board=new char[10][10];
          
           BufferedReader
br1 = null;
       BufferedReader br2 = null;
      
       // read board file
       try {
          
    br1 = new BufferedReader(new
FileReader(filename1));
          
    String line="";
          
    int j=0;
          
    while((line=br1.readLine())!=null){
          
        // fill each character in
board
          
        for(int
i=0;i<line.length();i++){
          
           
board[j][i]=line.charAt(i);
          
        }
          
        // increase line number
          
        j++;
          
    }
          
   
          
    br1.close();
           } catch
(IOException e) {
          
    e.printStackTrace();
          
}  
      
      
      
       // read robot command
       try {
          
    br2 = new BufferedReader(new
FileReader(filename2));
          
    String line="";
          
    while((line=br2.readLine())!=null){
          
        String [] splt=line.split("
");
          
        // add move name in
queue
          
        queue.enqueue(splt[1]);
          
    }
          
   
          
    br2.close();
           } catch
(IOException e) {
          
    e.printStackTrace();
          
}  
      
       // for command
           int command
=0;
          
           // initial x,y
position
           int
x=0,y=0;
          
           // true for
crash
           boolean
b=false;
          
          
while(queue.isEmpty()==false){
          
    // robot positioned
          
    board[x][y]='O';
          
   
          
    // print board
          
    for(int i=0;i<10;i++){
          
        for(int
j=0;j<10;j++){
          
           
System.out.print(board[i][j]);
          
        }
          
        System.out.print("\n");
          
    }
          
    // reset position
          
    board[x][y]='_';
          
   
          
    // dequeue move from queue
          
    String str=queue.denqueue();
          
   
          
    // for next x and y position using move
          
   
if(str.toUpperCase().equals("RIGHT")==true){y++;}
          
   
if(str.toUpperCase().equals("LEFT")==true){y--;}
          
   
if(str.toUpperCase().equals("UP")==true){x--;}
          
   
if(str.toUpperCase().equals("DOWN")==true){x++;}
          
   
          
    if(command==0){
          
       
System.out.println("Simulation begin");
          
    }
          
   
          
    // display command
          
    System.out.println("Command "+command);
          
   
          
    // for crash of robot
          
    if(x<0 || x>10 || y<0 || y>10 ||
board[x][y]=='X'){
          
        System.out.println("CRASH!
");
          
        b=false;
          
        break;
          
    }
          
    else{  
          
        b=true;
          
        command++;
          
    }
           }
          
           // for last
print of board
           // if robot nor
crashing
          
if(b==true){
          
    board[x][y]='O';
          
    for(int i=0;i<10;i++){
          
        for(int
j=0;j<10;j++){
          
           
System.out.print(board[i][j]);
          
        }
          
        System.out.print("\n");
          
    }
           }
          
          
System.out.println("Simulation end");
          
System.out.println("Quit? Enter \"true\" to quit or hit enter to
run another simulation");
           // next board
and robot
           String
tf=sc.next();
           // true for
quit
          
if(tf.toUpperCase().equals("TRUE")==true){
          
    break;
           }
           // otherwise
continue
          
queue.clear();
       }
      
       sc.close();
   }
}
///////////////////////////////


Welcome to the Robot Simulator
Enter file for the Board
board.txt
Enter file for the Robot Commands
robotCommands.txt
O____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Simulation begin
Command 0
_O___X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 1
_____X____
_O_____X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 2
_____X____
_______X__
_O_____X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 3
_____X____
_______X__
_______X__
XO___X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 4
_____X____
_______X__
_______X__
X_O__X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 5
_____X____
_______X__
_______X__
X__O_X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 6
_____X____
_______X__
_______X__
X___OX____
_X________
__X___X___
_________X
___X__X___
___X______
__XX______
Command 7
_____X____
_______X__
_______X__
X____X____
_X__O_____
__X___X___
_________X
___X__X___
___X______
__XX______
Command 8
_____X____
_______X__
_______X__
X____X____
_X___O____
__X___X___
_________X
___X__X___
___X______
__XX______
Command 9
_____X____
_______X__
_______X__
X____X____
_X____O___
__X___X___
_________X
___X__X___
___X______
__XX______
Command 10
_____X____
_______X__
_______X__
X____X____
_X_____O__
__X___X___
_________X
___X__X___
___X______
__XX______
Command 11
_____X____
_______X__
_______X__
X____X____
_X______O_
__X___X___
_________X
___X__X___
___X______
__XX______
Command 12
_____X____
_______X__
_______X__
X____X____
_X________
__X___X_O_
_________X
___X__X___
___X______
__XX______
Command 13
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
________OX
___X__X___
___X______
__XX______
Command 14
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X_O_
___X______
__XX______
Command 15
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X____O_
__XX______
Command 16
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX____O_
Command 17
_____X____
_______X__
_______X__
X____X____
_X________
__X___X___
_________X
___X__X___
___X______
__XX_____O
Simulation end
Quit? Enter "true" to quit or hit enter to run another
simulation
True