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
- "Pacman" world - with images
- Killian Mills
- 11368701
- */
- import java.awt.image.*;
- import java.io.*;
- import java.util.*;
- import javax.imageio.*;
- import java.awt.Point;
- import org.w2mind.net.*;
- // See SkeletonWorld.java for background and extra comments.
- // Note: This code has been stripped down to give you less to read.
- // The aim of this code was not a perfect Java program, but to give you as little as possible to read.
- public class killianPacWorld extends AbstractWorld{
- //public static final int GRID_SIZE = 10; //2-dimensional grid
- public static final int ROWS = 31;
- public static final int COLUMNS = 28;
- public static int[][] maze={
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},
- {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},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0,0,0,1,0,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,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
- {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},
- {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}
- };
- //positions of pacman and ghosts, point for x and y coordinates
- Point pacmanPos, bluePos, redPos, orangePos, pinkPos;
- //holds the last movement of pacman and ghosts
- int blueLast, redLast;
- int MAX_STEPS = 100; // number of steps in a run
- int numCatches; // number of times pacman is caught
- int movements; // number of movements pacman makes before he dies
- List <String> scoreCols; // headers for the score fields
- int timestep;
- // will be used by the mind, hence public static
- public static final int ACTION_LEFT = 0;
- public static final int ACTION_RIGHT = 1;
- public static final int ACTION_UP = 2;
- public static final int ACTION_DOWN = 3;
- public static final int STAY_STILL = 4;
- public static final int NO_ACTIONS = 5;
- public static final int ACTION_RAND = 6;
- String SUPPORT_DIR = "images"; // support files
- //pictures for pacman
- String IMG_PACMAN = SUPPORT_DIR + "/Pacman1.jpg";
- String IMG_CAUGHT = SUPPORT_DIR + "/caught1.jpg";
- String IMG_BLUE = SUPPORT_DIR + "/blueGhost1.jpg";
- String IMG_RED = SUPPORT_DIR + "/redGhost1.jpg";
- String IMG_ORANGE = SUPPORT_DIR + "/orangeGhost1.jpg";
- String IMG_PINK = SUPPORT_DIR + "/pinkGhost1.jpg";
- String IMG_BLOCK = SUPPORT_DIR + "/block1.jpg";
- // transient - don't serialise these:
- // array, streams for the pacman and ghosts, buffered images for pacman and ghosts
- private transient ArrayList <BufferedImage> buf;
- private transient InputStream pacmanStream, caughtStream, blueStream, redStream, orangeStream, pinkStream, blockStream;
- private transient BufferedImage pacmanImg, caughtImg, blueImg, redImg, orangeImg, pinkImg, blockImg;
- int imgWidth, imgHeight;
- private Point randomPos(){
- Random r = new Random();
- return new Point(r.nextInt(COLUMNS),r.nextInt(ROWS));
- }
- // initial position
- private void initPos(){
- pacmanPos = new Point(14,23); //middle bottom
- bluePos = new Point(23,5); //top right
- redPos = new Point(23,26); //bottom right
- orangePos = new Point(4,26); //bottom left
- pinkPos = new Point(4,5); //top left
- }
- // random movement
- private int randomAction(){
- Random r = new Random();
- return ( r.nextInt( NO_ACTIONS ) );
- }
- // a movement for left or right
- private Point move ( Point startPos, int direction ){
- while(true){
- //LEFT
- if ( direction == ACTION_LEFT ){
- if(maze[startPos.y][startPos.x-1] == 0){
- direction = randomAction();
- }
- else{
- startPos.x=((startPos.x-1));
- break;
- }
- }
- //RIGHT
- else if ( direction == ACTION_RIGHT ){
- if(maze[startPos.y][startPos.x+1] == 0){
- direction = randomAction();
- }
- else{
- startPos.x=((startPos.x+1));
- break;
- }
- }
- //UP
- else if ( direction == ACTION_UP ){
- if(maze[startPos.y-1][startPos.x] == 0){
- direction = randomAction();
- }
- else{
- startPos.y=((startPos.y-1) );
- break;
- }
- }
- //DOWN
- else{ //if ( direction == ACTION_DOWN ){
- if(maze[startPos.y+1][startPos.x] == 0){
- direction = randomAction();
- }
- else{
- startPos.y=((startPos.y+1) );
- break;
- }
- }
- }
- return startPos;
- }
- // run the program and generate images until you hit the max, returns true until it hits max
- private boolean runFinished(){
- return ( timestep >= MAX_STEPS );
- }
- // initalise the images
- private void initImages(){ // sets up new buffer to hold images
- if ( imagesDesired ){
- buf = new ArrayList <BufferedImage> (); // buffer is cleared for each timestep, multiple images per timestep
- if ( pacmanStream == null ){ // block is only executed once (only read from disk once)
- try{
- ImageIO.setUseCache(true); // use memory, not disk, for temporary images
- // read from disk
- pacmanStream = getClass().getResourceAsStream ( IMG_PACMAN );
- blueStream = getClass().getResourceAsStream ( IMG_BLUE );
- redStream = getClass().getResourceAsStream ( IMG_RED );
- orangeStream = getClass().getResourceAsStream ( IMG_ORANGE );
- pinkStream = getClass().getResourceAsStream ( IMG_PINK );
- caughtStream = getClass().getResourceAsStream ( IMG_CAUGHT );
- blockStream = getClass().getResourceAsStream ( IMG_BLOCK );
- pacmanImg = javax.imageio.ImageIO.read( pacmanStream );
- blueImg = javax.imageio.ImageIO.read( blueStream );
- redImg = javax.imageio.ImageIO.read( redStream );
- orangeImg = javax.imageio.ImageIO.read( orangeStream );
- pinkImg = javax.imageio.ImageIO.read( pinkStream );
- caughtImg = javax.imageio.ImageIO.read( caughtStream );
- blockImg = javax.imageio.ImageIO.read( blockStream );
- // dimensions of jpg covering one square of the grid
- imgWidth = blockImg.getWidth();
- imgHeight = blockImg.getHeight();
- }
- catch ( IOException e ) {}
- }
- }
- }
- // add an image to the buffer
- private void addImage(){
- if ( imagesDesired ){
- BufferedImage img = new BufferedImage ( ( imgWidth * (COLUMNS) ), (imgHeight * (ROWS)), BufferedImage.TYPE_INT_RGB );
- //places block images where there is a 1
- for(int row = 0; row < ROWS; row++){
- for(int column = 0; column< COLUMNS; column++){
- if(maze[row][column]==0){
- img.createGraphics().drawImage ( blockImg, (imgWidth * column), (imgHeight * row), null );
- }
- }
- }
- //if pacman is caught by a ghost
- if (pacmanPos.equals(bluePos) || pacmanPos.equals(redPos) || pacmanPos.equals(orangePos) || pacmanPos.equals(pinkPos)){
- img.createGraphics().drawImage ( caughtImg, (imgWidth * pacmanPos.x), (imgHeight * pacmanPos.y), null );
- numCatches++;
- initPos();
- }
- //draw the new movement positions
- else{
- img.createGraphics().drawImage ( pacmanImg, (imgWidth * pacmanPos.x), (imgHeight * pacmanPos.y), null );
- img.createGraphics().drawImage ( blueImg, (imgWidth * bluePos.x), (imgHeight * bluePos.y), null );
- img.createGraphics().drawImage ( redImg, (imgWidth * redPos.x), (imgHeight * redPos.y), null );
- img.createGraphics().drawImage ( orangeImg, (imgWidth * orangePos.x), (imgHeight * orangePos.y), null );
- img.createGraphics().drawImage ( pinkImg, (imgWidth * pinkPos.x), (imgHeight * pinkPos.y), null );
- }
- buf.add(img); // adds the image to the buffer
- }
- }
- //====== World must respond to these methods: ==========================================================
- // newrun(), endrun()
- // getstate(), takeaction()
- // getscore(), getimage()
- //======================================================================================================
- // starts a new game
- public void newrun() throws RunError{
- //points for pacman and ghosts
- pacmanPos = new Point();
- bluePos = new Point();
- redPos = new Point();
- orangePos = new Point();
- pinkPos = new Point();
- timestep = 0;
- numCatches = 0;
- movements = 0;
- initPos();
- scoreCols = new LinkedList <String> ();
- scoreCols.add("Caught");
- scoreCols.add("Movements");
- }
- // end of an instance
- public void endrun() throws RunError{
- }
- // get the current state of the program, the positions of the people
- public State getstate() throws RunError{
- // x and y coordinates of pacman and ghosts
- String x = String.format ( "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", pacmanPos.x, pacmanPos.y, bluePos.x, bluePos.y,
- redPos.x, redPos.y, orangePos.x, orangePos.y, pinkPos.x, pinkPos.y );
- return new State ( x );
- }
- public State takeaction ( Action action ) throws RunError{
- // Add any number of images to a list of images for this step.
- // The first image on the list for this step should be the image before we take the action.
- initImages(); // If run with images off, imagesDesired = false and this does nothing.
- addImage(); // image before my move
- // If run with images off, imagesDesired = false and this does nothing.
- String s = action.toString();
- String[] a = s.split(",");
- int i = Integer.parseInt ( a[0] );
- pacmanPos = move ( pacmanPos, i );
- addImage(); // intermediate image, before opponent moves
- // pacman is caught
- if (pacmanPos == bluePos || pacmanPos == redPos || pacmanPos == orangePos || pacmanPos == pinkPos){
- numCatches++; // have already shown the "capture" image
- initPos(); // loop round, new image will be shown in next step
- }
- else{
- int redChaseAction;
- int blueChaseAction;
- // -----BLUE GHOST-----
- // move right to get pacman
- if ( bluePos.x < pacmanPos.x && maze[bluePos.y][bluePos.x+1] == 1){
- blueChaseAction = killianPacWorld.ACTION_RIGHT;
- }
- // move left to get pacman
- else if (bluePos.x > pacmanPos.x && maze[bluePos.y][bluePos.x-1] == 1){
- blueChaseAction = killianPacWorld.ACTION_LEFT;
- }
- //move down to get pacman
- else if(bluePos.y < pacmanPos.y && maze[bluePos.y+1][bluePos.x] == 1){
- blueChaseAction = killianPacWorld.ACTION_DOWN;
- }
- // otherwise move up
- else blueChaseAction = killianPacWorld.ACTION_UP;
- // ----RED GHOST-----
- // move right to get pacman
- // RIGHT PRIORITY
- if ( redPos.x < pacmanPos.x){
- //UP if wall to the right
- if( maze[redPos.y][redPos.x+1] == 0 && maze[redPos.y-1][redPos.x] == 1)
- redChaseAction = killianPacWorld.ACTION_UP;
- //DOWN if wall to the right and up
- else if( maze[redPos.y][redPos.x+1] ==0 && maze[redPos.y+1][redPos.x] == 1)
- redChaseAction = killianPacWorld.ACTION_DOWN;
- //RIGHT
- else
- redChaseAction = killianPacWorld.ACTION_RIGHT;
- }
- // LEFT PRIORITY
- else if (redPos.x > pacmanPos.x){
- //UP if wall to the left
- if( maze[redPos.y][redPos.x-1] ==0 && maze[redPos.y-1][redPos.x] == 1)
- redChaseAction = killianPacWorld.ACTION_UP;
- //DOWN if wall to the left and up
- else if(maze[redPos.y][redPos.x-1]== 0 && maze[redPos.y+1][redPos.x]== 1 )
- redChaseAction = killianPacWorld.ACTION_DOWN;
- //LEFT
- else
- redChaseAction = killianPacWorld.ACTION_LEFT;
- }
- // DOWN PRIORITY
- else if(redPos.y < pacmanPos.y){
- //RIGHT if wall to the bottom
- if( maze[redPos.y+1][redPos.x] ==0 && maze[redPos.y][redPos.x+1] ==1)
- redChaseAction = killianPacWorld.ACTION_RIGHT;
- //LEFT if wall to the bottom and right
- else if( maze[redPos.y+1][redPos.x] ==0 && maze[redPos.y][redPos.x-1] ==1)
- redChaseAction = killianPacWorld.ACTION_LEFT;
- //DOWN
- else
- redChaseAction = killianPacWorld.ACTION_DOWN;
- }
- // UP PRIORITY
- else{
- //RIGHT if wall to the top
- if( maze[redPos.y-1][redPos.x] ==0 && maze[redPos.x+1][redPos.y] ==1)
- redChaseAction = killianPacWorld.ACTION_RIGHT;
- //LEFT if wall to the top and right
- else if( maze[redPos.y-1][redPos.x] ==0 && maze[redPos.x-1][redPos.y] ==1)
- redChaseAction = killianPacWorld.ACTION_LEFT;
- //UP
- else
- redChaseAction = killianPacWorld.ACTION_UP;
- }
- //Store last movements
- blueLast = blueChaseAction;
- redLast = redChaseAction;
- // -- GHOSTS ALL MOVE --
- bluePos = move ( bluePos, blueChaseAction ); // ghost chases
- redPos = move ( redPos, redChaseAction ); // ghost shadow chases
- orangePos = move ( orangePos, randomAction() ); // ghost moves randomly
- pinkPos = move ( pinkPos, randomAction() ); // ghost moves randomly
- addImage(); // new image will be shown in next step
- if (pacmanPos == bluePos || pacmanPos == redPos || pacmanPos == orangePos || pacmanPos == pinkPos){
- addImage(); // show the "capture" image
- numCatches++;
- initPos(); // loop round, new image will be shown in next step
- }
- }
- timestep++;
- movements++;
- if ( runFinished() ){ // there will be no loop round
- addImage();
- }
- // The last timestep of the run shows the final state, and no action can be taken in this state.
- // Whatever is the last image built on the run will be treated as the image for this final state.
- return getstate();
- }
- public Score getscore() throws RunError{
- String s = String.format ( "%d,%d", numCatches, movements );
- List <Comparable> values = new LinkedList <Comparable> ();
- values.add( numCatches );
- values.add( movements );
- return new Score ( s, runFinished(), scoreCols, values );
- }
- public ArrayList <BufferedImage> getimage() throws RunError{
- // Return image(s) of World.
- // Image may show more information than State (what the Mind sees).
- // This method returns a list of images for this step - we allow multiple images per step.
- // e.g. You move, get one image, the opponent moves, next image, your turn again (this is 2 images per step).
- // This list of images should normally be built in takeaction method.
- // The first image on the list for this step should be the image before we take the action on this step.
- return buf;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement