-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameplayer.py
94 lines (79 loc) · 2.48 KB
/
gameplayer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from miniboard import X, O, BLANK
import bigboard
class Player:
'''
A class that represents a Tic-Tac-Toe Player.
'''
def __init__(self, checker):
self.checker = checker
def nextMove(self, board):
'''
Returns position where player wants to play
'''
return [0, 0]
class HumanPlayer(Player):
'''
A Player that can get the next move from the command line.
'''
def nextMove(self, board):
'''
Prompts the user to enter a move.
Continues prompting until the move is valid.
'''
return (self.getPosition(board))
def getBoardNum(self, board):
'''
Prompts the user to enter a column.
Continues prompting until the move is valid.
'''
while True:
column = (input(f"{self.checker}'s board column choice: "))
row = (input(f"{self.checker}'s board row choice: "))
try:
column = int(column) - 1
row = int(row) - 1
assert (0 <= column < 3), 'Not a valid column choice'
assert (0 <= row < 3), 'Not a valid row choice'
assert not board.getValue((column, row)).isWon(), 'Already won!'
except ValueError:
print('Invalid input: ')
except AssertionError as msg:
print(msg)
else:
return (column, row)
def getPosition(self, board):
'''
Prompts the user to enter a column.
Continues prompting until the move is valid.
'''
while True:
column = (input(f"{self.checker}'s column choice: "))
row = (input(f"{self.checker}'s row choice: "))
try:
column = int(column) - 1
row = int(row) - 1
assert (0 <= column < 3), 'Not a valid column choice'
assert (0 <= row < 3), 'Not a valid row choice'
assert not board.isPositionFull((column, row)), 'No room!'
except ValueError:
print('Invalid input: ')
except AssertionError as msg:
print(msg)
else:
return (column, row)
class MachinePlayer(Player):
'''
A silly machine who is not the best at meta tic tac toe
'''
def __init__(self, checker):
Player.__init__(self, checker)
def getBoardNum(self, board):
for i in range(3):
for x in range(3):
if (not board.getValue((i, x)).isWon()):
return (i, x)
def nextMove(self, board):
for i in range(3):
for x in range(3):
if (board.getValue((i, x)) is BLANK):
return (i, x)