-
Notifications
You must be signed in to change notification settings - Fork 429
Expand file tree
/
Copy pathTicTacToe.py
More file actions
127 lines (107 loc) · 3.66 KB
/
TicTacToe.py
File metadata and controls
127 lines (107 loc) · 3.66 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
import random
# Initialize the board
board = [' ' for i in range(10)]
def insertLetter(letter, pos):
board[pos] = letter
def spaceIsfree(pos):
return board[pos] == ' '
def printBoard(board):
print(" | | ")
print(" " + board[1] + " | " + board[2] + " | " + board[3])
print(" | | ")
print("-----------")
print(" | | ")
print(" " + board[4] + " | " + board[5] + " | " + board[6])
print(" | | ")
print("-----------")
print(" | | ")
print(" " + board[7] + " | " + board[8] + " | " + board[9])
print(" | | ")
def isBoardFull(board):
if board.count(" ") > 1:
return False
else:
return True
def isWinner(b, l): # b = board, l = letter
# Check all winning combinations
return ((b[1] == l and b[2] == l and b[3] == l) or
(b[4] == l and b[5] == l and b[6] == l) or
(b[7] == l and b[8] == l and b[9] == l) or
(b[1] == l and b[4] == l and b[7] == l) or
(b[2] == l and b[5] == l and b[8] == l) or
(b[3] == l and b[6] == l and b[9] == l) or
(b[1] == l and b[5] == l and b[9] == l) or
(b[3] == l and b[5] == l and b[7] == l))
def userMove():
run = True
while run:
pos = input("Enter a position between 1 to 9: ")
try:
pos = int(pos)
if 1 <= pos <= 9:
if spaceIsfree(pos):
run = False
insertLetter("X", pos)
else:
print("Sorry, this space is occupied.")
else:
print("Please enter a number between 1 and 9.")
except:
print("Please enter a valid number.")
def compMove():
possibleMoves = [x for x, letter in enumerate(board) if letter == " " and x != 0]
# Try to win or block the player
for let in ['O', 'X']:
for i in possibleMoves:
boardCopy = board[:]
boardCopy[i] = let
if isWinner(boardCopy, let):
return i
# Choose corner
cornersOpen = [i for i in possibleMoves if i in [1, 3, 7, 9]]
if cornersOpen:
return selectRandom(cornersOpen)
# Choose center
if 5 in possibleMoves:
return 5
# Choose edge
edgesOpen = [i for i in possibleMoves if i in [2, 4, 6, 8]]
if edgesOpen:
return selectRandom(edgesOpen)
return None # <-- ✅ Added: Return None if no moves available (board is full)
def selectRandom(list_):
return random.choice(list_)
def main():
print("Welcome to the Tic Tac Toe game!\n")
printBoard(board)
while not isBoardFull(board):
if not isWinner(board, "O"):
userMove()
printBoard(board)
else:
print("Sorry, you lose!")
break
if not isWinner(board, "X"):
move = compMove()
if move is None: # <-- ✅ Fixed: Properly detect tie when no move is possible
print("Tie game!")
break # <-- ✅ Added: Prevent crash and end the game
else:
insertLetter("O", move)
print(f"Computer placed 'O' on position {move}")
printBoard(board)
else:
print("You win!")
break
if isBoardFull(board): # <-- Optional: Final check to declare tie
if not isWinner(board, "X") and not isWinner(board, "O"):
print("\nGame tie!")
# Game loop
while True:
choice = input("Do you want to play a game (Y/N): ")
if choice.lower() == 'y':
board = [' ' for i in range(10)]
print("-----------------------------------------")
main()
else:
break