Question

In: Computer Science

Objective: Write a program that simulates a robot running a queue of commands to move around...

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

Solutions

Expert Solution

////////////////////// 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


Related Solutions

Objective: Write a program which simulates a hot potato game. In this version of a classic...
Objective: Write a program which simulates a hot potato game. In this version of a classic game, two or more players compete to see who can hold onto a potato the longest without getting caught. First the potato is assigned a random value greater than one second and less than three minutes both inclusive. This time is the total amount of time the potato may be held in each round. Next players are put into a circular list. Then each...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Write a python program that will allow a user to draw by inputting commands. The program...
Write a python program that will allow a user to draw by inputting commands. The program will load all of the commands first (until it reaches command "exit" or "done"), and then create the drawing. Must include the following: change attributes: color [red | green | blue] width [value] heading [value] position [xval] [yval] drawing: draw_axes draw_tri [x1] [y1] [x2] [y2] [x3] [y3 draw_rect [x] [y] [b] [h] draw_poly [x] [y] [n] [s] draw_path [path] random random [color | width...
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
Write a java program that simulates thousands of games and then calculate the probabilities from the...
Write a java program that simulates thousands of games and then calculate the probabilities from the simulation results. Specifically in the game, throw two dice, the possible summations of the results are: 2, 3, ..., 12. You need to use arrays to count the occurrence and store the probabilities of all possible summations. Try to simulate rolling two dice 100, 1000, 10,0000 times, or more if needed. Choose one simulation number so that the probabilities you calculated is within 1%...
Write a program that simulates a cashier terminal. Assume that a customer is purchasing an unknown...
Write a program that simulates a cashier terminal. Assume that a customer is purchasing an unknown number of different merchandise items, possibly with multiple quantities of each item. Use a while loop to prompt for the unit price and quantity. The loop should continue until the unit price is zero. Display a subtotal for each item. After the loop display the total amount due. Use currency format where appropriate. See Sample Run (inputs shown in blue). You must use a...
3. Write a program that simulates a vending machine.   The user will be prompted to enter...
3. Write a program that simulates a vending machine.   The user will be prompted to enter a number then a letter. As part of the prompt you must display a message to indicate the number and letter, along with the possible choices. That is, what selections will give user which item.   If the user enters a number or a letter that is not valid, then display the message “Bad Entry” and end the program. (100 pts) Selections with messages. 1a...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Write a program that allows the robot to do a 90-degree turn (the direction of the...
Write a program that allows the robot to do a 90-degree turn (the direction of the turn can be your choosing). There are two way of accomplishing this, the first is to move only one wheel, while the other is to move both wheels simultaneously in opposite directions. Micro:bit Block Coding
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT