Advertisement
Guest User

TicTacToe Program

a guest
Nov 26th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.71 KB | Source Code | 0 0
  1. import random
  2.  
  3. #Not Working as intended
  4.  
  5. #Simple TicTacToe program
  6.  
  7. def printBoard(Board): # Prints out the Board
  8.  
  9.     Board.setdefault('top-l', ' ')
  10.     Board.setdefault('top-m', ' ')
  11.     Board.setdefault('top-r', ' ')
  12.     Board.setdefault('mid-l', ' ')
  13.     Board.setdefault('mid-m', ' ')
  14.     Board.setdefault('mid-r', ' ')
  15.     Board.setdefault('bot-l', ' ')
  16.     Board.setdefault('bot-m', ' ')
  17.     Board.setdefault('bot-r', ' ')    
  18.  
  19.     print (Board['top-l'], '|', Board['top-m'], '|', Board['top-l'])
  20.     print('---------')
  21.     print (Board['mid-l'], '|', Board['mid-m'], '|', Board['mid-l'])
  22.     print('---------')
  23.     print (Board['bot-l'], '|', Board['bot-m'], '|', Board['bot-l'])
  24.    
  25.     return None
  26.  
  27.  
  28. def modifyGameState(Board, plrChar, botChar): #Gets the bot's and the player's choice and applies them to the Board dict
  29.                                                                                     #I think this is the wrong function
  30.     while True:
  31.  
  32.         checks = 0
  33.        
  34.         plrSelection = input('''What's your move? (top-, mid-, bot- & l, m, r) ''').lower()
  35.         botSelection = random.choice([k for k,v in Board.items() if v == ' '])
  36.  
  37.         if Board.get(plrSelection, 0) == ' ':
  38.             checks += 1
  39.  
  40.         else:
  41.             print('This position is occupied!')
  42.  
  43.         if Board.get(botSelection, 0) == ' ' and botSelection != plrSelection:
  44.             checks += 1
  45.  
  46.  
  47.         if checks == 2:
  48.            
  49.             Board[plrSelection] = plrChar
  50.             Board[botSelection] = botChar
  51.  
  52.             break
  53.  
  54.     return None
  55.  
  56.  
  57. def getPlrChar(): #Gets the player's X or O
  58.  
  59.     while True:
  60.        
  61.         plrChar = input('''Do you want to be X or O? ''').upper()
  62.         if plrChar == 'X' or plrChar == 'O':
  63.             break
  64.        
  65.         else:
  66.             print('''That's not a valid Character''')
  67.  
  68.     return plrChar
  69.  
  70.  
  71. def checkWinner(Board, plrChar, botChar): #Checks if Someone Won and if so Who
  72.  
  73.     if Board['top-l'] == Board['top-m'] == Board['top-r'] and (Board['top-l'] == plrChar or Board['top-l'] == botChar):
  74.  
  75.         if Board['top-l'] == plrChar:
  76.  
  77.             print('The Player Wins!')
  78.             return True
  79.        
  80.         else:
  81.  
  82.             print('The Machine Wins!')
  83.             return True
  84.        
  85.     if Board['mid-l'] == Board['mid-m'] == Board['mid-r'] and (Board['mid-l'] == plrChar or Board['mid-l'] == botChar):
  86.  
  87.         if Board['mid-l'] == plrChar:
  88.  
  89.             print('The Player Wins!')
  90.             return True
  91.  
  92.         else:
  93.  
  94.             print('The Machine Wins!')
  95.             return True
  96.  
  97.  
  98.     if Board['bot-l'] == Board['bot-m'] == Board['bot-r'] and (Board['bot-l'] == plrChar or Board['bot-l'] == botChar):
  99.  
  100.         if Board['bot-l'] == plrChar:
  101.  
  102.             print('The Player Wins!')
  103.             return True
  104.  
  105.         else:
  106.  
  107.             print('The Machine Wins!')
  108.             return True
  109.  
  110.  
  111.     if Board['top-l'] == Board['mid-l'] == Board['bot-l'] and (Board['top-l'] == plrChar or Board['top-l'] == botChar):
  112.  
  113.         if Board['top-l'] == plrChar:
  114.  
  115.             print('The Player Wins!')
  116.             return True
  117.  
  118.  
  119.     if Board['top-m'] == Board['mid-m'] == Board['bot-m'] and (Board['top-m'] == plrChar or Board['top-m'] == botChar):
  120.  
  121.         if Board['top-m'] == plrChar:
  122.  
  123.             print('The Player Wins!')
  124.             return True
  125.  
  126.         else:
  127.  
  128.             print('The Machine Wins!')
  129.             return True
  130.  
  131.  
  132.     if Board['top-r'] == Board['mid-r'] == Board['bot-r'] and (Board['top-r'] == plrChar or Board['top-r'] == botChar):
  133.  
  134.         if Board['top-r'] == plrChar:
  135.  
  136.             print('The Player Wins!')
  137.             return True
  138.  
  139.         else:
  140.  
  141.             print('The Machine Wins!')
  142.             return True
  143.  
  144.  
  145.     if Board['top-l'] == Board['mid-m'] == Board['bot-r'] and (Board['top-l'] == plrChar or Board['top-l'] == botChar):
  146.  
  147.         if Board['top-l'] == plrChar:
  148.  
  149.             print('The Player Wins!')
  150.             return True
  151.        
  152.         else:
  153.  
  154.             print('The Machine Wins!')
  155.             return True
  156.  
  157.  
  158.     if Board['top-r'] == Board['mid-m'] == Board['bot-l'] and (Board['top-r'] == plrChar or Board['top-r'] == botChar):
  159.  
  160.         if Board['top-l'] == plrChar:
  161.  
  162.             print('The Player Wins!')
  163.             return True
  164.  
  165.         else:
  166.  
  167.             print('The Machine Wins!')
  168.             return True
  169.  
  170.  
  171. # Functions: printBoard() modifyGameState() getPlrChar() CheckWinner()
  172.  
  173.  
  174. daBoard = {}
  175. playerChar = getPlrChar()
  176.  
  177. if playerChar == 'X':
  178.     robotChar = 'O'
  179.    
  180. else:
  181.     robotChar = 'X'
  182.  
  183. print('Let the games Begin!')
  184. while True:
  185.     printBoard(daBoard)
  186.     modifyGameState(daBoard, playerChar, robotChar)
  187.    
  188.     if checkWinner(daBoard, playerChar, robotChar) is True:
  189.         break
  190.  
  191.  
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement