Program Help - Java - Monty Hall

Discussion in 'OT Technology' started by SiempreFresco, Oct 26, 2005.

  1. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    Just need some idea to get this program for my CS class started:

    I'm pretty much set on these variables:
    int wc, the randomly-chosen winning curtain
    int uc, the user's initial curtain choice
    int rc, the revealed curtain (not wc or uc)
    int counter, counts the number of runs

    I dont know how I would get the program to automatically do eith switch or stay. I'd appreciate comments, hints, suggestions on the program. (especially from the dud that helped me out last time)

    :sadwavey:
     
  2. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    this is the part I can't really figure out
     
  3. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    Please post the code you have so far.
     
  4. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    I will once I finish classes for today.
     
  5. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    Ok so It desnt do everything exactly like it should yet, im fine tuning it, but give me some suggestion people:

    public class MontyHall
    {
    public static int pickADoor(){
    return 1 + (int)(Math.random()*3);
    }
    public static int openADoor(int rD, int cD){
    if (rD != cD) return 6 - (rD + cD);
    else
    switch (rD) {
    case 1: if (Math.random() < 0.5) return 2;
    else return 3;

    case 2: if (Math.random() < 0.5) return 1;
    else return 3;

    case 3: if (Math.random() < 0.5) return 1;
    else return 2;

    default: System.exit(1);
    break;
    }
    return 0;
    }



    public static void main (String[] args)
    {

    int switchWins = 0;
    int switchLosses = 0;
    int keepWins = 0;
    int keepLosses = 0;
    int rightDoor;
    int chosenDoor;


    int[] whereHidden = new int[3];
    int[] whereChosen = new int[3];

    for (int i = 0; i < 3; i++){
    whereHidden = 0;
    whereChosen = 0;
    }

    for (int tests = 0; tests < 1000; tests++){
    rightDoor = pickADoor();
    whereHidden[rightDoor-1]++;
    chosenDoor = pickADoor();
    whereChosen[chosenDoor-1]++;


    if (rightDoor == chosenDoor) keepWins++;
    else keepLosses++;


    if (rightDoor != chosenDoor) switchWins++;
    else switchLosses++;

    }

    System.out.println("Keeping won "+keepWins+
    " times, and lost "+keepLosses+" times.");
    System.out.println("Switching won "+switchWins
    +" times, and lost "+switchLosses
    +" times.");

    System.out.println();
    System.out.println("Prize was behind ...");
    for (int i = 0; i < 3; i++)
    System.out.println("Door " + (i+1) + " : " +
    whereHidden + " times.");

    System.out.println();
    System.out.println("Player chose ...");
    for (int i = 0; i < 3; i++)
    System.out.println("Door " + (i+1) + " : " +
    whereChosen + " times.");

    }
    }
     
  6. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
  7. SiempreFresco

    SiempreFresco New Member

    Joined:
    Jun 16, 2003
    Messages:
    7,142
    Likes Received:
    0
    Location:
    NYC
    Its ok people you can rest knowing that I figurec it out on my own...no thanks to OT :squint: :wtc: :hsd: :bigok:
     

Share This Page