XenoMorph616

TicTacToe - Py

Mar 28th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.94 KB | None | 0 0
  1. import random
  2. table = {}
  3.    
  4. def checkRow(position):
  5.     if(position % 3 == 0):#right-most column
  6.         if(table[position] == table[position - 1] == table[position - 2]):
  7.             return True
  8.     elif(position % 3 == 1):#left-most column
  9.         if(table[position] == table[position + 1] == table[position + 2]):
  10.             return True
  11.     elif(position % 3 == 2):# middle column
  12.         if(table[position] == table[position - 1] == table[position + 1]):
  13.             return True
  14.     return False
  15.  
  16. def checkCol(position):
  17.     if(position < 4):#first row
  18.         if(table[position] == table[position + 3] == table[position + 6]):
  19.             return True
  20.     elif(position < 7):#second row
  21.         if(table[position] == table[position - 3] == table[position + 3]):
  22.             return True
  23.     elif(position < 10):
  24.         if(table[position] == table[position - 3] == table[position - 6]):
  25.             return True
  26.     return False
  27.  
  28. def checkDiag(position):
  29.     if(position % 3 == 1):#top-left / bottom left
  30.         if(table[position] == table[(position + 4) % 6] == table[(position + 8) % 12]):
  31.             return True
  32.     elif(position % 3 == 0):#top-right / bottom-right
  33.         if(table[position] == table[(position + 2) % 6] == table[(position + 4) % 12]):
  34.             return True
  35.     elif(position % 3 == 2):#central position
  36.         if(table[position] == table[position - 4] == table[position + 4]):
  37.             return True
  38.         if(table[position] == table[position - 2] == table[position + 2]):
  39.             return True
  40.     return False
  41.  
  42. def check(position):
  43.     #   1 | 2 | 3
  44.     #   ---------
  45.     #   4 | 5 | 6
  46.     #   ---------
  47.     #   7 | 8 | 9
  48.     #elements at even positions need to be checked only for ROWS and COLUMNS
  49.     #elements at odd positions need to be checked for ROWS, COLUMNS & DIAGONALS also
  50.  
  51.     #In case if the position is even we don't have to worry about the diagonal checking
  52.     #so by initialising \\\\\diag_status = False///// we eliminate diag_status from the return statement
  53.     diag_status = False
  54.     if(position % 2 == 1):
  55.         diag_status = checkDiag(position)
  56.  
  57.     row_status = checkRow(position)
  58.     col_status = checkCol(position)
  59.     return row_status or col_status or diag_status
  60.  
  61. def displayBoard():
  62.     global table
  63.     print(table[1], table[2], table[3], sep=" | ")
  64.     for i in range(1, 3):
  65.         print("---------")
  66.         print(table[3 * i + 1], table[3 * i + 2], table[3 * i + 3], sep=" | ")
  67.        
  68. def getUserInput():
  69.     #user_position will give the position at which user wants to put their symbol
  70.     print("Enter the position at which you want to place", user, "\t", end="")
  71.     user_position = input()
  72.     #Check for valid position entry
  73.     while(not(user_position.isdigit())):
  74.         print("Please enter an integer")
  75.         user_position = input()
  76.     user_position = int(user_position)
  77.     while(user_position < 1 or user_position > 9):
  78.         print("Please enter a no. between 1 to 9")
  79.         print("Enter the position at which you want to place", user, "\t", end="")
  80.         user_position = int(input())
  81.     #Check if given position is vacant or not i.e. == " "
  82.     while(table.get(user_position) == "X" or table.get(user_position) == "O"):
  83.         print("Position", user_position, "is already occupied\nTry again")
  84.         print("Enter the position at which you want to place", user, "\t", end="")
  85.         user_position = int(input())
  86.     #Everything is OK so we put X/O in the user's desired postion
  87.     return user_position
  88.  
  89. #-------------------------------------------------------------------------------------------
  90. flag = False#Signifies the end of match. True = end, False = continue
  91. print("Welcome to tic-tac-toe")
  92. user = input("Choose your symbol [X or O]\t")
  93. #Check for symbol correctness
  94. while(not(user == 'X' or user == 'x' or user == 'O' or user == 'o')):
  95.     print("You entered an invalid symbol\nTry again")
  96.     user = input("Choose your symbol [X or O]\t")
  97. user = user.upper()
  98.  
  99. for i in range(1, 10):#initialising all values in table to their corresponding positions for displaying
  100.     table[i] = table.setdefault(i, i)
  101. displayBoard()
  102. for i in range(1, 10):#initialising all values in table[1-9] = ' '
  103.     table[i] = " "
  104. #displayBoard()
  105. if(user == "X"):
  106.     bot = "O"
  107. else:
  108.     bot = "X"
  109. while(flag == False and (" " in table.values())):
  110.     user_position = getUserInput()
  111.     table[user_position] = user
  112.     flag = check(user_position)
  113.     displayBoard()
  114.     if(flag == True):
  115.         print("You win")
  116.         break
  117.     print("\nBot's turn")
  118.     #Bot's turn
  119.     bot_position = random.randint(1, 9)
  120.     while(table.get(bot_position) == "X" or table.get(bot_position) == "O"):
  121.         bot_position = random.randint(1, 9)
  122.        
  123.     table[bot_position] = bot
  124.     flag = check(bot_position)
  125.     displayBoard()
  126.     if(flag == True):
  127.         print("Bot wins\nBetter luck next time")
  128.         break
  129.  
  130. if(flag == False):
  131.     print("It's a tie")
Add Comment
Please, Sign In to add comment