0

Trying to make a chess AI. The board is a dictionary with keys a-h, and each key is assigned to an empty list. Essentially this is an 'empty chessboard.'

# Create Pieces
'''
For 'alive', 
    - True is ALIVE
    - False is DEAD

For 'team',
    - True is WHITE
    - False is BLACK
'''


class pawn:

    def __init__(self, position, team, alive=True, value=10, name='pawn'):
        self.value = value
        self.name = name
        self.position = position
        self.alive = alive
        self.team = team

    def __str__(self):
        return self.name

    def __repr__(self):
        return ('white ' if self.team else 'black ') + self.name

    def __where__(self):
        return self.position


class bishop:

    def __init__(self, position, team, alive=True, value=30, name='bishop'):
        self.value = value
        self.name = name
        self.position = position
        self.alive = alive
        self.team = team

    def __str__(self):
        return self.name

    def __repr__(self):
        return ('white ' if self.team else 'black ') + self.name

    def __where__(self):
        return self.position


class knight:

    def __init__(self, position, team, alive=True, value=30, name='knight'):
        self.value = value
        self.name = name
        self.position = position
        self.alive = alive
        self.team = team

    def __str__(self):
        return self.name

    def __repr__(self):
        return ('white ' if self.team else 'black ') + self.name

    def __where__(self):
        return self.position


class rook:

    def __init__(self, position, team, alive=True, value=50, name='rook'):
        self.value = value
        self.name = name
        self.position = position
        self.alive = alive
        self.team = team

    def __str__(self):
        return self.name

    def __repr__(self):
        return ('white ' if self.team else 'black ') + self.name

    def __where__(self):
        return self.position


class queen:

    def __init__(self, position, team, alive=True, value=90, name='queen'):
        self.value = value
        self.name = name
        self.position = position
        self.alive = alive
        self.team = team

    def __str__(self):
        return self.name

    def __repr__(self):
        return ('white ' if self.team else 'black ') + self.name

    def __where__(self):
        return self.position


class king:

    def __init__(self, position, team, alive=True, value=900, name='king'):
        self.value = value
        self.name = name
        self.position = position
        self.alive = alive
        self.team = team

    def __str__(self):
        return self.name

    def __repr__(self):
        return ('white ' if self.team else 'black ') + self.name

    def __where__(self):
        return self.position


# Create the board

board = {}

lst = []
for i in range(8):
    lst.append('')
for i in range(8):
    lst[1] = pawn([chr(97 + i), 2], True)
    lst[6] = pawn([chr(97 + i), 7], False)
    board[chr(97 + i)] = lst

board['a'][0] = rook(['a', 1], True)

where rook is a class that, in this instance, created a rook with location a1, and true means it's a white piece.

however, my output is such. White pawns and black pawns worked fine, but every single 0 element became that, and if I tried to put a white bishop in a2, all white rooks are replaced with white bishop.

What's wrong? EDIT: I just found out that every pawn is the exact same, so the same error is happening there too

{'a': [white rook, white pawn, '', '', '', '', black pawn, ''], 'b': [white rook, white pawn, '', '', '', '', black pawn, ''], 'c': [white rook, white pawn, '', '', '', '', black pawn, ''], 'd': [white rook, white pawn, '', '', '', '', black pawn, ''], 'e': [white rook, white pawn, '', '', '', '', black pawn, ''], 'f': [white rook, white pawn, '', '', '', '', black pawn, ''], 'g': [white rook, white pawn, '', '', '', '', black pawn, ''], 'h': [white rook, white pawn, '', '', '', '', black pawn, '']}
  • 1
    What does your question have to do with [tag:css]?? – costaparas Jan 02 '21 at 04:10
  • 1
    Also, there isn't enough code in the question to wok out what the error could be. You need to [edit] your question with at least some of your code - to give a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – costaparas Jan 02 '21 at 04:14
  • sorry, im new to stack overflow and python overall. I'll remove the tag and revise the question – Syed Shakir Jan 03 '21 at 01:11
  • `lst` is the *same* list each time in `board[chr(97 + i)] = lst`. – mkrieger1 Jan 03 '21 at 01:22
  • you only have one list and give the same list to each key in board. so row B points to the same list as row D. so changing an element in the list onROW B will change that in all rows since they are all looking at the same list. – Chris Doyle Jan 03 '21 at 13:28

0 Answers0