-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_contest.py
155 lines (155 loc) · 5.28 KB
/
view_contest.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#
# View Contest
#
# Peter Turney, January 21, 2020
#
# Select two seeds from pickles and watch them battle.
#
import golly as g
import model_classes as mclass
import model_functions as mfunc
import model_parameters as mparam
import random as rand
import time
import pickle
import os
#
# Open a dialog window and ask the user to select two pickles.
#
g.note("View Contest\n\n" + \
"You will be presented with two dialog menus:\n" + \
" (1) Select a file of pickled seeds.\n" + \
" (2) Select another file of pickled seeds.\n" + \
"Two seeds, chosen randomly from each pickle, will then compete.")
#
path1 = g.opendialog("Select the first pickled seed file (*.bin)", \
"(*.bin)|*.bin", g.getdir("app"))
path2 = g.opendialog("Select the second pickled seed file (*.bin)", \
"(*.bin)|*.bin", g.getdir("app"))
#
# Some info to display to the user, in the header of the main window.
#
[head1, tail1] = os.path.split(path1)
[head2, tail2] = os.path.split(path2)
#
g.show("Red: " + path1 + " Blue: " + path2)
#
# Load pickles and select a seed from each pickle.
# The seeds are in order of decreasing fitness.
#
handle1 = open(path1, "rb") # rb = read binary
pickle1 = pickle.load(handle1)
handle1.close()
#
# Use "#" to select random or top choice
#
###seed1 = rand.choice(pickle1) # random seed from pickle1
seed1 = pickle1[0] # top seed from pickle1
#
handle2 = open(path2, "rb") # rb = read binary
pickle2 = pickle.load(handle2)
handle2.close()
#
# Use "#" to select random or top choice
#
###seed2 = rand.choice(pickle2) # random seed from pickle2
seed2 = pickle2[0] # top seed from pickle2
#
# Let the seeds play the Immigration Game.
#
width_factor = mparam.width_factor
height_factor = mparam.height_factor
time_factor = mparam.time_factor
#
# At the bottom of this loop, the user will be prompted to quit
# the loop, if desired.
#
while True:
# random rotation of seeds
s1 = seed1.random_rotate()
s2 = seed2.random_rotate()
# switch red to blue in the second seed
s2.red2blue()
# set up Golly
rule_name = "Immigration"
[g_width, g_height, g_time] = mfunc.dimensions(s1, s2, \
width_factor, height_factor, time_factor)
g.setalgo("QuickLife") # use the QuickLife algorithm
g.new(rule_name) # initialize cells to state 0
g.setrule(rule_name + ":T" + str(g_width) + "," + str(g_height)) # make a toroid
[g_xmin, g_xmax, g_ymin, g_ymax] = mfunc.get_minmax(g) # find range of coordinates
s1.insert(g, g_xmin, -1, g_ymin, g_ymax) # insert the first seed into Golly
s2.insert(g, +1, g_xmax, g_ymin, g_ymax) # insert the second seed into Golly
##g.setmag(mfunc.set_mag(g)) # set magnification
g.setmag(3)
#
g.update() # show the intial state
#
g.note("These are the intial seeds.\n" + \
"Red is on the left and blue is on the right.\n" + \
"The file names are in the header of the main window.\n" + \
"Drag this note to a new location if it is blocking your view.\n\n" + \
"Red seed directory: " + head1 + "\n" + \
"Red seed file: " + tail1 + "\n" + \
"Red seed size: {} x {}\n".format(seed1.xspan, seed1.yspan) + \
"Red seed density: {:.4f} ({} ones)\n\n".format(seed1.density(), \
seed1.count_ones()) + \
"Blue seed directory: " + head2 + "\n" + \
"Blue seed file: " + tail2 + "\n" + \
"Blue seed size: {} x {}\n".format(seed2.xspan, seed2.yspan) + \
"Blue seed density: {:.4f} ({} ones)\n\n".format(seed2.density(), \
seed2.count_ones()) + \
"Select OK to begin the competition.\n")
#
for t in range(g_time):
g.run(1) # run for 1 step without displaying
g.update() # now display
#
# see who won -- need to make adjustment here so that the
# score here matches with mfunc.score_pair()
#
[count1, count2] = mfunc.count_pops(g)
#
# s1.num_living = initial number of living cells in s1
# s2.num_living = initial number of living cells in s2
#
if (s1.num_living < count1):
count1 = count1 - s1.num_living
else:
count1 = 0
#
if (s2.num_living < count2):
count2 = count2 - s2.num_living
else:
count2 = 0
#
# Now we are ready to determine the winner.
#
if (count1 > count2):
result = "Red won! Red: {}. Blue: {}.\n\n".format(count1, count2)
elif (count2 > count1):
result = "Blue won! Red: {}. Blue: {}.\n\n".format(count1, count2)
else:
result = "Tie! Red: {}. Blue: {}.\n\n".format(count1, count2)
#
# Note that s2 is blue, which means it contains twos, not ones.
# Thus we use seed2, which is red, for density() and count_ones().
#
message = result + \
"Red seed directory: " + head1 + "\n" + \
"Red seed file: " + tail1 + "\n" + \
"Red seed size: {} x {}\n".format(seed1.xspan, seed1.yspan) + \
"Red seed density: {:.4f} ({} ones)\n\n".format(seed1.density(), \
seed1.count_ones()) + \
"Blue seed directory: " + head2 + "\n" + \
"Blue seed file: " + tail2 + "\n" + \
"Blue seed size: {} x {}\n".format(seed2.xspan, seed2.yspan) + \
"Blue seed density: {:.4f} ({} ones)\n\n".format(seed2.density(), \
seed2.count_ones()) + \
"Select Cancel to end.\n" + \
"Select OK to run again with new rotations and locations."
#
g.note(message) # here is where the user can quit
#
#
#