0

I have 2 python classes: Player, and Board. The board contains a dict with "A1" reference style for spaces.

player.py's Player class:

from board import Board

class Player:
    def __init__(self):
        self.name = input("Name: ")
        self.board = Board(3, ["A", "B", "C"])

board.py's Board class:

class Board:
    EMPTY = "0"
    spaces = {}
    def __init__(self, size, letters):
        for let in letters:
            for num in range(1, size + 1):
                self.spaces["{}{}".format(let, num)] = self.EMPTY

    def place_piece(self, spot):
        self.spaces[spot] = "X"

    def display_board(self):
        for let in letters:
            for num in range(1, size + 1):
                print("\n" + self.spaces["{}{}".format(let, num)]

When Player is instantiated, it creates a Board object inside. 2 players are created, and each is added to the list players[]. Each player's turn is selected with a simple 1/0 variable called current_player.

    from player import *
    from board import *

    current_player = 1
    players = []
    player_1 = Player()
    player_2 = Player()
    players.append(player_1)
    players.append(player_2)

    while True:
        # Switches players
        current_player = abs(current_player - 1)

        # Prints the current player's name
        print(players[current_player].name)

        # Calls some method which should alter the current player's board
        players[current_player].board.place_piece(input("> "))

        # Calls some method which should show the current player's board
        players[current_player].board.show_board()

Obviously, very simplified. The name prints out correctly every single time. The board, however, only uses the first player's board for both players. I know it's working because the players' name prints correctly.

The issue persists even if the boards are created separately and placed in their own list.

What am I doing wrong?

C-Love511
  • 358
  • 1
  • 7
  • 24
  • 1
    You need to put `Player` and `Board` classes in your question, and it would be better if you put `player.py` and `board.py` files here. – vishes_shell Nov 30 '16 at 06:43
  • By files i meant content of files. Because you making `from import *` which is [not good](http://stackoverflow.com/questions/2386714/why-is-import-bad). – vishes_shell Nov 30 '16 at 07:08
  • And what `print("{} place a piece on your board.".format(players[current_player].name) players[current_player].board.place_piece()` is that? Can you keep it as you have it in your code? ALSO: what is that function `place_piece()`? – vishes_shell Nov 30 '16 at 07:11

1 Answers1

1

As it is written now the code posted shouldn't be working. The two methods in Board, place_piece and display_board, need to accept 'self' as a first argument.

Assuming this is a typo here's what's happening. The class Board is created with a class member spaces. Each time you're referencing self.spaces in an object, it is not found in the object is instead looked up in the class. Meaning you're using the same dict for all object of the class Board. Instead, to create a regular member place the declaration in the init method as you do in the Player class:

class Board:
    EMPTY = "0"
    # Remove spaces = {}

    def __init__(self, size, letters):
        self.spaces = {}
        ...

Finally, since you're using Python 2x please let me encourage you to always use new style classes (i.e. write class Board(object)). See What is the difference between old style and new style classes in Python?

Community
  • 1
  • 1
agrinh
  • 1,835
  • 2
  • 10
  • 11
  • 1
    No $^#^$@ way. It was the `__init__():` issue. NEVER ONCE has that been told to me. If I ever meet you in real life, I'm buying you a pizza. My hat will forever stay off to you. – C-Love511 Nov 30 '16 at 17:15
  • Haha, I'm glad I could help! – agrinh Dec 02 '16 at 14:00