MAKE TIC-TAC-TOE GAME BY PYTHON( "PROJECT" ):
![]() |
Tic Tac Toe Built In Python and Tkinter |
CODE DESCRIPTION:
The code defines three main functions: sum, printBoard, and checkWin.
- sum(a, b, c): This function calculates the sum of three values a, b, and c. In this code, it's used to check the sum of three board positions to determine if a player has won.
- printBoard(xState, yState): This function takes two lists, xState and yState, representing the current state of the Tic-Tac-Toe board for Player 1 (X) and Player 2 (O) respectively. It prints the current state of the board, with 'X' representing Player 1's moves, 'O' representing Player 2's moves, and numbers representing empty positions.
- checkWin(xState, yState): This function checks if either player has won the game. It defines the winning combinations on the Tic-Tac-Toe board and checks if any of these combinations have been filled by a player's moves.
In the if __name__ == "__main__": block, the game is initialized:
- xState and yState are initialized as lists of 9 zeros, representing an empty board.
- turn is initialized to 1, indicating that Player 1 (X) starts the game.
- A welcome message is printed on the console.
The main game loop runs indefinitely (while True:) until the game is over. Within the loop:
- The current state of the board is displayed using the printBoard function.
- The player whose turn it is (Player 1 or Player 2) is displayed.
- The player is prompted to enter a value (0-8) corresponding to their move.
- Input validation is performed to ensure the entered value is between 0 and 8 and that the selected position is not already occupied.
- The selected position is updated with 'X' for Player 1 or 'O' for Player 2 in their respective xState or yState lists.
- The check-in function is called to check if the game has been won or if it's a tie.
If a player wins, the game prints a message indicating the winner and ends.
After each move, the turn is switched between Player 1 and Player 2 using turn = 1 - turn, ensuring that the players take turns.
The game continues until there is a winner or a tie, and the program exits the loop and terminates.
DOWNLOAD THE SOURCE CODE CLICK HERE
SOURCE CODE (1):
def sum(a, b, c):
return a + b + c
def printBoard(xState, yState):
zero = 'X' if xState[0] else ('O' if yState[0] else 0)
one = 'X' if xState[1] else ('O' if yState[1] else 1)
two = 'X' if xState[2] else ('O' if yState[2] else 2)
three = 'X' if xState[3] else ('O' if yState[3] else 3)
four = 'X' if xState[4] else ('O' if yState[4] else 4)
five = 'X' if xState[5] else ('O' if yState[5] else 5)
six = 'X' if xState[6] else ('O' if yState[6] else 6)
seven = 'X' if xState[7] else ('O' if yState[7] else 7)
eight = 'X' if xState[8] else ('O' if yState[8] else 8)
print(f"{zero} | {one} | {two} ")
print(f"--|---|---")
print(f"{three} | {four} | {five} ")
print(f"--|---|---")
print(f"{six} | {seven} | {eight} ")
def checkWin(xState, yState):
wins = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8],
[0, 4, 8], [2, 4, 6]]
for win in wins:
if sum(xState[win[0]], xState[win[1]], xState[win[2]]) == 3:
print("PLAYER 1 WON THE MATCH")
return 1
if sum(yState[win[0]], yState[win[1]], yState[win[2]]) == 3:
print("PLAYER 2 WON THE MATCH")
return 0
return -1
if __name__ == "__main__":
xState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
yState = [0, 0, 0, 0, 0, 0, 0, 0, 0]
turn = 1 # 1 for X and 0 for O
print("WELCOME TO TIC TAC TOE GAME")
while True:
printBoard(xState, yState)
if turn == 1:
print("PLAYER 1 CHANCE")
else:
print("PLAYER 2 CHANCE")
while True:
try:
value = int(input("ENTER A VALUE (0-8): "))
if value < 0 or value > 8:
raise ValueError("ENTER A VALID NUMBER BETWEEN 0 TO 8")
if xState[value] == 1 or yState[value] == 1:
raise ValueError("THIS POSITION IS ALREADY OCCUPIED,ENTER
A ANOTHOR POSITION")
break
except ValueError as e:
print(e)
if turn == 1:
xState[value] = 1
else:
yState[value] = 1
checkwin = checkWin(xState, yState)
if checkwin != -1:
print("MATCH OVER")
break
turn = 1 - turn
DOWNLOAD THE SOURCE CODE CLICK HERE
BY ANOTHER LOGIC SOURCE CODE (2):
def printBoard(board):
for row in board:
print(" | ".join(row))
if row != board[-1]:
print("-" * 9)
def checkWin(board, player):
for row in board:
if all(cell == player for cell in row):
return True
for col in range(3):
if all(board[row][col] == player for row in range(3)):
return True
if all(board[i][i] == player for i in range(3)) or
all(board[i][2 - i] == player for i in range(3)):
return True
return False
if __name__ == "__main__":
board = [[" " for _ in range(3)] for _ in range(3)]
players = ["X", "O"]
turn = 0 # 0 for player X, 1 for player O
print("WELCOME TO TIC TAC TOE GAME")
while True:
printBoard(board)
player = players[turn]
print(f"PLAYER {player} CHANCE")
while True:
try:
row = int(input("Enter row (0, 1, or 2): "))
col = int(input("Enter column (0, 1, or 2): "))
if 0 <= row < 3 and 0 <= col < 3 and board[row][col] == " ":
board[row][col] = player
break
else:
print("Invalid move. Try again.")
except ValueError:
print("Invalid input. Enter row and column as integers.")
if checkWin(board, player):
printBoard(board)
print(f"PLAYER {player} WINS!")
break
if all(cell != " " for row in board for cell in row):
printBoard(board)
print("DRAW!")
break
turn = 1 - turn
DOWNLOAD THE SOURCE CODE CLICK HERE
BY ANOTHER LOGIC SOURCE CODE (3):
def printBoard(board):
for i in range(0, 9, 3):
print(f"{board[i]} | {board[i + 1]} | {board[i + 2]}")
if i < 6:
print("--|---|--")
def checkWin(board, player):
win_conditions = [(0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7),
(2, 5, 8),(0, 4, 8), (2, 4, 6)]
for condition in win_conditions:
if all(board[i] == player for i in condition):
return True
return False
if __name__ == "__main__":
board = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
players = ["X", "O"]
turn = 0 # 0 for player X, 1 for player O
print("WELCOME TO TIC TAC TOE GAME")
while True:
printBoard(board)
player = players[turn]
print(f"PLAYER {player} CHANCE")
while True:
try:
position = int(input("Enter a position (1-9): "))
if 1 <= position <= 9 and board[position - 1] != "X"
and board[position - 1] != "O":
board[position - 1] = player
break
else:
print("Invalid move. Try again.")
except ValueError:
print("Invalid input. Enter a position as an integer (1-9).")
if checkWin(board, player):
printBoard(board)
print(f"PLAYER {player} WINS!")
break
if all(cell == "X" or cell == "O" for cell in board):
printBoard(board)
print("It's a DRAW!")
break
turn = 1 - turn
1 Comments
I liked it would be more helpful if you have provided all the code files in a single folder
ReplyDelete