Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- board = ["-", "-", "-",
- "-", "-", "-",
- "-", "-", "-"]
- currentPlayer = "X"
- winner = None
- gameRunning = True
- # print the game board
- def printBoard(board):
- print(board[0] + " | " + board[1] + " | " + board[2])
- print("----------")
- print(board[3] + " | " + board[4] + " | " + board[5])
- print("----------")
- print(board[6] + " | " + board[7] + " | " + board[8])
- # take player input
- def playerInput(board):
- inp = int(input("Enter a number 1-9: "))
- if 1 <= inp <= 9 and board[inp-1] == "-":
- board[inp-1] = currentPlayer
- else:
- print("Oops player is already in that spot or index os out of range!")
- while True:
- inp = int(input("Enter a number 1-9: "))
- if 1 <= inp <= 9 and board[inp - 1] == "-":
- board[inp - 1] = currentPlayer
- break
- else:
- print("Oops player is already in that spot or index is out of range!")
- # check for win ot tie
- def checkHorizontle(board):
- global winner
- if board[0] == board[1] == board[2] and board[1] != "-":
- winner = board[0]
- return True
- elif board[3] == board[4] == board[5] and board[3] != "-":
- winner = board[3]
- return True
- elif board[6] == board[7] == board[8] and board[6] != "-":
- winner = board[6]
- return True
- def checkRow(board):
- global winner
- if board[0] == board[3] == board[6] and board[0] != "-":
- winner = board[0]
- return True
- elif board[1] == board[4] == board[7] and board[1] != "-":
- winner = board[3]
- return True
- elif board[2] == board[5] == board[8] and board[2] != "-":
- winner = board[7]
- return True
- def checkDiag(board):
- global winner
- if board[0] == board[4] == board[8] and board[0] != "-":
- winner = board[0]
- return True
- elif board[2] == board[4] == board[6] and board[2] != "-":
- winner = board[2]
- return True
- def checkTie(board):
- global gameRunning
- if "-" not in board:
- printBoard(board)
- print("It is tie!")
- gameRunning = False
- def checkWin(board):
- if checkDiag(board) or checkHorizontle(board) or checkRow(board):
- printBoard(board)
- print(f"The winner is {winner}")
- global gameRunning
- gameRunning = False
- # computer
- def computer(board):
- while currentPlayer == "0":
- position = random.randint(0, 8)
- if board[position] == "-":
- board[position] = "0"
- switchPlayer()
- # switch the player
- def switchPlayer():
- global currentPlayer
- if currentPlayer == "X":
- currentPlayer = "0"
- else:
- currentPlayer = "X"
- # check for win ot tie again
- while gameRunning:
- printBoard(board)
- playerInput(board)
- checkWin(board)
- checkTie(board)
- if not gameRunning:
- break
- switchPlayer()
- computer(board)
- checkWin(board)
- checkTie(board)
- if not gameRunning:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement