-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxo.py
82 lines (73 loc) · 2.19 KB
/
oxo.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
import board
PLAYERS = ['O', 'X']
N_CELLS = 3
def turns():
#
# This is a convenience function which will keep
# going through the list of players (which doesn't
# have to be only two) returning the next one each
# time and then going back to the beginning.
#
while True:
for player in PLAYERS:
yield player
def get_coord(b, player):
#
# Get a move for the current player
# No error checking: just get the coordinates
# If the coord has already been used, warn and
# ask again
#
while True:
position = input("Player %s -- make a move x, y " % player)
coord = tuple(int(i.strip()) for i in position.split(","))
if b[coord]:
print("%s is already taken" % (coord,))
else:
return coord
def has_player_won(b, player):
#
# Brute force: look at all the possible runs
# of length <whatever> where <whatever> is the
# size of the board. If any of those runs consists
# entirely of the player character, then that
# player has won.
#
# If we end up having found no runs where this player
# is the only non-blank character, then this player
# has not won on this round
#
run_length = len(b.dimensions[0])
for coords, data in b.runs_of_n(run_length):
if all(i == player for i in data):
return True
return False
if __name__ == '__main__':
b = board.Board((N_CELLS, N_CELLS))
for turn in turns():
print()
b.draw()
coord = get_coord(b, turn)
b[coord] = turn
#
# Check to see if this player has won.
# We don't need to check all the players, because
# you can't cause another player to win on your
# own turn in noughts-and-crosses
#
if has_player_won(b, turn):
print("%s has won!" % turn)
break
#
# If no-one has won yet, check to see whether all the
# spots on the board are filled as this means we have
# a draw
#
elif len(b) == b.lendata():
print("No-one has won :(")
break
#
# Show the final state of the board
#
print()
b.draw()