Question: JAVA PROGRAM HELP I need someone to edit this program to do the following remaining things: - I need the chaser to run on a
JAVA PROGRAM HELP
I need someone to edit this program to do the following remaining things:
- I need the chaser to run on a timed interval. That is, I basically need it to chase the runner on its own, every 300 timesteps, instead of what it is doing now (making its turn only when the runner presses a key)
- I need the program to also stop running at x amount of seconds and pop up a window saying "You Won!" if the chaser never reached the runner for x amount of time
If you need more information for what I need, the instructions are found here: http://www.cs.utsa.edu/~cs1063/projects/index.html (Project 2: Survivor I)
Here if my code so far:
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, 800); 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); //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); } 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); } } //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 } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
