Advertisement
go6odn28

tic_tac_toe

Oct 5th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. import random
  2. board = ["-", "-", "-",
  3.          "-", "-", "-",
  4.          "-", "-", "-"]
  5.  
  6. currentPlayer = "X"
  7. winner = None
  8. gameRunning = True
  9.  
  10. # print the game board
  11. def printBoard(board):
  12.     print(board[0] + " | " + board[1] + " | " + board[2])
  13.     print("----------")
  14.     print(board[3] + " | " + board[4] + " | " + board[5])
  15.     print("----------")
  16.     print(board[6] + " | " + board[7] + " | " + board[8])
  17.  
  18.  
  19. # take player input
  20. def playerInput(board):
  21.     inp = int(input("Enter a number 1-9: "))
  22.     if 1 <= inp <= 9 and board[inp-1] == "-":
  23.         board[inp-1] = currentPlayer
  24.     else:
  25.         print("Oops player is already in that spot or index os out of range!")
  26.         while True:
  27.             inp = int(input("Enter a number 1-9: "))
  28.             if 1 <= inp <= 9 and board[inp - 1] == "-":
  29.                 board[inp - 1] = currentPlayer
  30.                 break
  31.             else:
  32.                 print("Oops player is already in that spot or index is out of range!")
  33.  
  34.  
  35. # check for win ot tie
  36. def checkHorizontle(board):
  37.     global winner
  38.     if board[0] == board[1] == board[2] and board[1] != "-":
  39.         winner = board[0]
  40.         return True
  41.     elif board[3] == board[4] == board[5] and board[3] != "-":
  42.         winner = board[3]
  43.         return True
  44.     elif board[6] == board[7] == board[8] and board[6] != "-":
  45.         winner = board[6]
  46.         return True
  47.  
  48. def checkRow(board):
  49.     global winner
  50.     if board[0] == board[3] == board[6] and board[0] != "-":
  51.         winner = board[0]
  52.         return True
  53.     elif board[1] == board[4] == board[7] and board[1] != "-":
  54.         winner = board[3]
  55.         return True
  56.     elif board[2] == board[5] == board[8] and board[2] != "-":
  57.         winner = board[7]
  58.         return True
  59.  
  60.  
  61. def checkDiag(board):
  62.     global winner
  63.     if board[0] == board[4] == board[8] and board[0] != "-":
  64.         winner = board[0]
  65.         return True
  66.     elif board[2] == board[4] == board[6] and board[2] != "-":
  67.         winner = board[2]
  68.         return True
  69.  
  70. def checkTie(board):
  71.     global gameRunning
  72.     if "-" not in board:
  73.         printBoard(board)
  74.         print("It is tie!")
  75.         gameRunning = False
  76.  
  77.  
  78. def checkWin(board):
  79.     if checkDiag(board) or checkHorizontle(board) or checkRow(board):
  80.         printBoard(board)
  81.         print(f"The winner is {winner}")
  82.         global gameRunning
  83.         gameRunning = False
  84.  
  85.  
  86. # computer
  87. def computer(board):
  88.     while currentPlayer == "0":
  89.         position = random.randint(0, 8)
  90.         if board[position] == "-":
  91.             board[position] = "0"
  92.             switchPlayer()
  93.  
  94.  
  95. # switch the player
  96. def switchPlayer():
  97.     global currentPlayer
  98.     if currentPlayer == "X":
  99.         currentPlayer = "0"
  100.     else:
  101.         currentPlayer = "X"
  102.  
  103.  
  104. # check for win ot tie again
  105. while gameRunning:
  106.     printBoard(board)
  107.     playerInput(board)
  108.     checkWin(board)
  109.     checkTie(board)
  110.     if not gameRunning:
  111.         break
  112.     switchPlayer()
  113.     computer(board)
  114.     checkWin(board)
  115.     checkTie(board)
  116.     if not gameRunning:
  117.         break
  118.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement