📄 tictactoegamecontrol.java
字号:
} return TicTacToeGameControl.GAME_DRAW; } for (String[] element : TicTacToeGameControl.wins) { if (this.localPlayerMoves.containsAll(Arrays.asList(element))) { this.winningSet = element; if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("getLocalPlayerBoardState GAME_WON"); } return TicTacToeGameControl.GAME_WON; } } System.out.println("end getlocalplayerboradstate"); return TicTacToeGameControl.GAME_IN_PLAY; } private void dispatchMove(final String moveId) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Dispatch Move : " + moveId); } final DialogMessage msg = (DialogMessage) this.templateMessage.clone(); final StringMessageElement commandElement = new StringMessageElement( TicTacToeGameControl.TAG_SESSION_COMMAND, TicTacToeGameControl.COMMAND_NEW_MOVE, null); msg.addMessageElement(TicTacToeGameControl.TAG_SESSION_COMMAND, commandElement); final StringMessageElement positionElement = new StringMessageElement( TicTacToeGameControl.TAG_POSITION_DATA, moveId, null); msg.addMessageElement(TicTacToeGameControl.TAG_POSITION_DATA, positionElement); this.tttDialog.dispatch(msg); } /** * callback from the view that local player has made a move, the ui has * already been updated * -the order of method calls is important here */ public void localMoveMade(final String moveId) { synchronized (this.localPlayerMoves) { this.localPlayerMoves.add(moveId); this.allMoves.add(moveId); } /** send move to remote peer */ dispatchMove(moveId); final Object[] p = this.localPlayerMoves.toArray(); for (Object element : p) { System.out.print(element); } System.out.println(""); final int boardState = getLocalPlayerBoardState(); if (boardState == TicTacToeGameControl.GAME_DRAW) { this.setPlayersTurn(TicTacToeGameControl.SESSION_ENDED); setGameState(TicTacToeGameControl.GAME_DRAW); this.tttView.setGameDraw(); localActionEndGame(); } if (boardState == TicTacToeGameControl.GAME_WON) { localActionEndGame(); this.setLocalPlayerTotalWins(this.localPlayerTotalWins + 1); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("GameWon localPlayerWins : " + this.localPlayerTotalWins); } setGameState(TicTacToeGameControl.GAME_WON); this.tttView.setLocalPlayerWon(this.winningSet); this.winningSet = null; setPlayersTurn(TicTacToeGameControl.SESSION_ENDED); } else { setPlayersTurn(TicTacToeGameControl.REMOTE_PLAYER); } } private void setGameState(final int gameStatus) { this.gameState = gameStatus; } /** * called when a message comes in from a remtoe peer */ private void remoteMoveMade(final String moveId) { synchronized (this.remotePlayerMoves) { this.remotePlayerMoves.add(moveId); this.allMoves.add(moveId); } /** send remote move to UI */ this.tttView.remoteMoveMade(moveId); final int boardState = getRemotePlayerBoardState(); if (boardState == TicTacToeGameControl.GAME_DRAW) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("GameLost remotePlayerWins : " + this.remotePlayerTotalWins); } setGameState(TicTacToeGameControl.GAME_DRAW); this.tttView.setGameDraw(); localActionEndGame(); } else if (boardState == TicTacToeGameControl.GAME_WON) { //relative to the remote player setGameState(TicTacToeGameControl.GAME_LOST); this.setRemotePlayerTotalWins(this.remotePlayerTotalWins + 1); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("GameLost remotePlayerWins : " + this.remotePlayerTotalWins); } this.tttView.setLocalPlayerLost(this.winningSet); this.winningSet = null; localActionEndGame(); } else { setPlayersTurn(TicTacToeGameControl.LOCAL_PLAYER); } } protected void localActionEndGame() { if (isLocallyInitiated()) { sendCommand(TicTacToeGameControl.COMMAND_END_GAME_REQUEST, TicTacToeGameControl.COMMAND_END_GAME_ACCEPT); setProtocolState(TicTacToeGameControl.SESSION_END_REQUEST_SENT); setPlayersTurn(TicTacToeGameControl.SESSION_ENDED); this.localPlayerMoves.clear(); this.remotePlayerMoves.clear(); this.allMoves.clear(); } } public void localActionInvitePeer() { if (this.getProtocolState() == TicTacToeGameControl.SESSION_DISCONNECTED) { sendCommand(TicTacToeGameControl.COMMAND_INVITE_REQUEST, TicTacToeGameControl.COMMAND_INVITE_ACCEPT); setProtocolState(TicTacToeGameControl.SESSION_INVITE_REQUEST_SENT); } } public boolean isLocallyInitiated() { return locallyInitiated; } protected int getGameState() { return this.gameState; } class GameEvent {// private String action = null;// private String originator = null;// private String moveId = null; private final static String ELEMENT_HEADER = "Jxta:TicTacToe"; private final static String ELEMENT_ACTION = ELEMENT_HEADER + "Action"; private final static String ELEMENT_ORIGINATOR = ELEMENT_HEADER + "Originator"; private final static String ELEMENT_MOVEID = ELEMENT_HEADER + "MoveId"; public GameEvent() { } public GameEvent(final Message m) {// this.action = getElement (m, ELEMENT_ACTION);// this.originator = getElement (m, ELEMENT_ORIGINATOR);// this.moveId = getElement (m, ELEMENT_MOVEID); } protected String getElement(final Message msg, final String tag) { final MessageElement me = msg.getMessageElement(tag); return me != null ? me.toString() : null; } public Message toMessage() { return null; } } /** * Called from the UI. This methodnotify's GameControl the user accepted * an invite to play. */ public void viewActionAcceptInviteRequest() { sendCommand(TicTacToeGameControl.COMMAND_INVITE_ACCEPT); setProtocolState(TicTacToeGameControl.SESSION_INVITE_ACCEPT_SENT); } public String getRemotePlayerName() { return this.remotePlayerName; } protected void localActionConfigAcceptReceived(final DialogMessage msg) { } protected void localActionConfigRequestReceived(final DialogMessage msg) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("localActionConfigRequestReceived"); } if (this.configWaitTimerTask != null) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("localActionConfigRequestReceived: cancel configWaitTimerTask"); } this.configWaitTimerTask.cancel(); } if (this.isConfigured()) { localActionSendConfigAccept(); } else { //we wait for user to configure } } private void parseConfigMessage(final DialogMessage msg) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("localActionReceivedIconCommand"); } final MessageElement iconTypeElement = msg.getMessageElement(TicTacToeGameControl.TAG_ICON_TYPE); this.tttView.setRemotePlayerName(msg.getOriginator()); if (iconTypeElement != null) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("iconTypeElement is not null"); } if (true) { final String iconType = iconTypeElement.toString(); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("icontype is " + iconType); } if (iconType.equals(TicTacToeDialogView.ICON_TYPE_X)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("iconType is iconX"); } this.tttView.setRemotePlayerIconType(TicTacToeDialogView.ICON_TYPE_X); this.tttView.setRemotePlayerIcon(this.tttView.getIconX()); } else if (iconType.equals(TicTacToeDialogView.ICON_TYPE_O)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("iconType is iconO"); } this.tttView.setRemotePlayerIconType(TicTacToeDialogView.ICON_TYPE_O); this.tttView.setRemotePlayerIcon(this.tttView.getIconO()); } else if (iconType.equals(TicTacToeDialogView.ICON_TYPE_CUSTOM)) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("icontype is custom"); } final MessageElement iconDataElement = msg.getMessageElement(TicTacToeGameControl.TAG_ICON_DATA); if (iconDataElement != null) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("icondataelement is not null"); } if (iconDataElement instanceof ByteArrayMessageElement) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("icondata element is a bytearray message element"); } final byte[] imageData = ((ByteArrayMessageElement) iconDataElement).getBytes(); this.tttView.setRemotePlayerIconType(TicTacToeDialogView.ICON_TYPE_CUSTOM); this.tttView.setRemotePlayerIcon(new ImageIcon(imageData));//this.getMemImage (imageData)); } } } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("remote icontype unknown[" + iconType + "][" + TicTacToeDialogView.ICON_TYPE_X + "]"); } } } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("icontypeelement not instance of bytearraymessageelement"); } } } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("icontypeelement is null"); } } } private void setConfigured(final boolean configured) { this.configured = configured; } private boolean isConfigured() { return this.configured; } public void viewActionConfigured() { this.setConfigured(true); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("viewActionConfigured"); } if (isLocallyInitiated()) { this.localActionSendConfigRequest(); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("viewActionConfigured: Locally initited"); } } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("viewActionConfigured: NOT Locally initited"); } if (this.getProtocolState() == SESSION_CONFIG_REQUEST_RECEIVED) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("viewActionConfigured: config request has been received"); } localActionSendConfigAccept(); } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("viewActionConfigured: config request has NOT been received. starting timer"); } /** we'll wait 40 second for a config to come from the controlling * peer. if none arrives then we go down. */ this.configWaitTimerTask = new TimerTask() { public void run() { if (getProtocolState() == SESSION_CONFIG_REQUEST_RECEIVED) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("configWaitTimerTask: CONFIG REQUEST Received"); } localActionSendConfigAccept(); } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("configWaitTimerTask: Timeout wating for remote config"); } destroy(); } } }; this.generalTimer.schedule(this.configWaitTimerTask, CONFIG_WAIT_TIMOUT);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -