Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os import system
- from colorama import init
- init()
- from colorama import Fore,Style,Back
- def printBoard(board, winningLocations = ['x']):
- if str(winningLocations[0]) in '012345678':
- for i in range(0,len(board)):
- if i in winningLocations:
- board[i] = Fore.YELLOW + f"{board[i]}" + Style.RESET_ALL
- if board[i] in '0123456789':
- board[i] = ' '
- print("\n Winning board:\n")
- else:
- print("\n Current board:\n")
- print(f" ―――――――――――――――――")
- print(f" | {board[0]} | {board[1]} | {board[2]} |")
- print(f" |―――――|―――――|―――――|")
- print(f" | {board[3]} | {board[4]} | {board[5]} |")
- print(f" |―――――|―――――|―――――|")
- print(f" | {board[6]} | {board[7]} | {board[8]} |")
- print(f" ―――――――――――――――――\n\n")
- def posToChange(Board,player):
- printBoard(Board)
- while True:
- posToReplace = input(f"Where would you like to put a '{player}' (type the number): ")
- if (str(posToReplace) in '123456789') and (Board[int(posToReplace)-1] not in 'XO'):
- posToReplace = int(posToReplace)
- break
- else:
- print("NO.\n")
- return posToReplace
- def findNextPlayer(prevPlayer):
- if(prevPlayer == 'X'):
- return 'O'
- else:
- return 'X'
- def findNextBoard(prevBoard,playerAns):
- nextBoard = prevBoard
- nextBoard[playerAns-1] = player
- return nextBoard
- def checkIfWon(Board,player):
- winningLocations = []
- # columns
- for i in range(0, 3):
- if Board[i] == Board[i+3] == Board[i+3+3]:
- winningLocations.append(i)
- winningLocations.append(i+3)
- winningLocations.append(i+3+3)
- return True,winningLocations
- #check rows:
- for i in range(0, 7,3):
- if Board[i] == Board[i+1] == Board[i+1+1]:
- winningLocations.append(i)
- winningLocations.append(i+1)
- winningLocations.append(i+1+1)
- return True,winningLocations
- # alachson starting from 0
- if Board[0] == Board[0+4] == Board[0+4+4]:
- winningLocations.append(0)
- winningLocations.append(4)
- winningLocations.append(8)
- return True,winningLocations
- # alachson starting from 2
- if Board[2] == Board[2+2] == Board[2+2+2]:
- winningLocations.append(2)
- winningLocations.append(4)
- winningLocations.append(6)
- return True,winningLocations
- return False,winningLocations
- board = ['1','2','3',
- '4','5','6',
- '7','8','9']
- player = 'O'
- timesPlayed = 0
- while timesPlayed < 9:
- system("cls")
- player = findNextPlayer(player)
- posToReplace = posToChange(board,player)
- board = findNextBoard(board,posToReplace)
- Won, winningLocations = checkIfWon(board,player)
- if Won == True:
- system("cls")
- printBoard(board,winningLocations)
- print(f"\n\n\nCongrats {player}! you won the game!\n\n\n")
- print("Would you like to try again?: ")
- input("")
- print("well thats too bad, you cant.")
- input("Press any key to continue . . .")
- timesPlayed+=1
- if timesPlayed == 9:
- print("\n\n\n ITS A DRAW!!!\n\n\n")
- input("Press any key to continue . . .")
- print(board)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement