Skip to content

Instantly share code, notes, and snippets.

@maufirf
Created July 26, 2019 18:56
Show Gist options
  • Save maufirf/d4afe2beaed8be2a1a2757a3818c4d77 to your computer and use it in GitHub Desktop.
Save maufirf/d4afe2beaed8be2a1a2757a3818c4d77 to your computer and use it in GitHub Desktop.
def atkchance(a,b):
return max(min(0.5 + ((a-b)/b),1.0),.0)
def newpower(a,b,astuple=False):
ans = (a+(0.5*b))/2
if astuple: return (ans,ans)
else: return ans
"""
Mensimulasikan 'rebutan' tiga daerah.
Parameters
----------
p: `double`
AP dari daerah p yang dimiliki oleh faction A
q: `double`
AP dari daerah rebutan q yang awalnya dimiliki oleh faction B
r: `double`
AP dari daerah q yang dimiliki oleh faction B
n: `int`
Berapa kali iterasi simulasi
Returns
-------
iteration_records: `list` of `tuples`
list tuple yang secara berurutan berisi iterasi ke-i,
AP p, AP q, AP r, dan chance attack (iterasi ganjil
berarti A merebut q dari B, dan genap berarti B kembali
merebut q dari A).
"""
def simulate(p,q,r,n):
iteration_record = [(-1,p,q,r,None)]
print(f'(A)p={p} | (B)q={q} | (B)r={r}\n')
print(f'iteration {i}, attack chance ',end='')
for i in range(n):
if i%2==0:
atkc=atkchance(p,q)
print(f'(A)p -> (B)q : {atkc}')
p,q=newpower(p,q,1)
print(f'(A)p={p} | (A)q={q} | (B)r={r}\n')
else:
atkc=atkchance(r,q)
print(f'(B)r -> (A)q : {atkc}')
p,q=newpower(p,q,1)
print(f'(A)p={p} | (B)q={q} | (B)r={r}\n')
iteration_record.append(i,p,q,r,atkc)
return iteration_record
def get_each_chances(iteration_record):
ir = iteration_record[1:]
return\
list(ir[i][-1] for i in range(0,len(ir),2)),\
list(ir[i][-1] for i in range(1,len(ir),2))
def mean(arr):
acc=0
for i in arr: acc+=i
return acc/len(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment