Example #1
0
 /**
  * The playfield constructor. This will initalise the playfield with a given <code>width</code> and <code>height</code>.<br>
  * It will create a 2D <code>array</code> (matrix) of the field and then call the function <code>setupPlayfield</code>.<br>
  * After that is done it will also create a new <code>MovingSpot</code>.
  * @param width		the width of the field.
  * @param height	the height of the field.
  * @param wall		the danger level of the edges of the field.
  * @since 1.0
  */
 public Playfield(int width, int height, int wall)
 {
     this.width  = width;
     this.height = height;
     // Create a new 2D array called cells of type Cell.
     cells = new Cell[height][];
     // Set the playfield up given the width and height.
     setupPlayfield(width, height, wall);
     // Create a new MovingSpot and link it to this playfield.
     spot = new MovingSpot(this, new GreedySearch(this));
 }
Example #2
0
        public Cell getNextMove(int x, int y, int xDirection, int yDirection, int remainingX, int remainingY, MovingSpot spot)
        {
            List <Cell> CellDanger = new List <Cell>();

            //System.out.println("RemainingY: " + remainingY);
            //System.out.println("RemainingX: " + remainingX);

            if (remainingY == 0)
            {
                return(playfield.cells[x + xDirection][y]);
            }
            else if (remainingX == 0)
            {
                return(playfield.cells[x][y + yDirection]);
            }

            if (y + yDirection < spot.playfield.height && y + yDirection >= 0)
            {
                CellDanger.Insert(0, spot.playfield.cells[x][y + yDirection]);
            }
            if (x + xDirection < spot.playfield.width && x + xDirection >= 0)
            {
                CellDanger.Insert(1, spot.playfield.cells[x + xDirection][y]);
            }
            if (x + xDirection < spot.playfield.width && x + xDirection >= 0 && y + yDirection < spot.playfield.height && y + yDirection >= 0)
            {
                CellDanger.Insert(2, spot.playfield.cells[x + xDirection][y + yDirection]);
            }


            for (int j = 0; j < CellDanger.Count; j++)
            {
                if (CellDanger[(j)].hasPlayer())
                {
                    CellDanger.RemoveAt(j);
                }
            }


            Cell best = CellDanger[(0)];

            remainingY--;
            String lastSet = "Y";

            for (int k = 1; k < CellDanger.Count; k++)
            {
                //System.out.println("Best: " + best.toString());
                if (CellDanger[(k)] != null)
                {
                    if (CellDanger[(k)].isGoal())
                    {
                        return(CellDanger[(k)]);
                    }
                    if (best.getDanger() >= CellDanger[(k)].getDanger() - 1)
                    {
                        best = CellDanger[(k)];
                        switch (lastSet)
                        {
                        case "Y": remainingY++; break;

                        case "X": remainingX++; break;

                        case "XY": remainingX++; remainingY++; break;
                        }
                        switch (k)
                        {
                        case 1: remainingX--; lastSet = "X"; break;

                        case 2: remainingY--; remainingX--; lastSet = "XY"; break;
                        }
                    }
                }
            }
            return(best);
        }
Example #3
0
        public List <Cell> findPath(GoalSpot goal, MovingSpot spot)
        {
            //System.out.println("MovingSpot: " + spot.toString());
            //System.out.println("GoalSpot: " + goal.toString());

            List <Cell> path  = new List <Cell>();
            int         xGoal = goal.getX();
            int         yGoal = goal.getY();
            int         xSpot = spot.getX();
            int         ySpot = spot.getY();
            int         xDirection;
            int         yDirection;
            int         remainingX = Math.Abs(goal.getX() - spot.getX());
            int         remainingY = Math.Abs(goal.getY() - spot.getY());

            //System.out.println("RemainingY: " + remainingY);
            //System.out.println("RemainingX: " + remainingX);

            if (xGoal - xSpot == 0)
            {
                xDirection = 0;
            }
            else if (xGoal - xSpot > 0)
            {
                xDirection = 1;
            }
            else
            {
                xDirection = -1;
            }

            if (yGoal - ySpot == 0)
            {
                yDirection = 0;
            }
            else if (yGoal - ySpot > 0)
            {
                yDirection = 1;
            }
            else
            {
                yDirection = -1;
            }

            Cell nextMove = getNextMove(xSpot, ySpot, xDirection, yDirection, remainingX, remainingY, spot);

            path.Add(nextMove);
            //System.out.println(nextMove.toString());
            bool foundTarget = false;

            while (!foundTarget)
            {
                remainingX = Math.Abs(goal.getX() - nextMove.getX());
                remainingY = Math.Abs(goal.getY() - nextMove.getY());
                nextMove   = getNextMove(path[(path.Count - 1)].getX(), path[(path.Count - 1)].getY(), xDirection, yDirection, remainingX, remainingY, spot);
                path.Add(nextMove);
                if (nextMove.getX() == goal.getX() && nextMove.getY() == goal.getY())
                {
                    foundTarget = true;
                }
            }
            //System.out.println("Path Length: " + path.size());
            //for(int i =0; i<path.size(); i++) {
            //System.out.println("Step " + (i+1) + ": [" + path.get(i).getX() + ", " + path.get(i).getY() + "]");
            //}
            return(path);
        }
Example #4
0
        public Cell getNextMove(int x, int y, int xDirection, int yDirection, int remainingX, int remainingY, MovingSpot spot)
        {
            List <Cell> CellDanger = new List <Cell>();

            if (y + 1 < spot.playfield.height)
            {
                CellDanger.Add(spot.playfield.cells[x][y + 1]);
            }
            if (y - 1 >= 0)
            {
                CellDanger.Add(spot.playfield.cells[x][y - 1]);
            }
            if (x + 1 < spot.playfield.width)
            {
                CellDanger.Add(spot.playfield.cells[x + 1][y]);
            }
            if (x - 1 >= 0)
            {
                CellDanger.Add(spot.playfield.cells[x - 1][y]);
            }
            if (x - 1 >= 0 && y - 1 >= 0)
            {
                CellDanger.Add(spot.playfield.cells[x - 1][y - 1]);
            }
            if (x + 1 < spot.playfield.width && y + 1 < spot.playfield.height)
            {
                CellDanger.Add(spot.playfield.cells[x + 1][y + 1]);
            }
            if (x - 1 >= 0 && y + 1 < spot.playfield.height)
            {
                CellDanger.Add(spot.playfield.cells[x - 1][y + 1]);
            }
            if (x + 1 < spot.playfield.width && y - 1 >= 0)
            {
                CellDanger.Add(spot.playfield.cells[x + 1][y - 1]);
            }
            Random random = new Random();
            int    picked = random.Next(0, CellDanger.Count);
            Cell   next   = CellDanger[picked];

            return(next);
        }