Do In Python Using the model for ticket-tac-toe described in chapter 5 create a
ID: 3735499 • Letter: D
Question
Do In Python Using the model for ticket-tac-toe described in chapter 5 create a complete tic-tac-toe game. The game should be able to determine if there is a winner and display who the winner is X or O. The game should have 1 player and two player mode. 2 player mode you would take turns with a friend. 1 player mode the computer would automatically select a free space. The game should only allow the user to select a free space for their move. After the game has finished and reported a winner the program should ask if the user would like to play again. - 1 or 2 player mode for tic-tac-toe game - error check space entered - display the winner at the end of each game - ask users to play again -the model for the tic-tac-toe board should be a dictionary that was defined in chapter 5 - only allow users to move to a space that is not occupied and is valicdExplanation / Answer
Below is complete code written in python language
import random
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'])
def checkvalid(board, move):
if(board[move]==' '):
return True
else :
return False
def moveofcomputer(board, listofpossiblemoves):
while(1):
idx = random.randint(0,8);
move = listofpossiblemoves[idx];
if(checkvalid(board,move)):
return move
def checkwincondition(board):
if(board['top-L']!=' ' and board['top-L']==board['top-M'] and board['top-M']==board['top-R']):
print(board['top-L'] +' won.')
return True
elif(board['mid-L']!=' ' and board['mid-L']==board['mid-M'] and board['mid-M']==board['mid-R']):
print(board['mid-L'] +' won.')
return True
elif(board['low-L']!=' ' and board['low-L']==board['low-M'] and board['low-M']==board['low-R']):
print(board['low-L'] +' won.')
return True
elif(board['top-L']!=' ' and board['top-L']==board['mid-L'] and board['mid-L']==board['low-L']):
print(board['mid-L'] +' won.')
return True
elif(board['top-M']!=' ' and board['top-M']==board['mid-M'] and board['mid-M']==board['low-M']):
print(board['low-M'] +' won.')
return True
elif(board['top-R']!=' ' and board['top-R']==board['mid-R'] and board['mid-R']==board['low-R']):
print(board['mid-R'] +' won.')
return True
elif(board['top-L']!=' ' and board['top-L']==board['mid-M'] and board['mid-M']==board['low-R']):
print(board['top-L'] +' won.')
return True
elif(board['top-R']!=' ' and board['top-R']==board['mid-M'] and board['mid-M']==board['low-L']):
print(board['top-R'] +' won.')
return True
return False
def playsinglemode(board, listofpossiblemoves):
flag = 1
for i in range(9):
if(flag):
while(1):
print('Turn for X. Move on which space?')
move = raw_input()
if(checkvalid(board,move)):
board[move]='X'
break
flag=0
else:
print('Turn for O. Bleep Blorp I'm a computer.')
move = moveofcomputer(board, listofpossiblemoves)
board[move]='O'
flag=1
printBoard(board)
if(checkwincondition(board)):
return None
if(checkwincondition(board)==False):
print('No one win')
def playtwomode(board, listofpossiblemoves):
flag = 1
for i in range(9):
if(flag):
while(1):
print('Turn for X. Move on which space?')
move = raw_input()
if(checkvalid(board,move)):
board[move]='X'
break
flag=0
else:
while(1):
print('Turn for O. Move on which space?')
move = raw_input()
if(checkvalid(board,move)):
board[move]='O'
break
flag=1
printBoard(board)
if(checkwincondition(board)):
return None
if(checkwincondition(board)==False):
print('No one win')
def resetboard(board):
board['top-L']=' '
board['top-M']=' '
board['top-R']=' '
board['mid-L']=' '
board['mid-M']=' '
board['mid-R']=' '
board['low-L']=' '
board['low-M']=' '
board['low-R']=' '
# Main part of code
theboard= {'top-L':' ', 'top-M':' ', 'top-R':' ',
'mid-L':' ', 'mid-M':' ', 'mid-R':' ',
'low-L':' ', 'low-M':' ', 'low-R':' '}
listofpossiblemoves=['top-L', 'top-M', 'top-R','mid-L', 'mid-M', 'mid-R','low-L', 'low-M', 'low-R']
while(1):
print('1 player or 2 player mode?(1or2)')
mode = input()
printBoard(theboard)
if(mode==1):
playsinglemode(theboard,listofpossiblemoves)
else:
playtwomode(theboard,listofpossiblemoves)
print('Do you want to play again?(y or n)')
ch = raw_input()
if(ch=='n'):
break
resetboard(theboard)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.