Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- javac -cp "*" killianPacWorld.java
- jar cf0 killianPacWorld.jar killianPacWorld.class images
- javac -cp "*" killianPacMind.java
- jar cf0 killianPacMind.jar killianPacMind.class
- java -cp "*" org.w2mind.toolkit.Main -mind killianPacMind -world killianPacWorld -g
- a Mind for SkeletonWorld
- Killian Mills
- 11368701
- */
- import java.util.*;
- import java.awt.Point;
- import org.w2mind.net.*;
- public class killianPacMind implements Mind{
- //====== Mind must respond to these methods: ===========================================================
- // newrun(), endrun()
- // getaction()
- //======================================================================================================
- public void newrun() throws RunError{
- }
- public void endrun() throws RunError{
- }
- public Action getaction ( State state ){
- // parse state:
- String s = state.toString();
- String[] x = s.split(","); // parsed into x[0], x[1], ...
- Point pacmanPos = new Point();
- Point bluePos = new Point();
- Point redPos = new Point();
- Point orangePos = new Point();
- Point pinkPos = new Point();
- int[][] mindMaze = killianPacWorld.maze;
- // Take the current positions of the ghosts and pacman from the world
- pacmanPos.x = Integer.parseInt ( x[0] ); //pacman
- pacmanPos.y = Integer.parseInt ( x[1] );
- bluePos.x = Integer.parseInt ( x[2] ); //blue ghost
- bluePos.y = Integer.parseInt ( x[3] );
- redPos.x = Integer.parseInt ( x[4] ); //red ghost
- redPos.y = Integer.parseInt ( x[5] );
- orangePos.x = Integer.parseInt ( x[6] ); //orange ghost
- orangePos.y = Integer.parseInt ( x[7] );
- pinkPos.x = Integer.parseInt ( x[8] ); //pink ghost
- pinkPos.y = Integer.parseInt ( x[9] );
- // Generate non-random action.
- // This ignores wraparound.
- // You could easily make a better Mind that uses wraparound.
- // pacman's current movement
- int movement;
- // distance of ghosts from pacman
- //BLUE
- int blueDistanceX = pacmanPos.x - bluePos.x;
- int blueDistanceY = pacmanPos.y - bluePos.y;
- int blueDistance = Math.abs(blueDistanceX) + Math.abs(blueDistanceY);
- //RED
- int redDistanceX = pacmanPos.x - redPos.x;
- int redDistanceY = pacmanPos.y - redPos.y;
- int redDistance = Math.abs(redDistanceX) + Math.abs(redDistanceY);
- //ORANGE
- int orangeDistanceX = pacmanPos.x - orangePos.x;
- int orangeDistanceY = pacmanPos.y - orangePos.y;
- int orangeDistance = Math.abs(orangeDistanceX) + Math.abs(orangeDistanceY);
- //PINK
- int pinkDistanceX = pacmanPos.x - pinkPos.x;
- int pinkDistanceY = pacmanPos.y - pinkPos.y;
- int pinkDistance = Math.abs(pinkDistanceX) + Math.abs(pinkDistanceY);
- // selects the closest ghost
- Point closestGhost;
- if( blueDistance < redDistance && blueDistance < orangeDistance && blueDistance < pinkDistance){
- closestGhost = bluePos;
- }
- else if( redDistance < blueDistance && redDistance < orangeDistance && redDistance < pinkDistance){
- closestGhost = redPos;
- }
- else if( orangeDistance < blueDistance && orangeDistance < redDistance && orangeDistance < pinkDistance){
- closestGhost = orangePos;
- }
- else closestGhost = pinkPos;
- //MIND LOGIC
- // RIGHT PRIORITY
- if ( closestGhost.x < pacmanPos.x){
- //UP if there is a wall to the right
- if( mindMaze[pacmanPos.y][pacmanPos.x+1] == 0 && mindMaze[pacmanPos.y-1][pacmanPos.x] == 1)
- movement = killianPacWorld.ACTION_UP;
- //DOWN if there is a if wall to the right and up
- else if( mindMaze[pacmanPos.y][pacmanPos.x+1] ==0 && mindMaze[pacmanPos.y+1][pacmanPos.x] == 1)
- movement = killianPacWorld.ACTION_DOWN;
- //RIGHT
- else
- movement = killianPacWorld.ACTION_RIGHT;
- }
- // LEFT PRIORITY
- else if (closestGhost.x > pacmanPos.x){
- //UP if there is a wall to the left
- if( mindMaze[pacmanPos.y][pacmanPos.x-1] ==0 && mindMaze[pacmanPos.y-1][pacmanPos.x] == 1)
- movement = killianPacWorld.ACTION_UP;
- //DOWN if wall to the left and up
- else if(mindMaze[pacmanPos.y][pacmanPos.x-1]== 0 && mindMaze[pacmanPos.y+1][pacmanPos.x]== 1)
- movement = killianPacWorld.ACTION_DOWN;
- //LEFT
- else
- movement = killianPacWorld.ACTION_LEFT;
- }
- // DOWN PRIORITY
- else if(closestGhost.y < pacmanPos.y){
- //RIGHT if there is a wall to the bottom
- if( mindMaze[pacmanPos.y+1][pacmanPos.x] ==0 && mindMaze[pacmanPos.y][pacmanPos.x+1] ==1)
- movement = killianPacWorld.ACTION_RIGHT;
- //LEFT if there is a wall to the bottom and right
- else if( mindMaze[pacmanPos.y+1][pacmanPos.x] ==0 && mindMaze[pacmanPos.y][pacmanPos.x-1] ==1)
- movement = killianPacWorld.ACTION_LEFT;
- //DOWN
- else
- movement = killianPacWorld.ACTION_DOWN;
- }
- // UP PRIORITY
- else{
- //RIGHT if there is a wall to the top
- if( mindMaze[pacmanPos.y-1][pacmanPos.x] ==0 && mindMaze[pacmanPos.x+1][pacmanPos.y] ==1)
- movement = killianPacWorld.ACTION_RIGHT;
- //LEFT if there is a wall to the top and right
- else if( mindMaze[pacmanPos.y-1][pacmanPos.x] ==0 && mindMaze[pacmanPos.x-1][pacmanPos.y] ==1)
- movement = killianPacWorld.ACTION_LEFT;
- //UP
- else
- movement = killianPacWorld.ACTION_UP;
- }
- String a = String.format ( "%d", movement );
- return new Action ( a );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement