Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Assignment
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Panphobia




PostPosted: Tue Jan 15, 2013 11:27 am   Post subject: Assignment

I have finished the graphics the gameplay, the grid, everything to do with the game rush hour, except the minimum number of moves to solve a grid, i currentley have this code for the bfs, could you hint me to what is wrong, i will give you all my code with the car class also
code:
package rushhourassign;

import java.util.*;
import java.io.*;

public class RushHourAssign {

    static int max = -Integer.MAX_VALUE;
    static Cars[] cars;

    public static void main(String[] args) throws FileNotFoundException {
        Scanner s = new Scanner(new File(System.getProperty("user.dir") + File.separator + "cars.txt"));
        String maze[][] = new String[8][8];
        while (s.hasNext()) {
            for (int i = 0; i < 8; ++i) {
                String[] text = s.nextLine().split(" ");
                for (int x = 0; x < 8; ++x) {
                    //puts data into 2d array
                    maze[x][i] = text[x];
                    if (isInteger(maze[x][i])) {
                        //max amount of cars
                        max = Math.max(max, Integer.parseInt(maze[x][i]));
                    }
                }
            }
            cars = new Cars[max + 1];
            //ArrayList of Integer arrays
            ArrayList<Integer[]> coordx = new ArrayList<Integer[]>();
            ArrayList<Integer[]> coordy = new ArrayList<Integer[]>();

            //checks for starting points of cars
            out:
            for (int num = 0; num < max + 1; ++num) {
                for (int y = 0; y < 8; ++y) {
                    for (int x = 0; x < 8; ++x) {
                        if (isInteger(maze[x][y])) {
                            if (Integer.parseInt(maze[x][y]) == num) {
                                //instantiating cars array and restarting the loop
                                //when found start of car
                                cars[num] = new Cars(x, y, maze);
                                coordx.add(cars[num].coordx());
                                coordy.add(cars[num].coordy());
                                continue out;
                            }
                        }
                    }
                }
            }
            //
            System.out.println(bfs(maze));
        }
    }
//method to determine whether integer or not

    public static boolean isInteger(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException nfe) {
        }
        return false;
    }

    public static String[][] slide(int carnum, char dir, String[][] maze) {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        int pos = 0;
        if (dir == 'u') {

            for (int i = 0; i < cars[carnum].coordy().length; ++i) {
                if (cars[carnum].coordy()[i] < max) {
                    max = cars[carnum].coordy()[i];
                    pos = i;
                }
            }
            if (!maze[cars[carnum].getx()][pos + 1].equals(".")) {
                return maze;
            }
            for (int i = 0; i < cars[carnum].getLength(); ++i) {

                maze[cars[carnum].getx()][pos + 1] = maze[cars[carnum].getx()][pos];
                maze[cars[carnum].getx()][pos] = ".";
                pos--;
            }
        } else if (dir == 'd') {
            for (int i = 0; i < cars[carnum].coordy().length; ++i) {
                if (cars[carnum].coordy()[i] > min) {
                    min = cars[carnum].coordy()[i];
                    pos = i;
                }
            }
            if (!maze[cars[carnum].getx()][pos - 1].equals(".")) {
                return maze;
            }
            for (int i = 0; i < cars[carnum].getLength(); ++i) {

                maze[cars[carnum].getx()][pos - 1] = maze[cars[carnum].getx()][pos];
                maze[cars[carnum].getx()][pos] = ".";
                pos--;
            }
        } else if (dir == 'r') {
            for (int i = 0; i < cars[carnum].coordx().length; ++i) {
                if (cars[carnum].coordx()[i] > min) {
                    min = cars[carnum].coordx()[i];
                    pos = i;
                }
            }
            if (!maze[pos + 1][cars[carnum].gety()].equals(".")) {
                return maze;
            }
            for (int i = 0; i < cars[carnum].getLength(); ++i) {
                maze[pos + 1][cars[carnum].gety()] = maze[pos][cars[carnum].gety()];
                maze[pos][cars[carnum].gety()] = ".";
                pos++;
            }
        } else if (dir == 'l') {
            for (int i = 0; i < cars[carnum].coordx().length; ++i) {
                if (cars[carnum].coordx()[i] < max) {
                    max = cars[carnum].coordx()[i];
                    pos = i;
                }
            }
            if (!maze[pos - 1][cars[carnum].getx()].equals(".")) {
                return maze;
            }
            for (int i = 0; i < cars[carnum].getLength(); ++i) {
                maze[pos - 1][cars[carnum].gety()] = maze[pos][cars[carnum].gety()];
                maze[pos][cars[carnum].gety()] = ".";
                pos--;
            }
        }
        return maze;
    }

    public static int bfs(String[][] maze) {
        Queue<Node> queue = new LinkedList<Node>();
        Node start = new Node(0, maze);
        queue.add(start);
        char[] up = {'u', 'd'};
        char[] left = {'l', 'r'};
        while (!maze[4][3].equals(".") && !maze[5][3].equals(".") && !maze[6][3].equals(".")) {
            Node array = queue.poll();
            int steps = array.steps;
            maze = array.maze.clone();
            if ((maze[4][3].equals(".") && maze[5][3].equals(".") && maze[6][3].equals("."))) {
                System.out.println(11);
                return steps;
            }
            for (int i = 0; i < cars.length; ++i) {
                if (isInteger(maze[cars[i].getx()][cars[i].gety()])) {
                    if (Integer.parseInt(maze[cars[i].getx()][cars[i].gety()]) % 2 == 0) {
                        for (int x = 0; x < 2; ++x) {
                            Node node = new Node(steps + 1, slide(i, left[x], maze));
                            queue.add(node);
                        }
                    } else {
                        for (int x = 0; x < 2; ++x) {
                            Node node = new Node(steps + 1, slide(i, up[x], maze));
                            queue.add(node);
                        }
                    }
                }
            }
        }
        return 0;
    }
}

