protected void setup() { String gameType = applet.getSelectedGameType(); Logic logic = applet.getGameLogic(gameType); if (logic == null) { applet.output("You must select a game type."); return; } controller = new GameController(logic); // Find out the X Player String xType = applet.getXPlayChoice(); // Find out the O Player String oType = applet.getOPlayChoice(); computerGoesFirst = false; computerPlaysSelf = true; boolean setOpponentO = false; boolean setOpponentX = false; Player p; if (oType.equals(TicTacToeApplet.Human)) { // Even though this is a human player, it will be involved in // p = new MousePlayer(Player.OMARK); p.score(new BoardEvaluation()); computerPlaysSelf = false; humanPlayer = (MousePlayer) p; } else { p = PlayerFactory.createPlayer(oType, Player.OMARK); if (p == null) { int ply = 5; // default try { ply = Integer.valueOf(applet.getOPly().getText()); } catch (Exception _e) { } p = PlayerFactory.createPlayerWithPly(oType, Player.OMARK, ply); setOpponentO = true; } } controller.setOPlayer(p); if (xType.equals(TicTacToeApplet.Human)) { p = new MousePlayer(Player.XMARK); p.score(new BoardEvaluation()); computerPlaysSelf = false; humanPlayer = (MousePlayer) p; } else { computerGoesFirst = true; p = PlayerFactory.createPlayer(xType, Player.XMARK); if (p == null) { int ply = 5; // default try { ply = Integer.valueOf(applet.getXPly().getText()); } catch (Exception _e) { } p = PlayerFactory.createPlayerWithPly(xType, Player.XMARK, ply); setOpponentX = true; } } controller.setXPlayer(p); // set logic. ((Player) controller.getXPlayer()).logic(logic); ((Player) controller.getOPlayer()).logic(logic); // set opponents. if (setOpponentX) { ((IntelligentAgent) controller.getXPlayer()).opponent(controller.getOPlayer()); } if (setOpponentO) { ((IntelligentAgent) controller.getOPlayer()).opponent(controller.getXPlayer()); } }
/** * React only to MouseClicked Events (where user presses and releases). * * <p>Can't be called until after the mousePlayer has been properly initialized. * * @param e the MouseEvent created by user. */ public void mouseClicked(MouseEvent e) { // no game set up yet. if (controller == null) { return; } // prevent any action for a game that isn't in progress. if (controller.getCurrentState() != GameController.IN_PROGRESS) { return; } // If no human player (i.e., computer vs. computer) do nothing if (humanPlayer == null) { return; } // leave if it is not OUR turn. if ((controller.getCurrentTurn() == GameController.XTURN) && (humanPlayer.getMark() != Player.XMARK)) { return; } if ((controller.getCurrentTurn() == GameController.OTURN) && (humanPlayer.getMark() != Player.OMARK)) { return; } int x = e.getX(); int y = e.getY(); // invert the calculations from drawSpot() Cell cell = controller.interpretXY(x, y); Move m = controller.interpretMove(cell, humanPlayer); if (m == null) { applet.output("Invalid move. Try again"); return; } // once we set the move for the player, it will be retrieved // by the controller who then asks the player for his next // move. Pretty tricky, huh? humanPlayer.setMove(m); int rc = controller.playTurn(); // refresh to ensure that move is visible again. applet.repaint(); if (rc != GameController.IN_PROGRESS) { if (rc == GameController.DRAW) { applet.output("Game is drawn"); } else { applet.output("Game over. You Win!"); } } else { // update humanPlayer and deal with auto-play Player currentPlayer; int turn = controller.getCurrentTurn(); if (turn == GameController.XTURN) { currentPlayer = (Player) controller.getXPlayer(); } else { currentPlayer = (Player) controller.getOPlayer(); } if (currentPlayer instanceof MousePlayer) { humanPlayer = (MousePlayer) currentPlayer; return; } // since the current player is NOT human, we auto play. rc = controller.playTurn(); if (rc != GameController.IN_PROGRESS) { if (rc == GameController.DRAW) { applet.output("Game is drawn"); } else { applet.output("Game over. You Lose!!!!!"); } } } }