Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- table = {}
- def checkRow(position):
- if(position % 3 == 0):#right-most column
- if(table[position] == table[position - 1] == table[position - 2]):
- return True
- elif(position % 3 == 1):#left-most column
- if(table[position] == table[position + 1] == table[position + 2]):
- return True
- elif(position % 3 == 2):# middle column
- if(table[position] == table[position - 1] == table[position + 1]):
- return True
- return False
- def checkCol(position):
- if(position < 4):#first row
- if(table[position] == table[position + 3] == table[position + 6]):
- return True
- elif(position < 7):#second row
- if(table[position] == table[position - 3] == table[position + 3]):
- return True
- elif(position < 10):
- if(table[position] == table[position - 3] == table[position - 6]):
- return True
- return False
- def checkDiag(position):
- if(position % 3 == 1):#top-left / bottom left
- if(table[position] == table[(position + 4) % 6] == table[(position + 8) % 12]):
- return True
- elif(position % 3 == 0):#top-right / bottom-right
- if(table[position] == table[(position + 2) % 6] == table[(position + 4) % 12]):
- return True
- elif(position % 3 == 2):#central position
- if(table[position] == table[position - 4] == table[position + 4]):
- return True
- if(table[position] == table[position - 2] == table[position + 2]):
- return True
- return False
- def check(position):
- # 1 | 2 | 3
- # ---------
- # 4 | 5 | 6
- # ---------
- # 7 | 8 | 9
- #elements at even positions need to be checked only for ROWS and COLUMNS
- #elements at odd positions need to be checked for ROWS, COLUMNS & DIAGONALS also
- #In case if the position is even we don't have to worry about the diagonal checking
- #so by initialising \\\\\diag_status = False///// we eliminate diag_status from the return statement
- diag_status = False
- if(position % 2 == 1):
- diag_status = checkDiag(position)
- row_status = checkRow(position)
- col_status = checkCol(position)
- return row_status or col_status or diag_status
- def displayBoard():
- global table
- print(table[1], table[2], table[3], sep=" | ")
- for i in range(1, 3):
- print("---------")
- print(table[3 * i + 1], table[3 * i + 2], table[3 * i + 3], sep=" | ")
- def getUserInput():
- #user_position will give the position at which user wants to put their symbol
- print("Enter the position at which you want to place", user, "\t", end="")
- user_position = input()
- #Check for valid position entry
- while(not(user_position.isdigit())):
- print("Please enter an integer")
- user_position = input()
- user_position = int(user_position)
- while(user_position < 1 or user_position > 9):
- print("Please enter a no. between 1 to 9")
- print("Enter the position at which you want to place", user, "\t", end="")
- user_position = int(input())
- #Check if given position is vacant or not i.e. == " "
- while(table.get(user_position) == "X" or table.get(user_position) == "O"):
- print("Position", user_position, "is already occupied\nTry again")
- print("Enter the position at which you want to place", user, "\t", end="")
- user_position = int(input())
- #Everything is OK so we put X/O in the user's desired postion
- return user_position
- #-------------------------------------------------------------------------------------------
- flag = False#Signifies the end of match. True = end, False = continue
- print("Welcome to tic-tac-toe")
- user = input("Choose your symbol [X or O]\t")
- #Check for symbol correctness
- while(not(user == 'X' or user == 'x' or user == 'O' or user == 'o')):
- print("You entered an invalid symbol\nTry again")
- user = input("Choose your symbol [X or O]\t")
- user = user.upper()
- for i in range(1, 10):#initialising all values in table to their corresponding positions for displaying
- table[i] = table.setdefault(i, i)
- displayBoard()
- for i in range(1, 10):#initialising all values in table[1-9] = ' '
- table[i] = " "
- #displayBoard()
- if(user == "X"):
- bot = "O"
- else:
- bot = "X"
- while(flag == False and (" " in table.values())):
- user_position = getUserInput()
- table[user_position] = user
- flag = check(user_position)
- displayBoard()
- if(flag == True):
- print("You win")
- break
- print("\nBot's turn")
- #Bot's turn
- bot_position = random.randint(1, 9)
- while(table.get(bot_position) == "X" or table.get(bot_position) == "O"):
- bot_position = random.randint(1, 9)
- table[bot_position] = bot
- flag = check(bot_position)
- displayBoard()
- if(flag == True):
- print("Bot wins\nBetter luck next time")
- break
- if(flag == False):
- print("It's a tie")
Add Comment
Please, Sign In to add comment