Last active
September 7, 2019 09:09
-
-
Save dietercastel/8f7c942d19a202bb78b360db55bc033d to your computer and use it in GitHub Desktop.
Julia count RPS combinations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using IterTools | |
using Test | |
# This script counts the amount of 'winning' combinations for a n-player game of Rock, Paper Scissors (RPS). | |
# To test the mycount function I calculated I wanted to generate all possible combinations and programatically count them. | |
# That's what the actualcount function does. | |
x = [2,3,5] | |
#x = ['R','P','S'] | |
#Generates a tuple as argument input for the product function. | |
function genArg(n) | |
Tuple(fill(x,n)) | |
end | |
@show genArg(2) | |
@show genArg(3) | |
#Caclulates whether the combination t is a ty based on modulo calculus. | |
function isTie(t,n) | |
prod = reduce(*,t) | |
if prod % (2*3*5) == 0 | |
return true | |
elseif prod % 2 ^ n == 0 | |
return true | |
elseif prod % 3 ^ n == 0 | |
return true | |
elseif prod % 5 ^ n == 0 | |
return true | |
end | |
return false | |
end | |
#Some testing of the product function. | |
#for p in Iterators.product(genArg(n)...) | |
# @show p | |
#end | |
#Generates all possible outcomes for n-player RPS | |
function actualcount(n) | |
totalcount = 3^n | |
tiecount = count(x -> isTie(x,n), Iterators.product(genArg(n)...)) | |
wincount = totalcount - tiecount | |
#winrate = wincount / totalcount | |
end | |
# Hand calculated function for my blogpost | |
function mycount(g) | |
3 * 2 * (2^(g-1) -1) | |
end | |
#Test the function up to n=15 | |
@testset "test my count" begin | |
for t in Array(3:15) | |
@test mycount(t) == actualcount(t) | |
end | |
end; | |
@time actualcount(15) | |
@time actualcount(16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment