forked from jmportilla/Complete-Python-Bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe
More file actions
94 lines (70 loc) · 2.64 KB
/
Copy pathtictactoe
File metadata and controls
94 lines (70 loc) · 2.64 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
import random
from IPython.display import clear_output
def print_board(board):
clear_output()
print(board[6] + '|'+ board[7] +'|'+ board[8])
print('_'+' '+ '_' +' '+ '_')
print(board[3] + '|'+ board[4] +'|'+ board[5])
print('_'+' '+ '_' +' '+ '_')
print(board[0] + '|'+ board[1] +'|'+ board[2])
def playerinput(player):
marker = raw_input(player + ' ' + "Enter X or O:")
if marker == 'X':
return ('X', 'O')
elif marker == 'O':
return ('O', 'X')
def marker_position(player, marker):
position = raw_input(player + ' ' + "Enter marker position:")
return (player, marker, int(position))
def insert_in_board(player, marker, position, board):
board[position] = marker.upper()
def win_loose(player, marker, board):
if ((board[0] == marker and board[1] == marker and board[2] == marker)
or (board[3] == marker and board[4] == marker and board[5] == marker)
or (board[6] == marker and board[7] == marker and board[8] == marker)
or (board[0] == marker and board[4] == marker and board[8] == marker)
or (board[3] == marker and board[5] == marker and board[6] == marker)
or (board[0] == marker and board[3] == marker and board[6] == marker)
or (board[1] == marker and board[4] == marker and board[7] == marker)
or (board[2] == marker and board[5] == marker and board[8] == marker)):
return True
def board_full(board):
count = 1
for i in range(8):
if board[i] in ('X', 'O'):
count = count + 1
if count == len(board):
return True
def replay():
choice = raw_input("Do you want to continue to play? yes or no:")
return choice
def choose_first():
flip = random.randint(0,1)
if flip == 0:
return 'player1'
else:
return 'player2'
#########################
board = [' '] * 9
marker = ''
player = choose_first()
player1_marker, player2_marker = playerinput(player)
while True:
if player == 'player1':
player, marker, pos = marker_position(player, player1_marker)
elif player == 'player2':
player, marker, pos = marker_position(player, player2_marker)
insert_in_board(player, marker, pos, board)
print_board(board)
if board_full(board):
print ("game over, it's a tie!")
replay = replay()
if win_loose(player, marker, board):
print (player + ' ' + 'has won the game')
replay = replay()
if player == 'player1':
player = 'player2'
elif player == 'player2':
player = 'player1'
if replay == 'no':
break;