-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe.py
executable file
·52 lines (39 loc) · 1.39 KB
/
tic_tac_toe.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
#!/usr/bin/env python
"""
tic_tac_toe.py
Author: Lukas Linauer
Main module, parses command line arguments and sets up game
"""
import sys
import argparse
from game import Game
def main():
"""Main function"""
# parse arguments
parser = argparse.ArgumentParser(description='Train an Reinforcement Learning agent in Tic '
'Tac Toe and play against it')
parser.add_argument('-a', '--action', choices=['train', 'play'],
help='Train the agent or play against it', type=str)
parser.add_argument('-n', help='Number of rounds for training (Default: 100)', dest='n_rounds',
default=100)
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
# check if an action was given
if 'action' not in args:
print('Please provide a valid action. Valid actions are: train, play')
return
if args.action == 'train':
tic_tac_toe_game = Game()
try:
n_rounds = int(args.n_rounds)
except ValueError:
print('Invalid value for n')
return
tic_tac_toe_game.computer_vs_computer(n_rounds)
elif args.action == 'play':
# initialize new game
tic_tac_toe_game = Game(vs_human=True)
tic_tac_toe_game.player_vs_computer()
if __name__ == '__main__':
main()