-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.py
executable file
·153 lines (140 loc) · 5.1 KB
/
tictactoe.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
x_player = False
o_player = False
game_board = ['#','1','2','3','4','5','6','7','8','9']
exit = False
def display(board):
board_length = len(board)
board_top = ' | | '
board_middle = '___|___|___'
#board_top = [' ', ' ', ' ', '|',' ', ' ', ' ', '|',' ', ' ', ' ', '|' ]
print(board_top)
print(f' {board[board_length - 3]} | {board[board_length - 2]} | {board[board_length- 1]} ')
print(board_middle)
print(board_top)
print(f' {board[board_length - 6]} | {board[board_length - 5]} | {board[board_length- 4]} ')
print(board_middle)
print(board_top)
print(f' {board[board_length - 9]} | {board[board_length - 8]} | {board[board_length- 7]} ')
print(board_top)
def player_input(player1, player2):
player_choice = 'WRONG'
while player_choice not in ['X', 'O', 'Q']:
player_choice = input('\nWho would you like to go first X or O? ')
player_choice = player_choice.upper()
if player_choice not in ['X', 'O']:
print('\nInvalid choice select either X or O\n')
elif player_choice == 'X':
player1 = True
player2 = False
print('You have chosen for X to go first\n')
return player1, player2
elif player_choice == 'O':
player1 = False
player2 = True
print('You have chosen for 0 to go first\n')
return player1, player2
def select_square():
global x_player
global o_player
global game_board
global exit
choice = 'WRONG'
refresh_flag = False
while choice not in range(0,10) or exit == False or refresh_flag == False:
if x_player == True:
choice = input('Player X select what square you would like to place (1-9)\nYou may also enter Q to quit or R to refresh the board: ')
#choice = int(choice)
if choice not in game_board and choice not in ['Q', 'q',]:
print('\nYour selection is invalid')
elif choice in ['Q', 'q'] :
exit = True
break
else:
return int(choice)
if o_player == True:
choice = input('Player O select what square you would like to place (1-9)\nYou may also enter Q to quit or R to refresh the board: ')
#choice = int(choice)
if choice in ['Q', 'q']:
exit = True
break
elif choice in ['R', 'r']:
refresh_board()
refresh_flag = True
break
elif choice not in game_board:
print('\nYour selection is invalid')
else:
return int(choice)
def place_square(location):
global game_board
global x_player
global o_player
player = 'WRONG'
#print(player)
while player not in ['X', 'O']:
if x_player == True:
player = 'X'
if str(location) in game_board:
game_board[location] = player
x_player = False
o_player = True
elif o_player == True:
player = 'O'
if str(location) in game_board:
game_board[location] = player
x_player = True
o_player = False
else:
pass
def refresh_board():
global x_player
global o_player
global game_board
x_player = False
o_player = False
game_board = ['#','1','2','3','4','5','6','7','8','9']
print('\nBoard has been refreshed!')
x_player,o_player = player_input(x_player, o_player)
def winner_detection(current_board):
x_win = ['X', 'X', 'X']
o_win = ['O', 'O', 'O']
diag1 = [1, 5, 9]
diag2 = [7, 5, 3]
x_diag1 = [current_board[i] for i in diag1]
x_diag2 = [current_board[i] for i in diag2]
for i in range(len(current_board) - len(x_win) + 1):
if i == 2 or i == 3 or i == 6:
continue
elif current_board[i:i+len(x_win)] == x_win:
print('\nX has won!')
refresh_board()
break
elif x_diag1 == x_win or x_diag2 == x_win:
print('\nX has won!')
refresh_board()
break
for i in range(len(current_board) - len(o_win) + 1):
if i == 2 or i == 3 or i == 6:
continue
elif current_board[i:i+len(o_win)] == o_win:
print('\nO has won!')
refresh_board()
break
elif x_diag1 == o_win or x_diag2 == o_win:
print('\nO has won!')
refresh_board()
break
def main():
global x_player
global o_player
global game_board
global exit
print('Welcome to tic tac toe!\nFirst let select what player you would like to go first\n')
x_player,o_player = player_input(x_player, o_player)
while not exit:
display(game_board)
player_choice = select_square()
# print(player_choice)
place_square(player_choice)
winner_detection(game_board)
main()