Advertisement
AyanUpadhaya

Two Player Tic Tac Toe Game

May 8th, 2021 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.43 KB | None | 0 0
  1. #Tic-tac-toe Multiplayer Game by Ayan Upadhaya contact: ayanu881@gmail.com
  2. #player one =X
  3. #player two =O
  4.  
  5. #keyboard moves for players
  6. #7-top left corner
  7. #8-top middle
  8. #9-top right corner
  9. #4-left side
  10. #5-middle
  11. #6-right side
  12. #1-bottom left corner
  13. #2-bottom middle
  14. #3-bottom right corner
  15.  
  16. import random
  17.  
  18. def game():
  19.  
  20.     board=[' ']*10
  21.  
  22.     playerOneLetter='x'.upper()
  23.     playerTwoLetter='o'.upper()
  24.  
  25.     def drawBoard():
  26.         #first we will print spaces with characters
  27.         print(board[7]+' | '+board[8]+' | '+board[9])
  28.         print('---'+'---'+'---')
  29.         print(board[4]+' | '+board[5]+' | '+board[6])
  30.         print('---'+'---'+'---')
  31.         print(board[1]+' | '+board[2]+' | '+board[3])
  32.        
  33.     def whoGoesfirst():
  34.         #checks to see who goes first
  35.         letter=random.choice(['X','O'])
  36.         if letter=='X':
  37.             return "playerOne"
  38.         return "playerTwo"
  39.  
  40.     def isSpaceFree(board,move):
  41.         #checks to see if the move spot is free to pass the move on board
  42.         if board[move]==" ":
  43.             return True
  44.  
  45.     def isPlayerOneWinner(board):
  46.         #first passing the board which is a list to check every possible condition
  47.         #if player one wins return true
  48.         if((board[7]=='X' and board[8]=='X' and board[9]=='X') or #horizontal
  49.         (board[4]=='X' and board[5]=='X' and board[6]=='X') or
  50.         (board[1]=='X' and board[2]=='X' and board[3]=='X') or
  51.         (board[7]=='X' and board[4]=='X' and board[1]=='X') or #vertical
  52.         (board[8]=='X' and board[5]=='X' and board[2]=='X') or
  53.         (board[9]=='X' and board[6]=='X' and board[3]=='X') or
  54.         (board[7]=='X' and board[5]=='X' and board[3]=='X') or #diagonal
  55.         (board[1]=='X' and board[5]=='X' and board[9]=='X')):
  56.             return True
  57.  
  58.     def isPlayerTwoWinner(board):
  59.         #first passing the board which is a list to check every possible condition
  60.         #if player two wins return true
  61.         if((board[7]=='O' and board[8]=='O' and board[9]=='O') or #horizontal
  62.         (board[4]=='O' and board[5]=='O' and board[6]=='O') or
  63.         (board[1]=='O' and board[2]=='O' and board[3]=='O') or
  64.         (board[7]=='O' and board[4]=='O' and board[1]=='O') or #vertical
  65.         (board[8]=='O' and board[5]=='O' and board[2]=='O') or
  66.         (board[9]=='O' and board[6]=='O' and board[3]=='O') or
  67.         (board[7]=='O' and board[5]=='O' and board[3]=='O') or #diagonal
  68.         (board[1]=='O' and board[5]=='O' and board[9]=='O')):
  69.             return True
  70.  
  71.  
  72.     #just draw the board first
  73.     drawBoard()
  74.     print()#for space
  75.     turn=whoGoesfirst()
  76.     print(f'{turn}'+' is going first')
  77.     print()#for space
  78.    
  79.     #thegameloop
  80.     totalmoves=0
  81.     while totalmoves!=9:
  82.  
  83.         if turn=='playerOne':
  84.             print("Give a move playerone:")
  85.             try:
  86.                 moveOne=int(input())
  87.                 if moveOne in [1,2,3,4,5,6,7,8,9] and isSpaceFree(board,moveOne):
  88.                     board[moveOne]=playerOneLetter
  89.                 else:
  90.                     continue
  91.             except ValueError:
  92.                 continue
  93.             drawBoard()
  94.             turn='playerTwo'
  95.         else:
  96.             print("Give a move playertwo:")
  97.             try:
  98.                 moveTwo=int(input())
  99.                 if moveTwo in [1,2,3,4,5,6,8,7,9] and isSpaceFree(board,moveTwo):
  100.                     board[moveTwo]=playerTwoLetter
  101.                 else:
  102.                     continue
  103.             except ValueError:
  104.                 continue
  105.             drawBoard()
  106.             turn='playerOne'
  107.        
  108.         if isPlayerOneWinner(board):
  109.             print("Player One wins the game")
  110.             break
  111.         if isPlayerTwoWinner(board):
  112.             print("Player two wins the game")
  113.             break
  114.  
  115.         totalmoves+=1
  116.  
  117.     if totalmoves==9:
  118.         print("It's a tie")
  119. #mainloop
  120. choice=''
  121. while choice!='n':
  122.     choice=input("Do you want to play a game,y/n?:").lower()
  123.     if choice in ['y','n']:
  124.         if choice=='y':
  125.             game()
  126.     else:
  127.         continue
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement