2 Replies - 3699 Views - Last Post: 23 September 2009 - 10:56 AM Rate Topic: -----

#1 MarcusM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-June 08

8 Tile Puzzle in ME

Post icon  Posted 23 September 2009 - 09:29 AM

Hey, trying create an 8 tile puzzle that will eventually solve itself. For now just trying to draw the 8 tiles (1-8) wondering if there is a loop i can implement to draw these tiles instead of manually drawing 8 tiles by co-ordinates. Thanks for the help!

Two classes Tile.java and Puzzle.java:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;

public class Tile extends Canvas {

	private int x, y, width, height;
	private String label;
	


public void Tile(int x, int y, int width, int height, String label){

	this.x = x;
	this.width = width;
	this.height = height;
	this.y = y;
	this.label = label;
	
	}

public void paint (Graphics g){
		int c = g.getColor();
		g.fillRect(x, y, width, height, label);
		g.setColor(0x000000);
		g.drawRect(x, y, width, height, label);
		g.setColor(c);
	}
}

//Here is my Puzzle class.

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Random;

class MyCanvas extends Canvas implements CommandListener{
	
		private int width, height, x, y;
		private Command cmdExit;
		private Tile[] tiles;
		private int numTiles;
		private Puzzle midlet;
		private int state;

		
	public MyCanvas(Puzzle midlet){
		
		this.midlet = midlet;
		width = getWidth();
		height = getHeight();
		numTiles = 8;
		tiles = new Tile[numTiles];

		tiles[0] = new Tile(height/2 , width/2 , width, height, "0");
		
		cmdExit = new Command("EXIT", Command.EXIT, 1);
		addCommand(cmdExit);
		setCommandListener(this);
		state = 1;
		numTiles = 1;
		
	}

	public void paint(Graphics g){
		g.setColor(0xffffff);
		g.fillRect(0, 0, width, height, "");		
		for(int i = 0; i < numTiles; i++)
			tiles[i].paint(g);
		}

	/*public void keyPressed(int keyCode){
		int action = getGameAction(keyCode);
		if(action == LEFT){
			if(numBalls > 1)
				numBalls --; }

		if(action == RIGHT){
			if(numBalls < 31)
				numBalls ++; }
		}

	public void run(){
		while(state == 1){
			for(int i = 0; i < numBalls; i++)
				balls[i].updatePosition();

			try{
				repaint();
			Thread.sleep(INTERVAL); 
			}
			catch(InterruptedException e){
			}
		}
	}*/

	public void commandAction(Command c, Displayable d){
		
		if(c == cmdExit){
	
			state = 0;

		midlet.exitMIDlet(); }
	}
}

public class Puzzle extends MIDlet{
		MyCanvas mc;
	
	public Puzzle(){
		mc = new MyCanvas(this);
		mc.repaint(); }
	
	public void startApp(){
		Display.getDisplay(this).setCurrent(mc);
		}

	public void pauseApp(){ }

	public void destroyApp(boolean unconditional) { }
	
	public void exitMIDlet(){
		destroyApp(true);
		notifyDestroyed();
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: 8 Tile Puzzle in ME

#2 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4083
  • View blog
  • Posts: 18,159
  • Joined: 24-September 08

Re: 8 Tile Puzzle in ME

Posted 23 September 2009 - 09:48 AM

//pseudocode

for (each row until max number of rows) {

for (each column until max number of colums per row) {

drawtile at xlocation;
xlocation = xlocation + tilewidth;

}

ylocation = ylocation + tileheight;

}


Was This Post Helpful? 0
  • +
  • -

#3 MarcusM  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-June 08

Re: 8 Tile Puzzle in ME

Posted 23 September 2009 - 10:56 AM

Thanks for the loop! Trying to draw them using a side variable where height and width will always be the same, but now i cannot draw them !?!?! Error in code somewhere tried several times to fix/remove lines but NOTHING. Any help is appreciated !! :D

Tile.java then Puzzle.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;

public class Tile extends Canvas {

	private int x, y, side;
	private String label;
		


public Tile(int x, int y, int side, String label){

	this.x = x;
	this.y = y;
	this.side = side;
	this.label = label;
	
	}

public void paint (Graphics g){
		int c = g.getColor();
		g.fillRect(x, y, side, side);
		g.setColor(0x00ff00);
		g.drawRect(x, y, side, side);
		g.setColor(c);
		
	}
}
		
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.Random;

class MyCanvas extends Canvas implements CommandListener{
	
		private int x, y, side;
		private Command cmdExit;
		private Tile[] tiles;
		private int numTiles;
		private Puzzle midlet;
		private int state;

		
	public MyCanvas(Puzzle midlet){
		
		this.midlet = midlet;
		this.side = side;
		numTiles = 8;
		this.x = x;
		this.y = y;

		tiles = new Tile[numTiles];

		tiles[0] = new Tile(0, 0, side, "1");
		tiles[1] = new Tile(0, 1, 1 * side, "2");
		tiles[2] = new Tile(0, 2, 2 * side, "3");
		tiles[3] = new Tile(1, 0, side, "4");
		tiles[4] = new Tile(1, 1, 1 * side, "5");
		tiles[5] = new Tile(1, 2, 2 * side, "6");
		tiles[6] = new Tile(2, 0, side, "7");
		tiles[7] = new Tile(2, 1, 1 * side, "8");
		
		cmdExit = new Command("EXIT", Command.EXIT, 1);
		addCommand(cmdExit);
		setCommandListener(this);
		state = 1;
		numTiles = 1;
		
	}

	public void paint(Graphics g){
		g.setColor(0xffffff);
		g.fillRect(0, 0, side, side);	
		for(int i = 0; i < numTiles; i++)
			tiles[i].paint(g);
		}

	/*public void keyPressed(int keyCode){
		int action = getGameAction(keyCode);
		if(action == LEFT){
			if(numBalls > 1)
				numBalls --; }

		if(action == RIGHT){
			if(numBalls < 31)
				numBalls ++; }
		}

	public void run(){
		while(state == 1){
			for(int i = 0; i < numBalls; i++)
				balls[i].updatePosition();

			try{
				repaint();
			Thread.sleep(INTERVAL); 
			}
			catch(InterruptedException e){
			}
		}
	}*/

	public void commandAction(Command c, Displayable d){
		
		if(c == cmdExit){
	
			state = 0;

		midlet.exitMIDlet(); }
	}
}

public class Puzzle extends MIDlet{
		MyCanvas mc;
	
	public Puzzle(){
		mc = new MyCanvas(this);
		mc.repaint(); }
	
	public void startApp(){
		Display.getDisplay(this).setCurrent(mc);
		}

	public void pauseApp(){ }

	public void destroyApp(boolean unconditional) { }
	
	public void exitMIDlet(){
		destroyApp(true);
		notifyDestroyed();
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1