forked from fabiohirano/Processing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prisoners_dilemma.pyde
53 lines (48 loc) · 1.41 KB
/
prisoners_dilemma.pyde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import random
def value(plA, plB):
if (plA == 1 and plB == 1):
return (2, 2)
if (plA == 1 and plB == -1):
return (0, 3)
if (plA == -1 and plB == 1):
return (3, 0)
if (plA == -1 and plB == -1):
return (1, 1)
def randNum():
if (random.randint(0,100) % 2 == 1):
return 1
else:
return -1
def paint(x):
if x == 1:
return 255
else:
return 0
def setup():
global nSquare, sizeSquare
global m
size(500, 500)
nSquare = 100
sizeSquare = width/nSquare
m = [[0 for i in range(nSquare)] for j in range(nSquare)]
for i in range (nSquare):
for j in range(nSquare):
m[i][j] = randNum()
fill(paint(m[i][j]))
rect(i * sizeSquare, j * sizeSquare, sizeSquare, sizeSquare)
def draw():
# fill(paint(m[i][j]))
# rect(i * sizeSquare, j * sizeSquare, sizeSquare, sizeSquare)
for i in range (nSquare):
for j in range (nSquare):
if j < nSquare-1:
(vlA, vlB) = value (m[i][j], m[i][j+1])
if vlA > vlB:
m[i][j+1] = m[i][j]
elif vlB > vlA:
m[i][j] = m[i][j+1]
else:
m[i][j] = m[i][j]
fill(paint(m[i][j]))
rect(i * sizeSquare, j * sizeSquare, sizeSquare, sizeSquare)
delay(1000)