-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerClass.py
More file actions
59 lines (50 loc) · 2.44 KB
/
Copy pathPlayerClass.py
File metadata and controls
59 lines (50 loc) · 2.44 KB
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
#
# ps10pr2.py (Problem Set 10, Problem 2)
#
# A Connect Four Player class
#
from BoardClass import Board
# Write your class below.
class Player:
""" Player class to represent a player of
the Connect Four game
"""
def __init__(self, checker):
""" constructor for player object """
assert(checker == 'X' or checker == 'O')
self.checker = checker
self.num_moves = 0
def __repr__(self):
""" returns a string representation for
the player object
"""
return 'Player ' + self.checker
def opponent_checker(self):
""" returns a string representation for
the Player object opponent's checker
"""
if self.checker == 'X':
return 'O'
elif self.checker == 'O':
return 'X'
def next_move(self, board):
""" asks the user for a column to put a
checker in the board object input and
returns the column number if it is valid,
and will ask for a column until a valid
one is given
"""
col = eval(input('Emter a column: '))
col = int(col)
if board.can_add_to(col) == True:
return col
else:
a = False
while a == False:
print('Try again!')
col = eval(input('Enter a column: '))
col = int(col)
if board.can_add_to(col) == True:
a = True
self.num_moves += 1
return col