add cli and improved board interface

This commit is contained in:
randomuser 2022-07-09 22:27:42 -05:00
parent c7f28a5c64
commit b30c1a7ffe
2 changed files with 85 additions and 2 deletions

View File

@ -71,10 +71,11 @@ class Board:
buf = [] buf = []
for i in range(3): for i in range(3):
for j 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 k in range(3):
for l 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 return buf
def possible(self): def possible(self):

82
cli.py Normal file
View File

@ -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()