-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
190 lines (166 loc) · 5.88 KB
/
main.py
File metadata and controls
190 lines (166 loc) · 5.88 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
# This function asks the user for their name and prints it to the screen
import random
# Assignment 2
def grab_name():
# Prompts user to enter their name
name = input("What is your name?")
# Print user's name
print(f'Nice to meet you, {name}!')
# Asks the user for a number
number = float(input("Please respond with a number:"))
# Multiplies the number by itself and printers result
print(number * number)
# Ask the user for a word
countphrase = input("Please enter a word:")
# Prints the length of the string
print(len(countphrase))
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
# Assignment 3
# This function uses a two-dimensional array and loops to create text art
def w4loops():
# Creates the array
art = [['.', '*', 'o', '.', '*', 'o'],
['.', 'o', 'o', '*', '.', '*'],
['o', 'o', '*', '*', '*', '.'],
['.', '*', 'o', '.', '*', 'o'],
['o', 'o', '*', '*', 'o', '.'],
['.', 'o', 'o', '*', '*', 'o'],
['*', 'o', 'o', 'o', '*', '.'],
['*', 'o', 'o', 'o', '*', 'o'],
['*', '*', '.', 'o', 'o', '*']]
# Loops print
for star in art:
for x in star:
print(x, end=' ')
print()
# This function simulates a coin flip
def w4coin_flip():
# Welcomes user and prompts them to enter a number corresponding to heads or tails
user_choice = int(input('Welcome to CoinFlip! Please enter 0 or 1.'))
# Uses an RNG to randomly generate a 1 or 0
pc_random = random.randint(0, 1)
# Determines winner
if user_choice == 1 and pc_random == 1:
print(f"I got {pc_random} and you chose {user_choice}")
print('I win!')
else:
print(f"I got {pc_random} and you chose {user_choice}")
print('You win!')
# Assignment 4
# Computer = X
# User = O
grid = [[1, 2, 3],
[4, 'X', 6],
[7, 8, 9]]
free_spaces = []
def display_board(board):
# The function accepts one parameter containing the board's current status
# and prints it out to the console.
print(f"""+-------+-------+-------+
| | | |
| {board[0][0]} | {board[0][1]} | {board[0][2]} |
| | | |
+-------+-------+-------+
| | | |
| {board[1][0]} | {board[1][1]} | {board[1][2]} |
| | | |
+-------+-------+-------+
| | | |
| {board[2][0]} | {board[2][1]} | {board[2][2]} |
| | | |
+-------+-------+-------+""")
def enter_move(board):
# The function accepts the board's current status, asks the user about their move,
# checks the input, and updates the board according to the user's decision.
while True:
try:
user_choice = int(input("Enter your move: "))
for i in range(len(free_spaces)):
if user_choice == board[free_spaces[i][0]][free_spaces[i][1]]:
board[free_spaces[i][0]][free_spaces[i][1]] = 'O'
return
# Makes an exception for an error so the function can work as intended.
except ValueError:
continue
def make_list_of_free_fields(board):
# The function browses the board and builds a list of all the free squares;
# the list consists of tuples, while each tuple is a pair of row and column numbers.
for x in range(0, 3):
for y in range(0, 3):
if str(board[x][y]) != 'O' and str(board[x][y]) != 'X':
free_spaces.append(tuple((x, y)))
def victory_for(board, sign):
# The function analyzes the board's status in order to check if
# the player using 'O's or 'X's has won the game
if sign == 'X':
player = "AI"
else:
player = "User"
# Checks for wins by column
count = 0
for x in range(0, 3):
if count == 3:
break
else:
count = 0
for y in range(0, 3):
if board[x][y] == sign:
count += 1
# Checks for wins by row
if count != 3:
for x in range(0, 3):
if count == 3:
break
else:
count = 0
for y in range(0, 3):
if board[y][x] == sign:
count += 1
# Checks for diagonal wins
if board[0][0] == sign and board[1][1] == sign and board[2][2] == sign:
count = 3
elif board[0][2] == sign and board[1][1] == sign and board[2][0] == sign:
count = 3
if count == 3:
print(f"The winner is: {player}!")
exit()
else:
tie = 0
for x in range(0, 3):
for y in range(0, 3):
if board[x][y] == 'X' or board[x][y] == 'O':
tie += 1
if tie == 9:
print("The game is a tie!")
exit()
def draw_move(board):
# The function draws the computer's move and updates the board.
while True:
ai_rand = random.randrange(1, 10)
for j in range(len(free_spaces)):
if ai_rand == board[free_spaces[j][0]][free_spaces[j][1]]:
board[free_spaces[j][0]][free_spaces[j][1]] = 'X'
return
# Main caller
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# print_hi('Zack')
# grab_name()
# w4loops()
# w4coin_flip()
display_board(grid)
while True:
make_list_of_free_fields(grid)
enter_move(grid)
display_board(grid)
victory_for(grid, 'O')
make_list_of_free_fields(grid)
draw_move(grid)
display_board(grid)
victory_for(grid, 'X')