diff --git a/Tic-Tac-Toe/TicTacToe.py b/Tic-Tac-Toe/TicTacToe.py index abb03fb6..4660346b 100644 --- a/Tic-Tac-Toe/TicTacToe.py +++ b/Tic-Tac-Toe/TicTacToe.py @@ -1,19 +1,21 @@ +import random + +# Initialize the board board = [' ' for i in range(10)] -def insertLetter(letter,pos): +def insertLetter(letter, pos): board[pos] = letter - def spaceIsfree(pos): - return board[pos] == ' ' + return board[pos] == ' ' def printBoard(board): print(" | | ") - print(" " +board[1] + " | "+ board[2]+ " | " + board[3]) + print(" " + board[1] + " | " + board[2] + " | " + board[3]) print(" | | ") print("-----------") print(" | | ") - print(" " +board[4] + " | "+ board[5]+ " | " + board[6]) + print(" " + board[4] + " | " + board[5] + " | " + board[6]) print(" | | ") print("-----------") print(" | | ") @@ -26,112 +28,99 @@ def isBoardFull(board): else: return True -def isWinner(b,l): # b = board, l = letter - # check all possibilities - 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 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 (pos > 0) and (pos < 10): + if 1 <= pos <= 9: if spaceIsfree(pos): run = False - insertLetter("X" , pos) + insertLetter("X", pos) else: - print("Sorry this space is occupied") - + print("Sorry, this space is occupied.") else: - print("Please enter a number range between 1 to 9") - + print("Please enter a number between 1 and 9.") except: - print("Please enter a number ") + print("Please enter a valid number.") def compMove(): - possibleMoves = [x for x,letter in enumerate(board) if letter == " " and x != 0] - move = 0 + possibleMoves = [x for x, letter in enumerate(board) if letter == " " and x != 0] - for let in ['O','X']: + # 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 - if isWinner(boardCopy,let): - move = i - return move - - cornorOpen = [] - for i in possibleMoves: - if i in [1,3,7,9]: - cornorOpen.append(i) - - if len(cornorOpen) > 0: - move = selectRandom(cornorOpen) - return move + # 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: - move = 5 - return move + return 5 - edgeOpen = [] - for i in possibleMoves: - if i in [2,4,6,8]: - edgeOpen.append(i) + # Choose edge + edgesOpen = [i for i in possibleMoves if i in [2, 4, 6, 8]] + if edgesOpen: + return selectRandom(edgesOpen) - - if len(edgeOpen) > 0: - move = selectRandom(edgeOpen) - return move + return None # <-- ✅ Added: Return None if no moves available (board is full) def selectRandom(list_): - import random - ln = len(list_) - r = random.randrange(0,ln) - - return list_[r] - + return random.choice(list_) def main(): - print("Welcome to the tic tac toe game\n") + print("Welcome to the Tic Tac Toe game!\n") printBoard(board) - while not(isBoardFull(board)): - if not(isWinner(board, "O")): + while not isBoardFull(board): + if not isWinner(board, "O"): userMove() printBoard(board) - else: - print("Sorry you loose! ") + print("Sorry, you lose!") break - - if not(isWinner(board, "X")): + if not isWinner(board, "X"): move = compMove() - if move == 0: - print("Tie game") - + 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 place O on position {move}") + print(f"Computer placed 'O' on position {move}") printBoard(board) - else: - print("You win! ") + print("You win!") break - if isBoardFull(board): - print("\nGame tie") - + 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)] + board = [' ' for i in range(10)] print("-----------------------------------------") main() else: