Question

PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1....

PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1. Create the data structure – Nine slots that can each contain an X, an O, or a blank. – To represent the board with a dictionary, you can assign each slot a string-value key. – String values in the key-value pair to represent what’s in each slot on the board: ■ 'X' ■ 'O' ■ ‘ ‘ 2. Create a function to print the board dictionary onto the screen 3. Add the code that allows the players to enter their moves (Note: This isn’t a complete tic-tac-toe game — for instance, it doesn’t ever check whether a player has won — but it’s enough to see how data structures can be used in programs. )

Homework Answers

Answer #1

turn = 'X'


# Board taken as dictionary where key as string value and key value is space default
theBoard = {'1': ' ', '2': ' ', '3': ' ',
'4': ' ', '5': ' ', '6': ' ',
'7': ' ', '8': ' ', '9': ' '}


def printBoard(board):
"""
This function print nine slot with space
:param board:
:return: None
"""
print(board["1"] + '|' + board["2"] + '|' + board["3"])
print('-+-+-')
print(board["4"] + '|' + board["5"] + '|' + board["6"])
print('-+-+-')
print(board["7"] + '|' + board["8"] + '|' + board["9"])


for i in range(9):
# Iterate i value upto 9
# Print board
printBoard(theBoard)
print('Turn for :' + turn + ' Choose a (1 to 9): ')
# user input
move = input()
  
while not theBoard[move] == ' ':
print('Do not try to steal a place! ')
move = input()

theBoard[move] = turn
  
if turn == 'X':
turn = '0'
else:
turn = 'X'
printBoard(theBoard)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Can you create a 2-player Tic Tac Toe game (also known as noughts and crosses) on...
Can you create a 2-player Tic Tac Toe game (also known as noughts and crosses) on MATLAB, using nested loops (for, if, else, while loops) arrays, conditional execution and functions, as well as displaying the game board of X and O on a plot.
JAVA Programming * create a 3 dimensional tic tac toe game. make 1 player the computer.
JAVA Programming * create a 3 dimensional tic tac toe game. make 1 player the computer.
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Part 1: Create the grid tic-tac-toe gameboard using buttons and iteration. Part 2: Human user gets...
Part 1: Create the grid tic-tac-toe gameboard using buttons and iteration. Part 2: Human user gets to select an open cell on the grid - place an X on that button selected Part 3: Check for a win using DOM iteration - new game option if row or column matching X pattern Part 4: Computer gets to select an open cell on the grid - place an O on that button selected Part 5: Check for a win using DOM...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a...
in c++ : Problem Domain: The classic TicTacToe game alternatively place Xs and Os on a 3x3 grid. The winner is the first player to place 3 consecutive marks in a horizontal, vertical or diagonal row. Understanding the Problem: In this assignment, you will: 1) Create a tic tac toe board data structure. 2) Check for game over. 3) Clear the tic tac toe game for each game. 4) Display the tic tac toe board to screen. 5) Simulate playing...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary...
Python (Using for reference please comment which is which!) Exercise 1: Store Unique Words as Dictionary Keys Write a program that stores unique words as keys in a dictionary. Don't worry about upper/lowercase for now (i.e., it's ok for you to have separate entries for "The" and "the"), and just assign 1 as the value for each key. Use an input loop (while loop with an input function) to accepts values putting them into the dictionary until the user enters...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
convert to python 3 from python 2 from Tkinter import * # the blueprint for a...
convert to python 3 from python 2 from Tkinter import * # the blueprint for a room class Room(object): # the constructor def __init__(self,name,image): # rooms have a name, exits (e.g., south), exit locations (e.g., to the south is room n), # items (e.g., table), item descriptions (for each item), and grabbables (things that can # be taken and put into the inventory) self.name = name self.image = image self.exits = {} self.items = {} self.grabbables = [] # getters...
Consumerization of Technology at IFG3 “There’s good news and bad news,” Josh Novak reported to the...
Consumerization of Technology at IFG3 “There’s good news and bad news,” Josh Novak reported to the assembled IT management team at their monthly status meeting. “The good news is that our social media traffic is up 3000% in the past two years. Our new interactive website, Facebook presence, and our U-Tube and couponing promotions have been highly successful in driving awareness of our ‘Nature’s Glow’ brand and are very popular with our target demographic—the under-30s. Unfortunately, the bad news is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT