Notification texts go here Contact Us Click here

Tic-Tac-Toe game in Python

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated
  1. Tic-Tac-Toe game in Python:
  2.  
  3. def print_board(board):
  4. for row in board:
  5. print(" | ".join(row))
  6. print("-" * 9)
  7.  
  8. def check_winner(board, player):
  9. for row in board:
  10. if all([cell == player for cell in row]):
  11. return True
  12.  
  13. for col in range(3):
  14. if all([board[row][col] == player for row in range(3)]):
  15. return True
  16.  
  17. if all([board[i][i] == player for i in range(3)]) or all([board[i][2 - i] == player for i in range(3)]):
  18. return True
  19.  
  20. return False
  21.  
  22. def is_board_full(board):
  23. return all([cell != " " for row in board for cell in row])
  24.  
  25. def main():
  26. board = [[" " for _ in range(3)] for _ in range(3)]
  27. players = ["X", "O"]
  28. current_player = 0
  29.  
  30. while True:
  31. print_board(board)
  32.  
  33. player_symbol = players[current_player]
  34. print(f"Player {player_symbol}'s turn.")
  35. row = int(input("Enter the row (0, 1, or 2): "))
  36. col = int(input("Enter the column (0, 1, or 2): "))
  37.  
  38. if board[row][col] == " ":
  39. board[row][col] = player_symbol
  40. if check_winner(board, player_symbol):
  41. print_board(board)
  42. print(f"Player {player_symbol} wins!")
  43. break
  44.  
  45. if is_board_full(board):
  46. print_board(board)
  47. print("It's a tie!")
  48. break
  49.  
  50. current_player = 1 - current_player
  51. else:
  52. print("That cell is already taken. Try again.")
  53.  
  54. if __name__ == "__main__":
  55. main()
  56.  
  57. OUTPUT :
  58. | |
  59. ---------
  60. | |
  61. ---------
  62. | |
  63. ---------
  64. Player X's turn.
  65. Enter the row (0, 1, or 2): 1
  66. Enter the column (0, 1, or 2): 0
  67. | |
  68. ---------
  69. X | |
  70. ---------
  71. | |
  72. ---------
  73. Player O's turn.
  74. Enter the row (0, 1, or 2): 0
  75. Enter the column (0, 1, or 2): 0
  76. O | |
  77. ---------
  78. X | |
  79. ---------
  80. | |
  81. ---------
  82. Player X's turn.
  83. Enter the row (0, 1, or 2): 1
  84. Enter the column (0, 1, or 2): 1
  85. O | |
  86. ---------
  87. X | X |
  88. ---------
  89. | |
  90. ---------
  91. Player O's turn.
  92. Enter the row (0, 1, or 2): 0
  93. Enter the column (0, 1, or 2): 1
  94. O | O |
  95. ---------
  96. X | X |
  97. ---------
  98. | |
  99. ---------
  100. Player X's turn.
  101. Enter the row (0, 1, or 2): 1
  102. Enter the column (0, 1, or 2): 2
  103. O | O |
  104. ---------
  105. X | X | X
  106. ---------
  107. | |
  108. ---------
  109. Player X wins!
  110. >
  111.  

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.