Package vrampal.connectfour.core

Examples of vrampal.connectfour.core.Game


  private GameData gameData;

  @Before
  public void setUp() {
    GameFactory gameFactory = GameFactoryImpl.getInstance();
    Game game = gameFactory.createGame();

    game.begin();
    game.dropDisc(3);
    game.dropDisc(2);
    game.dropDisc(5);
    game.dropDisc(2);
    game.dropDisc(3);

    gameData = new GameData(game);
  }
View Full Code Here


   * or to handle exceptions.
   */
  void handleRequest(HttpServletRequest req) {
    // Get elements from the session
    HttpSession session = req.getSession();
    Game game = (Game) session.getAttribute(SESSION_GAME_KEY);

    // Create game or reset it if necessary
    String resetStr = req.getParameter(PARAM_RESET_KEY);
    if ((game == null) || (resetStr != null)) {
      // TODO use dependency injection to avoid dependency on implementation
      game = GAME_FACTORY.createGame();
      game.begin();
    }

    // Handle play parameter
    String subMessage = "";
    String colStr = req.getParameter(PARAM_PLAY_KEY);
    if (colStr != null) {
      try {
        int colIdx = Integer.parseInt(colStr);
        game.dropDisc(colIdx - 1);
      } catch (NumberFormatException e) {
        subMessage = "Invalid parameter col: " + colStr;
        log.error("Invalid column", e);
      } catch (ConnectFourException e) {
        subMessage = e.getMessage();
        log.error("Unable to play", e);
      }
    }

    // Build main message
    String mainMessage = "";
    if (game.getStatus() == GameStatus.ONGOING) {
      Player player = game.getCurrentPlayer();
      mainMessage = "Now playing: " + player.getName();
    } else if (game.getStatus() == GameStatus.FINISHED) {
      Player winner = game.getWinner();
      if (winner != null) {
        mainMessage = winner.getName() + " won the game.";
      } else {
        mainMessage = "It's a draw game.";
      }
    }

    // Save request attributes for the view
    req.setAttribute(ATTR_MAIN_MESSAGE_KEY, mainMessage);
    req.setAttribute(ATTR_SUB_MESSAGE_KEY, subMessage);
    req.setAttribute(ATTR_GAME_ID_KEY, game.getId());

    // Save elements in the session
    session.setAttribute(SESSION_GAME_KEY, game);
  }
View Full Code Here

    long beginTime = System.currentTimeMillis();
    for (int i = 0; i < NB_TOTAL_GAME; i++) {
      GameRunner gameRunner = new GameRunner(playerItf, playerItf);
      gameRunner.run();

      Game game = gameRunner.getGame();
      Player winner = game.getWinner();
      analyeWinner(winner);
    }
    long endTime = System.currentTimeMillis();

    if (log.isInfoEnabled()) {
View Full Code Here

    Board board = mock(Board.class);
    when(board.getWidth()).thenReturn(3);
    when(board.getHeight()).thenReturn(2);
    when(board.getCell(anyInt(), anyInt())).thenReturn(player);

    Game game = mock(Game.class);
    when(game.getStatus()).thenReturn(GameStatus.ONGOING);
    when(game.getBoard()).thenReturn(board);

    String retVal = printer.printBoard(game);

    // TODO find a better assert.
    assertNotNull(retVal);
View Full Code Here

TOP

Related Classes of vrampal.connectfour.core.Game

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.