-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththe_bot.py
131 lines (121 loc) · 4.56 KB
/
the_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pandas as pd
import numpy as np
import chess
import gpt_2_simple as gpt2
import random
import os
from chessboard import display
import sys
import argparse
# os.chdir('/Users/biscuit/Documents/GitHub/Personal-Projects/speed_test')
# os.chdir('/home/tbarton/machine_learning_scripts/speed_test')
RUN_NAME = 'new_run_large'
MODEL_NAME = None
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess, run_name=RUN_NAME)
chess_chrs = {
'b_checker': u'\u25FB',
'p': u'\u265F',
'r': u'\u265C',
'n': u'\u265E',
'b': u'\u265D',
'k': u'\u265A',
'q': u'\u265B',
'w_checker': u'\u25FC',
'P': u'\u2659',
'R': u'\u2656',
'N': u'\u2658',
'B': u'\u2657',
'K': u'\u2654',
'Q': u'\u2655'
}
def lets_play_chess(player_color='white', previous_moves=None, fancy_display=False):
if player_color == 'white':
player_color = True
elif player_color == 'black':
player_color = False
else:
player_color = None
if fancy_display:
display.start()
board = chess.Board()
game_so_far = '<#endofdoc#>'
tries = 0
turn_counter = 1
while not board.is_game_over():
if board.turn and game_so_far[-1] != '.':
game_so_far += f' {board.fullmove_number}.'
if board.turn == player_color:
proposed_move = input(f'{"White" if player_color else "Black"}\'s turn: ')
try:
move_san = board.parse_san(proposed_move)
except:
print('that is not a legal move. Try again')
continue
if board.is_legal(move_san):
board.push_san(proposed_move)
game_so_far += ' ' + str(proposed_move)
print(board.unicode(borders=True, empty_square=' '))
if fancy_display:
display.update(board.fen())
else:
print('that is not a legal move. Try again')
continue
else:
proposed_move, tries = predict_next_move(game_so_far, tries)
try:
move_san = board.parse_san(proposed_move)
board.push_san(proposed_move)
game_so_far += ' ' + str(proposed_move)
print(f'The computer succeeded on attempt number {tries} to make a legal move.')
print(board.unicode(borders=True, empty_square=' '))
if fancy_display:
display.update(board.fen())
tries = 0
except:
print(f'try #{tries}: {proposed_move}')
if tries > 10:
print('Looks like I need a little help... lets move randomly and move on')
random_moves = random.sample(board.legal_moves.__str__().split(' ')[3:], 1)[0]
random_moves = random_moves.replace('(', '').replace(')', '').replace('>', '').replace(',', '')
board.push_san(random_moves)
game_so_far += ' ' + str(proposed_move)
print(board.unicode(borders=True, empty_square=' '))
if fancy_display:
display.update(board.fen())
tries = 0
else:
continue
# if board.is_legal(move_san):
#
# else:
print(game_so_far)
if fancy_display:
display.checkForQuit()
print(f'the game is over! congrats to {"white" if board.result() == "1-0" else "black" if board.result() == "0-1" else "both on the tie"}')
if fancy_display:
display.terminate()
def predict_next_move(game_so_far, tries=0):
if tries > 0:
temperature = .7 + .7**(10/tries)
else:
temperature = .7
single_text = gpt2.generate(sess, prefix=game_so_far, return_as_list=True, run_name=RUN_NAME,
model_name=MODEL_NAME, temperature=temperature, length=100)[0]
tries += 1
return_text = single_text[len(game_so_far):].split(' ')
if return_text[0] == '':
return str(return_text[1]), tries
else:
return str(return_text[1]), tries
if __name__ == '__main__':
if len(sys.argv) > 1:
parser = argparse.ArgumentParser()
parser.add_argument('--player_color', action="store")
parser.add_argument('--previous_moves', action="store")
parser.add_argument('--fancy_display', action='store')
args = parser.parse_args()
lets_play_chess(player_color=args.player_color, previous_moves=args.previous_moves,
fancy_display=args.fancy_display)
else:
lets_play_chess()