-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe_game.py
151 lines (129 loc) · 5.35 KB
/
tictactoe_game.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from random import randrange
## Corrine Clapp ##
def starting_conditions():
# determine who goes first
while(True):
order = raw_input("Would you like to go first or second?[f/s] ")
if order == "f":
order = True
break
elif order == "s":
order = False
break
else:
print("Sorry, I don't know what you mean.")
# determine who plays which piece
while(True):
piece = raw_input("Would you like to be X or O?[X/O] ")
if piece == "X" or piece == "O":
break
else:
print("Sorry, I don't know what you mean.")
# set difficulty
while(True):
diff = raw_input("What difficulty would you like to play on? ")
if diff == "easy":
diff = 50
break
elif diff == "medium":
diff = 25
break
elif diff == "hard":
diff = 10
break
else:
print("Sorry, I don't know what you mean.")
return order, piece, diff;
def randomizer(diff):
# if random number falls below diff, then robot will make random move
rand = randrange(1, 101)
if rand < diff:
return True
else:
return False;
def switch_piece(piece):
if piece == "X":
piece = "O"
elif piece == "O":
piece = "X"
return piece;
def win_conditions(gameboard, piece):
n = len(gameboard)
# checks whether all elements in any generated list are the same
for index in check_win(n):
if all(gameboard[row][col] == piece for row, col in index):
return True
return False;
def check_win(n):
# generator holds lists of every possible win arrangement
# returns lists of every row
for row in range(n):
yield[(row, col) for col in range(n)]
# returns lists of every column
for col in range(n):
yield[(row, col) for row in range(n)]
# returns lists of diagonal from top left to bottom right
yield[(i, i) for i in range(n)]
# returns lists of diagonal from bottom left to top right
yield[(i, n - 1 - i) for i in range(n)];
def fill_random(gameboard, piece):
# robot fills random unoccupied space
while True:
randr = randrange(0,3)
randc = randrange(0,3)
if gameboard[randr][randc] == " ":
gameboard[randr][randc] = piece
return;
def player_move(gameboard, piece):
# player selects a spot to place piece
while True:
row = int(input("What row would you like to place your piece in? (1-3) ")) - 1
col = int(input("What column would you like to place your piece in? (1-3) ")) - 1
if (row < 0 or row > 2) or (col < 0 or col > 2):
print("That's not a valid space!")
elif gameboard[row][col] == " ":
gameboard[row][col] = piece
break
else:
print("That space is already occupied!")
return;
def print_board(gameboard):
for row in gameboard:
print row;
def tic_tac_toe_game():
player, piece, diff = starting_conditions()
# EVENTUALLY: search for physical gameboard
# for now, creates 3x3 board
gameboard = [[" " for i in range(3)] for j in range(3)]
# play game until win conditions are true
while True:
# player's turn
if player:
print("Your turn!")
player_move(gameboard, piece)
print_board(gameboard)
# robot's turn
else:
print("My turn!")
# either randomizes move or determines next move with call to algorithm
if randomizer(diff):
fill_random(gameboard, piece)
else:
row, col = getNextMove(gameboard, switch_piece(piece), piece)
gameboard[row][col] = piece
print_board(gameboard)
# check for win/tie
if win_conditions(gameboard, piece):
if player:
print("You win!")
else:
print("I win!")
break
elif not any(' ' in x for x in gameboard):
print("Tie!")
break
# switch to other player/piece
player = not player
piece = switch_piece(piece)
return;
tic_tac_toe_game()