Advertisement
KillianMills

killianPacWorld.java

Jul 21st, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.99 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. "Pacman" world - with images
  11.  
  12. Killian Mills
  13. 11368701
  14. */
  15.  
  16. import java.awt.image.*;
  17. import java.io.*;
  18. import java.util.*;
  19. import javax.imageio.*;
  20. import java.awt.Point;
  21. import org.w2mind.net.*;
  22.  
  23. // See SkeletonWorld.java for background and extra comments.
  24.  
  25. // Note: This code has been stripped down to give you less to read.
  26. // The aim of this code was not a perfect Java program, but to give you as little as possible to read.
  27.  
  28.  
  29. public class killianPacWorld extends AbstractWorld{
  30.  
  31.     //public  static final      int GRID_SIZE = 10; //2-dimensional grid
  32.    
  33.     public static  final int ROWS       = 31;
  34.     public static  final int COLUMNS    = 28;
  35.    
  36.     public static int[][] maze={   
  37.                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  38.                     {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
  39.                     {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  40.                     {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  41.                     {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  42.                     {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
  43.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  44.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  45.                     {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
  46.                     {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  47.                     {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  48.                     {0,1,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0},
  49.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  50.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  51.                     {0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0},
  52.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  53.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  54.                     {0,1,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0},
  55.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  56.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  57.                     {0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
  58.                     {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  59.                     {0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
  60.                     {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
  61.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  62.                     {0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
  63.                     {0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
  64.                     {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
  65.                     {0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
  66.                     {0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
  67.                     {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  68.                 };
  69.      
  70.     //positions of pacman and ghosts, point for x and y coordinates
  71.     Point pacmanPos, bluePos, redPos, orangePos, pinkPos;
  72.    
  73.     //holds the last movement of pacman and ghosts
  74.     int blueLast, redLast;
  75.  
  76.     int MAX_STEPS = 100;    // number of steps in a run
  77.     int numCatches;     // number of times pacman is caught
  78.     int movements;      // number of movements pacman makes before he dies
  79.     List <String> scoreCols;    // headers for the score fields
  80.  
  81.     int timestep;  
  82.    
  83.     // will be used by the mind, hence public static
  84.  
  85.     public  static final   int ACTION_LEFT      = 0;
  86.     public  static final   int ACTION_RIGHT     = 1;
  87.     public  static final   int ACTION_UP        = 2;
  88.     public  static final   int ACTION_DOWN      = 3;
  89.     public  static final   int STAY_STILL       = 4;   
  90.     public  static final   int NO_ACTIONS       = 5;
  91.     public  static final   int ACTION_RAND      = 6;
  92.    
  93.     String SUPPORT_DIR  = "images";                 // support files
  94.     //pictures for pacman
  95.     String IMG_PACMAN   = SUPPORT_DIR + "/Pacman1.jpg";
  96.     String IMG_CAUGHT   = SUPPORT_DIR + "/caught1.jpg";
  97.     String IMG_BLUE     = SUPPORT_DIR + "/blueGhost1.jpg";
  98.     String IMG_RED      = SUPPORT_DIR + "/redGhost1.jpg";
  99.     String IMG_ORANGE   = SUPPORT_DIR + "/orangeGhost1.jpg";
  100.     String IMG_PINK     = SUPPORT_DIR + "/pinkGhost1.jpg";
  101.     String IMG_BLOCK    = SUPPORT_DIR + "/block1.jpg";
  102.  
  103.  
  104.     // transient - don't serialise these:
  105.     // array, streams for the pacman and ghosts, buffered images for pacman and ghosts
  106.     private transient ArrayList <BufferedImage> buf;
  107.    
  108.     private transient InputStream pacmanStream, caughtStream, blueStream, redStream, orangeStream, pinkStream, blockStream;
  109.     private transient BufferedImage pacmanImg, caughtImg, blueImg, redImg, orangeImg, pinkImg, blockImg;
  110.    
  111.     int imgWidth, imgHeight;
  112.  
  113.  
  114.     private Point randomPos(){
  115.    
  116.         Random r = new Random();
  117.         return new Point(r.nextInt(COLUMNS),r.nextInt(ROWS));        
  118.     }
  119.  
  120.     // initial position
  121.     private void initPos(){
  122.    
  123.         pacmanPos   = new Point(14,23); //middle bottom
  124.        
  125.         bluePos     = new Point(23,5);  //top right
  126.         redPos      = new Point(23,26); //bottom right
  127.         orangePos   = new Point(4,26);  //bottom left
  128.         pinkPos     = new Point(4,5);   //top left
  129.      
  130.     }
  131.  
  132.     // random movement
  133.     private int randomAction(){
  134.    
  135.         Random r = new Random();
  136.         return ( r.nextInt( NO_ACTIONS ) );      
  137.     }
  138.    
  139.  
  140.     // a movement for left or right
  141.     private Point move ( Point startPos, int direction ){
  142.        
  143.         while(true){
  144.             //LEFT
  145.             if ( direction == ACTION_LEFT ){
  146.                 if(maze[startPos.y][startPos.x-1] == 0){
  147.  
  148.                         direction = randomAction();
  149.                 }
  150.                 else{
  151.                     startPos.x=((startPos.x-1));
  152.                     break;
  153.                 }
  154.             }
  155.            
  156.             //RIGHT
  157.             else if ( direction == ACTION_RIGHT ){
  158.                 if(maze[startPos.y][startPos.x+1] == 0){
  159.  
  160.                         direction = randomAction();
  161.                 }
  162.                 else{
  163.                     startPos.x=((startPos.x+1));
  164.                     break;
  165.                 }
  166.             }
  167.            
  168.             //UP
  169.             else if ( direction == ACTION_UP ){
  170.                 if(maze[startPos.y-1][startPos.x] == 0){
  171.                
  172.                         direction = randomAction();
  173.                 }
  174.                 else{
  175.                     startPos.y=((startPos.y-1) );
  176.                     break;
  177.                 }
  178.             }
  179.            
  180.             //DOWN
  181.             else{ //if ( direction == ACTION_DOWN ){
  182.                 if(maze[startPos.y+1][startPos.x] == 0){
  183.  
  184.                         direction = randomAction();
  185.                 }
  186.                 else{
  187.                     startPos.y=((startPos.y+1) );
  188.                     break;
  189.                 }
  190.             }
  191.         }
  192.        
  193.         return startPos;
  194.    
  195.     }
  196.  
  197.     // run the program and generate images until you hit the max, returns true until it hits max
  198.     private boolean runFinished(){
  199.    
  200.         return ( timestep >= MAX_STEPS );
  201.     }        
  202.  
  203.     // initalise the images
  204.     private void initImages(){  // sets up new buffer to hold images  
  205.    
  206.         if ( imagesDesired ){
  207.             buf = new ArrayList <BufferedImage> (); // buffer is cleared for each timestep, multiple images per timestep
  208.  
  209.             if ( pacmanStream  == null ){   // block is only executed once (only read from disk once)
  210.                 try{
  211.                     ImageIO.setUseCache(true);  // use memory, not disk, for temporary images
  212.  
  213.                     // read from disk
  214.                     pacmanStream    = getClass().getResourceAsStream ( IMG_PACMAN );
  215.                     blueStream      = getClass().getResourceAsStream ( IMG_BLUE );
  216.                     redStream       = getClass().getResourceAsStream ( IMG_RED );
  217.                     orangeStream    = getClass().getResourceAsStream ( IMG_ORANGE );
  218.                     pinkStream      = getClass().getResourceAsStream ( IMG_PINK );
  219.                     caughtStream    = getClass().getResourceAsStream ( IMG_CAUGHT );
  220.                     blockStream     = getClass().getResourceAsStream ( IMG_BLOCK );
  221.                    
  222.                     pacmanImg    = javax.imageio.ImageIO.read( pacmanStream );
  223.                     blueImg      = javax.imageio.ImageIO.read( blueStream );
  224.                     redImg       = javax.imageio.ImageIO.read( redStream );
  225.                     orangeImg    = javax.imageio.ImageIO.read( orangeStream );
  226.                     pinkImg      = javax.imageio.ImageIO.read( pinkStream );
  227.                     caughtImg    = javax.imageio.ImageIO.read( caughtStream );
  228.                     blockImg     = javax.imageio.ImageIO.read( blockStream );
  229.  
  230.                     // dimensions of jpg covering one square of the grid
  231.                     imgWidth  = blockImg.getWidth();
  232.                     imgHeight = blockImg.getHeight();
  233.                 }
  234.                 catch ( IOException e ) {}
  235.             }
  236.         }
  237.     }
  238.  
  239.  
  240.     // add an image to the buffer
  241.     private void addImage(){
  242.  
  243.         if ( imagesDesired ){
  244.             BufferedImage img = new BufferedImage ( ( imgWidth * (COLUMNS) ), (imgHeight * (ROWS)), BufferedImage.TYPE_INT_RGB );
  245.                  
  246.             //places block images where there is a 1
  247.             for(int row = 0; row < ROWS; row++){
  248.                 for(int column = 0; column< COLUMNS; column++){
  249.                     if(maze[row][column]==0){
  250.                         img.createGraphics().drawImage ( blockImg, (imgWidth * column), (imgHeight * row), null );
  251.                     }
  252.                 }
  253.             }
  254.            
  255.             //if pacman is caught by a ghost
  256.             if (pacmanPos.equals(bluePos) || pacmanPos.equals(redPos) || pacmanPos.equals(orangePos) || pacmanPos.equals(pinkPos)){
  257.                 img.createGraphics().drawImage ( caughtImg, (imgWidth * pacmanPos.x),   (imgHeight * pacmanPos.y),  null );
  258.                 numCatches++;
  259.                 initPos();
  260.             }
  261.             //draw the new movement positions
  262.             else{
  263.                 img.createGraphics().drawImage ( pacmanImg, (imgWidth * pacmanPos.x),   (imgHeight * pacmanPos.y),  null );
  264.                 img.createGraphics().drawImage ( blueImg,   (imgWidth * bluePos.x),     (imgHeight * bluePos.y),    null );
  265.                 img.createGraphics().drawImage ( redImg,    (imgWidth * redPos.x),      (imgHeight * redPos.y),     null );
  266.                 img.createGraphics().drawImage ( orangeImg, (imgWidth * orangePos.x),   (imgHeight * orangePos.y)null );
  267.                 img.createGraphics().drawImage ( pinkImg,   (imgWidth * pinkPos.x),     (imgHeight * pinkPos.y),    null );
  268.             }
  269.             buf.add(img); // adds the image to the buffer
  270.         }
  271.     }
  272.  
  273.  
  274.    
  275.     //====== World must respond to these methods: ==========================================================
  276.     //  newrun(), endrun()
  277.     //  getstate(), takeaction()
  278.     //  getscore(), getimage()
  279.     //======================================================================================================
  280.  
  281.  
  282.     // starts a new game
  283.     public void newrun() throws RunError{
  284.    
  285.         //points for pacman and ghosts
  286.         pacmanPos   = new Point();
  287.         bluePos     = new Point();
  288.         redPos      = new Point();
  289.         orangePos   = new Point();
  290.         pinkPos     = new Point();
  291.    
  292.    
  293.         timestep = 0;
  294.         numCatches = 0;
  295.         movements = 0;
  296.  
  297.                    
  298.         initPos();
  299.  
  300.         scoreCols = new LinkedList <String> ();    
  301.         scoreCols.add("Caught");
  302.         scoreCols.add("Movements");
  303.     }
  304.          
  305.  
  306.     // end of an instance
  307.     public void endrun() throws RunError{
  308.     }
  309.  
  310.  
  311.  
  312.     // get the current state of the program, the positions of the people
  313.     public State getstate() throws RunError{
  314.  
  315.         // x and y coordinates of pacman and ghosts
  316.         String x = String.format ( "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", pacmanPos.x, pacmanPos.y, bluePos.x, bluePos.y,
  317.                                                 redPos.x, redPos.y, orangePos.x, orangePos.y, pinkPos.x, pinkPos.y );
  318.         return new State ( x );
  319.     }
  320.        
  321.  
  322.  
  323.     public State takeaction ( Action action ) throws RunError{
  324.     // Add any number of images to a list of images for this step.
  325.     // The first image on the list for this step should be the image before we take the action.
  326.    
  327.         initImages();           // If run with images off, imagesDesired = false and this does nothing.
  328.         addImage();             // image before my move
  329.                                 // If run with images off, imagesDesired = false and this does nothing.  
  330.  
  331.         String  s = action.toString();       
  332.         String[] a = s.split(",");             
  333.         int i = Integer.parseInt ( a[0] ); 
  334.        
  335.  
  336.         pacmanPos = move ( pacmanPos, i );
  337.         addImage();             // intermediate image, before opponent moves  
  338.        
  339.         // pacman is caught
  340.         if (pacmanPos == bluePos || pacmanPos == redPos || pacmanPos == orangePos || pacmanPos == pinkPos){
  341.             numCatches++;           // have already shown the "capture" image    
  342.             initPos();              // loop round, new image will be shown in next step
  343.         }                    
  344.  
  345.         else{
  346.             int redChaseAction;
  347.             int blueChaseAction;
  348.        
  349.             // -----BLUE GHOST-----
  350.             // move right to get pacman
  351.             if ( bluePos.x < pacmanPos.x && maze[bluePos.y][bluePos.x+1] == 1){
  352.                 blueChaseAction = killianPacWorld.ACTION_RIGHT;    
  353.             }  
  354.            
  355.             // move left to get pacman
  356.             else if (bluePos.x > pacmanPos.x && maze[bluePos.y][bluePos.x-1] == 1){
  357.                 blueChaseAction = killianPacWorld.ACTION_LEFT;
  358.             }
  359.            
  360.             //move down to get pacman
  361.             else if(bluePos.y < pacmanPos.y && maze[bluePos.y+1][bluePos.x] == 1){
  362.                 blueChaseAction = killianPacWorld.ACTION_DOWN;
  363.             }
  364.            
  365.             // otherwise move up
  366.             else blueChaseAction = killianPacWorld.ACTION_UP;  
  367.  
  368.            
  369.             // ----RED GHOST-----
  370.             // move right to get pacman
  371.             // RIGHT PRIORITY
  372.             if ( redPos.x < pacmanPos.x){
  373.            
  374.                 //UP if wall to the right
  375.                 if( maze[redPos.y][redPos.x+1] == 0 && maze[redPos.y-1][redPos.x] == 1)
  376.                     redChaseAction = killianPacWorld.ACTION_UP;
  377.                
  378.                 //DOWN if wall to the right and up
  379.                 else if( maze[redPos.y][redPos.x+1] ==0 && maze[redPos.y+1][redPos.x] == 1)
  380.                     redChaseAction = killianPacWorld.ACTION_DOWN;  
  381.                
  382.                 //RIGHT
  383.                 else
  384.                     redChaseAction = killianPacWorld.ACTION_RIGHT;     
  385.             }  
  386.            
  387.             // LEFT PRIORITY
  388.             else if (redPos.x > pacmanPos.x){
  389.            
  390.                 //UP if wall to the left
  391.                 if( maze[redPos.y][redPos.x-1] ==0 && maze[redPos.y-1][redPos.x] == 1)
  392.                     redChaseAction = killianPacWorld.ACTION_UP;
  393.                
  394.                 //DOWN if wall to the left and up
  395.                 else if(maze[redPos.y][redPos.x-1]== 0 && maze[redPos.y+1][redPos.x]== 1 )
  396.                     redChaseAction = killianPacWorld.ACTION_DOWN;
  397.                
  398.                 //LEFT
  399.                 else
  400.                     redChaseAction = killianPacWorld.ACTION_LEFT;
  401.             }
  402.            
  403.             // DOWN PRIORITY
  404.             else if(redPos.y < pacmanPos.y){
  405.            
  406.                 //RIGHT if wall to the bottom
  407.                 if( maze[redPos.y+1][redPos.x] ==0 &&  maze[redPos.y][redPos.x+1] ==1)
  408.                     redChaseAction = killianPacWorld.ACTION_RIGHT;
  409.                
  410.                 //LEFT if wall to the bottom and right
  411.                 else if( maze[redPos.y+1][redPos.x] ==0  && maze[redPos.y][redPos.x-1] ==1)
  412.                     redChaseAction = killianPacWorld.ACTION_LEFT;
  413.                
  414.                 //DOWN
  415.                 else
  416.                     redChaseAction = killianPacWorld.ACTION_DOWN;
  417.             }
  418.            
  419.             // UP PRIORITY
  420.             else{
  421.            
  422.                 //RIGHT if wall to the top
  423.                 if( maze[redPos.y-1][redPos.x] ==0 && maze[redPos.x+1][redPos.y] ==1)
  424.                     redChaseAction = killianPacWorld.ACTION_RIGHT;
  425.                
  426.                 //LEFT if wall to the top and right
  427.                 else if( maze[redPos.y-1][redPos.x] ==0 && maze[redPos.x-1][redPos.y] ==1)
  428.                     redChaseAction = killianPacWorld.ACTION_LEFT;
  429.                
  430.                 //UP
  431.                 else
  432.                     redChaseAction = killianPacWorld.ACTION_UP;
  433.             }
  434.                
  435.             //Store last movements
  436.             blueLast = blueChaseAction;
  437.             redLast = redChaseAction;
  438.            
  439.             // -- GHOSTS ALL MOVE --
  440.             bluePos     = move ( bluePos,   blueChaseAction );  // ghost chases
  441.             redPos      = move ( redPos,    redChaseAction );   // ghost shadow chases
  442.            
  443.             orangePos   = move ( orangePos, randomAction() );   // ghost moves randomly
  444.             pinkPos     = move ( pinkPos,   randomAction() );   // ghost moves randomly
  445.  
  446.             addImage();         // new image will be shown in next step    
  447.  
  448.             if (pacmanPos == bluePos || pacmanPos == redPos || pacmanPos == orangePos || pacmanPos == pinkPos){
  449.                 addImage();         // show the "capture" image    
  450.                 numCatches++;
  451.                 initPos();          // loop round, new image will be shown in next step
  452.             }
  453.          }
  454.  
  455.         timestep++;
  456.         movements++;
  457.  
  458.         if ( runFinished() ){       // there will be no loop round
  459.             addImage();    
  460.         }
  461.  
  462.     // The last timestep of the run shows the final state, and no action can be taken in this state.
  463.     // Whatever is the last image built on the run will be treated as the image for this final state.
  464.  
  465.         return getstate();
  466.     }
  467.        
  468.  
  469.  
  470.      
  471.     public Score getscore() throws RunError{
  472.         String s = String.format ( "%d,%d", numCatches, movements );
  473.    
  474.         List <Comparable> values = new LinkedList <Comparable> ();
  475.         values.add( numCatches      );
  476.         values.add( movements   );
  477.  
  478.  
  479.         return new Score ( s, runFinished(), scoreCols, values );
  480.     }
  481.  
  482.              
  483.      
  484.  
  485.     public ArrayList <BufferedImage> getimage() throws RunError{
  486.     // Return image(s) of World.
  487.     // Image may show more information than State (what the Mind sees).
  488.     // This method returns a list of images for this step - we allow multiple images per step.
  489.     // e.g. You move, get one image, the opponent moves, next image, your turn again (this is 2 images per step).
  490.     // This list of images should normally be built in takeaction method.
  491.     // The first image on the list for this step should be the image before we take the action on this step.
  492.  
  493.         return buf;
  494.     }
  495.  
  496.  
  497.  
  498. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement