Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- #Not Working as intended
- #Simple TicTacToe program
- def printBoard(Board): # Prints out the Board
- Board.setdefault('top-l', ' ')
- Board.setdefault('top-m', ' ')
- Board.setdefault('top-r', ' ')
- Board.setdefault('mid-l', ' ')
- Board.setdefault('mid-m', ' ')
- Board.setdefault('mid-r', ' ')
- Board.setdefault('bot-l', ' ')
- Board.setdefault('bot-m', ' ')
- Board.setdefault('bot-r', ' ')
- print (Board['top-l'], '|', Board['top-m'], '|', Board['top-l'])
- print('---------')
- print (Board['mid-l'], '|', Board['mid-m'], '|', Board['mid-l'])
- print('---------')
- print (Board['bot-l'], '|', Board['bot-m'], '|', Board['bot-l'])
- return None
- def modifyGameState(Board, plrChar, botChar): #Gets the bot's and the player's choice and applies them to the Board dict
- #I think this is the wrong function
- while True:
- checks = 0
- plrSelection = input('''What's your move? (top-, mid-, bot- & l, m, r) ''').lower()
- botSelection = random.choice([k for k,v in Board.items() if v == ' '])
- if Board.get(plrSelection, 0) == ' ':
- checks += 1
- else:
- print('This position is occupied!')
- if Board.get(botSelection, 0) == ' ' and botSelection != plrSelection:
- checks += 1
- if checks == 2:
- Board[plrSelection] = plrChar
- Board[botSelection] = botChar
- break
- return None
- def getPlrChar(): #Gets the player's X or O
- while True:
- plrChar = input('''Do you want to be X or O? ''').upper()
- if plrChar == 'X' or plrChar == 'O':
- break
- else:
- print('''That's not a valid Character''')
- return plrChar
- def checkWinner(Board, plrChar, botChar): #Checks if Someone Won and if so Who
- if Board['top-l'] == Board['top-m'] == Board['top-r'] and (Board['top-l'] == plrChar or Board['top-l'] == botChar):
- if Board['top-l'] == plrChar:
- print('The Player Wins!')
- return True
- else:
- print('The Machine Wins!')
- return True
- if Board['mid-l'] == Board['mid-m'] == Board['mid-r'] and (Board['mid-l'] == plrChar or Board['mid-l'] == botChar):
- if Board['mid-l'] == plrChar:
- print('The Player Wins!')
- return True
- else:
- print('The Machine Wins!')
- return True
- if Board['bot-l'] == Board['bot-m'] == Board['bot-r'] and (Board['bot-l'] == plrChar or Board['bot-l'] == botChar):
- if Board['bot-l'] == plrChar:
- print('The Player Wins!')
- return True
- else:
- print('The Machine Wins!')
- return True
- if Board['top-l'] == Board['mid-l'] == Board['bot-l'] and (Board['top-l'] == plrChar or Board['top-l'] == botChar):
- if Board['top-l'] == plrChar:
- print('The Player Wins!')
- return True
- if Board['top-m'] == Board['mid-m'] == Board['bot-m'] and (Board['top-m'] == plrChar or Board['top-m'] == botChar):
- if Board['top-m'] == plrChar:
- print('The Player Wins!')
- return True
- else:
- print('The Machine Wins!')
- return True
- if Board['top-r'] == Board['mid-r'] == Board['bot-r'] and (Board['top-r'] == plrChar or Board['top-r'] == botChar):
- if Board['top-r'] == plrChar:
- print('The Player Wins!')
- return True
- else:
- print('The Machine Wins!')
- return True
- if Board['top-l'] == Board['mid-m'] == Board['bot-r'] and (Board['top-l'] == plrChar or Board['top-l'] == botChar):
- if Board['top-l'] == plrChar:
- print('The Player Wins!')
- return True
- else:
- print('The Machine Wins!')
- return True
- if Board['top-r'] == Board['mid-m'] == Board['bot-l'] and (Board['top-r'] == plrChar or Board['top-r'] == botChar):
- if Board['top-l'] == plrChar:
- print('The Player Wins!')
- return True
- else:
- print('The Machine Wins!')
- return True
- # Functions: printBoard() modifyGameState() getPlrChar() CheckWinner()
- daBoard = {}
- playerChar = getPlrChar()
- if playerChar == 'X':
- robotChar = 'O'
- else:
- robotChar = 'X'
- print('Let the games Begin!')
- while True:
- printBoard(daBoard)
- modifyGameState(daBoard, playerChar, robotChar)
- if checkWinner(daBoard, playerChar, robotChar) is True:
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement