I n the game of Monopoly, players roll a pair of dice when it is their turn to p
ID: 661437 • Letter: I
Question
In the game of Monopoly, players roll a pair of dice when it is their turn to play. The player moves a marker forward on the playing board by the sum of the numbers showing on the dice, and then completes his or her turn. However, if the roll of the dice results in "doubles" (the same number on each die), the player gets to roll the dice again (effectively taking another turn without waiting for the other players to have a turn). The same rules apply to the second roll of the dice. It is possible to get a third successive turn by rolling two double in a row. However, if on the third turn doubles result again, the player must "go to jail," which ends the turn.
write in java please, thank you
Design a program which models a single player's turn in Monopoly using a finite-state with output. Let = {doubles, not doubles} and Gamma = {turn done, roll again, go to jail}. You do not need to have transitions from any state that signals the end of the players turn.Explanation / Answer
//YOU MUST IMPORT THE FOLLOWING import java.util.*; import java.io.*; import javax.swing.*; import java.awt.*; public class Monopoly { //THESE VARIABLES ARE OPTIONAL public static final int NUM_SPACES = 40; public static final int MAX_OUT_OF_JAIL_ATTEMPTS = 3; //THESE VARIABLES ARE MANDATORY (I recommend using these names) public static ArrayList players; public static ArrayList properties; public static Die roller; public static Token tokens; public static MonopVars vars; public static Scanner in; //USE MY MAIN METHOD AS A GUIDE FOR BUILDING YOUR PROGRAM /** * This is the main method of the program. * * @param args this is the necessary String array (not used). */ public static void main (String args[]) throws IOException, BadDataException { boolean quitGame = false; int whoseTurn = 0; //YOU MUST INCLUDE THIS LINE OF CODE initializeData (); //THE REMAINING CODE IS OPTIONAL //print the opening message System.out.println (getOpeningMessage ()); //receive all player information //receivePlayerInformation(); Player p1 = new Player ("Tom", 0); Player p2 = new Player ("Evan", 1); players.add (p1); players.add (p2); //show the instructions if the user wishes to see them. if (chooseShowInstructions ()) showInstructions (); //print the message to begin the game. System.out.println (getStartGameMessage ()); //loop until a player chooses to quit the game do { Player curPlayer = players.get(whoseTurn); System.out.print (" *************************************" + " * * * * * * * * * * * * * * " + " ************************************* "); if (!curPlayer.isInJail ()) quitGame = performTurn (curPlayer); else quitGame = performJailTurn (curPlayer); whoseTurn++; if (whoseTurn == players.size()) whoseTurn = 0; } while (!quitGame); }//end main method /** * This method prints a menu of choices for the player * to choose from on each turn. * * @return a char representing the player's choice. */ public static int enterMenuChoice (Player player) { System.out.println (player.getName() + ", it is your turn, what would you like to do?"); System.out.print (" 1: Roll 2: Quit Enter choice: "); return in.nextInt(); }//end enterMenuChoice method /** * Perform the proper game action based upon the player's choice. * * Precondition: The enterMenuChoice method should * be called and the result sent as the parameter choice. * * @param player the player taking the turn. */ public static boolean performTurn (Player player) { boolean done = false; do{ int choice = enterMenuChoice (player); if (choice == 1) { int die1 = roller.nextRoll (); int die2 = roller.nextRoll (); int diceTotal = die1 + die2; if(die1 != die2) { System.out.println ("You rolled a " + die1 + " and a " + die2 + ". Move forward " + diceTotal + " spaces."); player.movePiece(diceTotal); done = true; } else { if(player.getDoubles() < 2) { player.addDoubles(); System.out.println ("You rolled two " + die1 + "'s. Move forward " + diceTotal + " spaces and roll again"); } else { player.addDoubles(); System.out.println("You rolled doubles three times in a row and must go to jail."); } } }//end if else if (choice == 2) { return true; }//end else if else { System.out.println ("Invalid input"); }//end else }while(!done); return false; }//end performTurn method /** * This method prints a menu of choices for the player * to choose from if in jail. * * @return a char representing the player's choice. */ public static int enterJailMenuChoice (Player player) { return 1; }//end enterJailMenuChoice method /** * Perform the proper action for getting out of jail based upon * the player's choice. *
* Precondition: The enterJailMenuChoice method should * be called and the result sent as the parameter choice. * * @param player the player taking the turn. */ public static boolean performJailTurn (Player player) { return false; }//end performJailTurn method //ADD THE FOLLOWING TWO METHODS /* * This method initializes all data for the game. */ public static void initializeData () throws IOException, BadDataException { boolean fileFound = false; //Initialize the objects in = new Scanner (System.in); players = new ArrayList(); properties = new ArrayList(); tokens = new Token(); vars = new MonopVars(); MonopolyDataReader reader = new MonopolyDataReader(); do { //try to obtain a valid file try { reader.readMonopolyFile (chooseFile(), vars, tokens, properties); fileFound = true; }//end try catch (FileNotFoundException e) { System.out.println (" File not found. Please try again."); }//end catch }//end do while (!fileFound); roller = new Die (vars.getNumDiceSides()); }// end of initializeData method /* * This method uses a JFileChooser to locate the data file * to be used in this version of Monopoly. * * @return the name of the file to be used for this game */ public static String chooseFile () { String filename = new String(); boolean fileChosen = false; //This window is the parent for the JFileChooser JWindow window = new JWindow(); //YOU CAN CHANGE THE INITIAL PATH FOR THE JFileChooser TO LOOK IN! //Perhaps the Desktop? JFileChooser chooser = new JFileChooser ("/users/ehorgan/Desktop/monopoly_v2 - Dietzler version/MonopolyData.txt"); do { try { //Opens the JFileChooser chooser.showOpenDialog(window); //Returns the name of the file that was chosen. filename = chooser.getName (chooser.getSelectedFile()); fileChosen = true; }//end try catch (NullPointerException e) { System.out.println ("Please choose a file before continuing."); }//end catch } while (!fileChosen); return filename; }// end of chooseFile method //THE REMAINING METHODS ARE OPTIONAL /** * This method can be altered to incorporate different welcome messages. * * @return returns the message. */ public static String getOpeningMessage () { return new String (" " + "** ** ****** *** ** ****** ****** ****** ** ** ** " + "*** *** ** ** **** ** ** ** ** ** ** ** ** ** ** " + "******** ** ** **** ** ** ** ****** ** ** ** ** ** " + "*** *** ** ** *** *** ** ** ** ** ** ** *** " + "*** *** ** ** *** ** ** ** ** ** ** ***** *** " + "*** *** ****** *** ** ****** ** ****** ***** *** " + " " + (vars.getVersion()).toUpperCase() + " EDITION"); }//end getOpeningMessage method /** * This method receives all information for and initializes the player * objects. */ public static void receivePlayerInformation () { String nameEntry = new String (); int pieceChoice = 0, numPlayers = 0; char choice; //Receive the number of players numPlayers = enterNumPlayers(); for (int i = 0; i
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.