Question: Question: Using the Observer pattern, add a Model-View-Controller GUI to your dice game. If any design changes are required, make them as necessary when adding
Question: Using the Observer pattern, add a Model-View-Controller GUI to your dice game. If any design changes are required, make them as necessary when adding the GUI.
I have this code, how do I add a GUI for it? I have no idea.
mAin.java
package dice.game; //import dice.game.Dice; //import dice.game.SevenIsWin; //import dice.game.GUI;
import java.util.*;
public class mAin { protected static Scanner ui = new Scanner(System.in); /* * Implements a Scanner called ui for future use */ public static void main(String[] args){ /* * The main program where the GUI will be implemented in the future * This is where everything is called to make the program run */ SevenIsWin Mode1 = new SevenIsWin(); Mode1.SevenIsTheLucky(); } }
public class Dice { private int face = 0; public void RollAction(){ /* * This creates the roll action for a die * This randomizes the face of the die to simulate a roll */ face = 1 + (int) ((Math.random() * (6 - 1)) + 1); } int getFace(){ /* * Returns the value of the face of the die */ return face; } void printFace(){ /* * Prints out the value of the face of the die */ System.out.println(" You got number" + face); } Dice(){ /* * Constructor */ face = 0; } }
public class SevenIsWin { private int gameWin = 0; public void SevenIsTheLucky(){ /* * This game is where you take two dice and take their sum * If the sum = 7, the user wins * If the sum != 7, the user loses * The game asks if the user wants to play or not * If the user enters 1, y, yes, Y, or Yes, the game will play * If the user enters 2, n, no, N, or No, the game will exit * If the user enters anything else, the game repeats the question * The game loops until the user tells the game to exit */ Dice dice1 = new Dice(); Dice dice2 = new Dice(); String answer = "System.RepeatLoop"; while("System.RepeatLoop".equals(answer)){ System.out.println("Would you like to throw the dice?" +" 1: Yes 2: No "); answer = ui.nextLine(); while("1".equals(answer) || "yes".equals(answer) || "y".equals(answer) || "Y".equals(answer) || "Yes".equals(answer)) { dice1.RollAction(); dice2.RollAction(); int diceSum = dice1.getFace() + dice2.getFace(); System.out.println(" You got number "+diceSum); if(diceSum == 7){ System.out.println("You win!!! "); this.gameWin = 1; } if(diceSum != 7){ System.out.println("You lose!!! "); } System.out.println("Would you like to throw the dice?" + " 1.Yes 2.No "); answer = ui.nextLine(); } if("2".equals(answer) || "n".equals(answer) || "no".equals(answer) || "N".equals(answer) || "No".equals(answer)){ System.out.println(" Goodbye "); } else { System.out.println(" I'm sorry, I did not understand that. "); answer = "System.RepeatLoop"; } } } SevenIsWin(){ /* * Constructor */ gameWin = 0; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
