CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2009
    Posts
    440

    Rock, ..., Spock Random Numbers

    Hello,

    I wrote a Rock, Paper, Scissors, Lizard, Spock program in Haskell for my last project in a class. Haskell allows you to take a certain input and actually manually specify the answer for that particular input. So, what I wound up doing is keeping track of how many times the user chose rock, paper, etc. If the user chose them an equal amount of times I generated a character list with all choices and had the computer choose from that list. If the user chose say rock the most, I then had the computer choose from a list of the other options. I can post the Haskell code if you like... but I am ultimately trying to convert this into a c++ program.

    I am running into a...road block when it comes down to the algorithm. Ultimately I may have to do some hack like I did in Haskell, but I think there should be a better way. Since there are five options I am using srand() and rand() to generate the random numbers. However, I want this generation to change depending on the choice count. So, if we have:

    Rock = 3
    Paper = 2
    Scissors = 3
    Lizard = 5
    Spock = 4

    It will see that the user has selected the Lizard the most and remove that option from its set of choices. Ideally, if that number becomes so large then it will choose from one of the two options that are guaranteed to beat Lizard, etc. If two options are maxed out then it should choose from the remaining three. If three or more options have been chosen an equal number of time, then it defaults to picking from the entire set.

    Right now I have it find the maximum value in vector of ints. Then I iterate through that same vector in order to create another vector which contains a 1 in all the places the maximum value appears. At this point I have a vector that might contain [0,1,0,0,1]. The 0 index corresponds to Rock and 4 corresponds to Spock. The problem becomes choosing from the remaining options. If you need concrete code I can post it. Then again, I may decide to opt for a function which will return a string. The string would correspond to the set of appropriate options.

    I know this is long. I can post some code if you would like. But for now, I am going to go to bed. Thanks for any help/ideas.

    Edit: I believe I have figured out a solution to this. Guess I posted too soon.
    Last edited by Alterah; December 30th, 2010 at 08:16 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Rock, ..., Spock Random Numbers

    Seems like you are assuming that the player will choose randomly according to his historic probability distribution. That means you can assign a probability to each possible choice for the player. Then for each possible choice you have, you can calculate the probability that you will win. Then all you have to do is pick the choice that gives the highest probability of winning.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Aug 2009
    Posts
    440

    Re: Rock, ..., Spock Random Numbers

    Before you had replied I had already figured out a way to do this. Basically it is a game the user plays with the computer. What I wound up doing is specifying that if the player chooses and option so much to start out a game then the computer can use that. If the player chooses that option more than any other option combined then the computer will draw from the two choices guaranteed to beat it. Does that mean the player actually chose that option for the current game? No. But, I figured it was a good way to make the computer "smart".

    If the player chooses two options equally, the computer will choose from the remaining three. Probably not the best way to do it, but it makes some sense. Finally if the options are more or less equal, then I have the computer randomly choose between the entire set of options. If you'd like I can post the function that does this. I will need to think about what you suggested...seems like it would be a cleaner approach.

  4. #4
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Rock, ..., Spock Random Numbers

    Empirical investigations showed that all subjects always chose Spock (at least the four guys I know of), so picking lizard always wins
    What do you think about an algorithm that uses a range of numbers for each option? Using your data from 17 cases you can use an algorithm that chooses the counteroption for a given option depending on the frequency of the chosen option:

    option distribution
    Rock 3/17 = ~17%
    Paper 2/17 = ~11%
    Scissors 3/17 = ~17%
    Lizard 5/17 = ~29%
    Spock 4/17 = ~22%

    The algorithm is based on two steps:

    1) pick the two counter options that most probably win depending on the previous distribution.

    2) from these 2 counteroptions pick the one that least probably loses against its counteroptions

    Example:
    Most probable pick of the player is Lizard (29%), so the counteroptions are Scissors / Rock in 29% of the cases.
    Scissors lose to Spock (picked 4 times) and Rock (picked 3 times)
    Rock loses to Spock (picked 4 times) and Paper (picked twice).
    The total picks of Lizard´s counteroptions is 13 (7x Spock/Rock ~54% and 6x Spock/Paper ~46%), and Spock/Paper is a little bit less probable, so the algorithm should favor Rock (54%) over Scissors (46%).

    Bazinga!
    - Guido

  5. #5
    Join Date
    Aug 2009
    Posts
    440

    Re: Rock, ..., Spock Random Numbers

    That definitely would be a better way to implement it. I will have to work on doing that when I get some free time. How did your friends decide who would stop putting up Spock?

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Rock, ..., Spock Random Numbers

    I don´t know how my "friends" decided, they´re actually the four guys from the TV Series "The Big Bang Theory".
    - Guido

  7. #7
    Join Date
    Aug 2009
    Posts
    440

    Re: Rock, ..., Spock Random Numbers

    Hehe...I figured as much :P

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured