ultimate/cli.py

83 lines
1.9 KiB
Python
Raw Normal View History

2022-07-09 22:27:42 -05:00
#!/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()