import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.util.List; import java.util.ArrayList; import java.awt.*; import java.awt.image.*; import java.awt.geom.*; import javax.swing.*; public class Gutsy extends Mouse { //set up direction references private static final int EAST = 0; private static final int WEST = 1; private static final int NORTH = 2; private static final int SOUTH = 3; private int direction; //set up ints to be referenced int stepValue = 0; int routeValue = 0; int pathValue = 0; int wallNum; //set up booleans boolean reachedCenter = false; //set up arrays needed int[][] visited = new int [16] [16]; int[][] route = new int [16] [16]; int[][] homePath = new int [16][16]; int[][] paths = new int[16][16]; int[][] deadEnd = new int[16][16]; public Gutsy() { //Mouse information for the scorer super("Gutsy","Ryan","Orcimsoft"); //Preparing image for use on the maze GreenfootImage i = new GreenfootImage( "images/tux.png" ); i.scale( 20, 20 ); setImage( i ); setDirection(NORTH); } public void act() { if (route[7][7]==0) { reachedCenter = false; } if(canMove()) { move(); checkWalls(); int cellX = getX()/4; int cellY = getY()/4; visited[cellX][cellY] = visited[cellX][cellY] + 1; if (route[cellX][cellY]==0) { routeValue++; route[cellX][cellY] = routeValue; } else if ( ((visited[cellX][cellY]) > 1 )&& ( (visited[0][15]) <1 ) ) //if the cell has been visited before reaching the center { routeValue--; route[cellX][cellY] = routeValue; } } } public void move() { centerCheck(); if ((visited[7][7])>(visited[0][15])) // on the way home from the center { homeRoute(); // follow the route home } else // looking for center { if ( (visited[0][15]) == 0 ) //if the start cell has not been visited { followLeft(); //then follow the left wall } else { centerPath(); //use the path } } } public void homeRoute() { World myWorld = getWorld(); int cellX = getX()/4; int cellY = getY()/4; int x = getX(); int y = getY(); List southBrick = myWorld.getObjectsAt(x, y+2, Brick.class); List eastBrick = myWorld.getObjectsAt(x+2, y, Brick.class); List northBrick = myWorld.getObjectsAt(x, y-2, Brick.class); List westBrick = myWorld.getObjectsAt(x-2, y, Brick.class); pathValue++ ; homePath[cellX][cellY] = pathValue; if (cellY != 0) //if the cell above is not the top of the maze { if (((route[cellX][cellY-1]) == (route[cellX][cellY])-1)&& (northBrick.isEmpty()) ) { setLocation(getX(), getY() - 4); } } if (cellY != 15) { if (((route[cellX][cellY+1]) == (route[cellX][cellY])-1)&& (southBrick.isEmpty())) { setLocation(getX(), getY() + 4); } } if (cellX != 15) // if the cell to the right is not the end of the maze { if (((route[cellX+1][cellY]) == (route[cellX][cellY])-1)&& (eastBrick.isEmpty())) { setLocation(getX()+4, getY()); } } if (cellX != 0) // if the left cell is not 0 { if (((route[cellX-1][cellY]) == (route[cellX][cellY])-1)&& (westBrick.isEmpty())) { setLocation(getX()-4, getY()); } } } public void centerPath() { World myWorld = getWorld(); int cellX = getX()/4; int cellY = getY()/4; int x = getX(); int y = getY(); List southBrick = myWorld.getObjectsAt(x, y+2, Brick.class); List eastBrick = myWorld.getObjectsAt(x+2, y, Brick.class); List northBrick = myWorld.getObjectsAt(x, y-2, Brick.class); List westBrick = myWorld.getObjectsAt(x-2, y, Brick.class); if ( (cellX==0)&&(cellY==15) ) { pathValue++ ; homePath[cellX][cellY] = pathValue; } if (cellY != 0) //if the cell above is not the top of the maze { if ( ((homePath[cellX][cellY-1]) == ((homePath[cellX][cellY])-1) )) { setLocation(getX(), getY() - 4); } } if (cellY != 15) { if (((homePath[cellX][cellY+1]) == ((homePath[cellX][cellY])-1))) { setLocation(getX(), getY() + 4); } } if (cellX != 15) // if the cell to the right is not the end of the maze { if (((homePath[cellX+1][cellY]) == ((homePath[cellX][cellY])-1))) { setLocation(getX()+4, getY()); } } if (cellX != 0) // if the left cell is not 0 { if (((homePath[cellX-1][cellY]) == ((homePath[cellX][cellY])-1))) { setLocation(getX()-4, getY()); } } } public void centerCheck() { if ((visited[7][7])>(visited[0][15])) { reachedCenter = true; route[0][15] = 0; } else { reachedCenter = false; }; } public void followLeft() { World myWorld = getWorld(); int cellX = getX()/4; int cellY = getY()/4; int x = getX(); int y = getY(); List southBrick = myWorld.getObjectsAt(x, y+2, Brick.class); List eastBrick = myWorld.getObjectsAt(x+2, y, Brick.class); List northBrick = myWorld.getObjectsAt(x, y-2, Brick.class); List westBrick = myWorld.getObjectsAt(x-2, y, Brick.class); switch(direction) { case NORTH : if (paths[cellX][cellY] == 1) //if its a dead end { goBack(); //then go back to the previous cell } else { if (westBrick.isEmpty()) //if left wall empty go left { goLeft(); } // turn left else if (northBrick.isEmpty()) //if north is empty then go north { setLocation(getX(), getY() - 4); } else { goRight(); //if all else fails go right :P } } break; //end North case case EAST : if (paths[cellX][cellY] == 1) //if this is a dead end { goBack(); //go back... } else { if (northBrick.isEmpty()) //the code follows similar rules to above case. { goLeft(); } else if (eastBrick.isEmpty()) { setLocation(getX() + 4, getY()); } else { goRight(); } } break; //end east case case SOUTH : if (paths[cellX][cellY] == 1) { goBack(); } else { if (eastBrick.isEmpty()) //if you can go left then do so { goLeft(); } else if (southBrick.isEmpty()) { setLocation(getX(), getY() + 4); } else //if you can't got left or south then seems right is the option { goRight(); } } break;//end both cases case WEST : if (paths[cellX][cellY] == 1) //if this is a dead end { goBack(); } else { if (southBrick.isEmpty()) { goLeft(); } else if (westBrick.isEmpty()) { setLocation(getX() - 4, getY()); } else { goRight(); } } break;//end west case } } public void goRight() { World myWorld = getWorld(); int x = getX(); int y = getY(); switch(direction) { case SOUTH : setDirection(WEST); setLocation(getX()-4, getY() ); break; case EAST : setDirection(SOUTH); setLocation(getX(), getY() + 4); break; case NORTH : setDirection(EAST); setLocation(getX()+4, getY() ); break; case WEST : setDirection(NORTH); setLocation(getX(), getY() - 4); break; } } public void goBack() { int cellX = getX()/4; int cellY = getY()/4; switch(direction) { case NORTH : setDirection(SOUTH); setLocation(getX(), getY() + 4); break; case EAST : setDirection(WEST); setLocation(getX()-4, getY() ); break; case SOUTH : setDirection(NORTH); setLocation(getX(), getY() - 4); break; case WEST : setDirection(EAST); setLocation(getX()+4, getY() ); break; } } public void goLeft() { switch(direction) { case NORTH : setDirection(WEST); setLocation(getX()-4, getY() ); break; case EAST : setDirection(NORTH); setLocation(getX(), getY() - 4); break; case SOUTH : setDirection(EAST); setLocation(getX()+4, getY() ); break; case WEST : setDirection(SOUTH); setLocation(getX(), getY() + 4); break; } } public void checkWalls() { World myWorld = getWorld(); int x = getX(); int y = getY(); int cellX = getX()/4; int cellY = getY()/4; wallNum = 4; List southBrick = myWorld.getObjectsAt(x, y+2, Brick.class); List eastBrick = myWorld.getObjectsAt(x+2, y, Brick.class); List northBrick = myWorld.getObjectsAt(x, y-2, Brick.class); List westBrick = myWorld.getObjectsAt(x-2, y, Brick.class); if (northBrick.isEmpty()) { wallNum--; } if (eastBrick.isEmpty()) { wallNum--; } if (southBrick.isEmpty()) { wallNum--; } if (westBrick.isEmpty()) { wallNum--; } if (wallNum == 1) //only one wall can show the end of a dead end { paths[cellX][cellY] = 3; } if (wallNum == 2) //normal 2 walled path { paths[cellX][cellY] = 2; } if (wallNum == 3) //if there are 3 walls then it is a dead end { paths[cellX][cellY] = 1; if (cellX + cellY == 0) {deadEnd[cellX][cellY] = 0;} else {deadEnd[cellX][cellY] = 1;} } } public void setDirection(int direction) { this.direction = direction; switch(direction) { case SOUTH : setRotation(90); break; case EAST : setRotation(0); break; case NORTH : setRotation(270); break; case WEST : setRotation(180); break; default : break; } } public boolean canMove() { World myWorld = getWorld(); int x = getX(); int y = getY(); int cellX = getX()/4; int cellY = getY()/4; // test for outside border if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) { return false; } else if (x < 0 || y < 0) { return false; } return true; } }