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

📄 player.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 java.util.Vector;

/**
 * Abstract class for a player. Contains generic functionality and
 * has helper methods for sending events to any <code>PlayerListener</code>.
 * 
 * @author Peter Andersson
 */
public abstract class Player
{
  /** ID of this player */
  protected int m_id;
  /** Name of this player */
  protected char[] m_name;
  /** Player color */
  protected boolean m_white;
  /**
   * Listeners, vector of PlayerListener. Generally in J2ME ,
   * there is support for only one listener when using the observer pattern.
   * Here, we allow multiple listeners to easily enable extensions of the
   * game; even though there is only one listener in this implementation of
   * the game.
   */
  protected Vector m_listeners = new Vector();
  
  /**
   * Initializes this player.
   * @param id		The id of the player.
   * @param name	The name of the player.
   * @param white	The color of the player, true for white, false for black.
   */
  public void init(int id, char[] name, boolean white)
  {
    m_id = id;
    m_name = name;
    setWhite(white);
  }
  /**
   * Sets the color of the player. 
   * @param white	True for white, false for black.
   */
  public void setWhite(boolean white)
  {
    m_white = white;
  }
  /**
   * Returns the id of this player.
   * @return	The player id.
   */
  public int getId()
  {
    return m_id;
  }
  /**
   * Returns the name of this player.
   * @return	The player name.
   */
  public char[] getName()
  {
    return m_name;
  }
  /**
   * Returns the color of this player.
   * @return True for white, false for black.
   */
  public boolean isWhite()
  {
    return m_white;
  }
  /**
   * Adds a listener to this player.
   * @param listener	The listener to add.
   */
  public void addListener(PlayerListener listener)
  {
    m_listeners.addElement(listener);
  }
  
  /**
   * Fires a move made event to listeners.
   * @param possibleMoveIndex	The index of the possible move that
   * 							represents the move.
   */
  protected void fireMoveMade(int possibleMoveIndex)
  {
    synchronized(m_listeners)
    {
      for (int i = m_listeners.size() - 1; i >= 0; i--)
      {
        ((PlayerListener)m_listeners.elementAt(i)).moveMade(getId(), possibleMoveIndex);
      }
    }
  }
  /**
   * Fires a undo performed event to listeners.
   *
   */
  protected void fireUndoPerformed()
  {
    synchronized(m_listeners)
    {
      for (int i = m_listeners.size() - 1; i >= 0; i--)
      {
        ((PlayerListener)m_listeners.elementAt(i)).undoPerformed(getId());
      }
    }
  }
  /**
   * Fires a turn commit event to listeners.
   */
  protected void fireTurnCommit()
  {
    synchronized(m_listeners)
    {
      for (int i = m_listeners.size() - 1; i >= 0; i--)
      {
        ((PlayerListener)m_listeners.elementAt(i)).turnCommit(getId());
      }
    }
  }
  /**
   * Fires a game exited to listeners.
   * @param reason	The reason for exiting the game, one of
   * 	<code>PlayerListener.LOCAL_QUIT</code>, 
   * 	<code>PlayerListener.REMOTE_QUIT</code>, 
   *	<code>PlayerListener.LOCAL_GIVE_UP</code>, 
   * 	<code>PlayerListener.REMOTE_GIVE_UP</code>.
   */
  protected void fireGameExited(int reason)
  {
    synchronized(m_listeners)
    {
      for (int i = m_listeners.size() - 1; i >= 0; i--)
      {
        ((PlayerListener)m_listeners.elementAt(i)).gameExited(getId(), reason);
      }
    }
  }
  /**
   * Fires a message event to listeners.
   * @param message	The message content.
   */
  protected void fireMessageSent(char[] message)
  {
    synchronized(m_listeners)
    {
      for (int i = m_listeners.size() - 1; i >= 0; i--)
      {
        ((PlayerListener)m_listeners.elementAt(i)).messageSent(getId(), message);
      }
    }
  }
}

⌨️ 快捷键说明

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