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

📄 localplayer.java

📁 --- --- --- 基于J2ME的游戏程序--------很有技巧性的程序
💻 JAVA
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind. 
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, 
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A 
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. 
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)

package bluegammon.logic;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;

import bluegammon.Bluegammon;
import bluegammon.Resources;
import bluegammon.gui.BoardCanvas;
import bluegammon.gui.StringInputHandler;
import bluegammon.gui.popup.PopupListener;

/**
 * A local player has access to the actual device. A <code>LocalPlayer</code> is
 * coupled with the <code>BoardCanvas</code>, knowing the low-level gui states
 * and receives interaction calls from the gui.
 * 
 * @author Peter Andersson
 */
public class LocalPlayer extends Player 
	implements StringInputHandler, PopupListener
{
  /** The canvas that this local player uses */
  protected BoardCanvas m_canvas;
  
  /**
   * Constructs a local player.
   * @param id      The id of the local player.
   * @param name    The name of the local player.
   * @param white   The color of the local player.
   * @param canvas  The backgammon board canvas this local
   *                player acts upon.
   */
  public LocalPlayer(int id, char[] name, boolean white, BoardCanvas canvas)
  {
    init(id, name, white);
    m_canvas = canvas;
  }

  /**
   * Called from <code>BoardCanvas</code> when a key is pressed.
   * @param keyCode		The key code.
   * @param gameCode	The game code.
   * @return true if the keypress is consumed, false otherwise
   */
  public boolean keyPressed(int keyCode, int gameCode)
  {
    boolean white = BoardMediator.isCurrentPlayerWhite();
    boolean consumed = false;
    // Check if game is finished or if some state prevents interaction
    if (BoardMediator.isGameFinished() || !m_canvas.isUserMovable())
    {
      return consumed;
    }
    // Check if we are NOT awaiting a commit of user moves -
    // if so, update cursor or move piece
    else if (!m_canvas.waitingForCommit())
    {
        if (BoardMediator.countPossibleMoves() == 0
            && BoardMediator.countUndoableMoves() == 0)
        {
          // No possible moves, any key press changes turn
          BoardMediator.newTurn(!white);
          fireTurnCommit();
          consumed = true;
        }
        else
        {
          // User cursor/piece movement action
          switch (gameCode)
          {
          case Canvas.LEFT: // fall through
          case Canvas.RIGHT:
            m_canvas.cursorSwapSide();
            break;
          case Canvas.UP:
            m_canvas.cursorBackIndex();
            break;
          case Canvas.DOWN:
            m_canvas.cursorNextIndex();
            break;
          case Canvas.FIRE:
            if (m_canvas.isCursorValid())
            {
              int cursor = m_canvas.getCurrentCursorIndex();
              int index = BoardMediator.getPossibleMoves()[cursor][BoardState.PM_SOUR];
              int target = BoardMediator.getPossibleMoves()[cursor][BoardState.PM_DEST];
              BoardMediator.makePlayerMove(cursor);
              fireMoveMade(cursor);
              if (BoardMediator.countPossibleMoves() > 0)
              {
                // There are more possible moves, update cursor to next
                m_canvas.cursorNearestIndex(index, target);
              }
            }
            if (BoardMediator.countPossibleMoves() == 0)
            {
              // There are no possible moves left, query commit
              m_canvas.setQueryCommit(true);
            }

            m_canvas.updateUndoCommand();
            break;
          }

          m_canvas.updateCursor();
        }
        consumed = true;
    }

    // Awaiting commitment, any keypress will do
    else
    {
      // Commit moves
      BoardMediator.commitTurn();
      fireTurnCommit();
      consumed = true;
    }
    return consumed;
  }
  
  /**
   * Called from <code>BoardCanvas</code> when a command is executed.
   * @param c	The command.
   * @return true if event is consumed, false otherwise.
   */	
  public boolean commandAction(Command c)
  {
    boolean consumed = false;
    if (c == BoardCanvas.CMD_UNDO)
    {
      BoardMediator.undoLastMove();
      fireUndoPerformed();
      consumed = true;
    }
    else if (c == BoardCanvas.CMD_EXIT)
    {
      if (Bluegammon.getGameType() == Bluegammon.GAME_TYPE_LOCAL)
      {
        fireGameExited(PlayerListener.LOCAL_QUIT);
        BoardMediator.exitGame(PlayerListener.LOCAL_QUIT);
      }
      else
      {
        if (BoardMediator.isGameFinished())
        {
          fireGameExited(PlayerListener.LOCAL_QUIT);
          BoardMediator.exitGame(PlayerListener.LOCAL_QUIT);
        }
        else
        {
          char[][] alts =
          {
              Resources.getChars(Resources.TXT_GIVE_UP),
              Resources.getChars(Resources.TXT_SAVE_GAME),
              Resources.getChars(Resources.TXT_CANCEL)
          };
          Bluegammon.showPopup(Resources.getChars(Resources.TXT_QUIT_REMOTE), alts,
              0, 2, 2, this);
        }
      }
      consumed = true;
    }
    return consumed;
  }
  
  // see gui.InputHandler#handleInput(java.lang.String) javadoc
  public void handleStringInput(String s)
  {
    if (s != null)
    {
      fireMessageSent(s.toCharArray());
    }
  }

  /**
   * PopupListener implementation, called from a popup.
   */
  public void selectedChoice(byte choice, boolean timeOut)
  {
    switch(choice)
    {
      case 0: // give up
        fireGameExited(PlayerListener.LOCAL_GIVE_UP);
        BoardMediator.exitGame(PlayerListener.LOCAL_GIVE_UP);
        break;
      case 1: // save game
        fireGameExited(PlayerListener.LOCAL_QUIT);
        BoardMediator.exitGame(PlayerListener.LOCAL_QUIT);
        break;
      case 2: // cancel
        break;
    }
  }
}

⌨️ 快捷键说明

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