WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发Checkers游戏源码

Checkers游戏源码
作者:J2MEDEV  来源:J2MEDEV  发布时间:2006-7-25 9:04:09
ares one by one (starting from the 
    // currently selected square) until we find a square 
    // that contains one of the local player's pieces 
    // that can move:
    byte testX = mySelectedX;
    byte testY = mySelectedY;
    while(true) {
      testX++;
      if(testX >= X_LENGTH) {
  testX = 0;
  testY++;
  testY = (new Integer(testY % Y_LENGTH)).byteValue();
      }
      getMoves(testX, testY, myPossibleMoves, false);
      if((myPossibleMoves.size() != 0|| 
     ((testX == mySelectedX&& (testY == mySelectedY))) {
  mySelectedX = testX;
  mySelectedY = testY;
  break;
      }
    }
  }

  /**
   * This method starts from the currently selected square 
   * and finds the next square (to the left) that contains 
   * a piece that the player can move.
   */
  private void selectPrevious() {
    // Test the squares one by one (starting from the 
    // currently selected square) until we find a square 
    // that contains one of the local player's pieces 
    // that can move:
    byte testX = mySelectedX;
    byte testY = mySelectedY;
    while(true) {
      testX--;
      if(testX < 0) {
  testX += X_LENGTH;
  testY--;
  if(testY < 0) {
    testY += Y_LENGTH;
  }
      }
      getMoves(testX, testY, myPossibleMoves, false);
      if((myPossibleMoves.size() != 0|| 
   ((testX == mySelectedX&& (testY == mySelectedY))) {
  mySelectedX = testX;
  mySelectedY = testY;
  break;
      }
    }
  }

  //-------------------------------------------------------
  //   internal utilities

  /**
   * Once the user has selected the move to make, this 
   * updates the data accordingly.
   */
  private void move() {
    // the piece that was on the source square is 
    // now on the destination square:
    myGrid[myDestinationX][myDestinationY
      = myGrid[mySelectedX][mySelectedY];
    // the source square is emptied:
    myGrid[mySelectedX][mySelectedY0;
    if(myDestinationY == 0) {
      myGrid[myDestinationX][myDestinationY2;
    }
    // tell the communicator to inform the other player 
    // of this move:
    myCommunicator.move(mySelectedX, mySelectedY, 
      myDestinationX, myDestinationY);
    // deal with the special rules for jumps::
    if((mySelectedY - myDestinationY > 1|| 
       (myDestinationY - mySelectedY > 1)) {
      int jumpedY = (mySelectedY + myDestinationY)/2;
      int jumpedX = mySelectedX;
      int parity = mySelectedY % 2;
      // the coordinates of the jumped square depend on 
      // what row we're in:
      if((parity > 0&& (myDestinationX > mySelectedX)) {
          jumpedX++;
      else if((parity == 0&& (mySelectedX > myDestinationX)) {
          jumpedX--;
      }
      // remove the piece that was jumped over:
      myGrid[jumpedX][jumpedY0;
      // now get ready to jump again if possible:
      mySelectedX = myDestinationX;
      mySelectedY = myDestinationY;
      myDestinationX = -1;
      myDestinationY = -1;
      // see if another jump is possible.
      // The "true" argument tells the program to return 
      // only jumps because the player can go again ONLY 
      // if there's a jump:
      getMoves(mySelectedX, mySelectedY, myPossibleMoves, true);
      // if there's another jump possible with the same piece, 
      // allow the player to continue jumping:
      if(myPossibleMoves.size() != 0) {
  myIsJumping = true;
  byte[] landing = (byte[])myPossibleMoves.elementAt(0);
  myDestinationX = landing[0];
  myDestinationY = landing[1];
      else {
  myTurn = false;
  myCommunicator.endTurn();
      }
    else {
      // since it's not a jump, we just end the turn 
      // by deselecting everything.
      mySelectedX = -1;
      mySelectedY = -1;
      myDestinationX = -1;
      myDestinationY = -1;
      myPossibleMoves.removeAllElements();
      myTurn = false;
      // tell the other player we're done:
      myCommunicator.endTurn();
    }
  }
  /**
   * Given a square on the grid, get the coordinates 
   * of one of the adjoining (diagonal) squares.
   * 0 = top left
   * 1 = top right
   * 2 = bottom left
   * 3 = bottom right.
   @return the coordinates or null if the desired corner 
   * is off the board.
   */
  private byte[] getCornerCoordinates(byte x, byte y, byte corner) {
    byte[] retArray = null;
    if(corner < 2) {
      y--;
    else {
      y++;
    }
    // Where the corner is on the grid depends on 
    // whether this is an odd row or an even row:
    if((corner % == 0&& (y % != 0)) {
      x--;
    else if((corner % != 0&& (y % == 0)) {
      x++;
    }
    try {
      if(myGrid[x][y> -15) {
  // we don't really care about the value, this
  // if statement is just there to get it to 
  // throw if the coordinates aren't on the board.
  retArray = new byte[2];
  retArray[0= x;
  retArray[1= y;
      }
    catch(ArrayIndexOutOfBoundsException e) {
      // this throws if the coordinates do not correspond 
      // to a square on the board. It's not a problem, 
      // so we do nothing--we just return null instead 
      // of returning coordinates since no valid 
      // coordinates correspond to the desired corner.
    }
    return(retArray);
  }
  /**
   * Determines where the piece in the given 
   * grid location can move.  Clears the Vector
   * and fills it with the locations that 
   * the piece can move to.
   @param jumpsOnly if we should return only moves that 
   *        are jumps.
   */
  private void getMoves(byte x, byte y, Vector toFill, boolean jumpsOnly) {
    toFill.removeAllElements();
    // if the square does not contain one of the local player's 
    // pieces, then there are no corresponding moves and we just
    // return an empty vector.
    if(myGrid[x][y<= 0) {
      return;
    }
    // check each of the four corners to see if the 
    // piece can move there:
    for(byte i = 0; i < 4; i++) {
      byte[] coordinates = getCornerCoordinates(x, y, i);
      // if the coordinate array is null, then the corresponding 
      // corner is off the board and we don't deal with it.
      // The later two conditions in the following if statement
      // ensure that either the move is a forward move or the 
      // current piece is a king:
      if((coordinates != null&&
   ((myGrid[x][y1|| (i < 2))) {
  // if the corner is empty (and we're not looking 
  // for just jumps), then this is a possible move
  // so we add it to the vector of moves:
  if((myGrid[coordinates[0]][coordinates[1]] == 0&& (! jumpsOnly)) {
    toFill.addElement(coordinates);
    // if the space is o

上一页  [1] [2] [3] [4] [5] [6] [7] [8]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码