Question

Program in Java. There is an array of [0,1, ..., numCaves-1] with a policeman and a...

Program in Java. There is an array of [0,1, ..., numCaves-1] with a policeman and a bad guy. The bad guy may appears in the any position of this array, he will changes his position each day, but can only move left or right by one cell. If the policeman and the bad guy are in the same cell, the bad guy is said to be caught. Given any initial position of the bad guy, the objective is output the policeman’s strategy that can guarantee to catch the bad guy, and demo the whole process (preferbaly with a GUI).

Neither knows what position the other is at. The bad guy must move left or right, while the policeman can move left and right as well as wait in a spot.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

as GUI isn't mandatory i made it for cli..please comment if you need any gui version

for following o/p:

code:

import java.util.Random;

class PoliceBad{

char cavearray[];
int policepos;
int badguypos;
int turn;
PoliceBad(int numCaves)
{
this.cavearray=new char[numCaves];
int min=0;int max=numCaves-1;
for(int i=0;i<max;i++)
this.cavearray[i]=' ';
this.policepos=new Random(System.currentTimeMillis()).nextInt((max - min) + 1) + min;
this.badguypos=new Random(System.currentTimeMillis()+1000).nextInt((max - min) + 1) + min;
this.cavearray[this.badguypos]='b';
this.cavearray[this.policepos]='p';
this.turn=0;
}
void badMove()
{
int bpre=this.badguypos;
if(this.badguypos!=this.policepos)
{
if(this.badguypos==0)
this.badguypos=1;
else if(this.badguypos==this.cavearray.length-1)
this.badguypos=this.cavearray.length-2;
else{
int min=-1;int max=1;
int tmp=0;
while(tmp==0)
tmp=new Random(System.currentTimeMillis()).nextInt((max - min) + 1) + min; //-1 is left, 1 is right we loop until we won't get 0
this.badguypos=this.badguypos+tmp;
}
}
  
this.cavearray[bpre]=' ';
this.cavearray[this.badguypos]='b';
  
}

boolean catchBad()
{
if(this.policepos==this.badguypos)
{
return false;
}
return true;
}
void policeMove()
{
int ppre=this.policepos;
if(this.badguypos!=this.policepos)
{
if(this.policepos==0)
{int min=0;int max=1;
int tmp=new Random(System.currentTimeMillis()).nextInt((max - min) + 1) + min;
this.policepos+=tmp;}
else if(this.policepos==this.cavearray.length-1)
{int min=-1;int max=0;
int tmp=new Random(System.currentTimeMillis()).nextInt((max - min) + 1) + min;
this.policepos+=tmp;}
else{
int min=-1;int max=1;
int tmp=new Random(System.currentTimeMillis()).nextInt((max - min) + 1) + min; //-1 is left,0 is idle,1 is right
this.policepos+=tmp;
}
}
  
this.cavearray[ppre]=' ';
this.cavearray[this.policepos]='p';

  
}
public static void main(String[] args) {
int numCaves=5;
PoliceBad p=new PoliceBad(numCaves);
boolean flag=true;
while(flag)
{
System.out.print(" | ");
for(int i=0;i<p.cavearray.length;i++)
System.out.print(p.cavearray[i]+" | ");
System.out.println();
flag=p.catchBad();
p.badMove();
p.policeMove();
p.turn++;
}
System.out.println("police caught bad guy at turn: "+p.turn+" and pos: "+p.badguypos);

  
}
}

Add a comment
Know the answer?
Add Answer to:
Program in Java. There is an array of [0,1, ..., numCaves-1] with a policeman and a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • USE JAVA ONLY : public int canEscapeMaze(char[][] maze) Complete a method to determine if it is...

    USE JAVA ONLY : public int canEscapeMaze(char[][] maze) Complete a method to determine if it is possible to escape a maze while collecting all of the coins scattered throughout the maze. The maze is represented with a rectangular 2d array of chars. Each element of the 2d array is a cell and the maze has 6 types of cells: Starting cell: The Starting cell is marked with an S. There is only one Starting cell in the maze. The starting...

  • this is advance java Monster ARray Summary: Write a program that shows the path of an imaginary monster moving through a two dimensional array trying to "eat&#3...

    this is advance java Monster ARray Summary: Write a program that shows the path of an imaginary monster moving through a two dimensional array trying to "eat" the highest values in the array. As the monster goes, it leaves a trail behind it. Imagine a 40x40 array, displayed like this, where Os are printed as periods (dots), and non-zero valués are printed as normal. Then, place the values 2 through 9 randomly on the array. The rest of the document...

  • Write a JAVA program that plays a simple Pac-man game. The game plays on a 10...

    Write a JAVA program that plays a simple Pac-man game. The game plays on a 10 by 10 grid (absolutely, you can make it bigger). Pac-man is depicted as “#” and others as “*”. Please see the below.   You should ask how many pac-man plays before starting the game. Then, you should create the number of pac-man objects. The pac-mans are defined as classes, and the objects are stored in an array. The pac-man class has a “move()” method, which...

  • ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file...

    ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file that contains the maze. Read it into a two-dimensional array Remember you can use inputStream.get(c) to read the next character from the inputStream. This will read whitespace and non-whitespace characters Don’t forget to read the newline character at the end of each line Print the maze to the screen from your array You should include a ‘*’ in your maze to indicate where the...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares,...

    Maze Solving with Stacks Problem Statement Consider a maze made up of rectangular array of squares, such as the following one: X X X X X X X X X X X X X           X            X X X X    X X X           X               X     X X X     X X    X    X     X     X X X         X          X             X X X     X X X X X                X X X X X X X X X X X X X Figure...

  • Your program should declare an array with following initial setting: 1 4 15 7 8 10...

    Your program should declare an array with following initial setting: 1 4 15 7 8 10 2 11 14 3 6 13 12 9 5 16 In this table, 16 represents a blank. Your main function should have two variables named blankCol and blankRow which should keep track of position of blank. So for this initial setting blankRow and blankCol should both be set to 3. Your program should have a function named "Display" which takes the array as argument...

  • Things are tense on the battlefield and our army is on the verge of defeat. The only chance is to...

    Programming language is Java 8 Thank you Things are tense on the battlefield and our army is on the verge of defeat. The only chance is to retreat for now, regroup, and then initiate another attack. To do this however, the king must be rescued first. A brave soldier named S wants to rescue the king. S wants to get to the king as fast as possible while avoiding the obstacles on the field such as pits, trees, bodies, etc....

  • How to make a reversi/othello game in JAVA? Ideally with multiple classes and without GUI You...

    How to make a reversi/othello game in JAVA? Ideally with multiple classes and without GUI You are implementing a strategy board game played by two players, Black and White. It is played on an N by N board. The winner is the player who has more discs of his color than his opponent at the end of the game. This will happen when neither of the two players has a legal move or there are no spaces left on the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT