-
Notifications
You must be signed in to change notification settings - Fork 808
/
041_challenge_2_example.py
154 lines (122 loc) · 3.86 KB
/
041_challenge_2_example.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
152
153
154
# Video alternative: https://vimeo.com/954334009/67af9910fc#t=0
# Nice work on that last one! You might well want to
# consider taking the assessment at this point.
# However, if you did want some more challenge, here it is.
# We're going to tackle something really sophisticated.
# We're going to build a tic tac toe game!
# This will introduce us to lists of lists. Here's one:
a_list_of_lists = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# And to get items out we index in twice:
a_list_of_lists[0][0] # Evaluates to 1
a_list_of_lists[0][1] # Evaluates to 2
a_list_of_lists[0][2] # Evaluates to 3
a_list_of_lists[1][0] # Evaluates to 4
# Et cetera.
# Looks kind of like a grid right? We can use it to
# represent a tic-tac-toe board:
completed_board = [
["X", "O", "X"],
["O", "X", "O"],
["O", "X", "O"]
]
# We're going to implement a little game. We'll need three
# functions:
# 1. A function to format the board for the user.
# 2. A function to make a move.
# 3. A function to check if the game is over.
# Let's start with formatting the board:
def print_board(board):
formatted_rows = []
for row in board:
formatted_rows.append(" ".join(row))
grid = "\n".join(formatted_rows)
return grid
# Let's test it out:
starter_board = [
[".", ".", "."],
[".", ".", "."],
[".", ".", "."]
]
print("Our starting board:")
print(print_board(starter_board))
# Now let's write a function to make a move:
def make_move(board, row, column, player):
board[row][column] = player
return board
# And try it out:
print("After a move:")
print(print_board(make_move(starter_board, 0, 0, "X")))
# Now let's write a few functions to check if the game is
# over:
# This function will extract three cells from the board
def get_cells(board, coord_1, coord_2, coord_3):
return [
board[coord_1[0]][coord_1[1]],
board[coord_2[0]][coord_2[1]],
board[coord_3[0]][coord_3[1]]
]
# This function will check if the group is fully placed
# with player marks, no empty spaces.
def is_group_complete(board, coord_1, coord_2, coord_3):
cells = get_cells(board, coord_1, coord_2, coord_3)
return "." not in cells
# This function will check if the group is all the same
# player mark: X X X or O O O
def are_all_cells_the_same(board, coord_1, coord_2, coord_3):
cells = get_cells(board, coord_1, coord_2, coord_3)
return cells[0] == cells[1] and cells[1] == cells[2]
# We'll make a list of groups to check:
groups_to_check = [
# Rows
[(0, 0), (0, 1), (0, 2)],
[(1, 0), (1, 1), (1, 2)],
[(2, 0), (2, 1), (2, 2)],
# Columns
[(0, 0), (1, 0), (2, 0)],
[(0, 1), (1, 1), (2, 1)],
[(0, 2), (1, 2), (2, 2)],
# Diagonals
[(0, 0), (1, 1), (2, 2)],
[(0, 2), (1, 1), (2, 0)]
]
def is_game_over(board):
# We go through our groups
for group in groups_to_check:
# If any of them are empty, they're clearly not a
# winning row, so we skip them.
if is_group_complete(board, group[0], group[1], group[2]):
if are_all_cells_the_same(board, group[0], group[1], group[2]):
return True # We found a winning row!
# Note that return also stops the function
return False # If we get here, we didn't find a winning row
# Now let's put it all together:
def play_game():
board = [
[".", ".", "."],
[".", ".", "."],
[".", ".", "."]
]
player = "X"
while not is_game_over(board):
print(print_board(board))
print("It's " + player + "'s turn.")
# `input` asks the user to type in a string
# We then need to convert it to a number using `int`
row = int(input("Enter a row: "))
column = int(input("Enter a column: "))
board = make_move(board, row, column, player)
if player == "X":
player = "O"
else:
player = "X"
print(print_board(board))
print("Game over!")
# And try it out:
print("Game time!")
play_game()
# @TASK Run this file to play the game.
# Once you're done, move on to 042_challenge_2_exercise.py