How do I program a 1v1 Liar's Dice AI?

Not favoritedFavorited Favorited 0 favourites
From the Asset Store
Fully animated six sided dice, top down wooden tray and dice throwing cup
  • A local variable/number/value is not normally held in memory outside of the event block it is used in, so yes it resets to 1 each time. This is a key property of how local variables work as opposed to global variables. Note that this can be overwritten with the "static" option set, as described in the manual.

    https://www.construct.net/en/make-games/manuals/construct-3/interface/dialogs/event-variable

    That makes sense, thank you!

  • It's been a while! I was away for quite a bit learning Unreal Engine 5 and applying this knowledge to that. I also got my old math professor from college to explain the mathematics to me a bit more, so I have a more solid understanding of the terms, like factorial and combination and such. Thanks for helping me despite my lack of understanding, haha.

    Now I do have a few fresh questions for you guys! If I show some gaps in my understanding, please let me know.

    K = (number of dice in the claim) - (number of dice in the player's hand that match the pip or is wild (one))
    N = (number of dice that the other player has)
    Probability = 0
    
    compare: K=0
    -- set probability to 1
    else
    compare: K<=N
    Repeat K times
    -- add factorial(N)/(factorial(loopindex+1)*factorial(N-loopindex-1))*((1/3)^(loopindex+1))*((2/3)^(N-loopindex-1)) to probability
    
    function factorial(num)
    -- return (num=0 | num=1)?1:factorial(num-1)

    Here's the same thing in formula form in case I made typos.

    Anyways, hopefully some of that helps.

    I am actually grasping this without much trouble now! What I notice with the code that my professor helped me with and this is that yours uses "1/3" and "2/3" in the exact calculation. This is because the original premise uses wilds, right? Since a 1 or the wagered pip could both count, there is a 2/6 chance to roll what you are trying to roll, which gets simplified to 1/3. Same thing for the 2/3, which is the 4/6 chance you won't roll what you want simplified.

    So here's my first question. If I want to have the rules change to either be "Ones are Wild" or not, then I need to use a boolean to first check if "Ones are Wild" or not. If ones are NOT wild, then I would still need to use the 1/6 and 5/6 from the math that my professor taught me. Like this...

    If "Ones are Wild", then I'd need to use the 1/3 and 2/3... but... does that still hold up if the player wagers One pips? There's only one way to roll a One, so I would need to do a second check to see if the player wagered faces equal to or above 2, right? If so, they use the "Wild Exact" formula, but if they wagered Ones, then they'd need to still use the "Not Wild Exact" formula... I think.

    Secondly, I've been trying to figure out how to implement a "Dead On" rule. With this, a participant may, rather than calling the other player a liar for overestimating the number of dice on the table, may call "Dead on", essentially "There number of dice wagered exactly matches what is on the table.".

    At present, I am rolling a float between 0 and 1 and comparing it to the "At Least" calculation. If we roll under the calculated result, then the AI believes the player is telling the truth. I actually set up a quick loop that simulates it 10,000 times with a single click, and the AI wins a lot when it plays perfectly like this, almost 80 percent of the time.

    However, when I tried to implement "Dead On" rules, the AI was either calling Dead On hundreds of times or calling it no more than 10 or so times in 10,000 rounds, and they all were wrong...

    I wish I had saved my code, but I quit out of frustration and forgot to save. I believe my idea was to subtract one of the results (Exact chance and At Least chance) from the other to give myself ranges that the AI could randomly roll the dice for. So if, for example, Exact was 0.1 and At Least was 0.3, then if the AI random roll was less than 0.1, the AI called Dead On. If the AI roll was between 0.1 and 0.3, it would consider you to tell the truth, and if it rolled above 0.3, then you were lying.

    I haven't gotten to having the AI choose a new wager yet, but I want to solve this small conundrum first.

  • I’m already unfamiliar with the game and the formula. But I’m assuming the formula is mostly correct.

    I’d want to verify this, but assuming it’s correct this should give to probability that exactly k of the unknown n dice are the same value.

    Prob(n,k) = fact(n)/(fact(k)*fact(n-k))*((1/3)^k)*((2/3)^(n-k))

    That’s with wilds. Without wilds 1/6 and 5/6 sounds reasonable.

    Normally for the ai to decide to say Liar or not it looks at the probability that there are at least k dice with the same value.

    So for example if there are four unknown dice.

    The probability of at least 2 being the same would be:

    Prob(4,2)+prob(4,3)+prob(4,4)

    And the probability of exactly two being the same would be

    Prob(4,2)

    The formula with the summarization I gave before would find at most or

    Prob(4,2)+prob(4,1)

    Which doesn’t seem right.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Is it weird to say that some of these solutions presented themselves to me in a dream? I was sleeping and dreaming about the code I guess, and I popped out of sleep with a sort of "oh wow, that would work" moment and decided to implement it, and it worked, haha.

    The formula with the summarization I gave before would find at most or

    Prob(4,2)+prob(4,1)

    Which doesn’t seem right.

    I thought so, too. I figure, ok, the figure for the exact number of dice correct will ALWAYS be less, so that's why I was subtractiing them.

    In my dream I could see all the numbers coming out of my computer like old school ticker tape, and I was thinking "Well why is the computer taking such risky guesses, these numbers stink, it should be more careful".

    Then I woke up and thought, yeah, it SHOULD be more careful. If I make a debug display of wins and losses I can see what the AI is choosing to do and how successful it is. I might have a tenuous grasp on this math, but I understand wins and losses, so if I make little changes to the code and just see how many times the AI wins, then I can BASICALLY figure out the AI code. All I have to do is record what methods return better or worse results, and then use all those different methods to scale the difficulty!

    So first I did a lot more variables and recording of information to help myself debug. Here's the little readout with my very basic "just guess the hand" without wilds and without calling dead on.

    With this method, I roll both the player hands and AI hands, then randomly set the wager quantity to somewhere between 1 and 4, and the wager face to somewhere between 1 and 4. After that, I check to see if the "remainder dice" is 0: if it is, the AI automatically raises, since it's a 100 percent guarantee. If the remainder dice is more than 0, then I have the AI take a guess on an action and evaluate whether or not the assessment was accurate. It usually had around 70-80% correct guesses. Not shabby!

    So then I decided to add the Dead-On rule, so that the AI would occasionally attempt to guess Dead-On. In this code, any time the AI rolled under the "exact amount" number, it checks to see if the "remainder dice" is 1. After that, it has a 30 percent chance to risk it and make a Dead On call.

    With this, I can adjust how risky the AI wants to play by increasing or decreasing that percentage chance. I also decided to add a chance for the AI to guess Dead On even when the remainder dice is 0 using the same 30 percent chance as above.

    A slight increase in failures, but still the Dead On call and success ratesa are around the same: Sparingly used and correct about 1/3 of the time.

    If feels a bit cheaty to just brute force the results like this, but I guess figuring out what numbers I can move around to make the AI smarter works. Like, if I just have the AI check if the "At Least" is more or less than 50 percent and choose from there, it's right around 85 percent of the time. But that's a little boring, I think.

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)