Learn AlphaGo & Python (02)

Source: Deep Learning on Medium

Learn AlphaGo & Python (02)

Printing an empty game board every second

Put existing code into function main():

🐍 vim print_board.py 
🐍 python3 print_board.py
19 . . . . . . . . . . . . . . . . . . .
18 . . . . . . . . . . . . . . . . . . .
17 . . . . . . . . . . . . . . . . . . .
16 . . . . . . . . . . . . . . . . . . .
15 . . . . . . . . . . . . . . . . . . .
14 . . . . . . . . . . . . . . . . . . .
13 . . . . . . . . . . . . . . . . . . .
12 . . . . . . . . . . . . . . . . . . .
11 . . . . . . . . . . . . . . . . . . .
10 . . . . . . . . . . . . . . . . . . .
9 . . . . . . . . . . . . . . . . . . .
8 . . . . . . . . . . . . . . . . . . .
7 . . . . . . . . . . . . . . . . . . .
6 . . . . . . . . . . . . . . . . . . .
5 . . . . . . . . . . . . . . . . . . .
4 . . . . . . . . . . . . . . . . . . .
3 . . . . . . . . . . . . . . . . . . .
2 . . . . . . . . . . . . . . . . . . .
1 . . . . . . . . . . . . . . . . . . .
A B C D E F G H J K L M N O P Q R S T

print_board.py:

COLS = 'ABCDEFGHJKLMNOPQRST'def main():
for row in range(19, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, 19 + 1):
line.append(". ")
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:19]))
if __name__ == '__main__':
main()

Create function print_board(num_rows, num_cols):

COLS = 'ABCDEFGHJKLMNOPQRST'def print_board(num_rows, num_cols):
for row in range(num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, num_cols + 1):
line.append(". ")
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:num_cols]))
def main():
print_board(19, 19)
if __name__ == '__main__':
main()

Create class Board:

🐍 mkdir dlgo
🐍 touch dlgo/__init__.py
🐍 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

Use class Board in print_board.py

🐍 vim print_board.py

print_board.py

from dlgo.goboard_slow import BoardCOLS = 'ABCDEFGHJKLMNOPQRST'def print_board(num_rows, num_cols):
for row in range(num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, num_cols + 1):
line.append(". ")
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:num_cols]))
def main():
board = Board(19, 19)
print_board(board.num_rows, board.num_cols)
if __name__ == '__main__':
main()

Output game board (size changed to 9×9)every second:

🐍 vim print_board.py
🐍 python3 print_board.py
9 . . . . . . . . .
8 . . . . . . . . .
7 . . . . . . . . .
6 . . . . . . . . .
5 . . . . . . . . .
4 . . . . . . . . .
3 . . . . . . . . .
2 . . . . . . . . .
1 . . . . . . . . .
A B C D E F G H J
9 . . . . . . . . .
8 . . . . . . . . .
7 . . . . . . . . .
6 . . . . . . . . .
5 . . . . . . . . .
4 . . . . . . . . .
3 . . . . . . . . .
2 . . . . . . . . .
1 . . . . . . . . .
A B C D E F G H J

print_board.py

from dlgo.goboard_slow import Board
import time
COLS = 'ABCDEFGHJKLMNOPQRST'def print_board(num_rows, num_cols):
for row in range(num_rows, 0, -1):
bump = " " if row <= 9 else ""
line = []
for col in range(1, num_cols + 1):
line.append(". ")
print('%s%d %s' % (bump, row, ''.join(line)))
print(' ' + ' '.join(COLS[:num_cols]))
def main():
board = Board(9, 9)
while True:
time.sleep(1.0)
print_board(board.num_rows, board.num_cols)
print()
if __name__ == '__main__':
main()