Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The objective is to build a simple robot game where the robots are modeled as ob

ID: 3664786 • Letter: T

Question

The objective is to build a simple robot game where the robots are modeled as objects and move around in a 2- dimensional grid as shown in the diagram below. The position of the robot is expressed as x- and ycoordinates, which can take any value within the limits of the grid. The goal of the game is to move the robot so that reaches the grid number (8,8) from grid number (1,1). The robot also faces one of three directions (Up, Right or Diagonal). It can move one, two or three steps in the direction it faces. It can also collect points as it moves across the grid.

Start by defining a robot object (Robot.java) with the following attributes:

• name (a String)

• x position (an integer)

• y position (an integer)

• direction (it faces) (you can decide on this data type)

• points collected (an integer)

Implement the following methods:

• Constructor that sets the name and the direction it faces to user-given values. The constructor should also

set the x and y positions to (1,1) and the points collected to 0.

• Get and set methods

• move that moves the robot by the given number of steps in the direction it faces.

Up

Diagonal

Right

• toString method that returns the attributes of the robot

• amIAhead method that compares the x and y coordinates of this robot with the x and y coordinates of

another robot and returns true if this robot is closer to (8,8). A simple test is to just add the x and y values

and see which is larger. For example, a robot at (3,3) is ahead than the robot at (1,4). As another example,

a robot at (7,1) and another at (4,4) are at the same distance from (8,8).

You may add other methods if you find them necessary to complete your program.

Now build a RobotGame.java class that contains the main method to test the above class and run a simple

simulation game using two robots as follows:

Prompt the user to enter the name and direction for two robots and create the two robots.

Next, move each robot 1, 2 or 3 steps by generating random numbers. You can use Math.random() to do this.

(int)(Math.random()*(max-°©min)+1)+min

generates a random number between min and max, both inclusive.

For example,

