-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.py
More file actions
162 lines (125 loc) · 3.59 KB
/
TicTacToe.py
File metadata and controls
162 lines (125 loc) · 3.59 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
from IPython.display import clear_output
#BOARD
def display(row1):
clear_output()
print(row1[0],"|", row1[1],"|", row1[2])
print(row1[3],"|", row1[4],"|", row1[5])
print(row1[6],"|", row1[7],"|", row1[8])
#POSITION SELECTION
def user_choice():
# VARIABLES
# Initial
choice = ('initial_value')
acceptable_range = range(1,11)
within_range = False
#TWO CONDIITONS TO CHECK
# DIGIT OR WITHIN_RANGE == False
while choice.isdigit() == False or within_range == False:
choice = input("Please enter a number (1-9): (in relation to the position you want on the board)")
#Digit Check
if choice.isdigit() == False:
print('Sorry that is not a digit')
#Range Check
if choice.isdigit() == True:
if int(choice) in acceptable_range:
within_range = True
else:
within_range = False
print('Sorry that is not within the acceptable range')
return int(choice)-1
#CHOICE SELECTED
def replacement_choice(game_list, position, user_marker):
while game_list[position] == 'X' or game_list[position] == 'O':
print('This slot is already taken')
position = user_choice()
game_list[position] = user_marker
return game_list
def player_input():
marker = ''
#KEEP ASKING PLAYER 1 to choose X or 0
while marker != 'X' and marker != 'O':
marker = input('Player 1, choose X or O: ').upper()
player1 = marker
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
return (player1, player2)
#Continue playing
def gameon_choice():
choice = 'wrong'
while choice not in ['Y', 'N']:
choice = input("Keep playing? (Y or N)").upper()
if choice not in ['Y', 'N']:
print("Sorry, I don't understand, please choose Y or N")
if choice == 'Y':
return True
else:
return False
#WINNER
def gamewinner_result(row):
if row[0] == row[1] and row[2] == row[1]:
return True
elif row[3] == row[4] and row[5] == row[4]:
return True
elif row[6] == row[7] and row[8] == row[7]:
return True
elif row[0] == row[3] and row[6] == row[3]:
return True
elif row[1] == row[4] and row[7] == row[4]:
return True
elif row[2] == row[5] and row[8] == row[5]:
return True
elif row[0] == row[4] and row[8] == row[4]:
return True
elif row[2] == row[4] and row[6] == row[4]:
return True
else:
return False
#TIE GAME
def tie_game(board):
acceptable_values = ['X', 'O']
count = 0
for i in board:
if i in acceptable_values:
count += 1
if count == len(board):
return True
else:
return False
#PLAY
def game(game_on, game_winner, board, player1_marker, player2_marker):
winner = 'Player'
while game_winner == False:
#check for TIE game
if tie_game(board) == True:
print('The game result is a tie!')
break
display(board)
print('Player 1:')
position = user_choice()
board = replacement_choice(board, position, player1_marker)
display(board)
game_winner = gamewinner_result(board)
if game_winner == True:
winner = 'Player 1'
break
#check for TIE game
if tie_game(board) == True:
print('The game result is a tie!')
return
print('Player 2:')
position = user_choice()
board = replacement_choice(board, position, player2_marker)
game_winner = gamewinner_result(board)
winner = 'Player 2'
print(winner, 'you win!')
#VARIABLES
game_on = True
game_winner = False
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
while game_on:
player1_marker, player2_marker = player_input()
game(game_on, game_winner, board, player1_marker, player2_marker)
game_on = gameon_choice()
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]