0

I am making a chess game and need to select a square and then move the piece on that square to a square of my choice.

I can correctly click on a square and found out what piece I've clicked on, but struggle on how to then set the pieces to the coordinates of the square selected after.

Each piece's position is based on a variable named pawn8whitepos, or queen1blackpos etc, where its value will be [80,240] etc. In my code to identify the piece, I made a function that only returns either pawn8white, or queen1black, etc.

I'd obviously then like to put a "pos" onto the end of that, so I can then do pawn8whitepos = mousecoords.

I've attempted this:

    piecetobemoved = str(whatpiece(x)+"pos")
    #whatpiece() is a function that returns the piece on a square when given the square.                       
    piecetobemoved = mousecoords2

But this just makes the variable piecetobemoved into say [240,60], and not pawn8blackpos into [240,60]

How can I use string manipulation to get the pos variable for the piece I've identifed, so I can then set the pieces position to mousecoords?

I know I could create 32 if statements like:

    if whatpiece(x) == "pawn7black":
         pawn7blackpos = mousecoords

But I'd like a more elegant solution if possible

EDIT

All relevant code can be found here, if I've posted this wrong please say as its my first time using github https://github.com/Fred330/Chess/commit/824bc8a57fcc4a173afcc28eb8b19eb664fcac0f

Fred B
  • 99
  • 6
  • you should hold your state in a 2d 8x8 array ... – Joran Beasley Aug 16 '20 at 17:10
  • Are you searching for [`eval`](https://docs.python.org/3/library/functions.html#eval)? – Rabbid76 Aug 16 '20 at 17:12
  • It may be easier if you post your code so we can see what's happening. – Mike67 Aug 16 '20 at 17:17
  • @Rabbid76 Considering I've never heard of eval and your link doesn't seem to ring a bell for my code I don't think so. – Fred B Aug 16 '20 at 17:19
  • @Mike67 I've added a github link – Fred B Aug 16 '20 at 17:23
  • 1
    As @Rabbid76 said (and the moderator provided link 'Using a string variable as a variable name') you can use `eval` to achieve what you are asking. However that is actually not the best idea. A cleaner/better approach is to rework your code and use either a dictionary to hold the data (which would allow you to access the information in the dict using the return value of `whatpiece(x)`) or probably the best choice would be to define a class to hold the information. `whatpiece(x)` would return a reference to the class instance that was at that position, not its stringname. – Glenn Mackintosh Aug 16 '20 at 18:07
  • @GlennMackintosh Would this then be as simple as dict1 = { pawn1blackpos : pawn1black ... }etc for all the pieces? – Fred B Aug 16 '20 at 18:16
  • Again, I would recommend using the class approach, but the dict approach would be something like `piece_positions = {'pawn1black': XXX, ..., 'pawn8white': YYY, 'queen1black': ZZZ}`. Where XXX, YYY, ZZZ are the intial positions. Change the position by using `piece_positions['pawn1black'] = BBB` – Glenn Mackintosh Aug 16 '20 at 18:31

0 Answers0