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. )


Output



Code...............
# This fucntion takes a dictionary as the input and then prints the values in the dictionary
def print_board(board):
i=0
print("-------------")
for key,value in board.items():
print("| {} ".format(value),end="")
i=i+1
# If we have printed 3 elements then we move to the next line
if i%3==0:
print("|")
print("-------------")
# This function contains the whole game play
def play_game():
# First we define a dictionary called board to store all the key value pairs in the dictionary
board={}
# Then we populate all the places in the dictionary with _ which means that they are empty
for i in range(1,10):
board[i]='_'
# The variable turns keeps the count of how many turns have been played.The total number of turns that can be played is 9
turns=0
# This variable is a boolean variable that tells whose turn it is
play1=True
# As long as turns are not 9 keep playing
while turns<9:
# Rows and columns are used for indexing the location where the user want to enter the input
row=0
col=0
place=0
# If the player1 is true its player1's turn or its player2's turn
if play1:
print("Player 1's turn")
# We take the inputs from the user
row=int(input("Enter the row(1-3) :"))
col=int(input("Enter the col(1-3) :"))
# Then we calculate the index number
place=(row-1)*3+col
# If the input given is wrong then we repeat the input statements again
# The place shouldnt be already filled
# The row and column index shouldnt be out of range
if row>3 or row<1 or col>3 or col<1 or board[place]=='X' or board[place]=='O':
print("Wrong Input.Play Again")
continue
# If there arent any errors then we place the user's input
board[place]='X'
# We make the user's turn to false to allow the other player to play
play1=False
# We print the board every time a user plays
print_board(board)
# Player2's turn
else:
print("Player 2's turn")
row=int(input("Enter the row(1-3) :"))
col=int(input("Enter the col(1-3) :"))
place=(row-1)*3+col
if row>3 or row<1 or col>3 or col<1 or board[place]=='X' or board[place]=='O':
print("Wrong Input.Play Again")
continue
board[place]='O'
play1=True
print_board(board)
turns=turns+1
print("All places filled...")
# We call the play_game function which takes care of the inputs and printing the board
play_game()
Hope it helps you..In case of any queries feel free to ask...
Happy coding :)
PYTHON Exercise 2. Tic-Tac-Toe In this exercise we are going to create a Tic-Tac-Toe game. 1....
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
Python
Complete the player_turn() function that completes a single turn of the game Tic-Tac-Toe (also called Noughts and Crosses). The function takes 3 parameters: 1. The first parameter is called board. It is a list of length 3, where each entry is a string with 3 characters representing a row on a Tic-Tac-Toe board. Each character in a row represents a slot on the Tic-Tac-Toe board with the "#" character indicating an empty slot. An empty board looks like this:...
(Tic-Tac-Toe) Create class TicTacToe that will enable you to write a complete app to play the game of Tic-Tac-Toe. The class contains a private 3-by-3 rectangular array of integers. The constructor should initialize the empty board to all 0s. Allow two human players. Wherever the first player moves, place a 1 in the specified square, and place a 2 wherever the second player moves. Each move must be to an empty square. After each move, determine whether the game has...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . 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...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...
1. Use Turtle Graphics to create a tic tac toe game in Python. Write a Python program that allows for one player vs computer to play tic tac toe game, without using turtle.turtle