Example #1
0
  @Override
  /**
   * Play the game. Alternate between the two players and print the board's current state between
   * each move.
   */
  public GameResult play() {
    Player player1 = getPlayer1();
    Player player2 = getPlayer2();
    while (!gameOver()) {
      Player player = (getTurn() < 0) ? player1 : player2;
      boolean successfulMove = playTurn(player);
      if (!successfulMove) incrementPass();
      else resetPasses();
      player.resetParameters();
      setTurn(-getTurn());
      player.addScoreToHistory(player.getScore());
    }

    player1.removeLastScoreFromHistory();
    player2.removeLastScoreFromHistory();

    for (Character letter : player1.getTilesOnHand()) {
      player1.removePointsFromScore(Alphabet.getLetterPoint(letter));
    }
    for (Character letter : player2.getTilesOnHand()) {
      player2.removePointsFromScore(Alphabet.getLetterPoint(letter));
    }

    player1.addScoreToHistory(player1.getScore());
    player2.addScoreToHistory(player2.getScore());
    GameResult result = new GameResult(player1, player2, isPlayer1StartsPlaying());

    return result;
  }
 private int calculateHighestBonus(String partWord, int bonus, char content, char letter) {
   switch (content) {
     case Square.THREE_LETTER_BONUS:
       int letterBonus = Alphabet.getLetterPoint(letter) * 3;
       bonus = (letterBonus > bonus) ? letterBonus : bonus;
       break;
     case Square.TWO_LETTER_BONUS:
       letterBonus = Alphabet.getLetterPoint(letter) * 2;
       bonus = (letterBonus > bonus) ? letterBonus : bonus;
       break;
     case Square.THREE_WORD_BONUS:
       int wordBonus = ScoreHandler.scoreOf(partWord) * 3;
       bonus = (wordBonus > bonus) ? wordBonus : bonus;
       break;
     case Square.TWO_WORD_BONUS:
       wordBonus = ScoreHandler.scoreOf(partWord) * 2;
       bonus = (wordBonus > bonus) ? wordBonus : bonus;
       break;
   }
   return bonus;
 }
Example #3
0
  public static void main(String[] args) {
    long startTime = System.currentTimeMillis();
    Alphabet.initializeAlphabet(GAME_LANGUAGE);
    Trie.initTrie("dictionary/dsso-1.52_utf8.txt");

    List<GameResult> results = new ArrayList<GameResult>();
    Player player1Type = null;
    Player player2Type = null;
    Board boardType;

    for (int i = 0; i < NR_OF_GAMES; i++) {

      //			System.out.println("Playing game nr " + i);
      boardType = new Board();

      player1Type = new BalancePlayer(boardType);
      player2Type = new HighestBonusRoundPlayer(boardType);

      boolean player1StartsPlaying;

      if (i > (NR_OF_GAMES / 2)) player1StartsPlaying = true;
      else player1StartsPlaying = false;

      GameResult result =
          new GameTests(player1StartsPlaying, player1Type, player2Type, boardType).play();
      results.add(result);
    }
    String player1Name = player1Type.getClass().getSimpleName();
    String player2Name = player2Type.getClass().getSimpleName();
    Date date = Calendar.getInstance().getTime();
    String currentTime = DateFormat.getTimeInstance().format(date);
    String currentDate = DateFormat.getDateInstance().format(date);
    String dateTime = currentDate + "_" + currentTime;
    String fileName =
        player1Type.getName()
            + "_"
            + player2Type.getName()
            + "_"
            + NR_OF_GAMES
            + "_"
            + dateTime
            + ".txt";
    String filePath = player1Name + "_" + player2Name + "/" + NR_OF_GAMES + "/";

    long endTime = System.currentTimeMillis();
    int runTimeSeconds = (int) ((endTime - startTime) / 1000);
    GameTests.printResultsToFile(results, fileName, filePath, runTimeSeconds);
    System.out.println("KLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAR!");
  }