(int)(Math.random()*101

will generate a random number between 0 and 100, both inclusive.

(int)(Math.random()*11 + 10)

will generate a random number between 10 and 20, both inclusive.

The Bug.java program in Lab No. 2 shows you how Math.random() works. You can also check out web resources

on Math.random(). Change the x and y positions of the robot accordingly. For example, if the robot is facing up and

it moves two steps from (1,1) which is the starting point for all robots, then its position changes to (1,3). If the robot

is facing Diagonal and it moves three steps, its position changes from (1,1) to (4,4). Note that the robot cannot go

beyond the grid. Therefore, if the robot is in box (1,7) and the random number that is generated is 2, it cannot make

the move – it stays in its box for this round.

Collect points for each move according to the following rules:

1. At the start, each robot earns 1+1=2 points for being in the box (1,1).

2. If a robot moves into a box (x,y) collect x+y points for that robot and add it to the current number of

points.

3. If a robot A’s move falls into a box (x,y) which is already occupied by another robot, deduct x+y points

from A and do not move A from its current position.

After each move, change the robot’s direction by generating another random number from 1 to 3. (1 means

direction is Up, 2 means Right and 3 means Diagonal).

After each move, print the attributes of each robot using the toString method. Also print which robot is ahead by

comparing using the amIAhead method. If neither one is ahead, print that they are at the same distance.

If one robot reaches (8,8), remove it from further moves (by using a boolean variable or otherwise).

Play the game by putting the moves in a loop and see which robot gets to (8,8) first and which robot collects the

most points!

A sample screen dialog would look something like this:

Welcome to the Robot Game Simulator!

Enter the name of the first robot: Artoo

Enter the direction of the first robot: Up

Enter the name of the second robot: Detoo

Enter the direction of the second robot: Right

Simulation started!

Artoo (1,1) Up 2 points

Detoo (1,1) Right 2 points

Move!

Artoo (1,3) Right 6 points

Detoo (2,1) Diag 5 points

Artoo ahead!

(this means that in the first move, Artoo’s random number was 2 for the steps and 2 for the direction, so it moved

up to (1,3) and turned right. The total points for Artoo now is 2 + 4 = 6. Detoo’s random number was 1 for the steps

and 3 for direction, so it moved to (2,1) and turned diagonal. The total points for Detoo is 2 + 3=5).

Move!

Artoo (2,3) Up 11 points

Detoo (4,3) Right 12 points

Detoo ahead!

Move!

Artoo (2,6) Diag 19 points

Detoo (4,4) Right 20 points

Both are at the same distance from the goal!

Etc.

Move!

Artoo (8,8) Up 56 points

Detoo (7,8) Up 51 points

Artoo has reached the goal (56 points)

Move!

Detoo (7,8) Right 51 points

Move!

Detoo (8,8) Up 66 points

Detoo has reached the goal (66 points)

Game over!

Explanation / Answer

Completed!!!!!!

public class RobotGame {
private Robot R1, R2;
  
public RobotGame(){
System.out.println("Welcome to the Robot Game Simulator!");
loadData(1);
loadData(2);
System.out.println(R1.toString());
System.out.println(R2.toString());
}
  
public void SimulationStarted(){
while(!R1.Finish() && !R2.Finish()){
System.out.println("Move!");
if (!R1.move())
System.out.println(R1.toString());
if (!R2.move())
System.out.println(R2.toString());
}
if (R1.getPoints() > R2.getPoints())
System.out.println(R1.getName()+" has reached the goal ("+R1.getPoints()+" points)");
else
if (R1.getPoints() < R2.getPoints())
System.out.println(R2.getName()+" has reached the goal ("+R2.getPoints()+" points)");
else
System.out.println(R1.getName()+" and "+R2.getName()+" have Tied ("+R2.getPoints()+" points)");
System.out.println("Game over!");
}
  
private void loadData(int R){
Scanner sc = new Scanner(System.in); //crear un objeto Scanner
String name, dir, r1;
if (R==1)
r1 = "first";
else
r1 = "second";
System.out.print("Enter the name of the "+ r1 +" robot: ");   
name = sc.nextLine(); //leer un String
System.out.print("Enter the direction of the "+ r1 +" robot: ");
dir = sc.nextLine(); //leer un entero
if (R == 1)
R1 = new Robot(name, dir);
else
R2 = new Robot(name, dir);
}
  
public static void main(String[] ar){
RobotGame rg = new RobotGame();
rg.SimulationStarted();
}
}

class Robot{
private String name;
private int x;
private int y;
private String dir; // up, rigth, diag
private int points;
  
public Robot(){
setName("Untitled");
setDir("Up");
setPosX(0);
setPosY(0);
points =0;
}
public Robot(String name, String dir){
setName(name);
setDir(dir);
setPosX(1);
setPosY(1);
points =0;
}
public void setName(String name){
this.name = name;
}
public void setDir(String dir){
this.dir = dir;
}
public void setPosX(int x){
this.x = x;
}
public void setPosY(int y){
this.y = y;
}
public void setPoints(int p){
this.points = p;
}
public String getName(){
return name;
}
public String getDir(){
return dir;
}
public int getPosX(){
return x;
}
public int getPosY(){
return y;
}
public int getPoints(){
return points;
}
public String toString(){
return name + " ("+x+","+y+") "+dir+" "+points+" points";
}
public boolean move(){
if (Finish())
return true;
  
int x=this.getPosX(), y=this.getPosY(), d=0, r=0;
String dir="";
boolean b=false;
  
while(!b && x <= 8 && y <= 8){
r = (int)(Math.random()*3)+1;
d = (int)(Math.random()*3)+1;
//System.out.println("steps "+r+" dir "+d);
switch (d){
case 1 : if (y+r <= 8){ dir = "Up"; y += r; b=true;} break;
case 2 : if (x+r <= 8){ dir = "Rigth"; x += r; b=true;} break;
default : if (x+r <= 8 && y+r <= 8){ dir = "Diag"; x += r; y += r; b=true;} break;
}
  
}
  
setDir(dir);
setPosX(x);
setPosY(y);
points += x+y;
  
return false;
}
public boolean amIAhead(Robot o){
int dThis = this.getPosX() + this.getPosY();
int dO = o.getPosX() + o.getPosY();
if (dThis > dO){
return true;
}
return false;
}
public boolean Finish(){
if (this.getPosX() == 8 && this.getPosY() == 8)
return true;
return false;
}
  
}