Skip to content

Instantly share code, notes, and snippets.

@mbeltagy
Created October 19, 2015 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbeltagy/7e5f45a38f30359d6205 to your computer and use it in GitHub Desktop.
Save mbeltagy/7e5f45a38f30359d6205 to your computer and use it in GitHub Desktop.
Calculating the probability of getting a double dice to move peg to any particular point after where it is in game of backgammon.
function backgammon_prob()
location_hits=zeros(Int,24)
for i=1:6, j=1:6
if(i!=j) # to avoid double counting doubles
location_hits[i]+=1
location_hits[j]+=1
else
location_hits[j]+=1
end
location_hits[i+j]+=1
if i==j
location_hits[3*i]+=1
location_hits[4*i]+=1
end
end
return location_hits//36
end
#Now for the plotting
using PyPlot
probs=backgammon_prob()
ax=subplot(1,1,1);
bar(1:24,probs,align="center")
tickLocations=[0.0:0.05:0.5...];
tickText=["$(iround(i*100))\%" for i in tickLocations]
ax[:set_yticks](tickLocations)
ax[:set_yticklabels](tickText)
xticks(1:24)
xlabel("Location of Dice Throw")
ylabel("Probability")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment