The parts of the code that need fixing are in the comments. Code is in Python.
The parts of the code that need fixing are in the comments. Code is in Python.
Chapter9: Using Classes And Objects
Section: Chapter Questions
Problem 1CP: In previous chapters, you have created programs for the Greenville Idol competition. Now create a...
Related questions
Question
class TicTacToePlayer:
# Player for a game of Tic Tac Toe
def __init__(self, symbol, name):
self.symbol = symbol
self.name = name
def move(self, board):
move = int(input(self.name + " make your move 1-9:"))
move -= 1
y = move % 3
x = move // 3
board[x][y] = self.symbol
def is_symbol(self, symbol):
if symbol == self.symbol:
return True
class TicTacToe:
# Game of TicTacToe in a class!
def __init__(
self, p1_symbol="X", p2_symbol="O", p1_name="Player1", p2_name="Player2"
):
self.p1 = TicTacToePlayer(p1_symbol, p1_name)
self.p2 = TicTacToePlayer(p2_symbol, p2_name)
self.board = [[" " for x in range(3)] for x in range(3)]
def play_game(self):
turn = 0
winner_symbol = False
while not winner_symbol:
self._print_board()
if turn % 2:
self.p2.move(self.board) # replace this line with a call to self._move
else:
self.p1.move(self.board) # replace this line with a call to self._move
turn += 1
winner_symbol = self._is_over()
self._print_win_from_symbol(winner_symbol)
def _move(self, player):
# Write this method to replace TicTacToePlayer.move
pass
def _print_board(self):
space_num = 1
print("-------")
for row in self.board:
print("|", end="")
for space in row:
if space == " ":
space = space_num
print(space, end="|")
space_num += 1
print()
print("-------")
def _print_win_from_symbol(self, symbol):
self._print_board()
if symbol == "C":
print("The Cat won again!")
elif self.p1.is_symbol(symbol):
print(self.p1.name + " wins!")
else:
print(self.p2.name + " wins!")
def _is_over(self):
blank_count = 0
for x in range(3):
for y in range(3):
if (
self.board[x][0] == self.board[x][1] == self.board[x][2]
and self.board[x][0] != " "
):
return self.board[x][0]
if (
self.board[0][y] == self.board[1][y] == self.board[2][y]
and self.board[0][y] != " "
):
return self.board[0][y]
if self.board[x][y] == " ": blank_count += 1
if (
self.board[0][2] == self.board[1][1] == self.board[2][0]
and self.board[1][1] != " "
):
return self.board[1][1]
if (
self.board[0][0] == self.board[1][1] == self.board[2][2]
and self.board[1][1] != " "
):
return self.board[1][1]
if blank_count == 0:
return "C"
return False
if __name__ == "__main__":
game = TicTacToe()
game.play_game()
The parts of the code that need fixing are in the comments.
Code is in Python.
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 1 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,