Autosolve

L

lyndamccrum

Hi there can anyone help me,
I am writing an applet and need an autosolve button to work.

I can get my frogs to change places, but I need a time delay so each
frogs move one at a time to show the moves.


Here is my autosolve method.

public void solveGame () {

kermit1.x = 185;
kermit1.y = 125;
kermit2.x = 185;
kermit2.y = 230;
kermit2.x = 85;
kermit2.y = 25;
kermit4.x = 85;
kermit4.y = 125;
kermit4.x = 285;
kermit4.y = 25;
kermit4.x = 185;
kermit4.y = 230;
kermit3.x = 185;
kermit3.y = 25;
kermit3.x = 285;
kermit3.y = 230;
kermit3.x = 85;
kermit3.y = 125;
kermit3.x = 285;
kermit3.y = 25;
kermit1.x = 85;
kermit1.y = 230;
kermit1.x = 185;
kermit1.y = 25;
kermit1.x = 285;
kermit1.y = 230;
kermit2.x = 185;
kermit2.y = 125;
kermit2.x = 85;
kermit2.y = 230;
kermit4.x = 85;
kermit4.y = 25;

repaint();

}

Each of these moves has to be one after the other so u can see them
moving


HELP!!!!!111
 
P

Peter MacMillan

Hi there can anyone help me,
I am writing an applet and need an autosolve button to work.

I can get my frogs to change places, but I need a time delay so each
frogs move one at a time to show the moves.

Each of these moves has to be one after the other so u can see them
moving

For the code you have now, you want to call repaint after each move
(otherwise it won't actually get drawn). You could then add a call to
Thread.sleep to specify a pause between moves.

For a more general solution, albet slightly more complicated, you could
have a 2d array of moves. The first dimension would be time (with 0
being now and +ve indices being subsequent moves in the future). The
second dimension would be the frog # and the value would be an x/y point
(you could either create/use a point class or just add another
dimension). Then all you would have to do is loop over the time index,
repainting and pausing after each iteration.

Hope that makes sense.
 
L

lyndamccrum

i'm new to this stuff can you show me how it would work within my
method

Sorry for buggin you
 
L

lyndamccrum

i'm new to this stuff can you show me how it would work within my
method

Sorry for buggin you
 
P

Peter MacMillan

i'm new to this stuff can you show me how it would work within my
method

Sorry for buggin you

public void solveGame () {

kermit1.x = 185;
kermit1.y = 125;

repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException err) {}

kermit2.x = 185;
kermit2.y = 230;

repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException err) {}

kermit2.x = 85;
kermit2.y = 25;

and so on... As I said in my first reply, this isn't the best way to do
it, but it should get you started.


Notice how you repaint and then sleep before you continue. Don't panic
about the try/catch stuff. It's just an exception thrown by Thread.sleep
if the thread is interrupted while it's sleeping.
 
L

lyndamccrum

would i have to start the thread somewhere else before i could pause
it,

i dont understand how the code you gave me never worked
 
P

Peter MacMillan

would i have to start the thread somewhere else before i could pause
it,

i dont understand how the code you gave me never worked

Thread.sleep will cause the current thread to sleep. You always have at
least one thread. If you just put "Thread.sleep(1000);" in an otherwise
empty main, the program will run and wait for a second and then quit.

Anyway, here's a quick and dirty example:

//
// MovementDemo.java
//

import java.awt.Color;
import java.awt.Graphics;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class MovementDemo extends JFrame implements Runnable {

private static final long serialVersionUID = 1L;

public static class Frog {
public int x;
public int y;
public Color c;
public Frog(int x, int y, Color c) {
this.x = x;
this.y = y;
this.c = c;
}
public void draw(Graphics g) {
g.setColor(c);
g.fillOval(x, y, 25, 25);
}
}

public MovementDemo(int width, int height) {
super();
this.setSize(width, height);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private int timeIndex = 0;

private Frog[] frogs = new Frog[] {
new Frog(50, 50, Color.RED),
new Frog(50, 100, Color.GREEN),
new Frog(100, 50, Color.BLUE)
};

@Override
public void paint(Graphics g) {
g.setColor(Color.GRAY);
g.fillRect(0,0, getWidth(), getHeight());
g.setColor(Color.WHITE);
for (int i = 0; i < frogs.length; ++i) {
frogs.draw(g);
}
}

private void moveFrogs() {
switch(timeIndex) {
case 0:
break;
case 1:
frogs[0].x = 20;
frogs[0].y = 20;
break;
case 2:
frogs[1].x = 40;
frogs[1].y = 20;
break;
case 3:
frogs[2].x = 60;
frogs[2].y = 20;
break;
}
}

public static void main(String[] args) {
try {
SwingUtilities.invokeAndWait(
new MovementDemo(300,300));
} catch (InterruptedException e) {
} catch (InvocationTargetException e) {
}
}

public synchronized void run() {
this.setVisible(true);
for (int i = 0; i < 4; ++i) {
System.out.println(timeIndex);
moveFrogs();
timeIndex++;
update(getGraphics());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}

}
//---
 
L

lyndamccrum

cheers

but you have totally lost me on that one, at least I knew what the
other one was doing

this is way above me
 
P

Peter MacMillan

cheers

but you have totally lost me on that one, at least I knew what the
other one was doing

this is way above me

moveFrogs();
timeIndex++;
update(getGraphics());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}

is practically identical to

kermit2.x = 185;
kermit2.y = 230;

repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException err) {}


The only differnece is that I have a method (moveFrogs) that changes the
position. So if that (bottom) code doesn't work, you've got a problem
elsewhere (maybe in repaint?).
 
L

lyndamccrum

this is my program here, its not v good as i am just a beginner with
java

import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class Froggame extends Applet implements MouseListener,
MouseMotionListener {

Image greenkermit;
Image pinkkermit;
Image toadstool;
Image lilypad;
private Point kermit1;
private Point kermit2;
private Point kermit3;
private Point kermit4;
private Point mouse;
private int select;
private Image dbImage;
private Graphics dbg;
int my;
int mx;
private boolean layOut = false;
private Button restart;
private Button solve;
private Font font;
private Font font1;
private int winner;
private int[] frogs = {2, 0, 2, 0, 0, 1, 1};


public void init() {

this.addMouseMotionListener(this);
this.addMouseListener(this);
this.setLayout(null);
restart = new Button ("Reset Frogs");
add(restart);
solve = new Button ("Game autosolve");
add(solve);
greenkermit = getImage(getCodeBase(), "kermitfeet.png");
pinkkermit = getImage(getCodeBase(), "kermitfeet1.png");
toadstool = getImage(getCodeBase(), "toadstool.png");
lilypad = getImage(getCodeBase(), "lilypad.png");
font = new Font ("Courier", Font.BOLD, 18);
font1 = new Font ("Courier", Font.ITALIC, 12);
select = 0;
kermit1 = new Point(85,25);
kermit2 = new Point(285,25);
kermit3 = new Point(85,230);
kermit4 = new Point(285, 230);
mouse = new Point();

}

//double buffer that will write image offscreen then back on, stops
flickering
http://teachers.henrico.k12.va.us/deeprun/lawson_g/oracle2/buffering.htm
public void update (Graphics g) {

if (dbImage == null) {

dbImage = createImage (this.getSize().width,
this.getSize().height);
dbg = dbImage.getGraphics ();

}

//clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

//draw elements in background
dbg.setColor (getForeground());
paint (dbg);

// draw image on the screen
g.drawImage (dbImage, 0, 0, this);

}

public void paint(Graphics g) {

g.setFont(font);
g.drawString("Four Frogs Game", 125, 20);
g.drawImage(lilypad, 80, 40, 250, 250, this);
g.setFont(font1);
g.setColor(Color.red);
g.drawString("Use your mouse to drag a frog to a toadstool", 70,
320);
g.setColor(Color.black);
g.drawLine(100, 70, 200, 270);
g.drawLine(200, 270, 300, 70);
g.drawLine(300, 70, 100, 170);
g.drawLine(100, 170, 300, 270);
g.drawLine(300, 270, 200, 70);
g.drawLine(200, 70, 100, 270);
g.drawLine(100, 270, 300, 170);
g.drawLine(300, 170, 100, 70);
g.setFont(font);
g.setColor(Color.white);
g.drawString("1", 95, 95);
g.drawString("2", 195, 95);
g.drawString("3", 295, 95);
g.drawString("4", 95, 195);
g.drawString("5", 295, 195);
g.drawString("6", 95, 300);
g.drawString("7", 195, 300);
g.drawString("8", 295, 300);
g.drawImage(toadstool, 85, 45, 35, 35, this);
g.drawImage(toadstool, 185, 45, 35, 35, this);
g.drawImage(toadstool, 285, 45, 35, 35, this);
g.drawImage(toadstool, 85, 145, 35, 35, this);
g.drawImage(toadstool, 285, 145, 35, 35, this);
g.drawImage(toadstool, 85, 250, 35, 35, this);
g.drawImage(toadstool, 185, 250, 35, 35, this);
g.drawImage(toadstool, 285, 250, 35, 35, this);
g.drawImage(greenkermit, kermit1.x, kermit1.y, 35, 35, this);
g.drawImage(greenkermit, kermit2.x, kermit2.y, 35, 35, this);
g.drawImage(pinkkermit, kermit3.x, kermit3.y, 35, 35, this);
g.drawImage(pinkkermit, kermit4.x, kermit4.y, 35, 35, this);

if (winner ==1) {

g.drawString("Well done you have solved the puzzle", 100, 330);

}

if(!this.layOut) {

this.restart.setLocation(140, 350);
this.restart.setSize(90, 20);
this.solve.setLocation(240, 350);
this.solve.setSize(90, 20);
this.layOut = true;
}

}

public boolean action(Event e, Object arg) {

if (e.target == restart)

resetGame();

if (e.target == solve) {

solveGame();
}

return true;
}

public void resetGame() {

kermit1.x = 85;
kermit1.y = 25;
kermit2.x = 285;
kermit2.y = 25;
kermit3.x = 85;
kermit3.y = 230;
kermit4.x = 285;
kermit4.y = 230;

repaint();
}

public void solveGame () {


kermit1.x = 185;
kermit1.y = 125;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit2.x = 185;
kermit2.y = 230;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit2.x = 85;
kermit2.y = 25;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit4.x = 85;
kermit4.y = 125;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit4.x = 285;
kermit4.y = 25;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit4.x = 185;
kermit4.y = 230;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit3.x = 185;
kermit3.y = 25;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}
repaint();

kermit3.x = 285;
kermit3.y = 230;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit3.x = 85;
kermit3.y = 125;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit3.x = 285;
kermit3.y = 25;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit1.x = 85;
kermit1.y = 230;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit1.x = 185;
kermit1.y = 25;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit1.x = 285;
kermit1.y = 230;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit2.x = 185;
kermit2.y = 125;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit2.x = 85;
kermit2.y = 230;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

kermit4.x = 85;
kermit4.y = 25;
repaint();

try {
Thread.sleep(1000);
}
catch(InterruptedException err) {}

}


public void mouseDragged(MouseEvent e) {

mouse = e.getPoint();

if (select == 1) kermit1 = mouse;
if (select == 2) kermit2 = mouse;
if (select == 3) kermit3 = mouse;
if (select == 4) kermit4 = mouse;


repaint();

}


// required for the interface
public void mouseMoved(MouseEvent e) {}


//select a counter using the mouse
public void mousePressed(MouseEvent e) {

mx = e.getX();
my = e.getY();

if (kermit1.x < mx && mx < kermit1.x + 30 &&
kermit1.y < my && my < kermit1.y + 30) {

select = 1;
}
else
if (kermit2.x < mx && mx < kermit2.x + 30 &&
kermit2.y < my && my < kermit2.y + 30) {

select = 2;
}
else
if (kermit3.x < mx && mx < kermit3.x + 30 &&
kermit3.y < my && my < kermit3.y + 30) {

select = 3;
}
else
if (kermit4.x < mx && mx < kermit4.x + 30 &&
kermit4.y < my && my < kermit4.y + 30) {

select = 4;

}

}


// required for the interface
public void mouseClicked(MouseEvent event){
}

public void mouseReleased(MouseEvent e){
}

public void mouseEntered(MouseEvent event){
}

public void mouseExited(MouseEvent event){
}

}
 
P

Peter MacMillan

this is my program here, its not v good as i am just a beginner with

Replace your solveGame() method with the following:

This creates a new thread that runs in the background to update the
positions.

//---
public void solveGame() {

new Thread(new Runnable() {
public void pause() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.err.println("Interrupted");
}
}
public void run() {

kermit1.x = 185;
kermit1.y = 125;
repaint();
pause();

/*
okay, to avoid repetition, I've cut out a lot of the moves. But the
pattern should be obvious:

set x; set y; repaint(); pause();
*/

kermit4.x = 85;
kermit4.y = 25;
repaint();
pause();

}}).start();

}
//---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top