Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.stream.*;
  3.  
  4. public class Deal
  5. {
  6.     //instance variables
  7.     private int prizeLoc;
  8.     private int userGuess;
  9.    private int view;
  10.    
  11.    private Random rand = new Random();
  12.    
  13.     //constructor
  14.     public Deal()
  15.     {
  16.         setPrizeLoc();
  17.         setUserGuess();
  18.     }
  19.    
  20.     //modifier method for userGuess (should call random number method)
  21.     public void setUserGuess() {
  22.       userGuess = rand();
  23.    }
  24.    
  25.     //modifier method for prizeLoc (should call random number method)
  26.     public void setPrizeLoc() {
  27.       prizeLoc = rand();
  28.    }
  29.    
  30.     //accessor method for userGuess
  31.     public int getUserGuess() {
  32.       return userGuess;
  33.    }
  34.    
  35.     //accessor method for prizeLoc
  36.     public int getPrizeLoc() {
  37.       return prizeLoc;
  38.    }
  39.    
  40.     //method to generate random number between 1 & 3
  41.     private int rand() {
  42.       return rand.nextInt(3) + 1;
  43.    }
  44.    
  45.     //method to reveal the door
  46.     public int view() {
  47.       try {
  48.          view = IntStream.rangeClosed(1, 3)
  49.                          .filter(n -> n != userGuess && n != prizeLoc)
  50.                          .findAny()
  51.                          .orElseThrow(() -> new Exception());
  52.       } catch(Exception e) {
  53.          System.err.println(e);
  54.          view = -1;
  55.       }
  56.       return view;
  57.    }
  58.    
  59.     //method to update user's guess
  60.     public int updateGuess() {
  61.       try {
  62.          return IntStream.rangeClosed(1, 3)
  63.                          .filter(n -> n != userGuess && n != view)
  64.                          .findAny()
  65.                          .orElseThrow(() -> new Exception());
  66.       } catch(Exception e) {
  67.          System.err.println(e);
  68.          return -1;
  69.       }
  70.    }
  71.    
  72.     //toString method
  73.     public String toString()
  74.     {
  75.         return "user guess: " + userGuess + " prizeLoc: " + prizeLoc;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement