Last active
March 7, 2018 05:05
-
-
Save hanss314/84781599b8eb36028a5704d4b576eaf3 to your computer and use it in GitHub Desktop.
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
import shlex | |
from game import MultiplayerAvocado | |
from subprocess import Popen, PIPE | |
MOVES = { | |
0: 'slice', | |
1: 'mash', | |
2: 'eat', | |
3: 'buy' | |
} | |
def fight(a, b): | |
a = shlex.split(a) | |
b = shlex.split(b) | |
game = MultiplayerAvocado() | |
while True: | |
if game.turn == 0: turn = a | |
else: turn = b | |
print(turn + [game.spoon] + game.previous) | |
proc = Popen(turn + [str(game.spoon)] + list(map(str, game.previous)), stdout=PIPE, stderr=PIPE) | |
out, err = proc.communicate() | |
if err: | |
break | |
print(out) | |
try: | |
out = int(out) | |
if len(game.previous) == 0: | |
game.set_avocados(out) | |
elif game.spoon == -1: | |
game.set_spoon(out) | |
else: | |
game.__getattribute__(MOVES[out])() | |
except (ValueError, KeyError): | |
break | |
return (game.turn + 1) % 2 |
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
class MultiplayerAvocado: | |
def __init__(self): | |
self.spoon = -1 | |
self.turn = 0 | |
self.previous = [] | |
def switch_turn(self): | |
if self.previous[-1] in self.previous[:-1] or self.previous[-1] < 0: | |
raise ValueError | |
else: | |
self.turn = (self.turn+1) % 2 | |
def set_avocados(self, count): | |
if count < 0: raise ValueError | |
self.previous.append(count % 256) | |
self.switch_turn() | |
def set_spoon(self, count): | |
if count < 1: raise ValueError | |
self.spoon = count % 256 | |
self.switch_turn() | |
def slice(self): | |
for x in reversed(range(2, self.avocados/2 + 2)): | |
if x % self.avocados == 0: | |
if x in [0, 1, self.avocados]: | |
raise ValueError | |
self.previous.append(x) | |
self.switch_turn() | |
def mash(self): | |
avocados = self.previous[-1] ** 2 | |
avocados %= 2 ** 8 | |
self.previous.append(avocados) | |
self.switch_turn() | |
def eat(self): | |
self.previous.append(self.previous[-1] - self.spoon) | |
self.switch_turn() | |
def buy(self): | |
avocados = self.previous[-1] + self.spoon | |
avocados %= 2 ** 8 | |
self.previous.append(avocados) | |
self.switch_turn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment