21
Name:
Anonymous
2011-06-20 11:56
I was going somewhere with this, but then I saw the function composition examples and noticed my implementation didn't support that at all, and gave up. Probably starting over later. I absolutely loathed the way they phrased everything. We know what fucking currying is, ICFP.
class Slot:
def __init__(self, value, vitality):
self.value = value
self.vitality = vitality
def alive(self):
return self.vitality > 0
def dead(self):
return self.vitality <= 0
class Player:
def __init__(self, pid):
self.pid = pid
self.slots = [Slot(self.card('I'), 10000) for i in range(256)]
def turn(self):
print('=== player {}'.format(self.pid))
for i, s in enumerate(self.slots):
if s.vitality == -1: # zombie
s.value = s.value(self.card('I')) # what the fuck, icfp
s.vitality = 0
if (s.vitality, s.value) != (10000, self.card('I')):
if type(s.value) == int:
sv = s.value
else:
sv = s.value.__func__.__name__[2:]
print("slot {}: ({}, {})".format(i, s.vitality, sv))
try:
choice = input('1 = card -> slot, 2 = slot -> card: ')
if choice == '1':
cn = input('card name: ')
card = self.card(cn)
sn = int(input('slot number: '))
slot = self.slots[sn]
slot.value = card(slot.value)
print('player {} applied card {} to slot {}'.format(
self.pid, cn, sn))
elif choice == '2':
sn = int(input('slot number: '))
slot = self.slots[sn]
cn = input('card name: ')
card = self.card(cn)
slot.value = slot.value(card)
print('player {} applied slot {} to card {}'.format(
self.pid, sn, cn))
else:
raise Exception("invalid option")
except AttributeError as e:
print("no such card!")
except Exception as e:
print("error!", e)
def card(self, name):
return eval('self.c_' + name)
def score(self):
return len([s for s in self.slots if s.alive()])
def c_I(self, x): return x
def c_zero(self, x): return 0
def c_succ(self, n):
if n < 65535:
return n + 1
return 65535
def c_dbl(self, n):
if n < 32768:
return n * 2
return 65535
def c_get(self, i):
s = self.slots[i]
if s.dead():
raise Exception("slot is dead")
return s.value
def c_put(self, i): return self.card('I')
def c_S(self, f): return lambda g: lambda x: f(x)(g(x))
def c_K(self, x): return lambda y: x
def c_inc(self, i):
s = self.slots[i]
if s.alive() and s.vitality < 65535:
s.vitality += 1
return self.card('I')
def c_dec(self, i):
s = self.opponent.slots[255-i]
if s.alive():
s.vitality -= 1
return self.card('I')
def c_attack(self, i):
def z1(j):
def z2(n):
s = self.slots[i]
v = s.vitality
if n > v: raise Exception("invalid attack")
t = self.opponent.slots[255-j]
t.vitality -= n * 9 // 10
if t.vitality < 0: t.vitality = 0
return self.card('I')
return z2
return z1
def c_help(self, i):
def z1(j):
def z2(n):
s = self.slots[i]
v = s.vitality
if n > v: raise Exception("invalid help")
t = self.slots[j]
t.vitality += n * 11 // 10
if t.vitality > 65535: t.vitality = 65535
return self.card('I')
return z2
return z1
def c_copy(self, i):
return self.opponent.slots[i]
def c_revive(self, i):
if self.slots[i].dead():
self.slots[i].vitality = 1
return self.card('I')
def c_zombie(self, i):
def z1(x):
t = self.opponent.slots[255-i]
if t.alive(): raise Exception("card is alive")
t.value = x
t.vitality = -1
return z1
players = (Player(0), Player(1))
players[0].opponent = players[1]
players[1].opponent = players[0]
for t in range(1, 100001):
print('*** turn {}'.format(t))
for p in players:
p.turn()