-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtictactoe.py
144 lines (117 loc) · 4.15 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
#Manrique Iriarte
#April 23, 2018
#Python Script to run tic-tac-toe game
import random
#displays board
def Display(board):
print('\n'*100)
print(board[7] + ' | ' + board[8] + ' | ' + board[9])
print("- | - | -")
print(board[4] + ' | ' + board[5] + ' | ' + board[6])
print("- | - | -")
print(board[1] + ' | ' + board[2] + ' | ' + board[3])
#gets users input for which mark they will be
def PlayerInput():
mark = ''
while mark != 'X' and mark != 'O':
mark = input('Player 1, choose "X" or "O"').upper()
player1 = mark
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
return(player1, player2)
#Places the users mark on the board at the given position
def PlaceMark(board, marker, position):
if position > 9 or (marker != 'X' and marker != 'x' and marker != 'O' and marker != 'o'):
pass
else:
board[position] = marker.upper()
#Returns true if there was a winner and false if there was not
def Winner(board, mark):
mark = mark.upper()
threeinarow = [mark, mark, mark]
return (board[1:4] == threeinarow or board[4:7] == threeinarow or
board[7:10] == threeinarow or board[1::4] == threeinarow or
board[3:8:2] == threeinarow or board[3::3] == threeinarow or
board[1:8:3] == threeinarow or board[2:9:3] == threeinarow)
#randomly determines who plays first
def ChooseTurn():
if random.randint(1,2) == 1:
return 'Player 1'
else:
return 'Player 2'
#Returns true if that given position is free and false if it is not
def Space(board, position):
return board[position] == ' '
#Returns true if the board is full and false if it is not
def IsFull(board):
for i in range(1,10):
if Space(board, i):
return False
return True
#Gets the users input for what space they want to put there mark.
#Also shows a second board with available spaces the user can choose from
def Choice(board, availablePos):
position = 0
while position not in range(1,10) or not Space(board, position):
#display available positions
print(f'{availablePos[7]} | {availablePos[8]} | {availablePos[9]}')
print("- | - | -")
print(f'{availablePos[4]} | {availablePos[5]} | {availablePos[6]}')
print("- | - | -")
print(f'{availablePos[1]} | {availablePos[2]} | {availablePos[3]}')
position = int(input("Enter number to place your mark: "))
availablePos[position] = ' '
return position
#Returns true if the user wants to replay the game and false if they do not
def Replay():
choice = ''
while choice != 'yes' and choice != 'Yes' and choice != 'no' and choice != 'No':
choice = input('Play Again?')
if choice == 'yes' or choice == 'Yes':
return True
else:
return False
while True:
#initialize my two boards
available = list(range(0,10))
board = [' '] * 10
p1, p2 = PlayerInput()
turn = ChooseTurn()
play = True
while play:
if turn == 'Player 1':
Display(board)
print(turn + "'s turn")
PlaceMark(board, p1, Choice(board, available))
if Winner(board, p1):
Display(board)
print('Congratulations ' + turn + ', you win! ')
play = False
else:
if IsFull(board):
Display(board)
print('The game is a draw!')
break
else:
turn = 'Player 2'
else:
Display(board)
print(turn + "'s turn")
position = Choice(board, available)
PlaceMark(board, p2, position)
if Winner(board, p2):
Display(board)
print('Congratulations ' + turn + ', you win! ')
play = False
else:
if IsFull(board):
Display(board)
print('The game is a draw!')
break
else:
turn = 'Player 1'
playAgain = Replay()
if not playAgain:
break