/** Opens a dialogue and prompts the user to select a game to load. */ private void loadGame() { final int returnVal = fileChooser.showDialog(this, "Load"); if (returnVal == JFileChooser.APPROVE_OPTION) { final File file = fileChooser.getSelectedFile(); if (fileChooser.accept(file)) { IO.loadTiles(game.getBoard(), file); } } }
/** * Scans each possible move and loops through all the possible words that can be formed and prints * highest scoring move. */ public void getBestMove(UI parent, String tilesInHand) { parent.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); parent.setTitle("ScrabbleBot, Finding best move..."); ScrabbleMove bestMove = null; for (final Regex regex : moveFinder.getValidTiles()) { for (final String word : IO.getWords(tilesInHand + regex.playOff)) { if (word.matches(regex.regex) && !word.equals(regex.playOff)) { final ScrabbleMove move = getScrabbleMove(regex, word); if (move != null) { if (bestMove == null || move.wordScore > bestMove.wordScore) { System.out.println("Better move found: " + move); bestMove = move; } } } } } parent.setCursor(Cursor.getDefaultCursor()); parent.setTitle("ScrabbleBot"); if (bestMove != null) { System.out.println("Best move: " + bestMove); final String newWord = bestMove.word.substring(0, 1).toUpperCase() + bestMove.word.substring(1).toLowerCase(); final int option = JOptionPane.showConfirmDialog( parent, "Do you want to add \"" + newWord + "\" to the board?", "Move found: " + newWord + ", which scores: " + bestMove.wordScore + ".", JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE); if (option == JOptionPane.OK_OPTION) { final char[] chars = bestMove.word.toCharArray(); for (int i = 0; i < chars.length && i < bestMove.tiles.size(); i++) { bestMove.tiles.get(i).setLetter(chars[i]); } } } else { JOptionPane.showConfirmDialog( parent, "No moves found.", "Warning.", JOptionPane.OK_OPTION, JOptionPane.WARNING_MESSAGE); } }
/** Opens a dialogue and prompts the user to select a file to save the current game to. */ private void saveGame() { if (fileChooser.showDialog(this, "Save") == JFileChooser.APPROVE_OPTION) IO.saveTiles(game.getBoard(), fileChooser.getSelectedFile()); }