Advertisement
KillianMills

killianPacMind.java

Jul 21st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. /*
  2. javac -cp "*" killianPacWorld.java
  3. jar cf0 killianPacWorld.jar killianPacWorld.class images
  4.  
  5. javac -cp "*" killianPacMind.java
  6. jar cf0 killianPacMind.jar killianPacMind.class
  7.  
  8. java -cp "*" org.w2mind.toolkit.Main -mind killianPacMind -world killianPacWorld -g
  9.  
  10. a Mind for SkeletonWorld
  11.  
  12. Killian Mills
  13. 11368701
  14. */
  15.  
  16. import java.util.*;
  17. import java.awt.Point;
  18. import org.w2mind.net.*;
  19.  
  20.  
  21. public class killianPacMind implements Mind{
  22.          
  23.  
  24.     //====== Mind must respond to these methods: ===========================================================
  25.     //  newrun(), endrun()
  26.     //  getaction()
  27.     //======================================================================================================
  28.  
  29.  
  30.  
  31.     public void newrun()  throws RunError{
  32.      
  33.     }
  34.  
  35.  
  36.     public void endrun()  throws RunError{
  37.      
  38.     }
  39.          
  40.  
  41.     public Action getaction ( State state ){
  42.    
  43.         // parse state:
  44.         String s    = state.toString();      
  45.         String[] x  = s.split(",");         // parsed into x[0], x[1], ...  
  46.        
  47.         Point pacmanPos     = new Point();
  48.         Point bluePos       = new Point();
  49.         Point redPos        = new Point();
  50.         Point orangePos     = new Point();
  51.         Point pinkPos       = new Point();
  52.        
  53.         int[][] mindMaze = killianPacWorld.maze;
  54.      
  55.         // Take the current positions of the ghosts and pacman from the world
  56.         pacmanPos.x = Integer.parseInt  ( x[0] );   //pacman
  57.         pacmanPos.y = Integer.parseInt  ( x[1] );
  58.        
  59.         bluePos.x   = Integer.parseInt  ( x[2] );   //blue ghost
  60.         bluePos.y   = Integer.parseInt  ( x[3] );
  61.        
  62.         redPos.x    = Integer.parseInt  ( x[4] );   //red ghost
  63.         redPos.y    = Integer.parseInt  ( x[5] );
  64.        
  65.         orangePos.x = Integer.parseInt  ( x[6] );   //orange ghost
  66.         orangePos.y = Integer.parseInt  ( x[7] );
  67.        
  68.         pinkPos.x   = Integer.parseInt  ( x[8] );   //pink ghost
  69.         pinkPos.y   = Integer.parseInt  ( x[9] );
  70.  
  71.         // Generate non-random action.
  72.         // This ignores wraparound.
  73.         // You could easily make a better Mind that uses wraparound.
  74.        
  75.         // pacman's current movement
  76.         int movement;
  77.        
  78.         // distance of ghosts from pacman
  79.         //BLUE
  80.         int blueDistanceX = pacmanPos.x - bluePos.x;
  81.         int blueDistanceY = pacmanPos.y - bluePos.y;
  82.         int blueDistance = Math.abs(blueDistanceX) + Math.abs(blueDistanceY);
  83.        
  84.         //RED
  85.         int redDistanceX = pacmanPos.x - redPos.x;
  86.         int redDistanceY = pacmanPos.y - redPos.y;
  87.         int redDistance = Math.abs(redDistanceX) + Math.abs(redDistanceY);
  88.        
  89.         //ORANGE
  90.         int orangeDistanceX = pacmanPos.x - orangePos.x;
  91.         int orangeDistanceY = pacmanPos.y - orangePos.y;
  92.         int orangeDistance = Math.abs(orangeDistanceX) + Math.abs(orangeDistanceY);
  93.          
  94.          //PINK
  95.         int pinkDistanceX = pacmanPos.x - pinkPos.x;
  96.         int pinkDistanceY = pacmanPos.y - pinkPos.y;
  97.         int pinkDistance = Math.abs(pinkDistanceX) + Math.abs(pinkDistanceY);
  98.        
  99.         // selects the closest ghost
  100.         Point closestGhost;
  101.        
  102.         if( blueDistance < redDistance && blueDistance < orangeDistance && blueDistance < pinkDistance){
  103.             closestGhost = bluePos;
  104.         }
  105.        
  106.         else if( redDistance < blueDistance && redDistance < orangeDistance && redDistance < pinkDistance){
  107.             closestGhost = redPos;
  108.         }
  109.        
  110.         else if( orangeDistance < blueDistance && orangeDistance < redDistance && orangeDistance < pinkDistance){
  111.             closestGhost = orangePos;
  112.         }
  113.        
  114.         else closestGhost = pinkPos;
  115.  
  116.         //MIND LOGIC
  117.        
  118.         // RIGHT PRIORITY
  119.         if ( closestGhost.x < pacmanPos.x){
  120.        
  121.             //UP if there is a wall to the right
  122.             if( mindMaze[pacmanPos.y][pacmanPos.x+1] == 0 && mindMaze[pacmanPos.y-1][pacmanPos.x] == 1)
  123.                 movement = killianPacWorld.ACTION_UP;  
  124.            
  125.             //DOWN if there is a if wall to the right and up
  126.             else if( mindMaze[pacmanPos.y][pacmanPos.x+1] ==0 && mindMaze[pacmanPos.y+1][pacmanPos.x] == 1)
  127.                 movement = killianPacWorld.ACTION_DOWN;
  128.            
  129.             //RIGHT
  130.             else
  131.                 movement = killianPacWorld.ACTION_RIGHT;       
  132.         }  
  133.        
  134.         // LEFT PRIORITY
  135.         else if (closestGhost.x > pacmanPos.x){
  136.        
  137.             //UP if there is a wall to the left
  138.             if( mindMaze[pacmanPos.y][pacmanPos.x-1] ==0 && mindMaze[pacmanPos.y-1][pacmanPos.x] == 1)
  139.                 movement = killianPacWorld.ACTION_UP;
  140.            
  141.             //DOWN if wall to the left and up
  142.             else if(mindMaze[pacmanPos.y][pacmanPos.x-1]== 0 && mindMaze[pacmanPos.y+1][pacmanPos.x]== 1)
  143.                 movement = killianPacWorld.ACTION_DOWN;
  144.            
  145.             //LEFT
  146.             else
  147.                 movement = killianPacWorld.ACTION_LEFT;
  148.         }
  149.        
  150.         // DOWN PRIORITY
  151.         else if(closestGhost.y < pacmanPos.y){
  152.        
  153.             //RIGHT if there is a wall to the bottom
  154.             if( mindMaze[pacmanPos.y+1][pacmanPos.x] ==0 &&  mindMaze[pacmanPos.y][pacmanPos.x+1] ==1)
  155.                 movement = killianPacWorld.ACTION_RIGHT;
  156.            
  157.             //LEFT if there is a wall to the bottom and right
  158.             else if( mindMaze[pacmanPos.y+1][pacmanPos.x] ==0  && mindMaze[pacmanPos.y][pacmanPos.x-1] ==1)
  159.                 movement = killianPacWorld.ACTION_LEFT;
  160.            
  161.             //DOWN
  162.             else
  163.                 movement = killianPacWorld.ACTION_DOWN;
  164.         }
  165.        
  166.         // UP PRIORITY
  167.         else{
  168.        
  169.             //RIGHT if there is a wall to the top
  170.             if( mindMaze[pacmanPos.y-1][pacmanPos.x] ==0 && mindMaze[pacmanPos.x+1][pacmanPos.y] ==1)
  171.                 movement = killianPacWorld.ACTION_RIGHT;
  172.            
  173.             //LEFT if there is a wall to the top and right
  174.             else if( mindMaze[pacmanPos.y-1][pacmanPos.x] ==0 && mindMaze[pacmanPos.x-1][pacmanPos.y] ==1)
  175.                 movement = killianPacWorld.ACTION_LEFT;
  176.            
  177.             //UP
  178.             else
  179.                 movement = killianPacWorld.ACTION_UP;
  180.         }
  181.        
  182.         String a = String.format ( "%d", movement );
  183.  
  184.         return new Action ( a );         
  185.     }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement