0

im fairly new to java and as my first assignment i need to simulate a MontyHall game using two classes. Door and MontyHall.

My main issue is with the class MontyHall. im having trouble simulating the selection of a random door and proceeding after that. Any help or hints to put me in the right direction would be much help.

import java.util.Random;
public class MontyHall {



  // ADD YOUR INSTANCE VARIABLES HERE

   private int door1; //door that holds the prize
   private int door2; //one of the remaining two doors that doesnt have a prize
   private int door3; //the last remaining door that doesnt have a prize
   private int door4; //a door that the user chooses



   Random generator = new Random();

  /** 
      * Initializes the three doors.
      */
   public MontyHall(){
 // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION

     Door door1 = new Door("prizeDoor");
     Door door2 = new Door("hostDoor");
     Door door3 = new Door("finalDoor");
     Door door4 = new Door("selectedDoor");



   }

  /** 
      * Simulates one Monty Hall game.  
      * <ol>
      * <li>Resets all three doors</li>
   * <li>Simulates the selection of one of the doors by the player</li>
   * <li>Simulates opening of an empty door by the host</li>
   * <li>prints the outcome for switching and not switching door to standard output</li>
   * </ol>
      */
   public void oneGame(){
 // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION

    int door1 = generator.nextInt(3)+1; //assign a value for door1

    System.out.println("The prize was behind door " + door1);

    while(door2 == door1){
      int door2 = generator.nextInt(3)+1; //assign a value for door2
    }


    while(door3 == door1 || door3 == door2){
      int door3 = generator.nextInt(3)+1; //assign a value for door3
    }


   if (((door4 == door1) && (Door.SelectedByPlayer() == true)) || ((door4 == door2) && (Door.SelectedByPlayer() == true))){
    System.out.println("Switching strategy would have lost");
   } else{
    System.out.println("Switching strategy would have won"); 
   }
   }


  /** 
      * Simulates a random selection of one of the three doors.
      * @return the door randomly selected  
      */
   private int pickADoor(){
 // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION



     int door4 = generator.nextInt(3)+1;
     return this.door4;


     System.out.println("The player selected door " + door4);
   }  





 /** 
      * Simulates the opening of one of the other doors once the player selected one.
      * It should  open a door chosen randomly among the ones that don't have the prize and
      * that are not selected by the player
      * 
      *   @param prizeDoor the door that hides the prize
      *   @param selectedDoor the door that was selected by the player
      *   @return the door opened
      */
   private int openOtherDoor(int prizeDoor, int selectedDoor){

 // REPLACE THE BODY OF THIS METHOD WITH YOUR OWN IMPLEMENTATION

     prizeDoor = door1;
     selectedDoor = door4;

     if(selectedDoor == prizeDoor){
       int hostDoor = generator.nextInt(3)+1;
     }
     while(door2 != selectedDoor && door2 != prizeDoor){
       int door2 = generator.nextInt(3)+1;
     }
     return this.door2;

   System.out.println("The host opened door " + door2);

   }
  • One word - [arrays](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). – Boris the Spider Jan 31 '16 at 23:20
  • Welcome to StackOverflow. Please take some time to visit the [help] and also read [ask]. You seem to have some basic conceptual problems in your code, both about Java and about programming in general, which are somewhat beyond StackOverflow's intended scope. SO is not a place for discussions, or where you can get tutorials or cover basic information that is available elsewhere. For your situation, I think talking to the professor or teaching assistant is the right direction. – Jim Garrison Jan 31 '16 at 23:30
  • @samer-alsaadi: did you learnt about arrays already? Also... Considering what kind of state a door has, and what operations a door has. – Luiz Tavares Jan 31 '16 at 23:46
  • As an example: Some doors have goats behind them - maybe you could represent that as a boolean? Also, doors can be .open()ed. hmmm... so maybe they need to track if they have been opened already. Also, MountyHall selects(aDoor), maybe? :) – Luiz Tavares Jan 31 '16 at 23:48
  • @luizTavares your putting me in the right direction. thank you. In order to track if doors have been opened, would if statement be useful? – Samer Alsaadi Feb 01 '16 at 00:31
  • @samer-alsaadi: Could be done that way. – Luiz Tavares Feb 01 '16 at 00:34
  • What I would do is... declare a boolean called "open" on door. declare a open() method, that will toggle that boolean on. – Luiz Tavares Feb 01 '16 at 00:35
  • so now doors know how to open themselves. :) then maybe you need a isOpen() on door, so montyHall can query each door to see if it's opened already :) – Luiz Tavares Feb 01 '16 at 00:36
  • OOP is a bit of a strange thing.... Door opening themselves, who would know that? :) – Luiz Tavares Feb 01 '16 at 00:37
  • @LuizTavares thanks again! these info will help me alot much appreciated – Samer Alsaadi Feb 01 '16 at 00:40
  • Don't give up, programming, logic, and oop is quite hard for most people at start. It will get better with time, practice will make it way easier. – Luiz Tavares Feb 01 '16 at 00:42
  • I completely agree. And it is actually quite alot of fun to be challenged this way – Samer Alsaadi Feb 01 '16 at 00:43
  • Lemme see if I can help a bit more. Everybody mentioned arrays - they're quite useful data structure when handling more than one thing of the same type. If your teacher taught you about them, he/she probably expect you to use it here ;). – Luiz Tavares Feb 01 '16 at 01:06
  • Also, there's four doors in your code, and as far as I know, monty hall only uses three doors: `Door door1 = new Door("prizeDoor"); Door door2 = new Door("hostDoor"); Door door3 = new Door("finalDoor"); Door door4 = new Door("selectedDoor");`. So... Door1 to Door3 are actual, honest doors. But there isn't really a fourth door, right? The guest selects between door1, door2 and door3. So... door4 shouldn't probably be a new instance. maybe `Door selectedDoor` should just point (or refer) to one of the other instances?! – Luiz Tavares Feb 01 '16 at 01:09
  • Yeah its probably a good idea to use arrays. So lets say i declare the 3 doors as String[] doors={"door1","door2","door3"), i then would want to use random generator and then assign each door a value. im guessing using praseInt would help? – Samer Alsaadi Feb 01 '16 at 01:10
  • Doors aren't Strings! They're Doors! How about Door[] doors = new Door[]; – Luiz Tavares Feb 01 '16 at 01:30
  • Then maybe create them? Keeping it simple: Door[0] = new Door()... and so on – Luiz Tavares Feb 01 '16 at 01:31
  • Ohhh gotcha! will fix it now – Samer Alsaadi Feb 01 '16 at 01:32
  • hmm... if you're using the array, why not use the array index to select a door? maybe if you can generate a random number between 0 and 2 (three doors, so they're numbered 0, 1,2, right?) we could use that to select a door :) – Luiz Tavares Feb 01 '16 at 01:32
  • So we got so far... We need An array, a RNG to select one of the doors.... – Luiz Tavares Feb 01 '16 at 01:33
  • After that, we'll probably need to remember which door has the prize. – Luiz Tavares Feb 01 '16 at 01:33
  • MountyHall can know this, but maybe the Door itself can know? hmmmm Tough call ;) – Luiz Tavares Feb 01 '16 at 01:34
  • How to model that? Well... it depends. The best way of model this is to ask yourself: do every instance of Door that I'll ever create is interested in knowing if they have a goat behind it? – Luiz Tavares Feb 01 '16 at 01:39
  • And now philosophy starts. If you're modeling Doors that will be use for more than MountyHall then NO, not all Doors are interested and this state belong somewhere else. – Luiz Tavares Feb 01 '16 at 01:40
  • on the other hand, if all your Doors are only ever be used to play Monty Hall, then Why not? If the goat is behind them, they seem to be the most interested part :) – Luiz Tavares Feb 01 '16 at 01:40
  • haha this is getting interesting. Okay im revising my code. What ill do now is: Create Door array that holds three doors. then use random generator to get a number and then assign that number to one of the doors in the array. and i would probably do the same for the other two doors. – Samer Alsaadi Feb 01 '16 at 01:42

1 Answers1

-2

The main cause of this difficulty is the random function you're using in your program. In fact, it is not efficient and it is a classical issue in java(and other programming languages). to know more about this topic check this post.

This post can be a good directive for your issue.

Hope this Helps.

Community
  • 1
  • 1
jmj
  • 649
  • 8
  • 17
  • 1
    Your wording is misleading. This mistake is in the OP's code, this has absolutely nothing to do with "_java(and other programming languages)_"; it has to do with basic logic. – Boris the Spider Jan 31 '16 at 23:28