Python Pbl2
Python Pbl2
return False
def check_draw(board):
return all(cell != " " for row in board for cell in row)
def tic_tac_toe():
board = [[" " for _ in range(3)] for _ in range(3)]
player = "X"
while True:
print_board(board)
while True:
try:
row = int(input("Enter row (0, 1, 2): "))
col = int(input("Enter column (0, 1, 2): "))
if board[row][col] == " ":
board[row][col] = player
break
else:
print("That space is already taken. Try again.")
except (ValueError, IndexError):
except (ValueError, IndexError):
print("Invalid input. Try again.")
if check_win(board, player):
print_board(board)
print(f"Player {player} wins!")
break
elif check_draw(board):
CODING print_board(board)
print("It's a draw!")
break
if __name__ == "__main__":
tic_tac_toe()
ABOUT THE CODE
1.print_board(board) Function:
This function is used to display the Tic Tac Toe game board in the console.
It takes the board as input, which is a 2D list representing the current state of
the game board.
It iterates through the rows and prints each row with "|" separators and
horizontal lines to create the board's visual representation.
2.check_win(board, player) Function:
This function checks if a player (X or O) has won the game.
It takes the board and the current player as input.
It checks for wins by examining the rows, columns, and diagonals on the
board.
If any row, column, or diagonal contains the same symbol (player), the
function returns True, indicating a win. Otherwise, it returns False.
3. check_draw(board) Function:
This function checks if the game has ended in a draw (a tie).
It takes the board as input.
It iterates through all the cells on the board and checks if there are no empty
spaces left (no " " characters).
If all cells are filled, the function returns True, indicating a draw. Otherwise, it
returns False.
4. tic_tac_toe() Function:
This is the main function that orchestrates the Tic Tac Toe game.
It initializes the game by creating an empty 3x3 board and setting the starting player to "X".
It enters a loop that continues until the game is over (either a player wins or it's a draw).
Inside the loop, it displays the current state of the board and prompts the current player to make a
move.
It handles user input, ensuring that the selected cell is empty, and it updates the board accordingly.
After each move, it checks for a win or a draw using the check_win and check_draw functions.
If there's a winner, it displays the final board and announces the winning player.
If it's a draw, it displays the final board and announces a draw.
Finally, it switches the player for the next turn (from "X" to "O" or vice versa) and continues the game
loop.
5. if __name__ == "__main__": Block:
This block ensures that the tic_tac_toe function is only executed when the script is run as the
main program.
It's a common Python idiom to prevent code from running when the script is imported as a
module.
The code allows two players to take turns playing Tic Tac Toe in the console. It checks for wins
and draws and displays the final result. Players can enter their moves by specifying the row and
column they want to place their symbol in. The game continues until there's a winner or it ends
in a draw.
THANK
YOU