aboutsummaryrefslogtreecommitdiff
path: root/api/game/voerbak_connector.py
diff options
context:
space:
mode:
authorlonkaars <l.leblansch@gmail.com>2021-04-16 16:57:26 +0200
committerlonkaars <l.leblansch@gmail.com>2021-04-16 16:57:26 +0200
commit07c2b124e4348b15f1e5ec18c6cdfd77248c6bc8 (patch)
treee4a29123d3ebedc1d25500390c904c66b3b02489 /api/game/voerbak_connector.py
parentaa2c999702dadba2afbcf2be9f597f890aafcc87 (diff)
spaces > tabs in python :(
Diffstat (limited to 'api/game/voerbak_connector.py')
-rw-r--r--api/game/voerbak_connector.py132
1 files changed, 66 insertions, 66 deletions
diff --git a/api/game/voerbak_connector.py b/api/game/voerbak_connector.py
index 0b51bd5..54f5fa6 100644
--- a/api/game/voerbak_connector.py
+++ b/api/game/voerbak_connector.py
@@ -13,81 +13,81 @@ if os.name == "nt": VOERBAK_LOCATION += ".exe"
class bord:
- def __init__(self, w, h):
- self.width = w
- self.height = h
- self.player_1 = True
- self.board = "0" * (w * h)
- self.board_full = False
- self.win_positions = []
- self.process = subprocess.Popen(
- [VOERBAK_LOCATION, f"-w {w}", f"-h {h}"],
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=None
- )
- self.process.stdin.flush()
+ def __init__(self, w, h):
+ self.width = w
+ self.height = h
+ self.player_1 = True
+ self.board = "0" * (w * h)
+ self.board_full = False
+ self.win_positions = []
+ self.process = subprocess.Popen(
+ [VOERBAK_LOCATION, f"-w {w}", f"-h {h}"],
+ stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,
+ stderr=None
+ )
+ self.process.stdin.flush()
- # get output from voerbak without trailing newline character (this might break on windows because crlf)
- def get_output(self):
- return self.process.stdout.readline().decode()[:-1]
+ # get output from voerbak without trailing newline character (this might break on windows because crlf)
+ def get_output(self):
+ return self.process.stdout.readline().decode()[:-1]
- def kill_voerbak(self):
- self.process.stdin.write(bytearray("0", "utf-8"))
- self.process.stdin.flush()
+ def kill_voerbak(self):
+ self.process.stdin.write(bytearray("0", "utf-8"))
+ self.process.stdin.flush()
- # read messages from voerbak
- def update_board(self):
- buffer = self.get_output()
- while not buffer.isdigit():
- # win message
- if buffer.startswith("w:"):
- self.win_positions.append(buffer[2:].split("-"))
- log.info(f"won: {buffer[2:].split('-')}")
- self.kill_voerbak()
- # error message
- elif buffer.startswith("e:"):
- log.warning(buffer[2:])
- # turn update message
- elif buffer.startswith("m:"):
- substr = buffer[2:]
- self.player_1 = True if substr == "true" else False
- # draw game message
- elif buffer.startswith("d:"):
- self.board_full = True
- self.kill_voerbak()
- buffer = self.get_output()
- self.board = buffer
+ # read messages from voerbak
+ def update_board(self):
+ buffer = self.get_output()
+ while not buffer.isdigit():
+ # win message
+ if buffer.startswith("w:"):
+ self.win_positions.append(buffer[2:].split("-"))
+ log.info(f"won: {buffer[2:].split('-')}")
+ self.kill_voerbak()
+ # error message
+ elif buffer.startswith("e:"):
+ log.warning(buffer[2:])
+ # turn update message
+ elif buffer.startswith("m:"):
+ substr = buffer[2:]
+ self.player_1 = True if substr == "true" else False
+ # draw game message
+ elif buffer.startswith("d:"):
+ self.board_full = True
+ self.kill_voerbak()
+ buffer = self.get_output()
+ self.board = buffer
- # debug board print function
- def print(self):
- for y in range(self.height - 1, -1, -1):
- for x in range(self.width):
- state = self.board[x + y * self.width]
- char = [EMPTY, DISC_A, DISC_B]
- print(char[int(state)], end=" ")
- print("\n", end="")
+ # debug board print function
+ def print(self):
+ for y in range(self.height - 1, -1, -1):
+ for x in range(self.width):
+ state = self.board[x + y * self.width]
+ char = [EMPTY, DISC_A, DISC_B]
+ print(char[int(state)], end=" ")
+ print("\n", end="")
- def drop_fisje(self, column):
- self.process.stdin.write(bytearray(f"{column}\n", "utf-8"))
- self.process.stdin.flush()
- self.update_board()
+ def drop_fisje(self, column):
+ self.process.stdin.write(bytearray(f"{column}\n", "utf-8"))
+ self.process.stdin.flush()
+ self.update_board()
# debug game
def main():
- gert = bord(7, 6)
- while True:
- print(gert.player_1)
- if len(gert.win_positions) > 0:
- print(f"won: {gert.win_positions}")
- exit(0)
- gert.print()
- column = int(input("column?: ")) - 1
- if column not in range(gert.width):
- continue
- gert.drop_fisje(column + 1)
+ gert = bord(7, 6)
+ while True:
+ print(gert.player_1)
+ if len(gert.win_positions) > 0:
+ print(f"won: {gert.win_positions}")
+ exit(0)
+ gert.print()
+ column = int(input("column?: ")) - 1
+ if column not in range(gert.width):
+ continue
+ gert.drop_fisje(column + 1)
if __name__ == "__main__":
- main()
+ main()