⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkersgame.java

📁 Creat mobile game Creat mobile game Creat mobile game Creat mobile game
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.frog_parrot.checkers;import java.util.Vector;/** * This class takes care of the underlying logic and data of  * the checkers game being played.  That includes where  * all of the pieces are on the board and where it is okay  * for them to move to.   * * @author Carol Hamer */public class CheckersGame {  //-------------------------------------------------------  //   static fields  /**   * The length of the checkerboard in the x-direction.   */  public static final byte X_LENGTH = 4;  /**   * A constant for optimizing calculations.   */  public static final byte X_LENGTH_MINUS_1 = 3;  /**   * The length of the checkerboard in the y-direction.   */  public static final byte Y_LENGTH = 8;  /**   * A constant for optimizing calculations.   */  public static final byte Y_LENGTH_MINUS_1 = 7;  //-------------------------------------------------------  //   instance fields  /**   * a handle to the move manager that keeps track of taking    * turns with the other player.   */  private MoveManager myMoveManager;  /**   * This array represents the black squares of the    * checkerboard.  The two dimensions of the array    * represent the two dimensions of the checkerboard.   * The value represents what type of piece is on    * the square.   * 0 = empty   * 1 = local player's piece   * 2 = local player's king   * -1 = remote player's piece   * -2 = remote player's king   */  private byte[][] myGrid;  /**   * If the user has currently selected a piece to move,    * this is its X grid coordinate. (-1 if none selected)   */  private byte mySelectedX = -1;  /**   * If the user has currently selected a piece to move,    * this is its Y grid coordinate.(-1 if none selected)   */  private byte mySelectedY = -1;  /**   * If the user has currently selected a possible    * destination square for a move, this is its X coordinate..   * (-1 if none selected)   */  private byte myDestinationX = -1;  /**   * If the user has currently selected a possible    * destination square for a move, this is its Y coordinate..   * (-1 if none selected)   */  private byte myDestinationY = -1;  /**   * This Vector contains the coordinates of all of the    * squares that the player could currently move to.   */  private Vector myPossibleMoves = new Vector(4);  /**   * This is true if the player has just jumped and can    * jump again.   */  private boolean myIsJumping = false;  //-------------------------------------------------------  //   get/set data    /**   * get the piece on the given grid square.   */  byte getPiece(byte x, byte y) {    return(myGrid[x][y]);  }  /**   * This is callsed by CheckersCanvas to determine if    * the square is currently selected (as containing    * a piece to move or a destination square).   */  boolean isSelected(byte x, byte y) {    boolean retVal = false;    if((x == mySelectedX) && (y == mySelectedY)) {      retVal = true;    } else if((x == myDestinationX) && (y == myDestinationY)) {      retVal = true;    }    return(retVal);  }  /**   * This tells whether or not the keystrokes should currently   * be taken into account.   */  boolean isMyTurn() {    return(myMoveManager.getState() == MoveManager.LOCAL_TURN);  }  /**   * This tells whether or not the game has ended.   */  boolean getGameOver() {    return(myMoveManager.getState() == MoveManager.GAME_OVER);  }  /**   * set the MoveManager object.   */  void setMoveManager(MoveManager manager) {    myMoveManager = manager;  }  //-------------------------------------------------------  //   initialization  /**   * Constructor puts the pieces in their initial positions:   */  CheckersGame() {    myGrid = new byte[X_LENGTH][];    for(byte i = 0; i < myGrid.length; i++) {      myGrid[i] = new byte[Y_LENGTH];      for(byte j = 0; j < myGrid[i].length; j++) {        if(j < 3) {          // fill the top of the board with remote players          myGrid[i][j] = -1;        } else if(j > 4) {          // fill the bottom of the board with local players          myGrid[i][j] = 1;        }      }    }  }  /**   * This is called just before the player makes the    * first move.   */  void start() {    mySelectedX = 0;    mySelectedY = 5;    getMoves(mySelectedX, mySelectedY, myPossibleMoves, false);  }  //-------------------------------------------------------  //   move the opponent  // to be called by MoveManager  /**   * This is called when the opponent wants to move   * its piece.   * @param moveData an array of four bytes:   * moveData[0] = opponent's initial X coordinate   * moveData[1] = opponent's initial Y coordinate   * moveData[2] = opponent's destination X coordinate   * moveData[3] = opponent's destination Y coordinate   */  void moveOpponent(byte[] moveData) {    // since both players appear on their own screens     // as the red side (bottom of the screen), you need     // to invert the opponent's move:    moveData[0] = (byte)(X_LENGTH_MINUS_1 - moveData[0]);    moveData[2] = (byte)(X_LENGTH_MINUS_1 - moveData[2]);    moveData[1] = (byte)(Y_LENGTH_MINUS_1 - moveData[1]);    moveData[3] = (byte)(Y_LENGTH_MINUS_1 - moveData[3]);    myGrid[moveData[2]][moveData[3]]       = myGrid[moveData[0]][moveData[1]];    myGrid[moveData[0]][moveData[1]] = 0;    // deal with an opponent's jump:    if((moveData[1] - moveData[3] > 1) ||        (moveData[3] - moveData[1] > 1)) {      int jumpedY = (moveData[1] + moveData[3])/2;      int jumpedX = moveData[0];      int parity = moveData[1] % 2;      if((parity > 0) && (moveData[2] > moveData[0])) {        jumpedX++;      } else if((parity == 0) && (moveData[0] > moveData[2])) {        jumpedX--;      }      myGrid[jumpedX][jumpedY] = 0;    }    // if the opponent reaches the far side,     // make him a king:    if(moveData[3] == Y_LENGTH - 1) {      myGrid[moveData[2]][moveData[3]] = -2;    }  }  /**   * This is called when the opponent's turn is over.   * Note that the turn doesn't automatically end after    * the opponent moves because the opponent may make    * a double or triple jump.   */  void endOpponentTurn() {    // Now begin the local player's turn:     // First select the first local piece that can be     // moved. (rightPressed will select an appropriate     // piece or end the game if the local player has     // no possible moves to make)    mySelectedX = 0;    mySelectedY = 0;    myDestinationX = -1;    myDestinationY = -1;    rightPressed();    // the local player's thread has been waiting     // for the opponent's turn to end.      synchronized(this) {      notify();    }  }  //-------------------------------------------------------  //   handle keystrokes  // to be called by CheckersCanvas  /**   * if the left button is pressed, this method takes    * the correct course of action depending on the situation.   */  void leftPressed() {    // in the first case the user has not yet selected a     // piece to move:    if(myDestinationX == -1) {      // find the next possible piece (to the left)       // that can move:      selectPrevious();      // if selectPrevious fails to fill myPossibleMoves, that       // means that the local player cannot move, so the game      // is over:      if(myPossibleMoves.size() == 0) {        myMoveManager.loseGame();      }    } else {      // if the user has already selected a piece to move,       // you give the options of where the piece can move to:      for(byte i = 0; i < myPossibleMoves.size(); i++) {        byte[] coordinates = (byte[])myPossibleMoves.elementAt(i);        if((coordinates[0] == myDestinationX) &&            (coordinates[1] == myDestinationY)) {          i++;          i = (new Integer(i % myPossibleMoves.size())).byteValue();          coordinates = (byte[])myPossibleMoves.elementAt(i);          myDestinationX = coordinates[0];          myDestinationY = coordinates[1];          break;        }      }    }  }  /**   * if the left button is pressed, this method takes    * the correct course of action depending on the situation.   */  void rightPressed() {    // in the first case the user has not yet selected a     // piece to move:    if(myDestinationX == -1) {      // find the next possible piece that can       // move:      selectNext();      // if selectNext fails to fill myPossibleMoves, that       // means that the local player cannot move, so the game      // is over:      if(myPossibleMoves.size() == 0) {        myMoveManager.loseGame();      }    } else {      // if the user has already selected a piece to move, 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -