0% found this document useful (0 votes)
12 views2 pages

Tic-Tac-Toe Program

The document contains code to print a blank tic-tac-toe board using a dictionary to represent the board and functions to print it. It then adds functionality to alternate turns between X and O and update the board dictionary with player moves.

Uploaded by

madhukeshs0742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Tic-Tac-Toe Program

The document contains code to print a blank tic-tac-toe board using a dictionary to represent the board and functions to print it. It then adds functionality to alternate turns between X and O and update the board dictionary with player moves.

Uploaded by

madhukeshs0742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# 1.

Program to Print Blank Tic-Tac-Toe board

theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',

'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',

'low-L': ' ', 'low-M': ' ', 'low-R': ' '}

def printBoard(board):

print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])

print('-+-+-')

print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])

print('-+-+-')

print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])

printBoard(theBoard)

# 2. Program to Print Blank Tic-Tac-Toe board

#Board Datastructure using dictionary

theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',

'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',

'low-L': ' ', 'low-M': ' ', 'low-R': ' '}

#code to print board

def printBoard(board):

print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])

print('-+-+-')

print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])

print('-+-+-')

print(board['low-L'] + '|' + board['low-M'] + '|' + board['low-R'])

turn = 'X'

for i in range(9):

printBoard(theBoard)

print('Turn for ' + turn + '. Move on which space?')

move = input()
theBoard[move] = turn

if turn == 'X':

turn = 'O'

else:

turn = 'X'

printBoard(theBoard)

You might also like