top of page

Ice Puzzle

Solo Project

OVERVIEW

TOOLS

NetBeans 12.6

This is a project that I made for the algorithms module. The idea of this is that we have a maze saved as a text file and I had to create a program that will find the shortest path between start and finish

LANGUAGE

Java

MEDIA GALLERY

Screenshot 2022-10-01 at 14.13.00.png
Screenshot 2022-10-01 at 14.13.46.png

SAMPLE CODE

Projectile class

import java.util.LinkedList;

​

public class Solver {

    public static int solve(int[][] map, int startX, int startY, int finX, int finY){

        Coordinate startCor = new Coordinate(startX, startY);

        Coordinate finCor = new Coordinate(finX, finY);

        LinkedList<Coordinate> q = new LinkedList <>();

        Coordinate[][] mapMap = new Coordinate[map.length][map[0].length];

        

        q.addLast(startCor);

        mapMap[startY][startX] = startCor;

        while(q.size() != 0){

            Coordinate cur = q.pollFirst();

            System.out.println(cur);

            for (Coordinate.Direction d : Coordinate.Direction.values()){

                Coordinate next = Coordinate.move(map,mapMap,cur,d);

                System.out.println("\t"+next);

                if(next!=null){

                    q.addLast(next);

                    mapMap[next.getY()][next.getX()]=new Coordinate(cur.getX(),cur.getY());

                    if(next.getY()==finY&&next.getX()==finX){

                        Coordinate temp = cur;

                        int c = 0;

                        while(temp!=startCor){

                            c++;

                            temp = mapMap[temp.getY()][temp.getX()];

                        }

                        return c;

                    }

                }     

            }

            System.out.println();

        }

    return -1; //returns -1 if the map is not solvable

    }

}

bottom of page