Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There is 1 process for the player, 1 process for the computer and 1 process for the game itself. I am not fully understanding how

There is 1 process for the player, 1 process for the computer and 1 process for the game itself. I am not fully understanding how I can get this working with all 3 of these processes. I am running into attribute error related to the multi processing. Any help on how to get this resolved and working is appreciated!

Below is code and error that I get 

 

Enter your move (row,column) -- Only 0, 1, or 2 for the row and column: Process Process-1:
Traceback (most recent call last):
 File "/Users/opt/anaconda3/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
   self.run()
 File "/Users/opt/anaconda3/lib/python3.9/multiprocessing/process.py", line 108, in run
   self._target(*self._args, **self._kwargs)
 File "/Users/Desktop/advanced python class/tic-tac-toe/test4.py", line 43, in _play_game
   if self._check_win():
 File "/Users/Desktop/advanced python class/tic-tac-toe/test4.py", line 68, in _check_win
   if self.board[i][0] == self.player1 and self.board[i][1] == self.player1 and self.board[i][2] == self.player1:
AttributeError: 'GameManager' object has no attribute 'player1'

 

import multiprocessing import random     class GameManager: def __init__(self): self.board = [['-','-','-'],['-','-','-'],['-','-','-']] self.queue = multiprocessing.Queue()   def _player1_move(self): self.player1 = 'X' player1_moves = multiprocessing.Process(target=self)     def _computer_move(self): self.player2 = 'O' computer_move = multiprocessing.Process(target=self)     def run_game(self): logic_process = multiprocessing.Process(target=self._play_game) logic_process.start() self._manage_user_input() logic_process.join()     def _manage_user_input(self): while True: try: move = input('Enter your move (row,column) -- Only 0, 1, or 2 for the row and column: ') row, column = map(int, move.split(',')) # Put the move into the queue self.queue.put((row, column)) except ValueError: print("Invalid input. Please enter a valid move.")     def _play_game(self): while True: # Check if there are any moves in the queue if not self.queue.empty(): # Get the latest move from the queue row, column = self.queue.get() self.board[row][column] = self._player1_move self._make_computer_move() self._print_board()   if self._check_win(): print('Player 1 Wins!') print('Play again?') response = input('Enter y for another game or n to exit: ') if(response == 'y'): self.board = [['-','-','-'],['-','-','-'],['-','-','-']] self._play_game else: print('exiting...') logic_process.stop() quit() break   def _make_computer_move(self): # Make a random move row = random.randint(0, 2) col = random.randint(0, 2) while not (self._is_valid_move(row, col)): row = random.randint(0, 2) col = random.randint(0, 2) self.board[row][col] = self._computer_move   def _check_win(self): # Check for horizontal and vertical win conditions for i in range(3): if self.board[i][0] == self.player1 and self.board[i][1] == self.player1 and self.board[i][2] == self.player1: return True if self.board[0][i] == self.player1 and self.board[1][i] == self.player1 and self.board[2][i] == self.player1: return True # Check for diagonal win conditions if self.board[0][0] == self.player1 and self.board[1][1] == self.player1 and self.board[2][2] == self.player1: return True if self.board[0][2] == self.player1 and self.board[1][1] == self.player1 and self.board[2][0] == self.player1: return True def _is_valid_move(self, row, col): return self.board[row][col] == '-' def _print_board(self): print() print(" 0 1 2") for i in range(3): print(str(i) + " " + str(self.board[i])) if __name__ == "__main__": game_manager = GameManager() game_manager.run_game()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Cost Management Accounting And Control

Authors: Don R. Hansen, Maryanne M. Mowen, Liming Guan

6th Edition

324559674, 978-0324559675

More Books

Students also viewed these Algorithms questions

Question

Describe the three major subdivisions of the Malleus Maleficarum.

Answered: 1 week ago