Facebook
From Hrishikesh Saha, 1 Year ago, written in Python.
Embed
Download Paste or View Raw
Hits: 97
  1. import time
  2.  
  3. board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
  4.  
  5.  
  6. print("****************Tic Tac Toe********************")
  7. print()
  8. player1 = input("Enter Player1's name:")
  9. player2 = input("Enter PLayer2's name:")
  10. player = 1
  11. Running = 0
  12. Win = 1
  13. Draw = -1
  14. Game = Running
  15. Mark = 'X'
  16.  
  17.  
  18. def CheckPosition(position):
  19.     if (board[position] == " "):
  20.         return True
  21.     else:
  22.         return False
  23.  
  24.  
  25. def CheckWin():
  26.     global Game
  27.     # Horizontal winning condition
  28.     if (board[1] == board[2] and board[2] == board[3] and board[1] != ' '):
  29.         Game = Win
  30.     elif (board[4] == board[5] and board[5] == board[6] and board[4] != ' '):
  31.         Game = Win
  32.     elif (board[7] == board[8] and board[8] == board[9] and board[7] != ' '):
  33.         Game = Win
  34.         # Vertical Winning Condition
  35.     elif (board[1] == board[4] and board[4] == board[7] and board[1] != ' '):
  36.         Game = Win
  37.     elif (board[2] == board[5] and board[5] == board[8] and board[2] != ' '):
  38.         Game = Win
  39.     elif (board[3] == board[6] and board[6] == board[9] and board[3] != ' '):
  40.         Game = Win
  41.         # Diagonal Winning Condition
  42.     elif (board[1] == board[5] and board[5] == board[9] and board[5] != ' '):
  43.         Game = Win
  44.     elif (board[3] == board[5] and board[5] == board[7] and board[5] != ' '):
  45.         Game = Win
  46.         # Match Tie or Draw Condition
  47.     elif (board[1] != ' ' and board[2] != ' ' and board[3] != ' ' and board[4] != ' ' and board[5] != ' ' and board[
  48.         6] != ' ' and board[7] != ' ' and board[8] != ' ' and board[9] != ' '):
  49.         Game = Draw
  50.     else:
  51.         Game = Running
  52.  
  53.  
  54. def DrawBoard():
  55.     print(" %c | %c | %c " % (board[1], board[2], board[3]))
  56.     print("___|___|___")
  57.     print(" %c | %c | %c " % (board[4], board[5], board[6]))
  58.     print("___|___|___")
  59.     print(" %c | %c | %c " % (board[7], board[8], board[9]))
  60.     print("   |   |   ")
  61.  
  62.  
  63. print()
  64. print(f"{player1} [X]            {player2} [0]")
  65. print()
  66. print()
  67. print("Please wait.....")
  68. time.sleep(3)
  69.  
  70.  
  71. while (Game == Running):
  72.     DrawBoard()
  73.     if (player % 2 != 0):
  74.         print(f"{player1}'s chance")
  75.         Mark = 'X'
  76.     else:
  77.         print(f"{player2}'s chance")
  78.         Mark = '0'
  79.     choice = int(input("Enter the position between [1-9] where you want to mark : "))
  80.     if (CheckPosition(choice) == True):
  81.         board[choice] = Mark
  82.         player += 1
  83.         CheckWin()
  84.  
  85.  
  86. DrawBoard()
  87. print()
  88. if (Game == Draw):
  89.     print("Game Draw")
  90. elif (Game == Win):
  91.     player = 1
  92.     if (player % 2 != 0):
  93.         print(f"{player1} won!")
  94.     else:
  95.         print(f"{player2} won!")
  96.