I have a problem with the execution of the methods in a for loop. I want my program to execute the programmedMoving() method 5 times.
This programmedMoving() method consists of two methods:
the first one ( chooseTheDirection() ) executes some algorithm and returns the Point2D towards which the object should move;
the second one ( moveToThePoint() ) should get this point and move the object.
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_H) {
for(int i=0; i<5; i++{
programmedMoving();
}
}
}
//////////////////////////////////////////////////////////////////
private void programmedMoving(){
chooseTheDirection(); //returns the Point2D
moveToThePoint();//according to the direction starts moving the point
}
The problem is that it executes chooseTheDirection() method 5 times without waiting for the moveToThePoint() method to finish it's execution. So, by the time the object on the JPanel starts actually moving, the chooseTheDirection() method already provides 5 Point2D points, whereas I need it to provide only one, and wait for the end of the next method.
Could anyone tell me what am I doing wrong? Thank you.
ADDITIONALLY:
private Direction chooseDirection(){
final List<Direction> directionList = Collections.unmodifiableList(Arrays.asList(Direction.values()));
int pick = random.nextInt(directionList.size());
dir = directionList.get(pick);
directionsArchive.add(dir);
System.out.println("dir " + dir + " " + directionsArchive);
if(directionsArchive.size() == 1){
dir = directionsArchive.get(0);
System.out.println("equal to one taken " + dir + " size of dir " + directionsArchive.size());
directionsArchive.add(dir);
}
if(directionsArchive.size() > 1){
int last = directionsArchive.size()-1;
System.out.println("index of last " + last);
if(directionsArchive.get(last).equals(dir)){
pick = random.nextInt(directionList.size());
dir = directionList.get(pick);
directionsArchive.add(dir);
}
System.out.println("more than one taken " + dir + " size of dir " + directionsArchive.size());
directionsArchive.add(dir);
}
else{
directionsArchive.add(dir);
System.out.println(" size of dir " + directionsArchive.size());}
return dir;
}
private void moveToThePoint(){
if(dir.equals(Direction.NORTH)){
this.robot.turnUp();
this.robot.go();
}
if(dir.equals(Direction.SOUTH)){
this.robot.turnDown();
this.robot.go();
}
if(dir.equals(Direction.EAST)){
this.robot.turnRight();
this.robot.go();
}
if(dir.equals(Direction.WEST)){
this.robot.turnLeft();
this.robot.go();
}
}
// SOME EXAMPLES TO THE MOVING METHODS. I PROVIDED ONLY ONE, CAUSE THEY ARE PRETTY SIMILAR
public void turnDown()
{
//System.out.println("Robot - Turn Down!");
this.m_nXDir = 0;
this.m_nYDir = 1;
}
public void go()
{
this.m_nStep = 1;
//System.out.println("Robot - Go!");
}
public void move(int d, int e)
{
//from start to finish
int x = d + this.m_nStep * this.m_nXDir;
int y = e + this.m_nStep * this.m_nYDir;
int w = getWidth();
int h = getHeight();
setBounds(x, y, w, h);
}
moveToThePoint doesn't actually move the object, it merely sets the step size to 1. I assume the actual move() function is called on a timer.
Here are 2 suggestions on how to fix it:
Re-run the decision algorithm (programmedMoving) for every step.
or
Queue up future moves. You store a queue of future moves for the object, and each times it is scheduled to move() you remove the next move from the queue and execute it.
Related
I am writing Java program which allows me to get the geometric center of random-generated 2-dimensional points. I want the calculations to be done by custom number of threads. I want to suspend/continue calculations at any time. Unfortunately, my code doesn't work, It seems like run() is never executed. Here is what I got:
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
System.out.println("running... " + Thread.currentThread().getName());
PointInterface p = pg.getPoint(); // getting random point(x,y)
pointCount++;
int[] pos = p.getPositions(); // getting (x,y)
System.out.println(pos[0] + ", " + pos[1] + " k");
sumxy[0] += pos[0];
sumxy[1] += pos[1];
geometricCenter[0] = (double) sumxy[0] / pointCount;
geometricCenter[1] = (double) sumxy[1] / pointCount;
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("exception caught in run()");
return;
}
}
}
Setting number of threads:
public void setNumberOfThreads(int threads) {
threadsList.clear();
for (int i = 0; i < threads; i++) {
threadsList.add(new Thread());
}
}
Starting the calculations:
public void start() {
try {
for (Thread t : threadsList) {
t.start();
}
} catch (Exception e) {
System.out.println("start() exception caught");
}
}
Suspending calculations:
public void suspendCalculations() {
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
System.out.println("SuspendCalculations() exception caught");
}
}
Resuming calculations:
I don't exactly know what I'm supposed to do here. Should I create new set of Threads like that?
public void continueCalculations() {
int x = threadsList.size();
threadsList.clear();
for (int i = 0; i < x; i++) {
threadsList.add(new Thread());
threadsList.get(i).start();
}
}
How I run my program:
public static void main(String[] args) {
ParallelCalculations pc = new ParallelCalculations(); // My main class object where run() and all the methods above are declared
PointGenerator g = new PointGenerator(); // class that allows me to generate points
PointInterface a = g.getPoint(); // getting random point
pc.setNumberOfThreads(3);
pc.setPointGenerator(g);
pc.start();
pc.suspendCalculations();
System.out.println(pc.getGeometricCenter()[0] + ", " + pc.getGeometricCenter()[1]);
pc.continueCalculations();
pc.suspendCalculations();
System.out.println(pc.getGeometricCenter()[0] + ", " + pc.getGeometricCenter()[1]);
}
If needed:
Point:
class Point {
public static final int MAX_POSITION = 16;
private int[] positions = new int[2];
Point(int _x, int _y) {
this.positions[0] = _x;
this.positions[1] = _y;
}
public int[] getPositions() {
return positions;
}
}
Point Generator:
class PointGenerator {
private int x = (int) (Math.random() * (Point.MAX_POSITION + 1));
private int y = (int) (Math.random() * (Point.MAX_POSITION + 1));
public PointInterface getPoint() { // Can be called by many Threads at the same time.
return new Point(x, y);
}
}
Your run() should be executed and should do it's thing.
Though there is a far bigger cause for random behaviour in this code: All the threads write to sumxy, geometricCenter and pointCount at he same time without any syncronisation-locks, causing more or less random behaviour. You at least need to implement some kind of synchronisation to prevent simultanious writes.
Maybe start here (Java synchonized tutorial by Oracle) if you have no clue.
But simply adding synchronisation to everything will probably just make it slower than a single thread, you will need some kind of buffer for each thread to work independently and than collect the results when they are suspended.
And now general problems:
A) Your suspendCalculations() doesn't do anything (for 1200ms to be percise). To break the calcualtion you would need to interrupt all the worker-threads since they terminate upon interruption. Call threadsList.get(i).Interrupt() to do so.
B) If you want to to be able to change the number of threads while suspended, this is a way to go. If this is not necessarry, it would be more efficient to create a constant
public static final Object LOCK = new Object();
make all the threads LOCK.wait() on that object, so resuming them is just a call to LOCK.notifyAll().
C) Instead of using your own implementaion of Point you can use java.awt.Point.
D) Returning the coordinates of a point simply in an array is really bad for readability of your code, java.awt.Point has getX() and getY() functions.
That sounds extremely philosophical doesn't it?
Anyways, I have a rather complex problem.
My main_activity class gathers all of the Zombies like so:
//Testing Runnable (used to compare the first zombie with the player)
private Runnable updateLocations = new Runnable(){
#Override
public void run(){
try {
while(true) {
image_player.getLocationInWindow(pLoc);
Zombie zoms = zombieCollection.next();
if(!zoms.equals(null)){
zoms.getZombieImage().getLocationInWindow(zLoc);
}
System.out.println("Zombie: x = " + zLoc[0] + "; y = " + zLoc[1]);
System.out.println("Player: x = " + pLoc[0] + "; y = " + pLoc[1]);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
My zombie class gathers information like so:
public class Zombie{
float X, Y;
int Width, Height;
Direction fdirc;
ImageView zImage;
Player player;
boolean dead;
int[] zLoc;
public Zombie(ImageView zImage, Player player){
zLoc = new int[2];
zImage.getLocationOnScreen(zLoc);
this.zImage = zImage;
this.X = zLoc[0];
this.Y = zLoc[1];
this.Width = zImage.getWidth();
this.Height = zImage.getHeight();
this.fdirc = Direction.EAST;
this.player = player;
this.dead = false;
Thread thread = new Thread(this.startZombieChase);
thread.start();
}
public ImageView getZombieImage(){
return zImage;
}
private Runnable startZombieChase = new Runnable() {
#Override
public void run() {
try {
while(!dead) {
moveTowardsPlayer();
Thread.sleep(10);
updateZombie.sendEmptyMessage(0);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
private Handler updateZombie = new Handler(Looper.getMainLooper()) {
public void handleMessage(android.os.Message msg) {
/** Because the zombie should always be on top! **/
zImage.getLocationOnScreen(zLoc);
zImage.bringToFront();
zImage.setX(X);
zImage.setY(Y);
}
};
private void moveTowardsPlayer(){
int player_x = player.getPosition()[0];
int player_y = player.getPosition()[1];
l("Where is it in zombie class : player - " + player_x + " " + player_y + "zombie - " + X + " " + Y);
float compareX = player_x - (int)X;
float compareY = player_y - (int)Y;
// Y is closer, so we're moving horizontally.
if(Math.abs(compareX) < Math.abs(compareY)){
//Moving North
if(player_y > Y){
Y+=1;
}
//Moving South
else if(player_y < Y){
Y-=1;
}
}
// X is closer, so we're moving vertically.
else{
//Moving East
if(player_x > X){
X+=1;
}
//Moving West
else if(player_x < X){
X-=1;
}
}
}
public void l(Object string){
System.out.println("Log - " + string);
}
}
The problem I'm having is that it will move relative to a number (So, it does move relative to something) but, not the correct thing.
logcat tells me this:
Where is it in zombie class: player - 750 451 zombie - 750 451
Where it is in main_activity: player - 750 451 zombie - 792 619
Could anyone help me understand what I'm doing wrong?
The entire project is located here.
Zombies that moves away from the Brainz, must be an ill Zombie. We can't have that, now can we?
To have a function that moves the Zombie towards the non-zombie, you use a function, but that function makes use of variables that are not arguments, and there fore hard to find out where they come from. I'd go with something like this: (this is a bit verbose, but clearly shows what is happening)
/*
* Function to update the position of the Zombie, aka walk to the Player.
* #player_pos - Where's the Brainz at?
* #zombie_pos - Where am I?
* Might want to build it overloaded with an option for the speed.
*
* #return - We return the new Zombie pos.
*/
private double [] moveTowardsPlayer(double [] player_pos, double [] zombie_pos) {
// To make sure we don't override the old position, we copy values. (Java stuff)
double [] player_pos_old = player_pos.clone();
double [] zombie_pos_old = zombie_pos.clone();
// Let's get the new X pos for the Zombie
double left_or_right = player_pos_old[0] - zombie_pos_old[0]; // pos number is right, neg is left
double zombie_pos_new_x;
if (left_or_right > 0) { // Right
zombie_pos_new_x = player_pos_old[0] + zombie_speed;
} else { // Left - this way we make sure we are always getting nearer to the Brainz.
zombie_pos_new_x = player_pos_old[0] - zombie_speed;
}
// TODO: do the same for the Y pos.
// Bring it together
double [] zombie_pos_new = {zombie_pos_new_x, zombie_pos_new_y};
// One step closer to the Brainz!
return zombie_pos_new;
}
And use like:
double [] zombie_pos = moveTowardsPlayer([2, 2], [5, 4]);
this.X = zombie_pos[0]; // I'd advice to keep these together as one var.
this.Y = zombie_pos[1]; // But it's your game.
And then figure out when the Zombie gets the Brainz (or the Bullet)
this here is not correct:
if(!zoms.equals(null)){
if you are trying to check that zoms is not pointing to a null reference then do
if(zoms != null ){
I have for the past few days been trying to get an example of the state pattern working. The task has been set as part of the module for the master course ive been doing. I did find a thread on here that used the same example however, in my opinion doesnt do it correctly, as the program should work with any sort of test criteria which i am trying to do. Thread here: state design pattern . I have narrowed it down to, i believe the if-statement in the normal state class, so ive cut out the set/getters for the state and ammo, however i can add it in later if they are needed
At the moment it is completing the first assertEquals perfectly fine, however one it goes to the second it throws a nullpointexception. SO my question is, what have i done wrong, or missed out, with the if-statement to return a nullpointexception for the second part of the if-statement? and the sarky reply i got wasn't helpful, as all i want is a fresh pair of eyes to look over what I have done to find the mistake, not to do my entire assignment as i just need a little bit of help to fix this small problem. Also I already knew what a NPE and in this case its happening because the 2nd part of the statement is not being initialised with the criteria specified.
Thank You in advance
public class Railgun {
static int MAX_AMMO = 10;
public String fire(Point p, int round){
System.out.println(p + " " + round);
// System.out.println("ASA W " + state.fire(p, round));
return state.fire(p, round);
}
}
Fire method in the normal state:
public String fire(Point p, int round) {
// NormalState normal = new NormalState();
NeedAmmoState needAmmo = new NeedAmmoState();
System.out.println("akhdka " + round);
ammo-= round;
int round1 = round;
int result1 = 0 + round1;
if(ammo >= 0 && result1 == round1)
{
System.out.println(result1);
return "Fire order: Success "+ result1 + "/" + round1;
}
else if((ammo < 0) && (ammo != -result1))
{
railgun.ammo = 0;
return "Fire order: Partial success " + result1 + "/" + round1;
}else
{ System.out.println("Fail: " + ammo);
railgun.setState(needAmmo);
return "Fire order: Failure "+ result1 + "/" + round1;
}
}
Junit Test:
public void testFire() {
final Railgun railgun = new Railgun();
final int numRounds = 6;
final int x = 100;
final int y = 340;
// This fire mission should be completely successful
String actualResult = railgun.fire(new Point(x, y), numRounds);
String expectedResult = "Fire order: Success 6/6";
System.out.println("ASAS " + actualResult);
assertEquals(expectedResult, actualResult);
// This fire mission should be partially successful
actualResult = railgun.fire(new Point(x, y), numRounds);
//System.out.println(actualResult);
expectedResult = "Fire order: Partial success 4/6";
assertEquals(expectedResult, actualResult);
// This fire mission should fail
actualResult = railgun.fire(new Point(x, y), numRounds); expectedResult = "Fire order: Failure 0/6";
assertEquals(expectedResult, actualResult);
// Check state change to NeedAmmo state
assertEquals(railgun.getState().getClass(), NeedAmmoState.class);
}
Is it possible to convert the function go into the non-recursive function? Some hints or a start-up sketch would be very helpful
public static TSPSolution solve(CostMatrix _cm, TSPPoint start, TSPPoint[] points, long seed) {
TSPSolution sol = TSPSolution.randomSolution(start, points, seed, _cm);
double t = initialTemperature(sol, 1000);
int frozen = 0;
System.out.println("-- Simulated annealing started with initial temperature " + t + " --");
return go(_cm, sol, t, frozen);
}
private static TSPSolution go(CostMatrix _cm, TSPSolution solution, double t, int frozen) {
if (frozen >= 3) {
return solution;
}
i++;
TSPSolution bestSol = solution;
System.out.println(i + ": " + solution.fitness() + " " + solution.time() + " "
+ solution.penalty() + " " + t);
ArrayList<TSPSolution> nHood = solution.nHood();
int attempts = 0;
int accepted = 0;
while (!(attempts == 2 * nHood.size() || accepted == nHood.size()) && attempts < 500) {
TSPSolution sol = nHood.get(rand.nextInt(nHood.size()));
attempts++;
double deltaF = sol.fitness() - bestSol.fitness();
if (deltaF < 0 || Math.exp(-deltaF / t) > Math.random()) {
accepted++;
bestSol = sol;
nHood = sol.nHood();
}
}
frozen = accepted == 0 ? frozen + 1 : 0;
double newT = coolingSchedule(t);
return go(_cm, bestSol, newT, frozen);
}
This is an easy one, because it is tail-recursive: there is no code between the recursive call & what the function returns. Thus, you can wrap the body of go in a loop while (frozen<3), and return solution once the loop ends. And replace the recursive call with assignments to the parameters: solution=bestSol; t=newT;.
You need to thinkg about two things:
What changes on each step?
When does the algorithm end?
Ans the answer should be
bestSol (solution), newT (t), frozen (frozen)
When frozen >= 3 is true
So, the easiest way is just to enclose the whole function in something like
while (frozen < 3) {
...
...
...
frozen = accepted == 0 ? frozen + 1 : 0;
//double newT = coolingSchedule(t);
t = coolingSchedule(t);
solution = bestSol;
}
As a rule of thumb, the simplest way to make a recursive function iterative is to load the first element onto a Stack, and instead of calling the recursion, add the result to the Stack.
For instance:
public Item recursive(Item myItem)
{
if(myItem.GetExitCondition().IsMet()
{
return myItem;
}
... do stuff ...
return recursive(myItem);
}
Would become:
public Item iterative(Item myItem)
{
Stack<Item> workStack = new Stack<>();
while (!workStack.isEmpty())
{
Item workItem = workStack.pop()
if(myItem.GetExitCondition().IsMet()
{
return workItem;
}
... do stuff ...
workStack.put(workItem)
}
// No solution was found (!).
return myItem;
}
This code is untested and may (read: does) contain errors. It may not even compile, but should give you a general idea.
OK so I have this HashMap
private Map<String, Player> players = new HashMap<String, Player>();
Here is what I use to remove:
public void destroy() {
players.remove("Red");
os.println(me.getUsername() + "|1|has left|yes|chat");
}
I say Red because it's just a TEST right now. I will get the eventual correct one later. Anyways...
I use THIS to check.
if (e.getKeyCode() == KeyEvent.VK_Q) {
for (Player playert : players.values()) {
c.append("\n < "+playert.getUsername() + " > ");
}
}
When I'm all by myself.. I press Q and I get:
< Dan >
then my friend Red logs in and I Press Q... I get:
< Dan >
< Red >
then he leaves I press Q and I get:
< Dan >
< Red >
So.. how come this isn't working?
Also, here is the code that gets called in init() when a player logs in the game (starts the applet)
public void playerLogin() throws IOException {
Random roll = new Random();
int newNo = roll.nextInt(200);
// me.getUsername() = "Guest #" + roll.nextInt(110);
// String me.getUsername() = getParameter("name");
me = new Player();
me.setUsername(getParameter("name"));
me.setPlayerImage(ImageIO.read(getClass().getResource("me.gif")));
me.setX(256);
me.setY(256);
me.setMap(1);
me.setCommand("move");
players.put(me.getUsername(), me);
repaint();
System.out.println(me.getUsername() + " was added. player: " + me);
os.println(me.getUsername() + "|" + me.getX() + "|" + me.getY() + "|"
+ me.getMap() + "|" + me.getCommand());
attack = 4;
defense = 5;
gold = 542;
level = 1;
exp = 53;
}
In other words, your Applet#destroy() method is not called at the moment you expect it is called? You should use Applet#stop(). The destroy() is only called when the object in question is eligible for GC and/or when the whole browser instance is been closed (and thus not only the current page/tab). JVM may namely keep running as long as the browser instance runs.
When you hit Q... you are checking the contents of players but where is your call to destroy()? Do you explicitly call destroy() anywhere in your code?