class Node {

    int steps;
    String[][] maze;

    public Node(int steps, String[][] maze) {
        this.maze = maze.clone();
        this.steps = steps;
    }
}

car class
code:
package rushhourassign;

import java.util.*;

public class Cars {

    private String[][] maze = new String[8][8];
    private int x, y;
    private ArrayList<Integer> coordx = new ArrayList<Integer>(), coordy = new ArrayList<Integer>();
    private int length;
private int lol;
    public Cars(int x1, int y1, String[][] mazes) {
        x = x1;
        y = y1;
        for (int i = 0; i < 8; ++i) {
            for (int a = 0; a < 8; ++a) {
                maze[a][i] = mazes[a][i];
                if(maze[a][i].equals(maze[x][y]))length++;
            }
        }
        lol = getLength1( x,  y, maze[x][y],maze);
    }

    public int getLength() {
        return length;
        //return getLength1(x, y, maze[x][y], maze, 0);
    }

    private int getLength1(int x, int y, String number, String[][] mazes) {
        if (!mazes[x][y].equals(number)) {
            return 0;
        }
        if (updown(Integer.parseInt(number))) {
            coordy.add(y);
            coordx.add(x);
            mazes[x][y] = ".";
            return 1 + getLength1(x + 1, y, number, mazes) + getLength1(x - 1, y, number, mazes);
        } else {
            coordy.add(y);
            coordx.add(x);
            mazes[x][y] = ".";
            return 1 + getLength1(x, y + 1, number, mazes) + getLength1(x, y - 1, number, mazes);
        }
    }
    public Integer[] coordy() {
        Integer[]arr = new Integer[coordy.size()];
        coordy.toArray(arr);
        return arr;
    }

    public Integer[] coordx() {
        Integer[]arr = new Integer[coordx.size()];
        coordx.toArray(arr);
        return arr;
    }

    public boolean updown(int number) {
        if (number % 2 == 0) {
            return true;
        } else {
            return false;
        }
    }

    public int getx() {
        return x;
    }

    public int gety() {
        return y;
    }
}

grid format number mod 2 ==0 left/right else up/down
code:
* * * * * * * *
* 2 2 . . . 7 *
* 1 . . 5 . 7 *
* 1 0 0 5 . 7 A
* 1 . . 5 . . *
* 3 . . . 4 4 *
* 3 . 6 6 6 . *
* * * * * * * *
Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: Tue Jan 15, 2013 6:38 pm   Post subject: RE:Assignment

See the bfs doesnt even run, it just skips the while loop and returns 0, does anyone know why?
Tony




PostPosted: Tue Jan 15, 2013 8:39 pm   Post subject: RE:Assignment

If it doesn't even run, then it's likely that some conditional step is evaluation to something other than what you expect it to. Time to trace through the program to see exactly how control flows through it during the execution.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Panphobia




PostPosted: Tue Jan 15, 2013 10:43 pm   Post subject: RE:Assignment

finished, I never would have thought I could code something that complex, I completely changed how I slide the pieces the only thing is, is that it counts move by move not combo moves, so every square moved is nummoves++
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: