Search code examples
pythonclassinstanceinstance-methods

manipulate class instance


I want to give the second instance of my Player class the remaining marker after the first instance gets one of them.

class Player():

    available_markers = ['X', 'O']

    num_of_players = 0

    player2_marker = ' '

    def __init__(self):

        self.name = input('Please enter your name : ')

        self.marker = ' '

        Player.num_of_players += 1

    def choose_marker(self):

        while Player.num_of_players != 2:

            self.marker = input('Choose X or O')
            if self.marker == Player.available_markers[0]:
                Player.player2_marker == Player.available_markers[-1]
            else:
                Player.player2_marker == Player.available_markers[0]
        else:
            self.marker = Player.player2_marke

I would like to accomplish this in the instance method but have went through a lot of code that doesn't quite work.


Solution

  • There are some misunderstandings of object-oriented coding in your code so I'll try to address them in my answer. One of the goals of object-oriented programming is the separation of concerns. If you have some logic about how a game works and you have some logic about how a player works, you don't want the logic for both in the same place intermixed.

    All the player really needs to know is what their name, marker and player number are:

    class Player():
        num_of_players = 0
    
        def __init__(self, name, marker):
            Player.num_of_players += 1
            self.name = name
            self.marker = marker
            self.number = Player.num_of_players
            print(self.number, self.marker)
    

    Separate from that is how you want to start the game and initialize the players. You could create another class called Game but for now, I'll just do a function:

    def start_game():
        available_markers = ['X', 'O']
        print("Player 1")
        name = input('Please enter your name : ')
    

    Let's not trust that the user of the program will enter the right thing:

        while True:
            marker = input('Choose X or O: ')
    

    We will loop forever and just break out of it if a valid option is chosen:

            if marker in available_markers:
                break
            else:
                print("Invalid choice. Please pick again")
    
        player1 = Player(name, marker)
    

    We'll remove that marker from the list so the list just has one element in it now:

        available_markers.remove(marker)
        print("Player 2")
        name = input('Please enter your name : ')
        player2 = Player(name, available_markers[0])
    
    start_game()
    
    # Player 1
    # Please enter your name : Adam
    # Choose X or O: X
    # 1 X
    # Player 2
    # Please enter your name : Joe
    # 2 O
    

    Note that I create two separate instances of Player.

    Let's talk briefly about class variables vs instant variables. I kept num_of_players as a class variable to track the total number of players (accessible by doing Player.num_of_players or player1.num_of_players both will return that there are 2 total players). I also created another variable number so each player can track what their number is. That's an instance variable and tracked separately for each instance.