Go Back   Java Programming Forums > Java Standard Edition Programming Help > Algorithms & Recursion


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 20-12-2009, 09:14 AM
Member
 

Join Date: Mar 2009
Posts: 48
Thanks: 3
Thanked 0 Times in 0 Posts
Koren3 is on a distinguished road
Default Game 3x3

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");
        }


    }

}
Class GamingTable
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;
    }


}
Class Solver
Java Code
public class Solver implements ISolver {

}
Interface ISolver
Java Code
interface ISolver {

}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 21-12-2009, 12:43 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,347
Thanks: 5
Thanked 285 Times in 257 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
Default Re: Game 3x3

The simplest method for performing such a search is a brute strength breadth-depth search.

The idea is simply start by trying all possible single moves with the current board configuration. If one of them solves the puzzle, then mark the puzzle as solved. If not, recursively pass each new modified board through the algorithm.

This will automatically find one of the possible minimum number of steps to solve this type of puzzle (can you see why?). It's not an efficient algorithm, but because this puzzle is so small it's sufficient to use such an algorithm (sorry, I don't know the running time for this algorithm )

A more efficient method of solving this puzzle is to apply anything you know about solving this type of puzzle (certain types of piece movements that will get the board into a configuration you want, etc.), which unfortunately for me is pretty much nill.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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

All times are GMT. The time now is 09:08 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.