add cli and improved board interface
This commit is contained in:
parent
c7f28a5c64
commit
b30c1a7ffe
|
@ -71,9 +71,10 @@ 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):
|
||||
if self.board[i][j][k][l] == Board.unplayed:
|
||||
buf.append(TwoDPos(TwoDPos.l, (i, j, k, l)))
|
||||
return buf
|
||||
|
||||
|
|
|
@ -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()
|
Loading…
Reference in New Issue