Advertisement
OriHackingTutorials

Tic Tac Toe

Jul 10th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.79 KB | None | 0 0
  1. from os import system
  2. from colorama import init
  3. init()
  4. from colorama import Fore,Style,Back
  5.  
  6. def printBoard(board, winningLocations = ['x']):
  7.  
  8.     if str(winningLocations[0]) in '012345678':
  9.         for i in range(0,len(board)):
  10.             if i in winningLocations:
  11.                 board[i] = Fore.YELLOW + f"{board[i]}" + Style.RESET_ALL
  12.             if board[i] in '0123456789':
  13.                 board[i] = ' '
  14.        
  15.         print("\n                               Winning board:\n")
  16.     else:
  17.         print("\n                               Current board:\n")
  18.    
  19.    
  20.     print(f"                              ―――――――――――――――――")
  21.     print(f"                             |  {board[0]}  |  {board[1]}  |  {board[2]}  |")
  22.     print(f"                             |―――――|―――――|―――――|")
  23.     print(f"                             |  {board[3]}  |  {board[4]}  |  {board[5]}  |")
  24.     print(f"                             |―――――|―――――|―――――|")
  25.     print(f"                             |  {board[6]}  |  {board[7]}  |  {board[8]}  |")
  26.     print(f"                              ―――――――――――――――――\n\n")
  27.        
  28.  
  29. def posToChange(Board,player):
  30.     printBoard(Board)
  31.  
  32.     while True:
  33.         posToReplace = input(f"Where would you like to put a '{player}' (type the number): ")
  34.         if (str(posToReplace) in '123456789') and (Board[int(posToReplace)-1] not in 'XO'):
  35.             posToReplace = int(posToReplace)
  36.             break
  37.         else:
  38.             print("NO.\n")
  39.    
  40.  
  41.     return posToReplace
  42.  
  43. def findNextPlayer(prevPlayer):
  44.     if(prevPlayer == 'X'):
  45.         return 'O'
  46.     else:
  47.         return 'X'
  48.  
  49. def findNextBoard(prevBoard,playerAns):
  50.     nextBoard = prevBoard    
  51.     nextBoard[playerAns-1] = player
  52.     return nextBoard
  53.  
  54. def checkIfWon(Board,player):
  55.     winningLocations = []
  56.     # columns
  57.     for i in range(0, 3):
  58.         if Board[i] == Board[i+3] == Board[i+3+3]:
  59.             winningLocations.append(i)
  60.             winningLocations.append(i+3)
  61.             winningLocations.append(i+3+3)
  62.             return True,winningLocations
  63.  
  64.     #check rows:
  65.     for i in range(0, 7,3):
  66.         if Board[i] == Board[i+1] == Board[i+1+1]:
  67.             winningLocations.append(i)
  68.             winningLocations.append(i+1)
  69.             winningLocations.append(i+1+1)
  70.             return True,winningLocations
  71.  
  72.     # alachson starting from 0
  73.     if Board[0] == Board[0+4] == Board[0+4+4]:
  74.         winningLocations.append(0)
  75.         winningLocations.append(4)
  76.         winningLocations.append(8)
  77.         return True,winningLocations
  78.  
  79.     # alachson starting from 2
  80.     if Board[2] == Board[2+2] == Board[2+2+2]:
  81.         winningLocations.append(2)
  82.         winningLocations.append(4)
  83.         winningLocations.append(6)
  84.         return True,winningLocations
  85.  
  86.     return False,winningLocations
  87.  
  88. board = ['1','2','3',
  89.          '4','5','6',
  90.          '7','8','9']
  91.  
  92. player = 'O'
  93.  
  94. timesPlayed = 0
  95.  
  96. while timesPlayed < 9:
  97.         system("cls")
  98.  
  99.         player = findNextPlayer(player)
  100.         posToReplace = posToChange(board,player)
  101.         board = findNextBoard(board,posToReplace)
  102.         Won, winningLocations = checkIfWon(board,player)
  103.  
  104.         if Won == True:
  105.             system("cls")
  106.             printBoard(board,winningLocations)
  107.             print(f"\n\n\nCongrats {player}! you won the game!\n\n\n")
  108.             print("Would you like to try again?: ")
  109.             input("")
  110.             print("well thats too bad, you cant.")
  111.             input("Press any key to continue . . .")
  112.         timesPlayed+=1
  113.         if timesPlayed == 9:
  114.             print("\n\n\n ITS A DRAW!!!\n\n\n")
  115.             input("Press any key to continue . . .")
  116.  
  117.  
  118. print(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement