forked from lordloh/xo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random1SSadistPlayer.py
77 lines (66 loc) · 2.29 KB
/
random1SSadistPlayer.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
#! /usr/bin/python3
from random import randint
from err import err
from copy import *
from numpy import *
class random1SSadistPlayer:
"""
The random player with 1 step look ahead finds a list of valid moves, and
chooses one among it such that it can win the next move, or prevent an
adversary from winning in the next move.
Essentially this is a 1 level min max algorithm.
"""
def __init__(self, play_as, name, game):
self.play_as = play_as
self.name = name
self.title = "Sadistic Player with one step look ahead"
self.game = game
def __del__(self):
print('Destructor - Sadist')
def reset(self):
pass
def play(self):
# Compute possible moves
possible_moves = []
n = 0
board = deepcopy(self.game.brd.board)
for b in board:
for e in b:
if (e == 0):
possible_moves = possible_moves + [n]
n += 1
move = -1
# See if our adversary can win if give a chance to play.
# Compute the adversary's id
temp = roll(self.game.play_order, 1)
adversary = temp[self.play_as - 1]
for m in possible_moves:
new_board = deepcopy(board)
x, y = self.game.brd.get_xy_from_pos(m)
new_board[x][y] = adversary
result = self.game.stateless_has_won(new_board, adversary)
if (result == err.WIN):
move = m
break
# The adversary cannot win in the next move.
if (move == -1):
# For each of the listed move, see if the player can win in the next move
for m in possible_moves:
new_board = deepcopy(board)
x, y = self.game.brd.get_xy_from_pos(m)
new_board[x][y] = self.play_as
result = self.game.stateless_has_won(new_board, self.play_as)
if (result == err.WIN):
move = m
break
if (move == -1):
# We cannot win in the next move.
move = possible_moves[randint(0, len(possible_moves)-1)]
else:
# We can win in the next move.
move = m
return int(move)
def main():
pass
if __name__ == '__main__':
main()