<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/simulating-persian-monarchs-gameplay-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://freerangestats.info/blog/2018/12/23/persian-monarchs"> free range statistics - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>‘I can teach you in a minute…’</h2>
<p>In a <a rel="nofollow" href="http://freerangestats.info/blog/2018/10/27/dice-games" target="_blank">recent post I simulated some simple dice games</a> and promised (or threatened) that this was the first of a series of posts about games of combined luck and chance. The main aim of that post was to show how even simple probabilistic games can become complicated with tweaks to the rules, but I also mentioned a key concept that “any game of chance can be converted to a complex game of skill by adding gambling”.</p>
<p>Now I want to explore this last idea further with the fictional game Persian Monarchs. As far as I can tell, this game was invented by P. G. Wodehouse for his classic 1939 comedic novel <a rel="nofollow" href="https://www.goodreads.com/book/show/13707721-uncle-fred-in-the-springtime" target="_blank">Uncle Fred in the Springtime</a>, surely a front runner for some of the best humorous writing of the twentieth century. Wodehouse was in true mid-season form with this, his first full length novel featuring “<a rel="nofollow" href="https://en.wikipedia.org/wiki/Uncle_Fred" target="_blank">Uncle Fred</a>” (Frederick Altamont Cornwallis Twistleton, 5th Earl of Ickenham); before much of his work became tired and formulaic in the post-WWII years.</p>
<p>The plot of the novel is complex and not really to the point for a statistics blog, so do yourself a favour and read it for yourself in your own time. But for our purposes, it’s important to know that several of the key plot twists revolve around substantial sums of money gambled on Persian Monarchs, described by one character thus:</p>
<blockquote>
<p>‘I could teach it you in a minute. In its essentials it is not unlike Blind Hooky. Here’s the way it goes. You cut a card, if you see what I mean, and the other fellow cuts a card, if you follow me. Then if the card you’ve cut is higher than the card the other fellow has cut, you win. While, conversely, if the card the other fellow’s cut is higher than the card you’ve cut, he wins.’</p>
</blockquote>
<blockquote>
<p>He shot an anxious glance at Mr Pott, as if wondering if he had been too abstruse. But Mr Pott appeared to have followed him perfectly.</p>
</blockquote>
<blockquote>
<p>‘I think I see the idea,’ he said. ‘Anyway, I’ll pick it up as I go along.’</p>
</blockquote>
<p>Claude Eustace ‘Mustard’ Pott – the “Mr Pott” in the quotation above – is actually a Persian Monarchs player of considerable accomplishment; in addition to being a former silver ring bookie and Shakespearan actor, and current private detective and part time card hustler. So having a not-too-bright but rich and friendly representative of the upper classes explain his own favourite game to him is as close to heaven as he gets.</p>
<p>It’s not clear from the novel whether Mr Pott (or, later, the Duke of Dunstable, who also shows himself to be a master of cardplay) win at Persian Monarchs through superior skill or through outright cheating. There are indications both ways. For my purposes, I’m going to put aside the question of cheating and consider how it’s possible for skill to shine through in such a simple game.</p>
<p>It all comes down to the addition of choices about offering and accepting wagers. As the gambling rules around Persian Monarchs are not described by Wodehouse, I’m going to assume that it’s something like the following:</p>
<ul>
<li>each player contributes one counter (or other gambling unit) at the beginning of a hand to a combined stake, before receiving their card;</li>
<li>after seeing their card, the non-dealer has the option of raising the wager by an extra amount, up to some specified limit (10 in my simulations below), but by zero if they wish. The dealer then has to either cover the increased wager, or decline on the spot in which case the non-dealer wins the existing stake in full regardless of who has the higher cards;</li>
<li>if the dealer has covered the extra wager, whomever has the highest card wins and collects the entire stake.</li>
</ul>
<p>I’m also going to assume that once a hand is finished, those cards are put aside and the next hand proceeds with a reduced pack; until the pack is finished at which point the cards are all brought together and reshuffled.</p>
<p>To skip to the chase, here are the results of simulations of three different strategies at Persian Monarchs:</p>
<ul>
<li>choosing to offer or accept extra wagers completely at random;</li>
<li>offering or accepting wagers partly at random (in order to avoid being predictable and easy to bluff) but with probabilities aimed at delivering optimal results in the long run, taking into account which cards have so far appeared;</li>
<li>as above, but without bothering to count the cards that have already appeared and been discarded, so basing choices on general probabilities about one’s card relative to a complete pack.</li>
</ul>
<p><img width="100%" src="http://freerangestats.info/img/0135-density.svg"></p>
<p>At the end of a game that goes through two whole packs of cards, the optimal strategy outperforms the non-card-counting by between 3.8 to 5.3 counters; and the random strategy by 35 to 37 counters. So skill counts big time. Even the marginal advantage given by counting the cards and adjusting one’s strategy accordingly makes a 4% difference in your rate of return.</p>
<h3>‘Optimal’ strategy?</h3>
<p>For what I’ve defined as the optimal strategy (without either proof or adequate demonstration by simulation), the non-dealer’s wager-offering algorithm is as follows:</p>
<ul>
<li>First choose whether to offer an increased wager or not. Do this at random, with a ‘probability of offering more’ equal to the estimated probability that the card I’ve got is higher than a random card from the pack.</li>
<li>Having decided to offer an increased wager, pick a random number between 1 and 10 to increase the wager by that.</li>
</ul>
<p>The decision of whether to accept or not is taken similarly.</p>
<p>The trick in constructing a decision rule here is that we want to avoid being predictable to our opponent. For example, if we only offered an increased wager when we had one of the top 50% cards in the pack, we have ruled out any bluffing, and once our opponent picks this up they can use it to their advantage. Similarly, if we increase the wager by more depending on our confidence, we are giving away information. So we want a compromise between strict determination and a bit of randomness always present (even if I draw the Queen of spades I might decline an increased wager, or with the 3 of clubs I might offer to increase the stake, albeit at low probability), with your opponent not quite sure where you’re coming from – let’s call it the ‘<a rel="nofollow" href="https://en.wikipedia.org/wiki/Madman_theory" target="_blank">madman strategy</a> of Persian Monarchs’.</p>
<h2>Writing a card game in R</h2>
<p>How did I implement all this?</p>
<p>First, I defined a few convenience objects and functions.</p>
<ul>
<li><code>pack</code> is a data frame of 52 rows with the value and suit of all the cards in a standard pack of cards, with precedence in order as per Bridge;</li>
<li><code>pm_draw</code> is a function that, given an existing (possibly incomplete) pack of cards, returns a drawn card and the remaining pack as two elements of a list:</li>
</ul>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>library</span><span>(</span><span>parallel</span><span>)</span><span>
</span><span>#' Define a starting full pack of cards. In sequence as per Bridge ie Ace of Spades highest, Two of Clubs lowest.
</span><span>pack</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>id</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>52</span><span>,</span><span>
</span><span>suit</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;clubs&quot;</span><span>,</span><span> </span><span>&quot;diamonds&quot;</span><span>,</span><span> </span><span>&quot;hearts&quot;</span><span>,</span><span> </span><span>&quot;spades&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>13</span><span>),</span><span>
</span><span>value</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>2</span><span>:</span><span>10</span><span>,</span><span> </span><span>&quot;Jack&quot;</span><span>,</span><span> </span><span>&quot;Queen&quot;</span><span>,</span><span> </span><span>&quot;King&quot;</span><span>,</span><span> </span><span>&quot;Ace&quot;</span><span>),</span><span> </span><span>4</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>value</span><span>,</span><span> </span><span>&quot;of&quot;</span><span>,</span><span> </span><span>suit</span><span>))</span><span>
</span><span>full_pack</span><span> </span><span>&lt;-</span><span> </span><span>pack</span><span>
</span><span>#' For a given pack (not necessarily a full one), pull out one card, and return
#' that card and the remaining pack (as a list with two elements)
#' #' @param pack a dataframe with such as that defined by `pack`, with at least
#' a column named `id`
</span><span>pm_draw</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>pack</span><span>){</span><span>
</span><span>drawn</span><span> </span><span>&lt;-</span><span> </span><span>sample_n</span><span>(</span><span>pack</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>remaining_pack</span><span> </span><span>&lt;-</span><span> </span><span>pack</span><span>[</span><span>pack</span><span>$</span><span>id</span><span> </span><span>!=</span><span> </span><span>drawn</span><span>$</span><span>id</span><span>,</span><span> </span><span>]</span><span>
</span><span>return</span><span>(</span><span>list</span><span>(</span><span>drawn</span><span> </span><span>=</span><span> </span><span>drawn</span><span>,</span><span> </span><span>remaining_pack</span><span> </span><span>=</span><span> </span><span>remaining_pack</span><span>))</span><span>
</span><span>}</span></code></pre>
</figure>
<p>Then the workhorse of the whole project comes in the next function, <code>pm_hand</code>, which plays a single hand of Persian Monarchs with two players. The wagering strategy can be “auto” (the best strategy I could think of), “ask” (a human gets asked to decide each wager decision) “random” and “non-card-counter”. In itself, this function is not much use, but it abstracts the work of a single hand of dealing, wagering, accepting and checking the result away from what is going to become the main loop of the game to ge defined later.</p>
<figure>
<pre><code data-lang="r"><span>#' Play a hand of Persian Monarchs
#' #' @param p1_counters number of counters owned by player 1 (non-dealer) at beginning of hand
#' @param p2_counters number of coutners owned by player 2 (dealer) at beginning of hand
#' @known_pack pack at the beginning of the hand, 'known' by the players if they have been counting cards.
#' @p1_method method for player 1 deciding whether to offer an increased wager
#' @p2_method method for player 2 deciding whether to accept an offered increased wager
#' @verbose Whether or not to print out extra information to console
#' @p1_lab Label for player 1, used in messages to console
#' @p2_lab Label for player 2, used in messages to console
#' @max_wager Maximum additional wager to be offered by player 1
#' @details Player 2 is the dealer and Player 1 gets the first choice - whether or not to offer an increased wager
#' on top of the original wager of 1 counter, and if so how much to make that additional wager. Player 2 then gets
#' to accept and cover that wager, of to fold and concede the current wager of 1 counter to Player 1.
</span><span>pm_hand</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>p</span><span>1</span><span>_</span><span>counters</span><span>,</span><span> </span><span>p</span><span>2</span><span>_</span><span>counters</span><span>,</span><span> </span><span>known_pack</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>&quot;ask&quot;</span><span>,</span><span> </span><span>&quot;random&quot;</span><span>,</span><span> </span><span>&quot;non-card-counter&quot;</span><span>),</span><span> </span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>&quot;ask&quot;</span><span>,</span><span> </span><span>&quot;random&quot;</span><span>,</span><span> </span><span>&quot;non-card-counter&quot;</span><span>),</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Non-dealer&quot;</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Dealer&quot;</span><span>,</span><span>
</span><span>max_wager</span><span> </span><span>=</span><span> </span><span>10</span><span>){</span><span>
</span><span># convenience function for printing a message to the console but substituting the correct label
</span><span> </span><span># for p1 or p2 (player 1 or player 2):
</span><span> </span><span>pm_cat</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>txt</span><span>){</span><span>
</span><span>txt</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;p1&quot;</span><span>,</span><span> </span><span>p</span><span>1</span><span>_</span><span>lab</span><span>,</span><span> </span><span>txt</span><span>)</span><span>
</span><span>txt</span><span> </span><span>&lt;-</span><span> </span><span>gsub</span><span>(</span><span>&quot;p2&quot;</span><span>,</span><span> </span><span>p</span><span>2</span><span>_</span><span>lab</span><span>,</span><span> </span><span>txt</span><span>)</span><span>
</span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>
</span><span>cat</span><span>(</span><span>txt</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>&lt;-</span><span> </span><span>match.arg</span><span>(</span><span>p</span><span>1</span><span>_</span><span>method</span><span>)</span><span>
</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>&lt;-</span><span> </span><span>match.arg</span><span>(</span><span>p</span><span>2</span><span>_</span><span>method</span><span>)</span><span>
</span><span># Both players start by putting in one counter each just to start to play
</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>-</span><span> </span><span>1</span><span>
</span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>-</span><span> </span><span>1</span><span>
</span><span>wagered</span><span> </span><span>&lt;-</span><span> </span><span>2</span><span>
</span><span># Draw a card each
</span><span> </span><span>tmp1</span><span> </span><span>&lt;-</span><span> </span><span>pm_draw</span><span>(</span><span>known_pack</span><span>)</span><span>
</span><span>tmp2</span><span> </span><span>&lt;-</span><span> </span><span>pm_draw</span><span>(</span><span>tmp1</span><span>$</span><span>remaining_pack</span><span>)</span><span>
</span><span>p</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>tmp1</span><span>$</span><span>drawn</span><span>
</span><span>p</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>tmp2</span><span>$</span><span>drawn</span><span>
</span><span># The probabilities as they appear to the players:
</span><span> </span><span>p</span><span>1</span><span>_</span><span>chance</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>known_pack</span><span>$</span><span>id</span><span> </span><span></span><span> </span><span>p</span><span>1</span><span>$</span><span>id</span><span>)</span><span> </span><span>/</span><span> </span><span>(</span><span>nrow</span><span>(</span><span>known_pack</span><span>)</span><span> </span><span>-</span><span> </span><span>1</span><span>)</span><span>
</span><span>p</span><span>2</span><span>_</span><span>chance</span><span> </span><span>=</span><span> </span><span>sum</span><span>(</span><span>known_pack</span><span>$</span><span>id</span><span> </span><span></span><span> </span><span>p</span><span>2</span><span>$</span><span>id</span><span>)</span><span> </span><span>/</span><span> </span><span>(</span><span>nrow</span><span>(</span><span>known_pack</span><span>)</span><span> </span><span>-</span><span> </span><span>1</span><span>)</span><span>
</span><span># Decision - will p1 wager more?
</span><span> </span><span>if</span><span>(</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>&quot;random&quot;</span><span>,</span><span> </span><span>&quot;non-card-counter&quot;</span><span>)){</span><span>
</span><span># If p1 is being rational, it's chance of increasing the wager comes from it's appreciation
</span><span> </span><span># of the probability of winning this hand (more is 1 if increasing the wager, 0 otherwise).
</span><span> </span><span># But if the method is random, then they choose at random
</span><span> </span><span>chance_more</span><span> </span><span>&lt;-</span><span> </span><span>case_when</span><span>(</span><span>
</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>==</span><span> </span><span>&quot;auto&quot;</span><span> </span><span>~</span><span> </span><span>p</span><span>1</span><span>_</span><span>chance</span><span>,</span><span> </span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>==</span><span> </span><span>&quot;random&quot;</span><span> </span><span>~</span><span> </span><span>runif</span><span>(</span><span>1</span><span>,</span><span> </span><span>0</span><span>,</span><span> </span><span>1</span><span>),</span><span>
</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>==</span><span> </span><span>&quot;non-card-counter&quot;</span><span> </span><span>~</span><span> </span><span>sum</span><span>(</span><span>full_pack</span><span>$</span><span>id</span><span> </span><span></span><span> </span><span>p</span><span>1</span><span>$</span><span>id</span><span>)</span><span> </span><span>/</span><span> </span><span>51</span><span>
</span><span>)</span><span>
</span><span>more</span><span> </span><span>&lt;-</span><span> </span><span>rbinom</span><span>(</span><span>prob</span><span> </span><span>=</span><span> </span><span>chance_more</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>n</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>extra</span><span> </span><span>&lt;-</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>max_wager</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>if</span><span>(</span><span>more</span><span> </span><span>==</span><span> </span><span>0</span><span>){</span><span>
</span><span>pm_cat</span><span>(</span><span>&quot;p1 does not add to the initial wager.\n&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>pm_cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;You drew the &quot;</span><span>,</span><span> </span><span>p</span><span>1</span><span>$</span><span>label</span><span>,</span><span> </span><span>&quot;. How much extra will you bet this hand?\n(maximum is &quot;</span><span>,</span><span> </span><span>max_wager</span><span>,</span><span> </span><span>&quot;, 0 is acceptable)&quot;</span><span>))</span><span>
</span><span>extra</span><span> </span><span>&lt;-</span><span> </span><span>-1</span><span>
</span><span>while</span><span>(</span><span>!</span><span>extra</span><span> </span><span>%in%</span><span> </span><span>0</span><span>:</span><span>max_wager</span><span>){</span><span>
</span><span>extra</span><span> </span><span>&lt;-</span><span> </span><span>as.numeric</span><span>(</span><span>readLines</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>1</span><span>))</span><span>
</span><span>}</span><span>
</span><span>more</span><span> </span><span>&lt;-</span><span> </span><span>as.integer</span><span>(</span><span>extra</span><span> </span><span>></span><span> </span><span>0</span><span>)</span><span>
</span><span>}</span><span> </span><span>if</span><span>(</span><span>more</span><span> </span><span>==</span><span> </span><span>1</span><span>){</span><span>
</span><span># Decision - will p2 try to accept the increased wager?
</span><span> </span><span>if</span><span>(</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>&quot;non-card-counter&quot;</span><span>)){</span><span>
</span><span>p</span><span>2</span><span>_</span><span>chance_enh</span><span> </span><span>&lt;-</span><span> </span><span>case_when</span><span>(</span><span>
</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>==</span><span> </span><span>&quot;auto&quot;</span><span> </span><span>~</span><span> </span><span>p</span><span>2</span><span>_</span><span>chance</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>==</span><span> </span><span>&quot;non-card-counter&quot;</span><span> </span><span>~</span><span> </span><span>sum</span><span>(</span><span>full_pack</span><span>$</span><span>id</span><span> </span><span></span><span> </span><span>p</span><span>2</span><span>$</span><span>id</span><span>)</span><span> </span><span>/</span><span> </span><span>51</span><span>
</span><span>)</span><span>
</span><span>p</span><span>2</span><span>_</span><span>expected</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>2</span><span>_</span><span>chance_enh</span><span> </span><span>*</span><span> </span><span>(</span><span>wagered</span><span> </span><span>+</span><span> </span><span>extra</span><span> </span><span>*</span><span> </span><span>2</span><span>)</span><span> </span><span>-</span><span> </span><span>(</span><span>1</span><span> </span><span>-</span><span> </span><span>p</span><span>2</span><span>_</span><span>chance_enh</span><span>)</span><span> </span><span>*</span><span> </span><span>(</span><span>wagered</span><span> </span><span>+</span><span> </span><span>extra</span><span> </span><span>*</span><span> </span><span>2</span><span>)</span><span>
</span><span>accept</span><span> </span><span>=</span><span> </span><span>p</span><span>2</span><span>_</span><span>expected</span><span> </span><span>></span><span> </span><span>(</span><span>-</span><span>wagered</span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>if</span><span>(</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>==</span><span> </span><span>&quot;ask&quot;</span><span>){</span><span>
</span><span>pm_cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;You drew the &quot;</span><span>,</span><span> </span><span>p</span><span>2</span><span>$</span><span>label</span><span>,</span><span> </span><span>&quot;. The other player raises the wager by &quot;</span><span>,</span><span> </span><span>extra</span><span>,</span><span> </span><span>&quot;. Do you accept? (Y/n)\n&quot;</span><span>))</span><span>
</span><span>input</span><span> </span><span>&lt;-</span><span> </span><span>&quot;&quot;</span><span>
</span><span>while</span><span>(</span><span>!</span><span>tolower</span><span>(</span><span>input</span><span>)</span><span> </span><span>%in%</span><span> </span><span>c</span><span>(</span><span>&quot;y&quot;</span><span>,</span><span> </span><span>&quot;n&quot;</span><span>)){</span><span>
</span><span>input</span><span> </span><span>&lt;-</span><span> </span><span>readLines</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>}</span><span>
</span><span>accept</span><span> </span><span>=</span><span> </span><span>(</span><span>tolower</span><span>(</span><span>input</span><span>)</span><span> </span><span>==</span><span> </span><span>&quot;y&quot;</span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span># random acceptance:
</span><span> </span><span>accept</span><span> </span><span>=</span><span> </span><span>as.logical</span><span>(</span><span>runif</span><span>(</span><span>1</span><span>,</span><span> </span><span>0</span><span> </span><span>,</span><span> </span><span>1</span><span>)</span><span> </span><span>></span><span> </span><span>0.5</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>if</span><span>(</span><span>accept</span><span> </span><span>&amp;</span><span> </span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>>=</span><span> </span><span>extra</span><span>){</span><span>
</span><span># the bet is on:
</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>-</span><span> </span><span>extra</span><span>
</span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>-</span><span> </span><span>extra</span><span>
</span><span>wagered</span><span> </span><span>&lt;-</span><span> </span><span>wagered</span><span> </span><span>+</span><span> </span><span>extra</span><span> </span><span>*</span><span> </span><span>2</span><span>
</span><span>pm_cat</span><span>(</span><span>&quot;p2 accepts the added wager.\n&quot;</span><span>)</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span># or else, p2 gives up:
</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>+</span><span> </span><span>wagered</span><span>
</span><span>wagered</span><span> </span><span>&lt;-</span><span> </span><span>0</span><span>
</span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>
</span><span>pm_cat</span><span>(</span><span>&quot;p2 did not accept the added wager.\n&quot;</span><span>)</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span># Who won?
</span><span> </span><span>if</span><span>(</span><span>p</span><span>1</span><span>$</span><span>id</span><span> </span><span>></span><span> </span><span>p</span><span>2</span><span>$</span><span>id</span><span>){</span><span>
</span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>+</span><span> </span><span>wagered</span><span>
</span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>
</span><span>pm_cat</span><span>(</span><span>paste0</span><span>(</span><span>p</span><span>1</span><span>$</span><span>label</span><span>,</span><span> </span><span>&quot; beats &quot;</span><span>,</span><span> </span><span>p</span><span>2</span><span>$</span><span>label</span><span>,</span><span> </span><span>&quot;; p1 wins.\n&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>&lt;-</span><span> </span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>+</span><span> </span><span>wagered</span><span>
</span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>
</span><span>pm_cat</span><span>(</span><span>paste0</span><span>(</span><span>p</span><span>1</span><span>$</span><span>label</span><span>,</span><span> </span><span>&quot; loses to &quot;</span><span>,</span><span> </span><span>p</span><span>2</span><span>$</span><span>label</span><span>,</span><span> </span><span>&quot;; p2 wins.\n&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>tmp2</span><span>$</span><span>remaining_pack</span><span>
</span><span>if</span><span>(</span><span>nrow</span><span>(</span><span>known_pack</span><span>)</span><span> </span><span></span><span> </span><span>2</span><span>){</span><span>
</span><span>pm_cat</span><span>(</span><span>&quot;Getting a fresh pack of cards.\n&quot;</span><span>)</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>pack</span><span>
</span><span>}</span><span>
</span><span>return</span><span>(</span><span>list</span><span>(</span><span>
</span><span>p</span><span>1</span><span>_</span><span>counters</span><span> </span><span>=</span><span> </span><span>p</span><span>1</span><span>_</span><span>counters</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>counters</span><span> </span><span>=</span><span> </span><span>p</span><span>2</span><span>_</span><span>counters</span><span>,</span><span>
</span><span>known_pack</span><span> </span><span>=</span><span> </span><span>known_pack</span><span>))</span><span>
</span><span>}</span></code></pre>
</figure>
<p>Having defined the functionality of a single hand, I then wrote a function for the main cycle of a game of user-specified rounds. This is basically a wrapper around <code>pm_hand</code>, making it user-friendly for a player with sufficient messages to the console.</p>
<figure>
<pre><code data-lang="r"><span>#-----------------------Human versus computer---------------------
</span><span>#' Play a human v computer game of Persian Monarchs
#' #' @param human number of chips human starts with
#' @param computer number of chips computer starts with
#' @param number of rounds to play (both players get a turn in a round)
</span><span>persian_monarchs_hvc</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>human</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>computer</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>rounds</span><span> </span><span>=</span><span> </span><span>10</span><span>){</span><span>
</span><span>start_pack</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>id</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>52</span><span>,</span><span>
</span><span>suit</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;clubs&quot;</span><span>,</span><span> </span><span>&quot;diamonds&quot;</span><span>,</span><span> </span><span>&quot;hearts&quot;</span><span>,</span><span> </span><span>&quot;spades&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>13</span><span>),</span><span>
</span><span>value</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>2</span><span>:</span><span>10</span><span>,</span><span> </span><span>&quot;Jack&quot;</span><span>,</span><span> </span><span>&quot;Queen&quot;</span><span>,</span><span> </span><span>&quot;King&quot;</span><span>,</span><span> </span><span>&quot;Ace&quot;</span><span>),</span><span> </span><span>4</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>value</span><span>,</span><span> </span><span>&quot;of&quot;</span><span>,</span><span> </span><span>suit</span><span>))</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>start_pack</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>rounds</span><span>){</span><span>
</span><span># Computer deals:
</span><span> </span><span>if</span><span>(</span><span>i</span><span> </span><span>!=</span><span> </span><span>1</span><span>)</span><span> </span><span>{</span><span>
</span><span>cat</span><span>(</span><span>&quot;Press enter for next hand.\n&quot;</span><span>)</span><span>
</span><span>readLines</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>}</span><span>
</span><span>message</span><span>(</span><span>&quot;Computer's deal.&quot;</span><span>)</span><span>
</span><span>cp</span><span> </span><span>&lt;-</span><span> </span><span>pm_hand</span><span>(</span><span>human</span><span>,</span><span>
</span><span>computer</span><span>,</span><span>
</span><span>known_pack</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;ask&quot;</span><span>,</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Human&quot;</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Computer&quot;</span><span>)</span><span>
</span><span>human</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>1</span><span>_</span><span>counters</span><span>
</span><span>computer</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>2</span><span>_</span><span>counters</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>known_pack</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;You have &quot;</span><span>,</span><span> </span><span>human</span><span>,</span><span> </span><span>&quot; counters.\n&quot;</span><span>))</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;There are &quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>known_pack</span><span>),</span><span> </span><span>&quot; cards left in the pack.\n&quot;</span><span>))</span><span>
</span><span># Human deals:
</span><span> </span><span>cat</span><span>(</span><span>&quot;Press enter for next hand.\n&quot;</span><span>)</span><span>
</span><span>readLines</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>1</span><span>)</span><span>
</span><span>message</span><span>(</span><span>&quot;Your deal.&quot;</span><span>)</span><span>
</span><span>cp</span><span> </span><span>&lt;-</span><span> </span><span>pm_hand</span><span>(</span><span>computer</span><span>,</span><span>
</span><span>human</span><span>,</span><span>
</span><span>known_pack</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;ask&quot;</span><span>,</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Computer&quot;</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Human&quot;</span><span>)</span><span>
</span><span>human</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>2</span><span>_</span><span>counters</span><span>
</span><span>computer</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>1</span><span>_</span><span>counters</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>known_pack</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;You have &quot;</span><span>,</span><span> </span><span>human</span><span>,</span><span> </span><span>&quot; counters.\n&quot;</span><span>))</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;There are &quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>known_pack</span><span>),</span><span> </span><span>&quot; cards left in the pack.\n&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span># Run an interactive game against the computer for three rounds
</span><span>persian_monarchs_hvc</span><span>(</span><span>rounds</span><span> </span><span>=</span><span> </span><span>3</span><span>)</span></code></pre>
</figure>
<p>This lets us play the game interactively in the R console, with a typical three round game shown in the screenshot below:</p>
<p><img src="https://i0.wp.com/freerangestats.info/img/0135-gameplay.png?w=100%25" data-recalc-dims="1"></p>
<p>As you can see, at the end of the three rounds I’d lost a total of one counter to the computer, playing a more or less ad hoc but sensible strategy of increasing the wager by substantial amounts if I draw a spade, less so for hearts, and minimal or nothing for the minor suits (and comparable strategy for acceptance).</p>
<h2>Simulating card game results</h2>
<p>It was fun to write a computer game in R, but this particular one isn’t really fascinating enough to play thousands of rounds to explore optimal strategy. To do that we want a function that lets a computer play against itself, so I adapt the <code>persian_monarchs_hvc</code> function to <code>persian_monarchs_cvc</code> (computer versus computer) as below. We can specify one of the three automated strategies for each player.</p>
<figure>
<pre><code data-lang="r"><span>#-------------------------computer versus computer-------------------
</span><span>#' Play a computer v computer game of Persian Monarchs
#' #' @param c1 number of chips Computer One starts with
#' @param c2 number of chips Computer Two starts with
#' @param c1_method one of &quot;auto&quot; (best choice given expected value) and &quot;random&quot; (coin flip) for
#' deciding whether Computer One offers or accepts an increased wager
#' @param c2_method one of &quot;auto&quot; (best choice given expected value) and &quot;random&quot; (coin flip) for
#' deciding whether Computer Two offers or accepts an increased wager
#' @param number of rounds to play (both players get a turn as dealer in a round, so a round is two hands)
#' @param verbose whether or not to print results to the screen
</span><span>persian_monarchs_cvc</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>c</span><span>1</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>c</span><span>2</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>&quot;random&quot;</span><span>,</span><span> </span><span>&quot;non-card-counter&quot;</span><span>),</span><span>
</span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>&quot;random&quot;</span><span>,</span><span> </span><span>&quot;non-card-counter&quot;</span><span>),</span><span> </span><span>rounds</span><span> </span><span>=</span><span> </span><span>10</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>){</span><span>
</span><span>c</span><span>1</span><span>_</span><span>method</span><span> </span><span>&lt;-</span><span> </span><span>match.arg</span><span>(</span><span>c</span><span>1</span><span>_</span><span>method</span><span>)</span><span>
</span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>&lt;-</span><span> </span><span>match.arg</span><span>(</span><span>c</span><span>2</span><span>_</span><span>method</span><span>)</span><span>
</span><span>start_pack</span><span> </span><span>&lt;-</span><span> </span><span>data_frame</span><span>(</span><span>
</span><span>id</span><span> </span><span>=</span><span> </span><span>1</span><span>:</span><span>52</span><span>,</span><span>
</span><span>suit</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>&quot;clubs&quot;</span><span>,</span><span> </span><span>&quot;diamonds&quot;</span><span>,</span><span> </span><span>&quot;hearts&quot;</span><span>,</span><span> </span><span>&quot;spades&quot;</span><span>),</span><span> </span><span>each</span><span> </span><span>=</span><span> </span><span>13</span><span>),</span><span>
</span><span>value</span><span> </span><span>=</span><span> </span><span>rep</span><span>(</span><span>c</span><span>(</span><span>2</span><span>:</span><span>10</span><span>,</span><span> </span><span>&quot;Jack&quot;</span><span>,</span><span> </span><span>&quot;Queen&quot;</span><span>,</span><span> </span><span>&quot;King&quot;</span><span>,</span><span> </span><span>&quot;Ace&quot;</span><span>),</span><span> </span><span>4</span><span>)</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>value</span><span>,</span><span> </span><span>&quot;of&quot;</span><span>,</span><span> </span><span>suit</span><span>))</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>start_pack</span><span>
</span><span>for</span><span>(</span><span>i</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>rounds</span><span>){</span><span>
</span><span># c2 deals:
</span><span> </span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>message</span><span>(</span><span>&quot;Computer Two's deal.&quot;</span><span>)}</span><span>
</span><span>cp</span><span> </span><span>&lt;-</span><span> </span><span>pm_hand</span><span>(</span><span>c</span><span>1</span><span>,</span><span>
</span><span>c</span><span>2</span><span>,</span><span>
</span><span>known_pack</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span>,</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>verbose</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Computer One&quot;</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Computer Two&quot;</span><span>)</span><span>
</span><span>c</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>1</span><span>_</span><span>counters</span><span>
</span><span>c</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>2</span><span>_</span><span>counters</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>known_pack</span><span>
</span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;Computer One has &quot;</span><span>,</span><span> </span><span>c</span><span>1</span><span>,</span><span> </span><span>&quot; counters.\n&quot;</span><span>))</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;There are &quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>known_pack</span><span>),</span><span> </span><span>&quot; cards left in the pack.\n&quot;</span><span>))</span><span>
</span><span>}</span><span> </span><span># c1 deals:
</span><span> </span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>
</span><span>message</span><span>(</span><span>&quot;Computer One's deal.&quot;</span><span>)</span><span> </span><span>}</span><span>
</span><span>cp</span><span> </span><span>&lt;-</span><span> </span><span>pm_hand</span><span>(</span><span>c</span><span>2</span><span>,</span><span>
</span><span>c</span><span>1</span><span>,</span><span>
</span><span>known_pack</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span>,</span><span>
</span><span>verbose</span><span> </span><span>=</span><span> </span><span>verbose</span><span>,</span><span>
</span><span>p</span><span>1</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Computer Two&quot;</span><span>,</span><span>
</span><span>p</span><span>2</span><span>_</span><span>lab</span><span> </span><span>=</span><span> </span><span>&quot;Computer One&quot;</span><span>)</span><span>
</span><span>c</span><span>1</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>2</span><span>_</span><span>counters</span><span>
</span><span>c</span><span>2</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>p</span><span>1</span><span>_</span><span>counters</span><span>
</span><span>known_pack</span><span> </span><span>&lt;-</span><span> </span><span>cp</span><span>$</span><span>known_pack</span><span>
</span><span>if</span><span>(</span><span>verbose</span><span>){</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;Computer One has &quot;</span><span>,</span><span> </span><span>c</span><span>1</span><span>,</span><span> </span><span>&quot; counters.\n&quot;</span><span>))</span><span>
</span><span>cat</span><span>(</span><span>paste0</span><span>(</span><span>&quot;There are &quot;</span><span>,</span><span> </span><span>nrow</span><span>(</span><span>known_pack</span><span>),</span><span> </span><span>&quot; cards left in the pack.\n&quot;</span><span>))</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span><span>return</span><span>(</span><span>data_frame</span><span>(</span><span>c</span><span>1</span><span> </span><span>=</span><span> </span><span>c</span><span>1</span><span>,</span><span> </span><span>c</span><span>2</span><span> </span><span>=</span><span> </span><span>c</span><span>2</span><span>))</span><span>
</span><span>}</span><span>
</span><span># examples:
</span><span>persian_monarchs_cvc</span><span>(</span><span>rounds</span><span> </span><span>=</span><span> </span><span>3</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>persian_monarchs_cvc</span><span>(</span><span>rounds</span><span> </span><span>=</span><span> </span><span>26</span><span>,</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>persian_monarchs_cvc</span><span>(</span><span>rounds</span><span> </span><span>=</span><span> </span><span>26</span><span>,</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;random&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span></code></pre>
</figure>
<p>Now that we have this function, it’s a simple matter of playing many thousands of games between computer opponents with differing strategies. This sort of thing usually needs parallel processing (ie running simulations on as many processors as are available at once, rather than just one at a time) to happen in a reasonable period of time. These days, R has plenty of packages to enable this, even on Windows.</p>
<p>The code below does the simulations (and modelling and presentation of results) of what I’ve called the “optimal” strategy versus an identical strategy, a “no card counting” alternative, and making wager decisions at random.</p>
<figure>
<pre><code data-lang="r"><span>#--------------------multiple simulations using parallel processing--------------------
# cluster with seven processors:
</span><span>cl</span><span> </span><span>&lt;-</span><span> </span><span>makeCluster</span><span>(</span><span>7</span><span>)</span><span>
</span><span># load the functionality we need onto the cluster:
</span><span>clusterEvalQ</span><span>(</span><span>cl</span><span>,</span><span> </span><span>{</span><span>library</span><span>(</span><span>tidyverse</span><span>)})</span><span>
</span><span>clusterExport</span><span>(</span><span>cl</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;persian_monarchs_cvc&quot;</span><span>,</span><span> </span><span>&quot;full_pack&quot;</span><span>,</span><span> </span><span>&quot;pack&quot;</span><span>,</span><span> </span><span>&quot;pm_hand&quot;</span><span>,</span><span> </span><span>&quot;pm_draw&quot;</span><span>))</span><span>
</span><span># number of simulations to do in each run. Takes about 90 seconds per run to do 10,000.
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>10000</span><span>
</span><span># Run 1 - with both players using the optimal strategy
</span><span>results_auto</span><span> </span><span>&lt;-</span><span> </span><span>parLapply</span><span>(</span><span>cl</span><span>,</span><span> </span><span>1</span><span>:</span><span>n</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>persian_monarchs_cvc</span><span>(</span><span>rounds</span><span> </span><span>=</span><span> </span><span>26</span><span>,</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>})</span><span>
</span><span>results_auto_df</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>results_auto</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;Best strategy&quot;</span><span>)</span><span>
</span><span># Run 2 - computer two plays ok but without counting cards left in the pack
</span><span>results_ncc</span><span> </span><span>&lt;-</span><span> </span><span>parLapply</span><span>(</span><span>cl</span><span>,</span><span> </span><span>1</span><span>:</span><span>n</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>persian_monarchs_cvc</span><span>(</span><span>rounds</span><span> </span><span>=</span><span> </span><span>26</span><span>,</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;non-card-counter&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>})</span><span>
</span><span>results_ncc_df</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>results_ncc</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;OK strategy but no card counting&quot;</span><span>)</span><span>
</span><span># Run 3 - computer two makes and accepts bets at random
</span><span>results_random</span><span> </span><span>&lt;-</span><span> </span><span>parLapply</span><span>(</span><span>cl</span><span>,</span><span> </span><span>1</span><span>:</span><span>n</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>){</span><span>
</span><span>persian_monarchs_cvc</span><span>(</span><span>rounds</span><span> </span><span>=</span><span> </span><span>26</span><span>,</span><span> </span><span>c</span><span>1</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;auto&quot;</span><span>,</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;random&quot;</span><span>,</span><span> </span><span>verbose</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span>
</span><span>})</span><span>
</span><span>results_random_df</span><span> </span><span>&lt;-</span><span> </span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span><span> </span><span>results_random</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;Random&quot;</span><span>)</span><span>
</span><span>stopCluster</span><span>(</span><span>cl</span><span>)</span><span>
</span><span>results_combined</span><span> </span><span>&lt;-</span><span> </span><span>results_auto_df</span><span> </span><span>%>%</span><span>
</span><span>rbind</span><span>(</span><span>results_random_df</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rbind</span><span>(</span><span>results_ncc_df</span><span>)</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>results_combined</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>c</span><span>2</span><span>_</span><span>method</span><span> </span><span>=</span><span> </span><span>fct_reorder</span><span>(</span><span>c</span><span>2</span><span>_</span><span>method</span><span>,</span><span> </span><span>c</span><span>2</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>c</span><span>2</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span>,</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_density</span><span>(</span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>#facet_wrap(~c2_method) +
</span><span> </span><span>labs</span><span>(</span><span>fill</span><span> </span><span>=</span><span> </span><span>&quot;Wagering decisions:&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Counters left at end of game&quot;</span><span>,</span><span>
</span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;Wagering decisions:&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>paste</span><span>(</span><span>&quot;Density of results from&quot;</span><span>,</span><span> </span><span>format</span><span>(</span><span>n</span><span>,</span><span> </span><span>big.mark</span><span> </span><span>=</span><span> </span><span>&quot;,&quot;</span><span>),</span><span> </span><span>&quot;simulations&quot;</span><span>))</span><span> </span><span>+</span><span>
</span><span>ggtitle</span><span>(</span><span>&quot;Results of two automated wagering strategies for Persian Monarchs&quot;</span><span>,</span><span>
</span><span>&quot;Counters left (having started with 100) after 26 rounds (two full packs) against a\nsound strategy&quot;</span><span>)</span><span>
</span><span>print</span><span>(</span><span>p</span><span>)</span><span>
</span><span>mod</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>c</span><span>2</span><span> </span><span>~</span><span> </span><span>c</span><span>2</span><span>_</span><span>method</span><span>,</span><span> </span><span>data</span><span> </span><span>=</span><span> </span><span>results_combined</span><span>)</span><span>
</span><span>confint</span><span>(</span><span>mod</span><span>)</span></code></pre>
</figure>
<h3>Reflection</h3>
<p>Astute readers will have noticed that the strategy I’ve sometimes called “optimal” is anything but. In fact, I’ve assumed away the chance of learning anything from the opponent’s own behaviour, whether a priori (for instance the reasonable assumption that they are more likely to accept or offer increased wagers with better cards) or after observing them for many rounds. So if we were seeking to create a Persian Monarchs master computer player, the algorithms above would be a sort of starting base case, to which we would need to add the ability to learn all sorts of tactics and strategies such as reading the opponent’s tendencies, swapping strategies oneself to confuse the opponent, bluffs and counter bluffs.</p>
<p>But that’s enough for now. In 2019 I’ll be extending this idea to some reflections on variants of Snakes and Ladders, and then to a classic 1970s text-based computer game that’s the granddaddy of <em>Civilization</em> and its turn-based strategic world-building ilk.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://freerangestats.info/blog/2018/12/23/persian-monarchs"> free range statistics - R</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/iryru3J3zHU" height="1"></a></div>