Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Robot class represents a class of robot objects. The class is partially defined and you need to fill it in as explained below. First

The Robot class represents a class of robot objects. The class is partially defined and you need to fill it in as explained below. First such a robot is placed in a square grid and moves randomly where at each point the permissible movements are one step up, down, right or left. In addition, there are following restrictions:

He can not go through a point he has already gone through again.

If it reaches an external point of the grid, then it stops and is considered to have escape.

If he can not move from the inside where he is because he has already passed by all the strong points, then it remains stagnant and trapped.

If it moves to another robot (even if it has one) destroyed), then a collision occurs and any colliding robot is destroyed,

remaining at the point of conflict. The same thing happens if a robot is stationary,

either because he escaped, or because he was trapped and another robot collided with him

of.

Complete the Robot class, the outline of which is given below :.

import java.awt.Color;

public class Robot {

private String name; // his name

private int Gsize; // the size of the square grid in which it is located

private int xpos; // the x coordinate of its current position

private int ypos; // the y coordinate of its current position

private boolean alive; // is it alive? initially every robot is alive

private boolean escaped; // has escaped, ie is outside

// grid?

private boolean trapped; // is trapped?

private int [] [] path; // the route traveled, ie the sequence of

// coordinates of the points it has passed -

// the starting point is given to the first row of the path

// and the current point located is given in the row

// plen-1 of the path

private int plen; // the length of its route so far

private Color col; // its color, which is produced randomly but

// must be compatible with white (WHITE)

// The only manufacturer was named after the robot

// coordinates of its original position, as well as its size

// grid and initializes the relevant fields of the robot accordingly.

// Only live robots are made.

public Robot (String n, int x, int y, int size) {

//. . . . .

}

// Is he alive? It is already defined

public boolean isAlive () {return alive;}

// Has he escaped? It is already defined

public boolean hasEscaped () {return escaped;}

// Is he trapped? It is already defined

public boolean isTrapped () {return trapped; {

// The robot is destroyed. It is already defined.

public void destroyed () {

alive = false;

}

// Returns the robot to its current position.

public int [] getPos () {

//. . . . . . . . .

}

// Returns the robot to its original position.

public int [] getStart () {

//. . . . . . . . . .

// Has it collided with robot r?

public boolean collides (Robot r) {

//. . . . . . . . .

}

// If and when he can, that is, he is alive, he has not escaped

// or not trapped, moves randomly against

// a step to a point from which it has never passed. The method

// modifies the affected fields accordingly and returns true if

// moving to a new point was possible, otherwise

// returns false. Additionally he paints on the grid his movement with his color,

// thus leaving the trace of its course on the grid (if then someone else

// robot passing through the same points will show its own trace with its own color).

public boolean move () {

//. . . . . . . . . . . .

}

// The presentation of the robot. The operation of the method is shown in

// following example of using the RandomRobots program which

// is a client of Robot.

public String toString () {

. . . . . . . . .

}

// Already some auxiliary methods for brightness (lum) and compatibility

// (compatible) two colors

private static double lum (Color c) {

int r = c.getRed ();

int g = c.getGreen (); int b =

c.getBlue ();

return .299 * r + .587 * g + .114 * b;

}

private static boolean compatible (Color a, Color b) {

return Math.abs (lum (a) - lum (b))> = 128.0;

}

}

3. RandomRobots.java program Build RandomRobots.java, which generates a given number of robots (10 in the example below) in a given grid size (20 x 20 in the example below), and

after presenting them as they initially appear, he moves them sequentially until each one has reached the end of its course, and when it does it presents the final end of each robot. Initially each robot is placed at random, but each in a different position.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions