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

📄 tictactoegamecontrol.java

📁 jxta官方例程
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/* * TicTacToePlugin.java * * Created on February 8, 2005, 2:34 AM */package net.jxta.myjxta.plugins.tictactoe;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import java.util.Timer;import java.util.TimerTask;import javax.imageio.ImageIO;import javax.imageio.ImageReader;import javax.imageio.stream.FileImageInputStream;import javax.imageio.stream.MemoryCacheImageInputStream;import javax.swing.ImageIcon;import net.jxta.endpoint.ByteArrayMessageElement;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.endpoint.StringMessageElement;import net.jxta.myjxta.View;import net.jxta.myjxta.dialog.Dialog;import net.jxta.myjxta.dialog.DialogListener;import net.jxta.myjxta.dialog.DialogMessage;import org.apache.log4j.Level;import org.apache.log4j.Logger;/** * * @author Jeff Moore */public class TicTacToeGameControl implements DialogListener, MoveListener{            /** Time to wait for response after a command  message is sent     *  out. Default is 10 seconds. */    private static final long DEFAULT_MESSAGE_ACK_TIMEOUT = 60000;        /** Minimum time we will wait till a message ack comes back */    private static final long MINIMUM_MESSAGE_ACK_TIMEOUT = 2000;    /** Timeout waiting for config request */    private static final long CONFIG_WAIT_TIMOUT = 20000;            /** Tag for an element element carrying voice data */    public static final String TAG_POSITION_DATA = "PositionData";        public static final String TAG_ICON_DATA = "IconData";        public static final String TAG_ICON_TYPE = "IconType";        /** Element tag denoting a message containing TTT session command data */    public static final String TAG_SESSION_COMMAND = "TTTSessionCommand";        /** commands sent btn peers managing the TTT session */        private static final int CONNECT_SLEEP_TIME = 200;        public static final String COMMAND_INVITE_REQUEST = "InviteRequest";        public static final String COMMAND_INVITE_ACCEPT = "InviteAccept";        public static final String COMMAND_START_GAME_REQUEST = "StartGameRequest";        public static final String COMMAND_START_GAME_ACCEPT = "StartGameAccept";        public static final String COMMAND_CONFIG_REQUEST = "ConfigRequest";        public static final String COMMAND_CONFIG_ACCEPT = "ConfigAccept";        public static final String COMMAND_NEW_MOVE = "NewMove";        public static final String COMMAND_NEW_MOVE_ACCEPT = "NewMoveAccept";        public static final String COMMAND_END_GAME_REQUEST = "EndGameRequest";        public static final String COMMAND_END_GAME_ACCEPT = "EndGameAccept";        public static final String COMMAND_DISCONNECT_REQUEST = "DisconnectRequest";        public static final String COMMAND_DISCONNECT_ACCEPT = "DisconnectAccept";            /** internal session state */            public static final int SESSION_DISCONNECTED = 10;        public static final int SESSION_DISCONNECTING = 11;        public static final int SESSION_CONNECTED = 12;        public static final int SESSION_CONNECTING = 13;        public static final int SESSION_STARTING = 20;        public static final int SESSION_STARTED = 30;        public static final int SESSION_ENDING = 40;        public static final int SESSION_ENDED = 50;        public static final int SESSION_PLAYING = 100;            public static final int SESSION_DISCONNECT_REQUEST_SENT = 210;        public static final int SESSION_DISCONNECT_REQUEST_RECEIVED = 220;        public static final int SESSION_DISCONNECT_ACCEPT_SENT = 230;        public static final int SESSION_DISCONNECT_ACCEPT_RECEIVED = 240;        public static final int SESSION_START_REQUEST_SENT = 340;        public static final int SESSION_START_REQUEST_RECEIVED = 350;        public static final int SESSION_START_ACCEPT_SENT = 360;        public static final int SESSION_CONFIG_REQUEST_SENT = 362;        public static final int SESSION_CONFIG_REQUEST_RECEIVED = 363;        public static final int SESSION_CONFIG_ACCEPT_SENT = 364;        public static final int SESSION_CONFIG_ACCEPT_RECEIVED = 365;        public static final int SESSION_START_ACCEPT_RECEIVED = 370;        public static final int SESSION_END_ACCEPT_RECEIVED = 380;        public static final int SESSION_END_ACCEPT_SENT = 390;        public static final int SESSION_END_REQUEST_RECEIVED = 410;        public static final int SESSION_END_REQUEST_SENT = 420;        public static final int SESSION_INVITE_REQUEST_SENT = 430;        public static final int SESSION_INVITE_REQUEST_RECEIVED = 440;        public static final int SESSION_INVITE_ACCEPT_SENT = 450;        public static final int SESSION_INVITE_ACCEPT_RECEIVED = 460;        public int sessionState = SESSION_DISCONNECTED;        public int protocolState = SESSION_DISCONNECTED;        private long messageAckTimeout = DEFAULT_MESSAGE_ACK_TIMEOUT;        private MessageAckThread messageAckThread = null;        private boolean locallyInitiated = false;    private DialogMessage templateMessage = null;    private TicTacToeDialogPanel tttView = null;        /** in bytes */    public final static long MAX_ICON_SIZE = 60000;        public final static int REMOTE_PLAYER = 2;        public final static int LOCAL_PLAYER = 1;                public final static int GAME_OVER = 1;    public final static int GAME_WAITING = 2;    public final static int GAME_IN_PLAY = 3;    public final static int GAME_LOST = 4;    public final static int GAME_WON = 5;    public final static int GAME_DRAW = 6;                public final static String SESSION_ICON_EXCHANGE = "iconExchange";    private int remotePlayerTotalWins = 0;    private int localPlayerTotalWins = 0;    private int gameState = GAME_OVER;    private int playersTurn = 0;    private List<String> localPlayerMoves = null;    private List<String> remotePlayerMoves = null;    private List<String> allMoves = null;    private Object localIconChosenLock = null;    private String remotePlayerName = null;    private Hashtable<Integer, String> sessionStateTable = null;    private Dialog tttDialog = null;    static final Logger LOG = Logger.getLogger (TicTacToeGameControl.class);    private boolean configured = false;    private TimerTask configWaitTimerTask = null;    private Timer generalTimer = null;        private String[] winningSet = null;        private static final String[] draw = {        "A0", "A1","A2",                "B0","B1","B2",                "C0","C1","C2"    };        private static final String[][] wins = {        { "A0", "A1", "A2" } ,        { "B0", "B1", "B2"},        { "C0", "C1", "C2" } ,        { "A0", "B0", "C0" } ,        { "A1", "B1", "C1" } ,        { "A2", "B2", "C2" } ,        { "A0", "B1", "C2" } ,        { "A2", "B1", "C0" }    };            /** Creates a new instance of TicTacToePlugin */    public TicTacToeGameControl (final TicTacToeDialogPanel tttView, final View myJxtaView, final Dialog tttDialog) {                LOG.setLevel (Level.INFO);                if (LOG.isEnabledFor (Level.INFO)) {            LOG.info ("TicTacToeGameCOntrol constructor");                    }        locallyInitiated=tttView.isLocallyInitiated();        this.sessionStateTable = new Hashtable<Integer, String> ();        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_CONFIG_ACCEPT_RECEIVED), "SESSION_CONFIG_ACCEPT_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_CONFIG_ACCEPT_SENT), "SESSION_CONFIG_ACCEPT_SENT");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_CONFIG_REQUEST_RECEIVED),"SESSION_CONFIG_REQUEST_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_CONFIG_REQUEST_SENT),"SESSION_CONFIG_REQUEST_SENT");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_CONNECTED),"SESSION_CONNECTED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_CONNECTING),"SESSION_CONNECTING");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_DISCONNECTED),"SESSION_DISCONNECTED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_DISCONNECTING),"SESSION_DISCONNECTING");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_DISCONNECT_ACCEPT_RECEIVED),"SESSION_DISCONNECT_ACCEPT_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_DISCONNECT_ACCEPT_SENT),"SESSION_DISCONNECT_ACCEPT_SENT");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_DISCONNECT_REQUEST_RECEIVED),"SESSION_DISCONNECT_REQUEST_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_ENDED),"SESSION_ENDED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_DISCONNECT_REQUEST_SENT),"SESSION_DISCONNECT_REQUEST_SENT");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_ENDING),"SESSION_ENDING");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_PLAYING),"SESSION_PLAYING");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_INVITE_ACCEPT_RECEIVED),"SESSION_INVITE_ACCEPT_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_INVITE_ACCEPT_SENT),"SESSION_INVITE_ACCEPT_SENT");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_INVITE_REQUEST_RECEIVED),"SESSION_INVITE_REQUEST_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_INVITE_REQUEST_SENT),"SESSION_INVITE_REQUEST_SENT");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_STARTED),"SESSION_STARTED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_STARTING),"SESSION_STARTING");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_START_ACCEPT_RECEIVED),"SESSION_START_ACCEPT_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_START_ACCEPT_SENT),"SESSION_START_ACCEPT_SENT");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_START_REQUEST_RECEIVED),"SESSION_START_REQUEST_RECEIVED");        this.sessionStateTable.put (new Integer (TicTacToeGameControl.SESSION_START_REQUEST_SENT),"SESSION_START_REQUEST_SENT");                        this.tttView = tttView;                this.tttDialog = tttDialog;                this.localIconChosenLock = new Object ();                this.templateMessage = new DialogMessage (this.tttDialog.getGroup ()        .getPeerGroup ().getPeerName (), null, this.tttDialog.getGroup ()        .getPeerGroup ().getPeerGroupID ().toString (), this.tttDialog                .getGroup ().getPeerGroup ().getPeerGroupName ());                this.localPlayerMoves = new ArrayList<String> ();                this.remotePlayerMoves = new ArrayList<String> ();                this.allMoves = new ArrayList<String>();                this.tttView.addMoveListener (this);                this.tttDialog.addListener (this);                // wait for pipes to connect        if (this.tttDialog != null) {            new Thread (new Runnable () {                public void run () {                    while (!TicTacToeGameControl.this.tttDialog.isConnected ()) {                        try {                            Thread.sleep (CONNECT_SLEEP_TIME);                                                        if (LOG.isEnabledFor (Level.INFO)) {                                LOG.info ("Waiting for is connected ");                                                            }                        } catch (final InterruptedException ignored) {                            // Interuption ignored                        }                    }                                                                            }            }, getClass ().getName () + ":isConnected").start ();        }                this.generalTimer = new Timer (true);                            }        public String getSessionStateString (final int sessionState) {                return this.sessionStateTable.get (new Integer (sessionState));    }        public void initSession () {        if(isLocallyInitiated ()) {            if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("locally inititated");            }                        this.sendCommand (TicTacToeGameControl.COMMAND_INVITE_REQUEST, TicTacToeGameControl.COMMAND_INVITE_ACCEPT);                        this.setProtocolState (TicTacToeGameControl.SESSION_INVITE_REQUEST_SENT);                        //generalTimer.schedule(roundTripTimerTask, this.ROUND_TRIP_INTERVAL, this.ROUND_TRIP_INTERVAL);        }else{            if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("NOT locally inititated");            }        }    }        public void gameControl (final String command, final DialogMessage msg) {                                                if(COMMAND_INVITE_REQUEST.equals (command) ) {            //call should be in disconnected state for this to happen                        if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("gameControl : received "+TicTacToeGameControl.COMMAND_INVITE_REQUEST+" received");            }            if (getProtocolState () == TicTacToeGameControl.SESSION_DISCONNECTED) {                                setProtocolState (TicTacToeGameControl.SESSION_INVITE_REQUEST_RECEIVED);                                this.sendCommand (TicTacToeGameControl.COMMAND_INVITE_ACCEPT);                                setProtocolState (TicTacToeGameControl.SESSION_INVITE_ACCEPT_SENT);                                                            }else{                if (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("expected "+TicTacToeGameControl.SESSION_DISCONNECTED+" got "+command);                }            }                    }else if(COMMAND_INVITE_ACCEPT.equals (command) ) {                        if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("gameControl : received "+TicTacToeGameControl.COMMAND_INVITE_ACCEPT+" received");            }                                    if (getProtocolState () == TicTacToeGameControl.SESSION_INVITE_REQUEST_SENT) {                                updateAckThread (command);                                setProtocolState (TicTacToeGameControl.SESSION_INVITE_ACCEPT_RECEIVED);                                //we wait on user to config and send config out                            }else{                if (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("expected "+TicTacToeGameControl.SESSION_INVITE_REQUEST_SENT+" got "+command);                }            }                    }else if(COMMAND_CONFIG_REQUEST.equals (command) ) {                        if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("gameControl : received "+TicTacToeGameControl.COMMAND_CONFIG_REQUEST+" received");            }                                    if (getProtocolState () == TicTacToeGameControl.SESSION_INVITE_ACCEPT_SENT) {                                parseConfigMessage(msg);                                setProtocolState (TicTacToeGameControl.SESSION_CONFIG_REQUEST_RECEIVED);                                localActionConfigRequestReceived (msg);                                                                // wait on user to send config accept                            }else{                if (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("expected "+TicTacToeGameControl.SESSION_INVITE_ACCEPT_SENT+" - got "+command);                }            }                    }else if(COMMAND_CONFIG_ACCEPT.equals (command) ) {                        if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("gameControl : received "+TicTacToeGameControl.COMMAND_CONFIG_ACCEPT+" received");            }                                    if (getProtocolState () == TicTacToeGameControl.SESSION_CONFIG_REQUEST_SENT) {                                                updateAckThread (command);                                parseConfigMessage(msg);                                setProtocolState (TicTacToeGameControl.SESSION_CONFIG_ACCEPT_RECEIVED);                                localActionConfigAcceptReceived (msg);                                                                sendCommand (TicTacToeGameControl.COMMAND_START_GAME_REQUEST, TicTacToeGameControl.COMMAND_START_GAME_ACCEPT);                                setProtocolState (TicTacToeGameControl.SESSION_START_REQUEST_SENT);                                /** remote player will make first move */                            }else{                if (LOG.isEnabledFor (Level.INFO)) {                    LOG.info ("expected "+TicTacToeGameControl.SESSION_CONFIG_REQUEST_SENT+" got "+command);                }            }                    }else if(COMMAND_START_GAME_REQUEST.equals (command) ) {                        if (LOG.isEnabledFor (Level.INFO)) {                LOG.info ("gameControl : received "+TicTacToeGameControl.COMMAND_START_GAME_REQUEST+" received");

⌨️ 快捷键说明

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