FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Python-Programs/TicTacToe.py at master · Scarlet-Coder/Python-Programs · GitHub
Scarlet-Coder
/
Python-Programs
Public
forked from
codehouseindia/Python-Programs
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Python-Programs
/
TicTacToe.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
169 lines (131 loc) · 3.87 KB
Breadcrumbs
Python-Programs
/
TicTacToe.py
Copy path
File metadata and controls
169 lines (131 loc) · 3.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# ____--Global Variable--____
game_still_going
=
True
current_player
=
"X"
winner
=
None
# Defining a Board
board
=
[
"-"
,
"-"
,
"-"
,
"-"
,
"-"
,
"-"
,
"-"
,
"-"
,
"-"
]
# Display the initial board
def
display_board
():
print
(
board
[
0
]
+
" | "
+
board
[
1
]
+
" | "
+
board
[
2
])
print
(
board
[
3
]
+
" | "
+
board
[
4
]
+
" | "
+
board
[
5
])
print
(
board
[
6
]
+
" | "
+
board
[
7
]
+
" | "
+
board
[
8
])
# Handling a turn of a particular player
def
handle_turn
(
player
):
print
(
player
+
" turn..."
)
position
=
input
(
"Choose position from 1-9: "
)
valid
=
False
while
not
valid
:
while
position
not
in
[
"1"
,
"2"
,
"3"
,
"4"
,
"5"
,
"6"
,
"7"
,
"8"
,
"9"
]:
position
=
input
(
"Invalid Output... Choose position from 1-9: "
)
position
=
int
(
position
)
-
1
if
board
[
position
]
==
"-"
:
valid
=
True
else
:
print
(
"You can't go there"
)
board
[
position
]
=
player
display_board
()
# Check If Game over
def
check_if_game_over
():
# Check for win
check_for_winner
()
# Check for tie
check_for_tie
()
return
#Check for the winner
def
check_for_winner
():
global
winner
# check rows
row_winner
=
check_row
()
# check columns
column_winner
=
check_columns
()
# check diagonals
diagonal_winner
=
check_diagonals
()
# If there is a row winner modify winner to row_winner {value}
if
row_winner
:
winner
=
row_winner
# if there is a column winner modify the winner to column_winner{value}
elif
column_winner
:
winner
=
column_winner
# if there is a diagonal winner modify the winner to diagonal_winner{value}
elif
diagonal_winner
:
winner
=
diagonal_winner
else
:
winner
=
None
return
def
check_row
():
# Set up a global variables
global
game_still_going
row_1
=
board
[
0
]
==
board
[
1
]
==
board
[
2
]
!=
"-"
row_2
=
board
[
3
]
==
board
[
4
]
==
board
[
5
]
!=
"-"
row_3
=
board
[
6
]
==
board
[
7
]
==
board
[
8
]
!=
"-"
if
row_1
or
row_2
or
row_3
:
game_still_going
=
False
# Returning the value of 'X' or 'O'
if
row_1
:
return
board
[
0
]
elif
row_2
:
return
board
[
3
]
elif
row_3
:
return
board
[
6
]
return
def
check_columns
():
# Set up a global variables
global
game_still_going
column_1
=
board
[
0
]
==
board
[
3
]
==
board
[
6
]
!=
"-"
column_2
=
board
[
1
]
==
board
[
4
]
==
board
[
7
]
!=
"-"
column_3
=
board
[
2
]
==
board
[
5
]
==
board
[
8
]
!=
"-"
if
column_1
or
column_2
or
column_3
:
game_still_going
=
False
# Returning the value of 'X' or 'O'
if
column_1
:
return
board
[
0
]
elif
column_2
:
return
board
[
1
]
elif
column_3
:
return
board
[
2
]
return
def
check_diagonals
():
# Set up a global variables
global
game_still_going
diagonal_1
=
board
[
0
]
==
board
[
4
]
==
board
[
8
]
!=
"-"
diagonal_2
=
board
[
2
]
==
board
[
4
]
==
board
[
6
]
!=
"-"
if
diagonal_1
or
diagonal_2
:
game_still_going
=
False
# Returning the value of 'X' or 'O'
if
diagonal_1
:
return
board
[
0
]
elif
diagonal_2
:
return
board
[
6
]
return
# Check whether game tie
def
check_for_tie
():
global
game_still_going
if
"-"
not
in
board
:
game_still_going
=
False
return
# Flip the turn if 'O' TO 'X' elif 'X' to 'O'
def
flip_player
():
global
current_player
if
current_player
==
"X"
:
current_player
=
"O"
elif
current_player
==
"O"
:
current_player
=
"X"
return
# Main function to play the game
def
play_game
():
# Display initial board
display_board
()
while
game_still_going
:
# Handle turn of a single player
handle_turn
(
current_player
)
# Check if game is over
check_if_game_over
()
# Flip the player
flip_player
()
if
winner
==
"X"
or
winner
==
"O"
:
print
(
f"
{
winner
}
Won..."
)
elif
winner
==
None
:
print
(
"Tie..."
)
play_game
()
Back
|
FazBrowse Home
|
New Git URL