forked from lordloh/xo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomPlayer.py
40 lines (32 loc) · 1001 Bytes
/
randomPlayer.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
#! /usr/bin/python3
from random import randint
class randomPlayer:
"""
The random player finds a list of valid moves, and chooses one among it.
"""
def __init__(o, play_as, name, game):
o.play_as = play_as
o.name = name
o.title = "Random Player"
o.game = game
def reset(self):
pass
def play(o):
possible_moves = []
n = 0
board = o.game.brd.board
for b in board:
for e in b:
if (e == 0):
possible_moves = possible_moves + [n]
n += 1
move = possible_moves[randint(0, len(possible_moves)-1)]
return int(move)
def main():
player = randomPlayer(1)
print(player.play([[0, 0, 0], [0, 0, 0], [0, 0, 0]]))
print(player.play([[0, 1, 0], [0, 0, 0], [0, 0, 0]]))
print(player.play([[0, 1, 2], [0, 0, 0], [0, 0, 0]]))
print(player.play([[0, 1, 2], [1, 2, 0], [0, 0, 0]]))
if __name__ == '__main__':
main()