#🔒 Data Structure help

48 messages · Page 1 of 1 (latest)

drifting locust
#

i have a dictionary board:
board={
"h2":"WP"
}
this represents a chessboard to be specific
so each key is a square. the value is a piece.
for each piece, it has different points on different squares.
how can i like create/use a data structure so that if i have smth like this:
for square in board:
i get the points of the piece on that square

wide moonBOT
#

@drifting locust

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

drifting locust
#

scores={"WP":{'a8': 0, 'b8': 0, 'c8': 0, 'd8': 0, 'e8': 0, 'f8': 0, 'g8': 0, 'h8': 0, 'a7': 50, 'b7': 50, 'c7': 50, 'd7': 50, 'e7': 50, 'f7': 50, 'g7': 50, 'h7': 50, 'a6': 10, 'b6': 10, 'c6': 20, 'd6': 30, 'e6': 30, 'f6': 20, 'g6': 10, 'h6': 10, 'a5': 5, 'b5': 5, 'c5': 10, 'd5': 25, 'e5': 25, 'f5': 10, 'g5': 5, 'h5': 5, 'a4': 0, 'b4': 0, 'c4': 0, 'd4': 20, 'e4': 20, 'f4': 0, 'g4': 0, 'h4': 0, 'a3': 5, 'b3': -5, 'c3': -10, 'd3': 0, 'e3': 0, 'f3': -10, 'g3': -5, 'h3': 5, 'a2': 5, 'b2': 10, 'c2': 10, 'd2': -20, 'e2': -20, 'f2': 10, 'g2': 10, 'h2': 5, 'a1': 0, 'b1': 0, 'c1': 0, 'd1': 0, 'e1': 0, 'f1': 0, 'g1': 0, 'h1': 0},

ive used this before but i feel like its too inefficient

#

this way i was able to get the square, and then write like the points as score[board[square]][square]

#

this is gonna happen millions of times so i need like a fast way to do it

copper terrace
#

you can make another dictionary containing the scores of a piece

#

lemme write out an example 1 sec

drifting locust
#

one dictionary for one piece?

copper terrace
#

!e

piece_scores = {'WP': 1, 'BP': 1, 'WQ': 9, 'BQ': 9,}  # I'm not writing out the whole thing for now

board = {'a1': 'WP', 'b1': 0, 'c1': 'BQ'}

for square, piece in board.items():
    if piece != 0:  # ensure square isn't empty
        score = piece_scores[piece]
        print("score of", piece, "is", score)
wide moonBOT
drifting locust
#

i dont

#

heh

copper terrace
#

you dont what

drifting locust
#

so uh

#

pieces have base scores yes

#

but for like more precise calculation of the best move

#

it then looks at how optimally the piece is placed

#

so the points for a piece are its base value +

#

its score for that square

#

and they have different scores for diff squares

copper terrace
#

hm yes i know what you're talking about

drifting locust
#

and then i could reference it as a key in the dictionary and then the square its on in the nested dictionary

#

but now that im trying to make like an efficient code i feel like this is prob not good :P

copper terrace
drifting locust
#

should i just have a bunch of if conditions and a dictionary for each piece?

drifting locust
#

integers

copper terrace
#

ohh omg i see it it

drifting locust
#

i couldnt understand that rn

copper terrace
#

sorry i misinterpreted your question 😭

drifting locust
#

oh np

copper terrace
drifting locust
#

write is fine ive written it already :P

#

it isnt slow to run? :O

copper terrace
#

nope

drifting locust
#

o ohk

copper terrace
#

dictionaries (which are really just hash maps) are blazingly fast to look up

drifting locust
#

ohkk

#

tyyy

#

:]

copper terrace
#

no probs

#

!close if you don't have anymore questions

drifting locust
#

ye ill do that

#

ty again :]

#

!close

wide moonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.