1.

Write a tic-tac-toe program in python.

Answer»

# tic-tac-toe program 

import random def drawboard (board): 

# This function is used to print the board

print (‘ | | ‘) 

print (‘ ‘ + board [7] + ‘ j ‘ + board [8] + ‘ | ‘ + board [9]) print (‘| | ‘) 

print (‘ ‘) 

print (‘| | ‘) 

print (‘ ‘ + board [4] + ‘ | ‘ + board [5] + ‘ | ‘ + board [6]) print (‘| | ‘) 

print (‘ ‘) 

Print (‘ | | ‘) 

print (‘ ‘ + board [1] + ‘ | ‘ + board [2] + ‘ | ‘ + board [3]) print (‘| | ‘) 

def input player letter (): 

# This function is used to display the user typed letter Letter = ” 

while not (letter = = ‘X’ or letter = = ‘O’): 

print (‘Do you want to be X or O ?’) 

letter = input () .upper () 

if letter = ‘X’ return [‘X’, ‘O’] 

else: 

return [‘O’, ‘X’] def whogoesFirst (): 

# This function is used to randomly choose the player 

if randomrandint (0,1) = = 0 : return ‘Computer’ 

else:

return ‘Player’ def Play Again (): 

# This function is used when player wants play again print 

(‘Do you want to play again ? (Yes or no)’) return input (), lower ().startswith (‘y’) 

def makeMove (board, letter, move): board[move] = letter def is Winner (bo, le): 

# This function returns true when plays has won return

(60[7] = = le and 60[8] = = le and 60[9] = = le) or 

(60[4] = = le and 60[5] = = le and 60[6] = = le) or 

(60[1 ] = = le and 60[2] = = le and 60[3] = = le) or 

(60[7] = = le and 60[4] = = le and 60[1] = = le) or 

(60[8] = = le and 60[5] = = le and 60[2] = = le) or 

(60[9] = = le and 60[6] = = le and 60[3] = = le) or 

(60[7] = = le and 60[5] = = le and 60[3] = = le) or 

(60[9] = = le and 60[5] = = le and 60[1] = = le) or

def getBoardcopy(board): 

# This function make a duplicate of the board dupeBoard = [ ] 

for i in board : 

dupeBoard.append(i) 

return depeBoard 

def isSpaceFree (board, move): 

# This function returns Ttue if the passed

move is free return boardfmove] = = ‘ ‘ def getPlayerMove(board): 

# This function is used to take player’s move move = ‘ ‘ 

while move not in’12345678 9’.split () or 

not is spaceFree (board, int(move)): 

print (‘What is your next move ? (1-9)’) 

move = input () return int(move) 

defchooseRandomMoveFromList(board, movelist): 

# This function returns a valid move from the passed list possible Moves = [ ] for i in moveList: 

if is SpaceFree (board, i):

possibleMOves.append(i)

if len(possibleMoves)! = 0 : 

return random.choice (possibleMoves)

else: 

return – None 

defgetComputerMove(board, computerletter): 

# This function returns the computer’s letter

if computer letter = = ‘X’: 

player letter = ‘O’ 

else: 

player letter = ‘X’ 

for i in range (1,10): 

copy = getBoardCopy (board) 

if isSpaceFree (copy, i): 

makeMove (copy, computerletter, i) 

if is Winner (copy, computerletter): 

return i for i in range (1,10): 

copy = getBoard Copy (board) 

if isSpaceFree (copy, i): 

makeMove (copy, player letter, i) 

if is Winner (copy, player letter): 

return i 

move=chooseRandomMoveFromList (board, [1, 3, 7, 9]) 

if move ! = None : 

return move 

if isSpaceFree (board, 5): 

return 5 

return ChooseRandomMoveFromList (board, [2,4,6,8]) 

def is BoardFull (board): 

# This function returns true if all space on board has been taken for i in range (1,10): 

if isSpaceFree (board, i): 

return False 

return True print 

(“Welcome to Tic-Tac-Toe !”) 

while True: 

the Board = [” * 10

playerletter, computerletter = input

 playerletter() 

turn = whoGoesFirst() 

print (‘The’ + turn + ‘will go first.’)

 gamelsPlaying = True 

while gemelsPlaying: 

if turn = = ‘player’: 

drawBoard (theBoard) 

move = getPlayerMove (theBoard)

makeMove (theBoard, playerletter, move) 

if is Winner (theBoard, playletter):

drawBoard (theBoard) 

print (‘Hooray ! You have won !’)

gamelsPlaying = False 

esle: 

if is BoardFull (theBoard): 

drawBoard (theBoard) 

print (‘The game is a tie.”) 

break 

else: 

turn = ‘computer’ 

else: 

move=getCompterMove(theBoard, Computerletter) 

makeMove (theBoard, computer letter, move) 

if is Winner (theBoard, computerletter): drawBoard (theBoard) 

print(‘The computer has beaten you!’) gamelsPlaying = False 

else: 

if isBoardFull (theBoard): 

drawBoard (theBoard) 

print (‘The game is a tie !’) 

break 

else: 

turn = ‘player’ if not play Again (): 

break



Discussion

No Comment Found