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)
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.
yup but how do i write that in code ???
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.
You could store it in a list.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks