|
1 | 1 | import numpy as np |
2 | | - |
| 2 | +import random |
3 | 3 |
|
4 | 4 | class RandomPlayer(): |
5 | | - def __init__(self, game): |
6 | | - self.game = game |
| 5 | + def __init__(self, game): |
| 6 | + self.game = game |
7 | 7 |
|
8 | | - def play(self, board): |
9 | | - a = np.random.randint(self.game.getActionSize()) |
10 | | - valids = self.game.getValidMoves(board, 1) |
11 | | - while valids[a]!=1: |
12 | | - a = np.random.randint(self.game.getActionSize()) |
13 | | - return a |
| 8 | + def play(self, board): |
| 9 | + valids = self.game.getValidMoves(board, 1) |
| 10 | + return random.choices(range(self.game.getActionSize()), weights=valids, k=1)[0] |
14 | 11 |
|
15 | 12 |
|
16 | 13 | class HumanOthelloPlayer(): |
17 | | - def __init__(self, game): |
18 | | - self.game = game |
19 | | - |
20 | | - def play(self, board): |
21 | | - # display(board) |
22 | | - valid = self.game.getValidMoves(board, 1) |
23 | | - for i in range(len(valid)): |
24 | | - if valid[i]: |
25 | | - print("[", int(i/self.game.n), int(i%self.game.n), end="] ") |
26 | | - while True: |
27 | | - input_move = input() |
28 | | - input_a = input_move.split(" ") |
29 | | - if len(input_a) == 2: |
30 | | - try: |
31 | | - x,y = [int(i) for i in input_a] |
32 | | - if ((0 <= x) and (x < self.game.n) and (0 <= y) and (y < self.game.n)) or \ |
33 | | - ((x == self.game.n) and (y == 0)): |
34 | | - a = self.game.n * x + y if x != -1 else self.game.n ** 2 |
35 | | - if valid[a]: |
36 | | - break |
37 | | - except ValueError: |
38 | | - # Input needs to be an integer |
39 | | - 'Invalid integer' |
40 | | - print('Invalid move') |
41 | | - return a |
| 14 | + def __init__(self, game): |
| 15 | + self.game = game |
| 16 | + |
| 17 | + def play(self, board): |
| 18 | + # display(board) |
| 19 | + valid = self.game.getValidMoves(board, 1) |
| 20 | + for i in range(len(valid)): |
| 21 | + if valid[i]: |
| 22 | + print("[", int(i/self.game.n), int(i%self.game.n), end="] ") |
| 23 | + while True: |
| 24 | + input_move = input() |
| 25 | + input_a = input_move.split(" ") |
| 26 | + if len(input_a) == 2: |
| 27 | + try: |
| 28 | + x,y = [int(i) for i in input_a] |
| 29 | + if ((0 <= x) and (x < self.game.n) and (0 <= y) and (y < self.game.n)) or \ |
| 30 | + ((x == self.game.n) and (y == 0)): |
| 31 | + a = self.game.n * x + y if x != -1 else self.game.n ** 2 |
| 32 | + if valid[a]: |
| 33 | + break |
| 34 | + except ValueError: |
| 35 | + # Input needs to be an integer |
| 36 | + 'Invalid integer' |
| 37 | + print('Invalid move') |
| 38 | + return a |
42 | 39 |
|
43 | 40 |
|
44 | 41 | class GreedyOthelloPlayer(): |
45 | | - def __init__(self, game): |
46 | | - self.game = game |
47 | | - |
48 | | - def play(self, board): |
49 | | - valids = self.game.getValidMoves(board, 1) |
50 | | - candidates = [] |
51 | | - for a in range(self.game.getActionSize()): |
52 | | - if valids[a]==0: |
53 | | - continue |
54 | | - nextBoard, _ = self.game.getNextState(board, 1, a) |
55 | | - score = self.game.getScore(nextBoard, 1) |
56 | | - candidates += [(-score, a)] |
57 | | - candidates.sort() |
58 | | - return candidates[0][1] |
| 42 | + def __init__(self, game): |
| 43 | + self.game = game |
| 44 | + |
| 45 | + def play(self, board): |
| 46 | + valids = self.game.getValidMoves(board, 1) |
| 47 | + candidates = [] |
| 48 | + for a in range(self.game.getActionSize()): |
| 49 | + if valids[a]==0: |
| 50 | + continue |
| 51 | + nextBoard, _ = self.game.getNextState(board, 1, a) |
| 52 | + score = self.game.getScore(nextBoard, 1) |
| 53 | + candidates += [(-score, a)] |
| 54 | + candidates.sort() |
| 55 | + return candidates[0][1] |
0 commit comments