Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from random import randint
- #Setup Variable
- Turn = None
- AI = None
- Player = None
- PrevPlayed = None
- Board = [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]
- ]
- VaildTeam = ["O", "X"]
- # Game Function
- def DrawBoard():
- global Board
- print("-"*13)
- for i in range(3):
- print(f"| {Board[i][0]} | {Board[i][1]} | {Board[i][2]} |")
- print("-"*13)
- def ChooseTeam():
- global Player, AI, VaildTeam
- while True:
- PlayerInput = input("Choose You Team O or X : ").upper()
- if PlayerInput in VaildTeam:
- Player = PlayerInput
- AI = "O" if Player=="X" else "O"
- break
- else:
- print("Invaild Team Choose Again!")
- continue
- def StartTurn():
- global Turn
- n=randint(1,2)
- Turn = Player if n==1 else AI
- def IsAbleToFill(row:int, col:int)-> bool:
- global Board
- return type(Board[row][col]) == int
- def IsTie() -> bool:
- global Board
- return all([ type(Board[i][j]) != int for i in range(3) for j in range(3)])
- def IsGameOver() -> bool:
- global Board
- for i in range(3):
- # Left or Right
- if Board[i][0] == Board[i][1] and Board[i][1] == Board[i][2]:
- return True
- #Up and Down
- elif Board[0][i] == Board[1][i] and Board[1][i] == Board[2][i]:
- return True
- # More
- if Board[0][0] == Board[1][1] and Board[1][1] == Board[2][2]:
- return True
- elif Board[0][2] == Board[1][1] and Board[1][1] == Board[2][0]:
- return True
- return False
- def PlayerFill(row : int, col : int):
- global Player, Board
- if IsAbleToFill(row, col):
- Board[row][col] = Player
- else:
- return
- def AutoPlayRound():
- global Turn, Player, AI, Board, PrevPlayed
- # AutoPlay Function
- def PlayerPlay():
- global Board, AI, Player, PrevPlayed, Turn
- row, col= None, None
- while True:
- PlayerInput = int(input("Choose Board to Fill in 1-9 : "))
- if PlayerInput > 9:
- print("Invaild Input Try Again")
- continue
- if PlayerInput <= 3:
- row, col = 0, PlayerInput-1
- if not IsAbleToFill(row, col):
- print("Board Already Filled Choose Another")
- continue
- PlayerFill(row, col)
- break
- elif PlayerInput <= 6:
- row, col = 1, PlayerInput-4
- if not IsAbleToFill(row, col):
- print("Board Already Filled Choose Another")
- continue
- PlayerFill(row, col)
- break
- elif PlayerInput <= 9:
- row, col = 2, PlayerInput-7
- if not IsAbleToFill(row, col):
- print("Board Already Filled Choose Another")
- continue
- PlayerFill(row, col)
- break
- PrevPlayed = Player
- Turn = AI
- def AIPlay():
- global Board, AI, Player, PrevPlayed, Turn
- print("AI is Thinking and Choosing...")
- AIFill()
- PrevPlayed = AI
- Turn = Player
- if Turn == Player:
- PlayerPlay()
- else:
- AIPlay()
- #AI MiniMax Function
- def MiniMaxCheckWinner():
- global Board
- for i in range(3):
- # Left or Right
- if Board[i][0] == Board[i][1] and Board[i][1] == Board[i][2]:
- return Board[i][0]
- #Up and Down
- elif Board[0][i] == Board[1][i] and Board[1][i] == Board[2][i]:
- return Board[0][i]
- # More
- if Board[0][0] == Board[1][1] and Board[1][1] == Board[2][2]:
- return Board[0][0]
- elif Board[0][2] == Board[1][1] and Board[1][1] == Board[2][0]:
- return Board[0][2]
- return None
- def MiniMax(ismaxing : bool) -> int:
- global AI, Player, Board
- if MiniMaxCheckWinner() == AI: return 1
- if MiniMaxCheckWinner() == Player: return -1
- if IsTie(): return 0
- # Minimax Main Block Code
- if ismaxing:
- maxscore = -math.inf
- for i in range(3):
- for j in range(3):
- if IsAbleToFill(i, j):
- temp = Board[i][j]
- Board[i][j] = AI
- score = MiniMax(False)
- if score > maxscore: maxscore = score
- Board[i][j] = temp
- return maxscore
- else:
- minscore = math.inf
- for i in range(3):
- for j in range(3):
- if IsAbleToFill(i, j):
- temp = Board[i][j]
- Board[i][j] = AI
- score = MiniMax(True)
- if score < minscore: minscore = score
- Board[i][j] = temp
- return minscore
- def AIFill():
- global AI, Player
- #Setup Variable for AI
- bestscore = -math.inf
- bestfill = None
- for i in range(3):
- for j in range(3):
- if IsAbleToFill(i, j):
- score = MiniMax(False)
- if score > bestscore:
- bestscore = score
- bestfill = (i, j)
- # AI Fill Here
- Board[bestfill[0]][bestfill[1]] = AI
- # Main Game Code
- ChooseTeam()
- StartTurn()
- while True:
- DrawBoard()
- if IsGameOver():
- print(f"Game Over Winner : {PrevPlayed}")
- break
- if IsTie():
- print("Game Tie!")
- break
- AutoPlayRound()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement