Advertisement
kingbode

Untitled

Sep 7th, 2022
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. '''
  2. source
  3. https://gist.github.com/CodeWithHarry/d83fed6958b7626ef51aa87c2d7130de?fbclid=IwAR27Zij46IBW2VNN1X3pcavktbTUZ0XFpzsKHgMHFnFRHvL_xX7enrd2NmA
  4. '''
  5. from colorama import Fore
  6.  
  7. def find_indices(list_to_check, item_to_find):
  8.     indices = []
  9.     for idx, value in enumerate(list_to_check):
  10.         if value == item_to_find:
  11.             indices.append(idx)
  12.     return indices
  13.  
  14.  
  15.  
  16. # declare colors
  17. # color of the cells
  18. cellsColor = Fore.LIGHTCYAN_EX
  19. Xcolor = Fore.LIGHTRED_EX
  20. Ocolor = Fore.LIGHTBLUE_EX
  21.  
  22. def printBoard(boardList):
  23.     # update the tick tac toe board
  24.     line = "║ "
  25.     print(cellsColor + f"╔═══╦═══╦═══╗")
  26.     for i in range(0, 9):
  27.         if (boardList[i][1] != 1):
  28.             boardList[i] = (str(i),None)
  29.             # boardList[i] = (" ",None)
  30.  
  31.         # rebuild the line based on color of the cell and X or O
  32.         if boardList[i][1] == 1 and boardList[i][0] == 'X':
  33.             line += Xcolor + boardList[i][0] + cellsColor + " ║ "
  34.         elif boardList[i][1] == 1 and boardList[i][0] == 'O':
  35.             line += Ocolor + boardList[i][0] + cellsColor + " ║ "
  36.         else:
  37.             line += boardList[i][0] + " ║ "
  38.  
  39.  
  40.         if ((i+1) % 3 == 0):
  41.             print(line)
  42.             if (i+1) != 9:
  43.                 print(cellsColor + f"╠═══╬═══╬═══╣")
  44.             else:
  45.                 print(cellsColor + f"╚═══╩═══╩═══╝")
  46.             line = "║ "
  47.  
  48.  
  49.  
  50. def checkWin(boardList):
  51.  
  52.     wins = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]
  53.     playerX_indices = find_indices(boardList, ('X', 1))
  54.     playerO_indices = find_indices(boardList, ('O', 1))
  55.  
  56.     # check if any of wins item is a subset of playerX_indices or playerO_indices
  57.     if any(set(win).issubset(playerX_indices) for win in wins):
  58.         printBoard(boardList)
  59.         print(Fore.LIGHTYELLOW_EX, "X Won the match")
  60.         return True
  61.     elif any(set(win).issubset(playerO_indices) for win in wins):
  62.         printBoard(boardList)
  63.         print(Fore.LIGHTYELLOW_EX, "O Won the match")
  64.         return True
  65.     else:
  66.         # check if all cells are filled and no one has won
  67.         if all(boardList[i][1] == 1 for i in range(9)):
  68.             printBoard(boardList)
  69.             print(Fore.LIGHTYELLOW_EX, "Match Draw")
  70.             return True
  71.  
  72.     return False
  73.  
  74.  
  75.  
  76. if __name__ == "__main__":
  77.  
  78.     boardList = []
  79.  
  80.     boardList = [("player",str(x)) for x in range(9)]
  81.  
  82.     players = ["O","X"]
  83.     colors = [Ocolor, Xcolor]
  84.     turn = True # 1 for X and 0 for O
  85.  
  86.     print(Fore.LIGHTYELLOW_EX + "Welcome to Tic Tac Toe")
  87.     while (True):
  88.         printBoard(boardList)
  89.  
  90.         print(colors[turn] + f"{players[turn]}'s Chance")
  91.  
  92.         value = int(input(colors[turn] + "Please enter a value: "))
  93.         # check if the value is already taken
  94.         if (boardList[value][1] == None):
  95.             boardList[value] = (players[turn],1)
  96.         else:
  97.             print(Fore.LIGHTMAGENTA_EX,"Value already taken")
  98.             continue
  99.  
  100.         if (checkWin(boardList)):
  101.             print("Match over")
  102.             break
  103.  
  104.         turn = not turn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement