|
||
|
|||
|
Hallo, I am doing GamingTable size 3x3(look at the picture)
. And i need generate some solution but i don´t know how can i do this. Target is find the best algorithm for solution.My code: Main class Java Code
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
GamingTable g1 = new GamingTable(3);
System.out.println(g1);
g1.move(GamingTable.Direction.UP);
System.out.println("Done");
System.out.println(g1);
} catch (NotPossibleMoveException ex) {
System.out.println("Not done");
}
}
}
Java Code
public class GamingTable {
private short data [][];
private int positionX;
private int positionY;
private final int EMPTY = -1;
public enum Direction {UP, DOWN, LEFT, RIGHT}
public GamingTable(int size)
{
data = new short[size][size];
short counter = 1;
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
data[x][y]=counter++;
}
}
positionX = size - 1;
positionY = size - 1;
data[positionX][positionY] = EMPTY;
}
public GamingTable()
{
this(4);
}
public GamingTable(GamingTable g)
{
this.positionX = g.positionX;
this.positionY = g.positionY;
this.data= new short[g.data.length][g.data.length];
for (int i = 0; i < data.length; i++)
{
System.arraycopy(g.data[i],0,this.data[i],0,data[i].length);
}
}
public GamingTable(short[][] data, int startX, int startY) throws InvalidTableConfigurationException
{
if(data.length < 1 || data.length != data[0].length || data[startX][startY] != EMPTY)
throw new InvalidTableConfigurationException();
this.positionX = startX;
this.positionY = startY;
this.data = new short[data.length][data.length];
for (int y = 0; y < data.length; y++)
{
for (int x = 0; x < data.length; x++)
{
this.data[x][y] = data[y][x];
}
}
}
@Override
public String toString()
{
String s = "";
for (int y = 0; y < data.length; y++)
{
for (int x = 0; x < data.length; x++)
{
s += data[x][y];
}
s +="\n";
}
return s;
}
public int fitness()
{
int fitness = 0;
short counter = 1;
for (int y = 0; y < data.length; y++)
{
for (int x = 0; x < data.length; x++)
{
if(data[x][y] == counter)
{
fitness ++;
}
counter ++;
}
}
return fitness;
}
public boolean isSolution()
{
return fitness() == data.length * data.length -1;
}
public void move(Direction dir) throws NotPossibleMoveException
{
switch(dir)
{
case UP:
if(positionY <= 0) throw new NotPossibleMoveException();
swap(positionX, positionY, positionX, positionY-1);
positionY -= 1;
break;
case DOWN:
if(positionY >= data.length-1) throw new NotPossibleMoveException();
swap(positionX, positionY, positionX, positionY+1);
positionY += 1;
break;
case LEFT:
if(positionX <= 0) throw new NotPossibleMoveException();
swap(positionX, positionY, positionX-1, positionY);
positionX -= 1;
break;
case RIGHT:
if(positionX >= data.length-1) throw new NotPossibleMoveException();
swap(positionX, positionY, positionX+1, positionY);
positionX += 1;
break;
}
}
private void swap(int x1, int y1, int x2, int y2)
{
short tmp = data[x1][y1];
data[x1][y1] = data [x2][y2];
data[x2][y2] = tmp;
}
}
Java Code
public class Solver implements ISolver {
}
Java Code
interface ISolver {
}
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help!how to get a graphical display of this game | makarov | What's Wrong With My Code? | 1 | 17-12-2009 03:15 PM |
| Breakout Game | Ceasar | What's Wrong With My Code? | 1 | 09-10-2009 05:30 AM |
| invisible box game | new2java | Loops & Control Statements | 1 | 27-09-2009 05:46 PM |
| help with an RPG game | MooncakeZ | Paid Java Projects | 7 | 18-09-2009 02:41 AM |
| [SOLVED] The bug in the Pong game | phoenix | What's Wrong With My Code? | 11 | 14-07-2009 06:19 PM |
|
100 most searched terms
Search Cloud
|
| actionlistener actionlistener in java addactionlistener addactionlister arraylist value into hash buffered read two integer value in java cannot find symbol method create an abstract class called shape with abstract methods+java double to integer in java double to integer java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread main java.lang.outofmemoryerror java heap space in eclipse format double java get files from folder java by threads get mouse position java how to convert list to map in java how to format double in java how to format doubles in java how to make a calculator in jframe using jcreator http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html iphone java java actionlistener java cos java deallocate java double format java double to int java format double java forum java forums java get mouse position java heap size exception java jbutton java nextline() java program to find dimensions of a room java programming codes using astirisks java programming forums java.lang.outofmemoryerror: java heap space java.lang.reflect.invocationtargetexception jbutton actionlistener jbutton with key enter jtable questions in java jtext bold jtextarea font jxl.read.biff.biffexception: unable to recognize ole stream kilowatts java program mean value decimal double java oops java assignments smack api messagelistener two dimensional arraylist in java |