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

📄 boardmediator.java

📁 --- --- --- 基于J2ME的游戏程序--------很有技巧性的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   * Returns the one <code>LocalPlayer</code>. If the game is local,
   * arbitrary <code>LocalPlayer</code> is returned. This method only makes
   * sense in remote games. 
   * @return The <code>LocalPlayer</code> in the game.
   */
  public static LocalPlayer getLocalPlayer()
  {
    Player p1 = BoardState.getInstance().getCurrentPlayer();
    Player p2 = BoardState.getInstance().getWaitingPlayer();
    if (p1 instanceof LocalPlayer)
    {
      return (LocalPlayer)p1;
    }
    else if (p2 instanceof LocalPlayer)
    {
      return (LocalPlayer)p2;
    }
    else
    {
      return null;
    }
  }
  
  /**
   * Returns whether the current turn belongs to a remote player or not.
   * @return true if current turn is a remote player, false otherwise.
   */
  public static boolean isRemoteTurn()
  {
    return BoardState.getInstance().getCurrentPlayer() instanceof RemotePlayer;
  }

  /**
   * Returns possible moves based on current board state.
   * @see bluegammon.logic.BoardState#getPossibleMoves(boolean)
   * @return Array with possible moves.
   */
  public static int[][] getPossibleMoves()
  {
    return BoardState.getInstance().getPossibleMoves(isCurrentPlayerWhite());
  }

  /**
   * Returns number of possible moves based on current board state
   * for player with current turn. If current turn is not yet resolved,
   * zero is returned.
   * @return	number of possible moves.
   */
  public static int countPossibleMoves()
  {
    if (BoardState.getInstance().getCurrentPlayer() == null)
    {
      return 0;
    }
    else
    {
      return BoardState.getInstance().countPossibleMoves(isCurrentPlayerWhite());
    }
  }

  /**
   * Returns whether the game is finished or not
   * @return	true if finished, false otherwise
   */
  public static boolean isGameFinished()
  {
    return BoardState.getInstance().isGameFinished();
  }
  
  /**
   * Forces the game state to be finished.
   */
  public static void forceGameFinished()
  {
    BoardState.getInstance().setGameFinished(true);
  }

  /**
   * Returns number of possible undoable moves
   * @return number of possible undoable moves
   */
  public static int countUndoableMoves()
  {
    return BoardState.getInstance().countUndoableMoves();
  }

  /**
   * Returns a new dice value, a random integer between 1 and 6.
   * @return A new dice value between 1 and 6.
   */
  public static int newDiceValue()
  {
      return Math.abs(Rand.random() % 6) + 1;
  }
  
  /**
   * Returns value of specified dice
   * @param index the dice, 0 or 1
   * @return a value between 1 and 6
   */
  public static int getDiceValue(int index)
  {
    return BoardState.getInstance().getDiceValue(index);
  }

	
  /**
   * Returns number of pieces on specified index in current state.
   * If there are white pieces,
   * number of white pieces are returned. If there are black pieces, number of
   * black pieces are returned. Otherwise zero is returned.
   * 
   * @param index	The board index
   * @return	Number of pieces on index.
   */
  public static int countStatePieces(int index)
  {
    return BoardState.getInstance().countPieces(index);
  }

  /**
   * Returns the boardcanvas singleton instance.
   * @return The boardcanvas singleton instance.
   */
  public static BoardCanvas getCanvas()
  {
    return BoardCanvas.getInstance();
  }
  
  /**
   * Finishes a game of backgammon. Saves the score to persistence.
   * @param whiteWinner		Color of winner, true for white, false for black.
   * @param loserPiecesLeft	Number of pieces left for loser.
   * @param points			Number of points per piece.
   * @param fireAnimation	Show winning animation
   */
  protected static void finishGame(boolean whiteWinner, int loserPiecesLeft, int points,
      boolean fireAnimation)
  {
    commitTurn();
    if (Bluegammon.getGameType() == Bluegammon.GAME_TYPE_LOCAL)
    {
      // Wipe out the local saved game since we're finished
      RmsFacade.setBoolean(Bluegammon.HAS_SAVED_LOCAL_GAME, false);
      RmsFacade.set(Bluegammon.SAVED_GAME_DATA, null);
      Audio.playSound(Audio.WINNER);
    }
    else
    {
      // Save the stats if this remote game
      boolean localWinner = getLocalPlayer().isWhite() && whiteWinner ||
      						!getLocalPlayer().isWhite() && !whiteWinner;
      GameRecord.updateGameResult(getOpponentPlayer(), localWinner,
          points * loserPiecesLeft);
      // Wipe out remote saved game since we're finished
      GameRecord.saveGame(getOpponentPlayer(), null);
      Audio.playSound(localWinner ? Audio.WINNER : Audio.LOSER);
    }
    BoardState.getInstance().setGameFinished(true);
    if (fireAnimation)
    {
      BoardCanvas.getInstance().finishGame(whiteWinner, loserPiecesLeft, points);
    }
  }
  
  // Loading and saving

  /**
   * Loads a saved game from specified input stream. If the game
   * is a remote game, the colors of the players are set as specified
   * in the data stream. If the data is loaded from a remote device
   * (i.e. the opponent), the player colors need to be flipped according
   * to what is specified in saved data.
   * 
   * @param is				The input stream to load saved game data from.
   * @param	flipColors		Flag indicating if the colors of the players
   * 						should be flipped according to what is specified
   * 						in the saved game data.
   * @throws IOException    If error occurs when loading data.
   */
  public static void loadGame(InputStream is, boolean flipColors) throws IOException
  {
    DataInputStream dis = null;
    BoardState state = BoardState.getInstance();
    BoardCanvas canvas = BoardCanvas.getInstance();
    try
    {
      canvas.invalidate();
      dis = new DataInputStream(is);
      state.loadState(dis);
      canvas.loadCanvas(dis);
      Rules.loadRules(dis);
      Rand.loadSeed(dis);
      boolean localWhite = dis.readBoolean();
      
      if (Bluegammon.getGameType() != Bluegammon.GAME_TYPE_LOCAL)
      {
        // Set colors
        localWhite ^= flipColors;
        boolean currentTurnWhite = state.getCurrentPlayer().isWhite();
        getLocalPlayer().setWhite(localWhite);
        getOpponentPlayer().setWhite(!localWhite);
        
        // Current player is set in state.loadState,
        // but since colors might have changed we 
        // need to set it again
        state.setCurrentPlayer(currentTurnWhite);
      }
      for (int i = 0; i < Board.MAX_POS; i++)
      {
        canvas.setPieces(true, i, state.countPieces(true, i));
        canvas.setPieces(false, i, state.countPieces(false, i));
      }
    }
    finally
    {
      if (dis != null)
      {
        dis.close();
      }
    }
    Player currentPlayer = state.getCurrentPlayer();
    canvas.setCurrentLocalPlayer(
        (currentPlayer instanceof LocalPlayer ? (LocalPlayer)currentPlayer : null));
    canvas.repaint();
  }
  
  /**
   * Saves current game to specified output stream.
   * @param os				The output stream to write saved game data to.
   * @return				Number of bytes written to output stream.
   * @throws IOException	If error occurs when writing saved game data.
   */
  public static int saveGame(OutputStream os) throws IOException
  {
    DataOutputStream dos = null;
    int len = 0;
    try
    {
      dos = new DataOutputStream(os);
      len += BoardState.getInstance().saveState(dos);
      len += BoardCanvas.getInstance().saveCanvas(dos);
      len += Rules.saveRules(dos);
      len += Rand.saveSeed(dos);
      dos.writeBoolean(getLocalPlayer().isWhite());
      len++;
    }
    finally
    {
      if (dos != null) dos.close();
    }
    return len;
  }

  // Player stats
  
  /**
   * Returns the color of current player.
   * @return	true for white player, false for black player
   */
  public static boolean isCurrentPlayerWhite()
  {
    return BoardState.getInstance().isCurrentPlayerWhite();
  }

  // BoardStateListener implementation
  
  // see interface javadoc
  public void turnChange(boolean whiteTurn)
  {
  }

  // see interface javadoc
  public void pieceMoved(boolean white, int from, int to)
  {
    BoardCanvas.getInstance().movePiece(white, from, to,
        BoardState.getInstance().countPieces(from),
        BoardState.getInstance().countPieces(to)-1);
  }

  // see interface javadoc
  public void undoAdded(int undos, int diceValue)
  {
  }

  // see interface javadoc
  public void undoPerformed(int undos, int diceValue)
  {
    BoardCanvas.getInstance().undoConsumedDiceValue(diceValue);
  }

  // see interface javadoc
  public void gameFinished(boolean whiteWinner, int loserPiecesLeft, int points)
  {
    finishGame(whiteWinner, loserPiecesLeft, points, true);
  }
  
  /** Prevent construction */
  private BoardMediator()
  {}
}

⌨️ 快捷键说明

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