-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknots_and_crosses.py
143 lines (109 loc) · 3.66 KB
/
knots_and_crosses.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
#Udemy milestone project 1
#Learnt error handling + game logic
#Knots and crosses using 2d list
#Functions
#given a 3 x 3 2d array, will print in tic tac toe format
def print_board(game_arr):
print(" 0 1 2")
print(f"0 {game_arr[0][0]} | {game_arr[0][1]} | {game_arr[0][2]}")
print(" ---------")
print(f"1 {game_arr[1][0]} | {game_arr[1][1]} | {game_arr[1][2]}")
print(" ---------")
print(f"2 {game_arr[2][0]} | {game_arr[2][1]} | {game_arr[2][2]}")
#convert input string of form "x,y" as tuple
def convert_coord(coord):
return tuple(coord.split(","))
#alternates user and returns 1 or 2
def alternate(user):
return (user + 1) % 2
#check win condition based on current x and y
def check_win(user_pos, game_arr):
x,y = user_pos
#check current row:
if game_arr[x][0] == game_arr[x][1] == game_arr[x][2]:
return True
#check current column
elif game_arr[0][y] == game_arr[1][y] == game_arr[2][y]:
return True
#always check diagonals
elif (game_arr[0][0] == game_arr[1][1] == game_arr[2][2]) or (game_arr[0][2] == game_arr[1][1] == game_arr[2][0]):
if game_arr[1][1] != " ":
return True
#no win condition met
return False
#take in player input coord
#checks if valid
#updates and prints board
def user_input(game_arr, user):
valid_form = False
valid_bound = False
valid_pos = False
bounds = [0,1,2]
coord = " , "
while (valid_form == False or valid_bound == False or valid_pos == False):
print_board(game_arr)
print(" ")
coord = input("Please input your desired coordinate in the form 'x,y': ")
#first: check correct format
if (len(coord) == 3 and coord[1] == ','):
valid_form = True
else:
#clear_output()
print("\033c")
print("Your coordinates are in the wrong format.")
continue
#convert into x,y
x,y = convert_coord(coord)
x = int(x)
y = int(y)
#second: check within bounds
if (x in bounds and y in bounds):
valid_bound = True
else:
#clear_output()
print("\033c")
print("Coordinates need to be in the range 0-2")
continue
#check if position is non-empty
if game_arr[x][y] == " ":
valid_pos = True
else:
#clear_output()
print("\033c")
print("That position is already taken")
continue
#inserting onto game and showing users
game_arr[x][y] = user_dict[str(user)]
return (x,y)
#check if player wants to replay
def replay():
print("Do you want to play again?")
return input("Yes or No ").lower() == "yes"
#"Main function"
if __name__ == "__main__":
play = True
user_dict = {"0": "X", "1":"O"}
while play == True:
game_arr = [[" ", " ", " "],[" ", " ", " "],[" ", " ", " "]]
#can be initialised as game_arr = [[" "]*3]*3
user = 0
win = False
tie = 0
while win == False and tie < 9:
print(f"Player {user + 1}")
user_pos = user_input(game_arr,user)
win = check_win(user_pos, game_arr)
user = alternate(user)
tie += 1
#clear_output()
print("\033c")
print_board(game_arr)
if win == True:
print (f"Player {alternate(user) + 1} wins!")
elif tie == 9:
print("Tie! No one wins")
play = replay()
#clear_output()
print("\033c")
#player has exited
print("Thank you for playing!")