-
Notifications
You must be signed in to change notification settings - Fork 35
/
Tic Tac Toe.py
176 lines (107 loc) · 3.52 KB
/
Tic Tac Toe.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
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
import os
def display_board(board):
print(" "+board[0]+" | "+board[1]+" | "+board[2])
print("-----------")
print(" "+board[3]+" | "+board[4]+" | "+board[5])
print("-----------")
print(" "+board[6]+" | "+board[7]+" | "+board[8])
def choose_marker():
player1 = ' '
player2 = ' '
while player1 not in ['X','O']:
player1 = input("Please choose marker - X or O: ")
if player1 == 'X':
player2 ='O'
else:
player2 = 'X'
return (player1,player2)
def position(board, player):
valid_pos = False
while valid_pos != True:
pos = int(input("Please enter position for {}: ".format(player)))
if board[pos-1] != " ":
print("Invalid position. Please try again!")
else:
valid_pos = True
return pos
def game_turn(mark, position, board, chance):
board[position-1] = mark
os.system('cls')
display_board(board)
chance += 1
win = game_win(board)
return chance, win
def game_win(board):
#Horizontal line wins:
if board[0] == board[1] == board[2]:
return board[0]
elif board[3] == board[4] == board[5]:
return board[3]
elif board[6] == board[7] == board[8]:
return board[6]
#Vertical line wins:
if board[0] == board[3] == board[6]:
return board[0]
elif board[1] == board[4] == board[7]:
return board[1]
elif board[2] == board[5] == board[8]:
return board[2]
#Diagonal Line wins:
if board[0] == board[4] == board[8]:
return board[0]
elif board[2] == board[4] == board[6]:
return board[2]
else:
return 0
def play_again():
choice = ' '
while choice not in ['Y','N']:
choice = input("Play Again? Y or N: ")
if choice == 'Y':
return True
else:
return False
def game():
print("Welcome to TIC TAC TOE!\n")
#Default Board
print("Default index positions will be as following: \n")
def_board = ["1","2","3","4","5","6","7","8","9"]
display_board(def_board)
print("\n")
#Empty Board Set
board = [" "," "," "," "," "," "," "," "," "]
#Choose markers
(mark1, mark2) = choose_marker()
#Game Starts
chance = 1
while chance in range(1,10):
#Which player's turn?
if chance in [1,3,5,7,9]:
player = 'Player 1'
mark = mark1
else:
player = 'Player 2'
mark = mark2
#Get position
positions = position(board, player)
#Take a turn
chance, win = game_turn(mark,positions,board,chance)
#Winner?
if win == mark1:
print("\nCongratulations Player 1. You won!\n")
break
elif win == mark2:
print("\nCongratulations Player 2. You won!\n")
break
#Draw?
if win != mark1 and win != mark2:
print("The match is a draw!\n")
#Play Again?
if play_again():
from IPython.display import clear_output
clear_output()
game()
else:
print("Thanks for playing.")
exit(1)
game()