-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path042_challenge_2_exercise.py
More file actions
130 lines (105 loc) · 3.71 KB
/
042_challenge_2_exercise.py
File metadata and controls
130 lines (105 loc) · 3.71 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
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
# Video alternative: https://youtu.be/Y-6WZfJ9I6c&t=1054s
# So far you've spent a lot of time writing new programs.
# This is great for learning the fundamentals of code, but
# actually isn't very realistic. Most software engineers
# spend their time modifying and maintaining existing
# programs, not writing entirely new ones.
# Below is the same program as in the example. Your
# challenge is to implement some improvements:
# 1. Right now users can place their tiles over the other
# user's tiles. Prevent this.
# 2. Right now if the game reaches a draw with no more free
# spaces, the game doesn't end. Make it end at that
# point.
# 3. If you want a real challenge, try to rework this
# program to support a 5x5 board rather than a 3x3 board.
# 4. If you're still not satisfied, try to rework this
# program to take a parameter `board_size` and play a
# game with a board of that size.
# This is getting really challenging now — and is entirely
# optional. Don't forget about your assessment!
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: "))
if is_valid_move(board, row, column):
board = make_move(board, row, column, player)
if player == "X":
player = "O"
else:
player = "X"
else:
print('Invalid move')
print(print_board(board))
print("Game over!")
def print_board(board):
formatted_rows = []
for row in board:
formatted_rows.append(" ".join(row))
grid = "\n".join(formatted_rows)
return grid
def is_valid_move(board, row, column):
return row >= 0 and row < 3 and column >= 0 and column < 3 and board[row][column] == '.'
def make_move(board, row, column, player):
board[row][column] = player
return board
# 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_board_full(board):
for row in board:
for cell in row:
if cell == '.': return False
return True
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
if is_board_full(board): return True
return False # If we get here, we didn't find a winning row
# And test it out:
print("Game time!")
play_game()