diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-02-16 14:36:20 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-02-16 14:36:20 +0100 |
commit | d2536926285b8cf8f2bcb7dfc300529d1914e4d8 (patch) | |
tree | 860f2bc717bdb556f4333028414a1c290d5775e7 /console | |
parent | 1200fd15d5a110b15c16a333936bd3c74f90cbca (diff) |
voerbak_connector working
Diffstat (limited to 'console')
-rw-r--r-- | console/voerbak_connector.py | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/console/voerbak_connector.py b/console/voerbak_connector.py index b300af0..fb6dbf2 100644 --- a/console/voerbak_connector.py +++ b/console/voerbak_connector.py @@ -11,37 +11,49 @@ class bord: def __init__(self, w, h): self.width = w self.height = h + self.board = "0" * (w * h) + self.win_positions = [] self.process = subprocess.Popen(["./voerbak"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) - msg = bytearray(f"{w} {h}\n", "utf-8") - print({"msg": msg}) - self.process.stdin.write(msg) + self.process.stdin.write(bytearray(f"{w} {h}\n", "utf-8")) self.process.stdin.flush() - # def print(self): - # for y in range(self.height -1, -1, -1): - # for x in range(self.width): - # print(self.board[x + y * self.width], end=" ") - # print("\n", end="") + def update_board(self): + buffer = self.process.stdout.readline() + while buffer.decode().startswith("w"): + self.win_positions.append(buffer.decode()[2:-1].split("-")) + buffer = self.process.stdout.readline() + self.board = buffer.decode() + + 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): - msg = bytearray(f"{column}\n", "utf-8") - print({"msg": msg}) - self.process.stdin.write(msg) + self.process.stdin.write(bytearray(f"{column}\n", "utf-8")) self.process.stdin.flush() + self.update_board() def main(): gert = bord(7, 6) while True: # gert.print() - column = int(input("column?: ")) - 1 + column = int(input("column?: ")) if column not in range(gert.width): continue # os.system("clear") gert.drop_fisje(column) - print(gert.process.stdout.readline()) + gert.print() + print(gert.win_positions) if __name__ == "__main__": main() |