From b30c1a7ffe54da4cd227a22158dc8509debb1e1c Mon Sep 17 00:00:00 2001 From: randomuser Date: Sat, 9 Jul 2022 22:27:42 -0500 Subject: [PATCH] add cli and improved board interface --- board2.py | 5 ++-- cli.py | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 cli.py diff --git a/board2.py b/board2.py index 62b252a..f263e12 100644 --- a/board2.py +++ b/board2.py @@ -71,10 +71,11 @@ class Board: buf = [] for i in range(3): for j in range(3): - if metaboard[i][j] == Board.unplayed: + if metaboard[j][i] == Board.unplayed: for k in range(3): for l in range(3): - buf.append(TwoDPos(TwoDPos.l, (i, j, k, l))) + if self.board[i][j][k][l] == Board.unplayed: + buf.append(TwoDPos(TwoDPos.l, (i, j, k, l))) return buf def possible(self): diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..d6a22bc --- /dev/null +++ b/cli.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python + +from board2 import Board +from pos import TwoDPos +from pos import TwoDUtils + +piecemaps = { + 0: '.', + 1: 'x', + 2: 'o', +} +mappings = TwoDUtils.reverse + +def render(board): + print("123 456 789") + moves = board.possible() + moves = [i.glob() for i in moves] + for i in range(9): + buf = [] + for j in range(9): + ri = mappings[i] + string = ''.join([ri, str(j + 1)]) + pos = TwoDPos(TwoDPos.g, string) + item = board.get(*pos.local()) + if string in moves: + buf.append('?') + else: + buf.append(piecemaps[item]) + if j in [2, 5]: + buf.append(" | ") + if j == 8: + buf.append(" " + ri) + print(''.join(buf)) + if i in [2, 5]: + print("----+-----+----") + + print("to play: " + str(board.turn)) + +def help(): + print("m - make a move") + print("p - see coordinates of legal moves") + print("t - see whose turn it is") + print("r - render the board to the screen") + print("q - quit program") + print("h - show help (this list)") + +def move(board, move): + move = TwoDPos(TwoDPos.g, move) + print(move) + board.append(move) + +def possibilities(board): + print(board.possible()) + +def turn(board): + print("to play: " + str(board.turn)) + +def main(): + b = Board() + print("type h for help") + while True: + try: + cmd = input("> ") + spl = cmd.split(' ') + if spl[0] == "m": + move(b, spl[1]) + elif spl[0] == "p": + possibilities(b) + elif spl[0] == "t": + turn(b) + elif spl[0] == "r": + render(b) + elif spl[0] == "h": + render(b) + elif spl[0] == "q": + break; + except: + print("error occured") + return + +if __name__ == "__main__": + main()