@Override public void handleRobotChangedState( Robot robot, RobotChangedStateListener.RobotChangedStateNotificationType type) { switch (type) { case Online: { view.setCurrentCondition("Online"); // If robot uses Bluetooth LE, Developer Mode can be turned on. // This turns off DOS protection. This generally isn't required. if (robot instanceof RobotLE) { ((RobotLE) robot).setDeveloperMode(true); } // Save the robot as a ConvenienceRobot for additional utility methods mRobot = new ConvenienceRobot(robot); mRobot.enableCollisions(true); // starts collision detection long sensorFlag = SensorFlag.VELOCITY.longValue() | SensorFlag.LOCATOR .longValue(); // use OR bitwise operator to enable velocity and position mRobot.enableSensors( sensorFlag, SensorControl.StreamingRate .STREAMING_RATE10); // enable the sensors using the streaming rate mRobot.addResponseListener(thread); // add the thread to respond to the options thread.setmRobot(mRobot); // give the robot to the thread thread.setRunning(true); // start the thread thread.start(); // start the AI view.repaint(); // update the UI break; } case Connected: { // update the view for each condition view.setCurrentCondition("Connected"); view.repaint(); break; } case Connecting: { view.setCurrentCondition("Connecting"); view.repaint(); break; } case Disconnected: { view.setCurrentCondition("Disconnected"); view.repaint(); break; } case FailedConnect: { view.setCurrentCondition("Failed Connect"); view.repaint(); break; } } }
/** * Allows the input player to play a single ply of Othello. (Make a single move on the board). If * the player has no available moves, this will switch to the other player w/o requesting a move. * If the game is over, this will switch to the GAMEOVER state. */ private void playPly() { if (stateManager.isStateChange()) { MoveList validMoves = GameLogic.getValidMoves(board, curPlayer.getState()); if (validMoves.size() > 0) { // Make your move. aiThread = new AIThread(curPlayer, board.clone(), validMoves.toPoints()); timer.reset(); aiThread.start(); return; } else { nextPlayer(); validMoves = GameLogic.getValidMoves(board, curPlayer.getState()); if (validMoves.size() > 0) { // Other player goes again. output.playerGetsToMoveAgain(curPlayer); aiThread = new AIThread(curPlayer, board.clone(), validMoves.toPoints()); timer.reset(); aiThread.start(); return; } else { // GameOver gameOver(); return; } } } if (aiThread.isReady()) { Move move = aiThread.getMove(); if (move != null) { FlipList flipList = GameLogic.getPointsFlipped(board, move); GameLogic.makeDestructiveMove(board, flipList); output.playerMadeMove(curPlayer, move, flipList, board); nextPlayer(); stateManager.setCurState(GameState.PLAYING); } else { // Forfeit turn due to invalid move. gameOver(matchup.otherPlayer(curPlayer)); return; } } else if (timer.expired()) { output.playerRanOutOfTime(curPlayer); gameOver(matchup.otherPlayer(curPlayer)); return; } }
protected void onStop() { // If the DiscoveryAgent is in discovery mode, stop it. if (DualStackDiscoveryAgent.getInstance().isDiscovering()) { DualStackDiscoveryAgent.getInstance().stopDiscovery(); } // If a robot is connected to the device, disconnect it if (mRobot != null) { mRobot.disconnect(); mRobot = null; } super.onStop(); thread.setRunning(false); }
protected void onDestroy() { DualStackDiscoveryAgent.getInstance().removeRobotStateListener(this); super.onDestroy(); thread.setRunning(false); }
public void onResume() { super.onResume(); thread.setRunning(true); }
public void onPause() { super.onPause(); thread.setRunning(false); }