#Tic Tac Toe import random def drawBoard(board): #This function draws the board #The board is a list of 10 strings representing the board (Ignore index 0) print (' | | ') def inputPlayerLetter: #Let's the player type which letter they want to be #Returns a list with the players letter as the first item and the computer as the second letter = ' ' while not (letter == 'X' or letter == 'O': print('Do you want to be X or O?') letter = input().upper() #The first letter in the list is the player's letter. The second is the computer's letter. if letter == 'X': return ['X', 'O'] else: return ['O', 'X'] def whoGoesFirst: #Randomly choose the player who goes first. if random.randint(0,1) == 0: return 'computer' else return 'player' def playAgain: #This function returns true if the player wants to play again. print ("Do you want to play again? (Yes or No)") return input().lower()startswith('y') def makeMove: board[move] = letter def isWinner(bo, le): #Given a board and player's letter, this function returns True if that player won. #We use bo instead of board and le instead of letter so we don't have to type as much. return((bo[7] == le, and bo[8] == le, and bo[9] == le) or #Across the Top return(bo[1] == le, and bo[2] == le, and bo[3] == le) or #Across the Bottom return(bo[7] ==le, and bo[4] == le, and bo[1] == le) or #Down the left side return(bo[9] == le, and bo[6] == le, and bo[3] == le) or #Down the right side return(bo[8] == le, and bo[5] == le, and bo[2] == le) or #Down the middle return(bo[4] == le, and bo[5] == le, and bo[6] == le) or #Across the middle return(bo[7] == le, and bo[5] == le, and bo[3] == le) or #Diagonal return(bo[9] == le, and bo[5] == le, and bo[1] == le)) #Diagonal def getBoardCopy(board): #Make a duplicate of the board list and return it to duplicate. dupeBoard = []
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question