import javax.swing.JOptionPane; public class GamePlayer { static Object[] gameOptions = {"QUIT", "GUESS", "NIM", "TIC_TAC_TOE"}; //Play a series of Human-Computer Interactive Games public static void main(String[] args) { InteractiveGame game; do { game = promptForGame(); if (game != null) play( game ); } while ( game != null ); } //Prompt the (human) player for the name of the game and its parameters: // 1) cyberSmart :Computer plays smart 2) humanFirst: Human plays first // //Returns a new instance of the selected game // or null if the player wants to quit private static InteractiveGame promptForGame() { //Choose a Human-Computer Interactive Game, or Quit String gameName = (String)JOptionPane.showInputDialog(null, "Choose a game, or QUIT", "GAME MENU", JOptionPane.INFORMATION_MESSAGE, null, gameOptions, gameOptions[0] ); if (gameName.equals(gameOptions[0])) return null; // To Quit //Prompt the Human for the Human-Computer Interactive Games options boolean cyberSmart = JOptionPane.showConfirmDialog(null, gameName + ": Computer to play SMART?") == JOptionPane.YES_OPTION; boolean humanFirst = JOptionPane.showConfirmDialog(null, gameName + ":Do you want to play FIRST?") == JOptionPane.YES_OPTION; switch ( gameName ) { case "GUESS" : return new GuessingGame(cyberSmart, humanFirst ); case "NIM" : return new Nim(cyberSmart, humanFirst ); //case "TIC_TAC_TOE": return new TicTacToe(cyberSmart, humanFirst ); default : return null; } } //Play a selected Human-Computer Interactive Games by alternating turns // between the HUMAN and the COMPUTER private static void play(InteractiveGame game) { while ( !game.isCompleted() ) if ( game.isPlayersTurn() ) game.makePlayersMove( promptForMove(game) ); else game.makeComputersMove(); String message = game + "\n\n" + (game.playerHasWon() ? "Congratulations! You WON!" : "Better Luck Next Time!" ); JOptionPane.showMessageDialog(null, message); } //Prompt the HUMAN PLAYER to enter their next move //The prompt is repeated if an invalid move is entered public static String promptForMove(InteractiveGame game) { do { String playersMove = JOptionPane.showInputDialog( null, game.movePrompt() + " ?" ); if ( game.isValidMove(playersMove) ) return playersMove; JOptionPane.showMessageDialog(null, playersMove + " is an INVALID move"); } while ( true ); } }