JAVA PROGRAM HELP I need to create a method for a program that makes the chaser
ID: 3810799 • Letter: J
Question
JAVA PROGRAM HELP
I need to create a method for a program that makes the chaser (the red square) chase the runner (green square) for a certain amount of time. If the chaser never reaches the runner in x amount of time, the game stops and a "YOU WON!!!" appears.
I am mainly struggling with creating a method that makes the chaser chase the runner. Right now the square does not move. This is the only issue to my program that I have so far.
If you need more information on the assignment as a whole it is found here: http://www.cs.utsa.edu/~cs1063/projects/index.html (Project 2: Survivor I) And the subsection that the question I'm asking is coming from the "movePlayers Method" subsection
This is my code so far
Explanation / Answer
// Iam Hoping that I have done what you exactly want. If that is true give my answer a like. If any problem in understanding, please leave me a comment.
import java.util.*;
import java.awt.*;
import javax.swing.JOptionPane;
public class SurvivorI {
public static void main(String[] args) {
// Create DrawingPanel and draw a box in the panel.
// The box is a square of this size.
int boxSize = 760;
DrawingPanel panel = new DrawingPanel(800, 700);
Graphics g = panel.getGraphics();
g.fillRect(10, 10, 10, 780);
g.fillRect(10, 10, 780, 10);
g.fillRect(780, 10, 10, 780);
g.fillRect(10, 780, 780, 10);
// Initialize positions of runner and chaser.
Point runner = new Point(200, 400);
Point chaser = new Point(600, 400);
boolean gameState = true;
// Variable for input from user to move runner.
char keyInput = ' ';
// The program should wait sleepTime ms between moves.
int sleepTime = 100;
// The runner should move moveSize (or zero) pixels each time step.
// The chaser should move moveSize - 1 pixels each time step.
int moveSize = 10;
// Wait one second before start of game.
panel.sleep(1000);
// Move the players according to parameters.
movePlayers(runner, chaser, keyInput, boxSize, moveSize);
// Display players using Color.GREEN and Color.RED (or whatever colors you want).
displayPlayers(panel, runner, chaser);
// NEED TO PUT SOME OF THESE STATEMENTS IN A FOR LOOP
while (gameState = true) {
displayPlayers(panel, runner, chaser);
char newKeyInput = panel.getKeyChar();
if (newKeyInput == 'w' || newKeyInput == 'a' || newKeyInput == 's' || newKeyInput == 'd') {
keyInput = newKeyInput;
erasePlayers(panel, runner, chaser);
//I have added this function to fulfill your requirement.
//this will find distance between current runner position and current chaser position
//now we will find all 4 possible distances between them if we take some move to chaser in future
//then we will find optimum move such that chaser will get nearer to runner
setChaserMoveSuchThatChaserIsNearToRunner(runner,chaser,moveSize);
movePlayers(runner, chaser, keyInput, boxSize, moveSize);
legalMove(runner, chaser);//I have changed the position of legalMove() function.Because we should make our move legal only after(not before) we take a move to both runner and chaser
}
if(true==isReached(runner, chaser)) {
JOptionPane.showMessageDialog (null, ""YOU LOST!!!"", "Result", JOptionPane.INFORMATION_MESSAGE);
panel.close();
break;
}
}
}
/*Method that displays the runner and chaser in the graphics panel. Green and red.*/
public static void displayPlayers(DrawingPanel panel, Point runner, Point chaser) {
Graphics g = panel.getGraphics();
g.setColor(Color.GREEN);
g.fillRect(runner.x, runner.y, 10, 10);
g.setColor(Color.RED);
g.fillRect(chaser.x, chaser.y, 10, 10);
}
/*Method that turns the runner and chaser white*/
public static void erasePlayers(DrawingPanel panel, Point runner, Point chaser) {
Graphics g = panel.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(runner.x, runner.y, 10, 10);
g.setColor(Color.WHITE);
g.fillRect(chaser.x, chaser.y, 10, 10);
}
//this function sets chaser's position such that the distance b/w runner and chaser is minimum
public static void setChaserMoveSuchThatChaserIsNearToRunner(Point runner,Point chaser,int moveSize){
System.out.println("Runner:"+runner.x+","+runner.y);
System.out.println("Chaser:"+chaser.x+","+chaser.y);
//need to calculate distance between runner and chaser
double distance = calculateDistanceBetweenRunnerAndChaser(runner,chaser);
System.out.println(distance);
//what will be the distance if chaser's move is 'w'
double distw = calculateDistanceBetweenRunnerAndChaser(runner,new Point(chaser.x,chaser.y-10));
//what will be the distance if chaser's move is 'a'
double dista = calculateDistanceBetweenRunnerAndChaser(runner,new Point(chaser.x-10,chaser.y));
//what will be the distance if chaser's move is 's'
double dists = calculateDistanceBetweenRunnerAndChaser(runner,new Point(chaser.x,chaser.y+10));
//what will be the distance if chaser's move is 'd'
double distd = calculateDistanceBetweenRunnerAndChaser(runner,new Point(chaser.x+10,chaser.y));
System.out.println(distw+"-"+dista+"-"+dists+"-"+distd);
//we should set chaser's position such that the distance between runner and chaser is minimum
if(distw<=dista && distw<=dists && distw<=distd){// if 'w' is our optimum move
chaser.translate(0, -moveSize);
}
else if(dista<=distw && dista<=dists && dista<=distd){// if 'a' is our optimum move
chaser.translate(-moveSize, 0);
}
else if(dists<=distw && dists<=dista && dists<=distd){// if 's' is our optimum move
chaser.translate(0, moveSize);
}
else if(distd<=distw && distd<=dista && distd<=dists){// if 'd' is our optimum move
chaser.translate(moveSize, 0);
}
//hope this helps
}
//this function will calculate distance between two points(can be runner and chaser)
public static double calculateDistanceBetweenRunnerAndChaser(Point runner,Point chaser){
return ((runner.x-chaser.x)*(runner.x-chaser.x))+((runner.y-chaser.y)*(runner.y-chaser.y));
}
public static void movePlayers(Point runner, Point chaser, char keyInput, int boxSize, int moveSize) {
if (keyInput == 'w') {
runner.translate(0, -moveSize);
}
if (keyInput == 's') {
runner.translate(0, moveSize);
}
if (keyInput == 'a') {
runner.translate(-moveSize, 0);
}
if (keyInput == 'd') {
runner.translate(moveSize, 0);
}
}
public static void legalMove(Point runner, Point chaser) {
if (runner.x >= 780 - 20) {
runner.x = 780 - 20;
}
if (runner.x <= 10 + 20) {
runner.x = 10 + 20;
}
if (runner.y >= 780 - 20) {
runner.y = 780 - 20;
}
if (runner.y <= 10 + 20) {
runner.y = 10 + 20;
}
if (chaser.x >= 780 - 20) {
chaser.x = 780 - 20;
}
if (chaser.x <= 10 + 20) {
chaser.x = 10 + 20;
}
if (chaser.y >= 780 - 20) {
chaser.y = 780 - 20;
}
if (chaser.y <= 10 + 20) {
chaser.y = 10 + 20;
}
}
private static boolean isReached(Point runner, Point chaser) {
//check runner and chaser points are equal or not
if((runner.getX()==chaser.getX())&&(runner.getY()==chaser.getY()))
return true;//if equal return true
return false;//other wise false
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.