Closed Thread
Results 1 to 5 of 5

Thread: Probability

  1. #1
    Stilmatz is offline Newbie
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Probability

    Hi,

    I just wanted to know if there is ne way to put probability logic into a simple program. Basically i have written a program that tries to predict wat the user chooses between heads and tails. at the moment i am using Math.random() to predict the computers choice so is there ne way of storing say the last three choices (e.g H, H, T) and then making a decision of what to choice so the next choice for the computer should be programmed to choose a Head.

    this is what i have written so far :
    The Bit i want modified is bold and underlined,

    would appreciate ne suggestions
    Code:
    public class MindReaderProgram {
    
    	public static final int HEADS = 1;
    	public static final int TAILS = 2;
    	public static final int QUIT = 3;
    	int iWon = 0;
    	int youWon = 0;
    	int totalTimesPlayed = 0;
    
    	public void start() {
    		String usersName = getUsersName();
    		boolean userWantsToPlay = true;
    
    		while (userWantsToPlay) {
    			showUserChoices();
    			showScores();
    			int userChoice = getUserChoice();
    			userWantsToPlay = userChoice != QUIT;
    			if (userWantsToPlay) {
    				int computerChoice = getComputerChoice();
    				displayPlayerChoice(usersName, userChoice);
    				displayPlayerChoice("I", computerChoice);
    				String winnerString = workOutWinner(userChoice, computerChoice, usersName);
    				displayWinner(winnerString);
    			}
    		}
    		displayGoodbyeMessage(usersName);
    	}
    
    	private String getUsersName() {
    		System.out.print("Hi!  What is your name? ");
    		String name = Keyboard.readInput();
    		System.out.println(name + " I will now attempt to read your mind!, choose either Heads or Tails: ");
    		System.out.println();
    		return name;
    	}
    
    	private void showUserChoices() {
    		System.out.println(HEADS + ". HEADS");
    		System.out.println(TAILS + ". TAILS");
    		System.out.println(QUIT + ". Quit");
    		System.out.println();
    	}
    
    	private void showScores() {
    		System.out.println ("I have Won so far: " + iWon);
    		System.out.println ("You have Won so far: " + youWon);
    		System.out.println ("Number of times played so far: " + totalTimesPlayed);
    	}
    	private int getUserChoice() {
    	   System.out.print("  Enter choice: ");
    	   String userString = Keyboard.readInput();
    
    	   userString = userString.trim();
    	   System.out.println();
    	   return Integer.parseInt(userString);
    	}
    
    	private int getComputerChoice() {
    		if (totalTimesPlayed > 2 && (computerChoice == userChoice)) {
    			return (!computerChoice);
    
    		return (int) (Math.random() * 2 + 1);
    	}
    
    	private String workOutWinner(int userChoice, int computerChoice, String usersName) {
    		String winnerString = " ";
    
    		if (userChoice == computerChoice) {
    			winnerString = "  I";
    		} else if (!(userChoice == computerChoice)) {
    			winnerString = " " + usersName;
    		}
    		winnerString = winnerString + " win because " + getResultString(userChoice, computerChoice);
    		return winnerString;
    	}
    
    	private void displayWinner(String winner) {
    		System.out.println(winner);
    		System.out.println();
    	}
    
    	private void displayGoodbyeMessage(String name) {
    		System.out.println("Goodbye " + name + ". Thanks for playing :)");
    	}
    
    
    
    //************************** Ex 1a ******************************
    
    	private void displayPlayerChoice(String player, int choice) {
    		if(choice == HEADS) {
    			System.out.println(player + " chose Heads");
    		}else if (choice == TAILS) {
    			System.out.println(player + " chose Tails");
    		}
    
    	}
    
    //************************** Ex 1b ******************************
    
    	private boolean userWins(int userChoice, int computerChoice) {
    		if (userChoice == HEADS && computerChoice == HEADS ){
    			return true;
    
    		}
    		if (userChoice == TAILS && computerChoice == TAILS){
    			return true;
    		}
    
    		return false;
    	}
    
    //************************** Ex 1c ******************************
    
    	private String getResultString(int choice1, int choice2) {
    
    		final String YOU_WINS = "OKAY, MAYBE I CAN'T READ MINDS";
    		final String COMPUTER_WINS = "I READ YOUR MIND!";
    
    		if(choice1 == choice2) {
    			iWon++;
    			totalTimesPlayed++;
    			return COMPUTER_WINS;
    		}
    		if (choice1 == HEADS && choice2 == TAILS ) {
    			youWon++;
    			totalTimesPlayed++;
    			return YOU_WINS;
    		}
    		if (choice1 == TAILS && choice2 == HEADS) {
    			youWon++;
    			totalTimesPlayed++;
    			return YOU_WINS;
    		}
    
    		return ""; // Replace this whole line with your own lines of code.
    
    	}
    }
    Last edited by WingedPanther; 02-10-2010 at 09:07 AM. Reason: add code tags (the # button)

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Probability

    I would store a list of the computer's guesses and the user's choices. I would keep a complete list, and try some pattern analysis on it. The simplest would be "if the user has done more heads than tails, guess tails" and vice-versa.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Stilmatz is offline Newbie
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Re: Probability

    yup but how do i write that in code ???

  5. #4
    bobdark's Avatar
    bobdark is offline Programmer
    Join Date
    Jan 2010
    Location
    Haifa, Israel
    Posts
    164
    Rep Power
    9

    Re: Probability

    You can hold two variables (counters) - one for heads and one for tails. Each time you increase the proper variable, and guess the one with the lower counts. Another possible option is to hold one variable - if you get head, you increase the counter by one and if you get tails, you DEcrease it by one. If the value is positive, guess tails, otherwise guess heads.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Probability

    You could store it in a list.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Probability for an event
    By mnirahd in forum C and C++
    Replies: 1
    Last Post: 11-08-2010, 12:19 PM
  2. algorithms based on probability theory and statistics
    By tubybugydum in forum General Programming
    Replies: 2
    Last Post: 03-04-2010, 02:05 AM
  3. Implementing probability theory
    By mess1ahh in forum General Programming
    Replies: 4
    Last Post: 10-17-2009, 02:58 AM
  4. math/probability expert needed!
    By askiba in forum C and C++
    Replies: 2
    Last Post: 03-17-2009, 09:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts