|
| 1 | +import sys |
| 2 | + |
1 | 3 | from Engine import Engine |
2 | 4 | from Board import Board |
3 | 5 |
|
4 | 6 |
|
5 | 7 | class Game(object): |
6 | 8 | """Handles the operations of a Game""" |
7 | 9 |
|
8 | | - def __init__(self): |
| 10 | + def __init__(self, position=None): |
9 | 11 | super(Game, self).__init__() |
10 | | - self.board = Board() |
11 | | - self.engine = Engine(self.board, depth=12) |
| 12 | + self.board = Board(position) |
| 13 | + self.engine = Engine(self.board, depth=7) |
12 | 14 |
|
13 | 15 | def input_move(self): |
14 | | - idx = int(input()) |
| 16 | + idx = None |
| 17 | + while True: |
| 18 | + try: |
| 19 | + idx = int(input()) |
| 20 | + except: |
| 21 | + print("Invalid Move") |
| 22 | + continue |
| 23 | + else: |
| 24 | + break |
| 25 | + if idx == 99: |
| 26 | + sys.exit() |
15 | 27 | return idx |
16 | 28 |
|
17 | 29 | def human_move(self): |
18 | 30 | moves = self.engine.generate_move_list() |
19 | 31 | self.board.show() |
20 | 32 | print("Please choose one move from the list.") |
21 | | - print(' | '.join("%d. %s" % (i, str(move)) for i, move in enumerate(moves))) |
| 33 | + print('\n'.join("%d. %s" % (i, str(move)) for i, move in enumerate(moves))) |
22 | 34 |
|
23 | 35 | self.engine._make_move(moves[self.input_move()]) |
24 | | - self.engine.make_best_move() |
25 | 36 |
|
26 | 37 |
|
27 | | -game = Game() |
| 38 | +def play(): |
| 39 | + game = Game() |
| 40 | + |
| 41 | + while not game.board.winner: |
| 42 | + game.human_move() |
| 43 | + if game.board.winner: |
| 44 | + break |
| 45 | + game.engine.make_best_move() |
| 46 | + |
| 47 | + game.board.show() |
| 48 | + print(game.board.winner) |
| 49 | + |
| 50 | + |
| 51 | +def play_random_game(): |
| 52 | + game = Game() |
| 53 | + # move_num = 1 |
| 54 | + while not game.board.winner: |
| 55 | + # print(move_num) |
| 56 | + # move_num += 1 |
| 57 | + game.engine.make_best_move() |
| 58 | + game.board.show() |
| 59 | + |
| 60 | + return game.board.winner |
| 61 | + |
28 | 62 |
|
29 | | -while not game.board.winner: |
30 | | - game.human_move() |
| 63 | +play() |
0 commit comments