-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
142 lines (111 loc) · 4.39 KB
/
run.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
#!/usr/bin/env python3.9
from player import Player
user=Player()
input_list=[" ", " ", " ", " ", " ", " ", " ", " ", " "]
def show_grid():
"""The method to draw the board"""
print("\n")
print(f' {input_list[0]} | {input_list[1]} | {input_list[2]} ')
print("-"*5+"|"+"-"*5+"|"+"-"*5)
print(f' {input_list[3]} | {input_list[4]} | {input_list[5]} ')
print("-"*5+"|"+"-"*5+"|"+"-"*5)
print(f' {input_list[6]} | {input_list[7]} | {input_list[8]} ')
def win_state():
"""Defines all possible winning alignment of the columns and rows on the board"""
if input_list[0]==input_list[1] and input_list[1]==input_list[2]:
if input_list[0] != " ":
return True
elif input_list[3]==input_list[4] and input_list[4]==input_list[5]:
if input_list[3] != " ":
return True
elif input_list[6]==input_list[7] and input_list[7]==input_list[8]:
if input_list[6] != " ":
return True
elif input_list[0]==input_list[3] and input_list[3]==input_list[6]:
if input_list[0] != " ":
return True
elif input_list[1]==input_list[4] and input_list[4]==input_list[7]:
if input_list[1] != " ":
return True
elif input_list[2]==input_list[5] and input_list[5]==input_list[8]:
if input_list[2] != " ":
return True
elif input_list[0]==input_list[4] and input_list[4]==input_list[8]:
if input_list[0] != " ":
return True
elif input_list[2]==input_list[4] and input_list[4]==input_list[6]:
if input_list[2] != " ":
return True
else:
return False
def main():
print("Welcome to the game.")
player1 = input("Player 1 enter your name.")
player2 = input("Player 2 enter your name.")
which_player=True
is_stalemate= True
print(" TIC-TAC_TOE")
print("This is how the board looks like\n")
print(" 1 | 2 | 3 ")
print("-"*5+"|"+"-"*5+"|"+"-"*5)
print(" 4 | 5 | 6 ")
print("-"*5+"|"+"-"*5+"|"+"-"*5)
print(" 7 | 8 | 9 ")
print("\n")
print(" START")
while len(user.moves_available)>0:
value_error=False
if which_player==True:
print(f"\n{player1}:")
print("Available moves:")
user.show_av_moves()
show_grid() #prints the board on the terminal
print("Player 1, make a move")
try:
slot=int(input()) #Accepts only input of type integer
except ValueError:
value_error=True
print("Kindly use numbers 1-9!!") #What is to be displayed once the input iss wrong
if value_error==False:
if user.validate_move(slot):
input_list[slot-1]="X"
user.move_made(slot)
which_player=False
if win_state():
show_grid()
print(f"\n----- {player1} WINS! -----\n END")
is_stalemate=False
break
else:
print("******Invalid move, try again******")
which_player=True
else:
print(f"\n{player2}:")
print("Available moves:")
user.show_av_moves()
show_grid()
print("Player 2 make a move.")
try:
slot=int(input())
except ValueError:
value_error=True
print("Kindly use numbers 1-9!!")
if value_error==False:
if user.validate_move(slot):
input_list[slot-1]="O"
user.move_made(slot)
which_player=True
if win_state():
show_grid()
print(f"\n-----{player2} WINS! -----")
print(" END")
is_stalemate=False
break
else:
print("Invalid Move! Repeat.")
which_player=False
if is_stalemate:
show_grid()
print("\nThe game has ended in a draw\n END")
if __name__ == "__main__":
main ()