
Learn AlphaGo & Python (03)
Landing a piece on board


Add a dictionary _grid
in dlgo/goboard_slow.py:
class Board():
def __init__(self, num_rows, num_cols):
self.num_rows = num_rows
self.num_cols = num_cols
self._grid = {}
Rename print_board.py to game.py.
🐍 mv print_board.py game.py
game.py:
from dlgo.goboard_slow import Board
from collections import namedtuple
import timeclass Point(namedtuple('Point', 'row col')):
passCOLS = 'ABCDEFGHJKLMNOPQRST'def print_board(board):
for row in range(board.num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, board.num_cols + 1):
p = Point(row=row, col=col)
if p in board._grid:
if board._grid[p] == 'x':
line.append("x ")
elif board._grid[p] == 'o':
line.append("o ")
else:
line.append(". ")
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:board.num_cols]))def main():
board = Board(19, 19)
point23 = Point(row=2, col=3)
board._grid[point23] = 'x'
while True:
time.sleep(0.3) print(chr(27) + "[2J")
print_board(board)
print()if __name__ == '__main__':
main()
Run game.py
🐍 python3 game.py
“Ctrl + C” to stop it.
19 . . . . . . . . . . . . . . . . . . .
18 . . . . . . . . . . . . . . . . . . .
17 . . . . . . . . . . . . . . . . . . .
16 . . . . . . . . . . . . . . . . . . .
15 . . . . . . . . . . . . . . . . . . .
14 . . . . . . . . . . . . . . . . . . .
13 . . . . . . . . . . . . . . . . . . .
12 . . . . . . . . . . . . . . . . . . .
11 . . . . . . . . . . . . . . . . . . .
10 . . . . . . . . . . . . . . . . . . .
9 . . . . . . . . . . . . . . . . . . .
8 . . . . . . . . . . . . . . . . . . .
7 . . . . . . . . . . . . . . . . . . .
6 . . . . . . . . . . . . . . . . . . .
5 . . . . . . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . . . . . . .
3 . . . . . . . . . . . . . . . . . . .
2 . . x . . . . . . . . . . . . . . . .
1 . . . . . . . . . . . . . . . . . . .
A B C D E F G H J K L M N O P Q R S T
Move out Point to a new file dlgo/gotypes.py.
🐍 vim dlgo/gotypes.py
dlgo/gotypes.py:
from collections import namedtuple
class Point(namedtuple('Point', 'row col')):
pass
game.py:
from dlgo.goboard_slow import Board
from dlgo.gotypes import Point
import timeCOLS = 'ABCDEFGHJKLMNOPQRST'def print_board(board):
for row in range(board.num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, board.num_cols + 1):
p = Point(row=row, col=col)
if p in board._grid:
if board._grid[p] == 'x':
line.append("x ")
elif board._grid[p] == 'o':
line.append("o ")
else:
line.append(". ")
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:board.num_cols]))def main():
board = Board(19, 19)
point23 = Point(row=2, col=3)
board._grid[point23] = 'x'
while True:
time.sleep(0.3)
print(chr(27) + "[2J")
print_board(board)
print()if __name__ == '__main__':
main()
Create function place_stone(..) and get(..) in class Board:
🐍 vim dlgo/goboard_slow.py
dlgo/goboard_slow.py:
class Board():
def __init__(self, num_rows, num_cols):
self.num_rows = num_rows
self.num_cols = num_cols
self._grid = {}def place_stone(self, point, piece):
self._grid[point] = piecedef get(self, point):
return self._grid.get(point)
game.py
from dlgo.goboard_slow import Board
from dlgo.gotypes import Point
import timeCOLS = 'ABCDEFGHJKLMNOPQRST'def print_board(board):
for row in range(board.num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, board.num_cols + 1):
p = Point(row=row, col=col)
if p in board._grid:
if board.get(p) == 'x':
line.append("x ")
elif board.get(p) == 'o':
line.append("o ")
else:
line.append(". ")
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:board.num_cols]))def main():
board = Board(19, 19)
point23 = Point(row=2, col=3)
board.place_stone(point23, 'x')
while True:
time.sleep(0.3)
print(chr(27) + "[2J")
print_board(board)
print()if __name__ == '__main__':
main()
Create enum Player in dlgo/gotypes.py:
import enum
from collections import namedtupleclass Player(enum.Enum):
black = 1
white = 2
class Point(namedtuple('Point', 'row col')):
pass
Modify function place_stone(..) in dlgo/goboard_slow.py:
class Board():
def __init__(self, num_rows, num_cols):
self.num_rows = num_rows
self.num_cols = num_cols
self._grid = {}def place_stone(self, player, point):
self._grid[point] = playerdef get(self, point):
return self._grid.get(point)
Add another white piece on board. Run it and press Ctrl + C to stop it.
🐍 vim game.py
🐍 python3 game.py19 . . . . . . . . . . . . . . . . . . .
18 . . . . . . . . . . . . . . . . . . .
17 . . . . . . . . . . . . . . . . . . .
16 . . . . . . . . . . . . . . . . . . .
15 . . . . . . . . . . . . . . . . . . .
14 . . . . . . . . . . . . . . . . . . .
13 . . . . . . . . . . . . . . . . . . .
12 . . . . . . . . . . . . . . . . . . .
11 . . . . . . . . . . . . . . . . . . .
10 . . . . . . . . . . . . . . . . . . .
9 . . . . . . . . . . . . . . . . . . .
8 . . . . . . . . . . . . . . . . . . .
7 . . . . . . . . . . . . . . . . . . .
6 . . . . . . . . . . . . . . . . . . .
5 . . . . . . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . . . . . . .
3 . . . . . . . . . . . . . . . . . . .
2 . . x o . . . . . . . . . . . . . . .
1 . . . . . . . . . . . . . . . . . . .
A B C D E F G H J K L M N O P Q R S T^CTraceback (most recent call last):
File "game.py", line 34, in <module>
main()
File "game.py", line 27, in main
time.sleep(0.3)
KeyboardInterrupt
🐍
game.py:
from dlgo.goboard_slow import Board
from dlgo import gotypes
import timeCOLS = 'ABCDEFGHJKLMNOPQRST'
STONE_TO_CHAR = {
None: '. ',
gotypes.Player.black: 'x ',
gotypes.Player.white: 'o ',
}def print_board(board):
for row in range(board.num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, board.num_cols + 1):
stone = board.get(gotypes.Point(row=row, col=col))
line.append(STONE_TO_CHAR[stone])
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:board.num_cols]))def main():
board = Board(19, 19)
board.place_stone(gotypes.Player.black, gotypes.Point(2, 3))
board.place_stone(gotypes.Player.white, gotypes.Point(2, 4))
while True:
time.sleep(0.3)
print(chr(27) + "[2J")
print_board(board)
print()if __name__ == '__main__':
main()