-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.py
More file actions
94 lines (83 loc) · 2.41 KB
/
Copy pathtictactoe.py
File metadata and controls
94 lines (83 loc) · 2.41 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
import random
# initialize game variables
board = ["-"] * 9
currentPlayer = "X"
gameRunning = True
# function to print game board
def printBoard(board):
fmtstr = '|'.join(board)
fmtstr = '\n'.join(fmtstr[i:i+6] for i in range(0, len(fmtstr), 6))
fmtstr = fmtstr[:5] + fmtstr[6:]
fmtstr = fmtstr[:11] + fmtstr[12:]
print(fmtstr)
# function for player to make a move
def playerMove(board):
while True:
ipn = int(input("Enter a number 1 - 9: ")) - 1
if board[ipn] == "-":
board[ipn] = currentPlayer
break
else:
print("That spot is already taken. Try again.")
def switchPlayer(board):
global currentPlayer
if currentPlayer == "X":
currentPlayer = "O"
else :
currentPlayer = "X"
# checkWinHorizontal function
def checkWinHorizontal(board):
if board[0] == board[1] == board[2] != "-":
print(f"{currentPlayer} Won !")
return True
elif board[3] == board[4] == board[5] != "-" :
print(f"{currentPlayer} Won !")
return True
elif board[6] == board[7] == board[8] != "-":
print(f"{currentPlayer} Won !")
return True
else:
return False
def checkWinRow(board):
if board[0] == board[4] == board[8] != "-":
print(f"{currentPlayer} Won !")
return True
elif board[1] == board[4] == board[7] != "-" :
print(f"{currentPlayer} Won !")
return True
elif board[2] == board[4] == board[6] != "-":
print(f"{currentPlayer} Won !")
return True
elif board[0] == board[3] == board[6] != "-":
print(f"{currentPlayer} Won !")
return True
elif board[1] == board[4] == board[7] != "-":
print(f"{currentPlayer} Won !")
return True
elif board[3] == board[6] == board[8] != "-":
print(f"{currentPlayer} Won !")
return True
else:
return False
def checkTie(board):
if "-" not in board:
print("Game is a tie!")
return True
else:
return False
# checkIfWin function
def checkIfWin(board):
global gameRunning
if checkWinHorizontal(board) or checkWinRow(board):
gameRunning = False
elif checkTie(board):
gameRunning = False
# main game loop
while gameRunning:
printBoard(board)
playerMove(board)
checkIfWin(board)
if not gameRunning:
break
checkTie(board)
switchPlayer(board)