Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.stream.*;
- public class Deal
- {
- //instance variables
- private int prizeLoc;
- private int userGuess;
- private int view;
- private Random rand = new Random();
- //constructor
- public Deal()
- {
- setPrizeLoc();
- setUserGuess();
- }
- //modifier method for userGuess (should call random number method)
- public void setUserGuess() {
- userGuess = rand();
- }
- //modifier method for prizeLoc (should call random number method)
- public void setPrizeLoc() {
- prizeLoc = rand();
- }
- //accessor method for userGuess
- public int getUserGuess() {
- return userGuess;
- }
- //accessor method for prizeLoc
- public int getPrizeLoc() {
- return prizeLoc;
- }
- //method to generate random number between 1 & 3
- private int rand() {
- return rand.nextInt(3) + 1;
- }
- //method to reveal the door
- public int view() {
- try {
- view = IntStream.rangeClosed(1, 3)
- .filter(n -> n != userGuess && n != prizeLoc)
- .findAny()
- .orElseThrow(() -> new Exception());
- } catch(Exception e) {
- System.err.println(e);
- view = -1;
- }
- return view;
- }
- //method to update user's guess
- public int updateGuess() {
- try {
- return IntStream.rangeClosed(1, 3)
- .filter(n -> n != userGuess && n != view)
- .findAny()
- .orElseThrow(() -> new Exception());
- } catch(Exception e) {
- System.err.println(e);
- return -1;
- }
- }
- //toString method
- public String toString()
- {
- return "user guess: " + userGuess + " prizeLoc: " + prizeLoc;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement