Skip to first unread message

Will Casey

unread,
Nov 7, 2017, 3:48:58 PM11/7/17
to mitappinv...@googlegroups.com
I'm currently making a chess game that works by storing where each piece is within a 2d list and after changing the list updating the position of the sprites. The problem im running into is that within my 2d list whenever I use "index in list" to find its index I only ever get 0. I'm curious if there is a way to find the index of something within a 2d list.

(a piece of TWODListStorage)
--it is too big too show the whole 8 by 8 list

















Here is the relevant part of the 2d list that contain the starting position of the  white chess pieces





When this is run I want to find the "Y" index (the first list) and the "X" index (the index of the list within the list). I attempt to do this in the procedure "FindPiece", but despite searching for something you can see is in the list, both XStorPLace and YStorePlace return 0.

Evan Patton

unread,
Nov 7, 2017, 4:27:34 PM11/7/17
to MIT App Inventor Forum
Hi Will,

The index in list block is not recursive. You would need to implement your own procedure where you would iterate over the outer list and then use index in list on the inner list to find the entry. You could then return a list of length 2 with the outer and inner indices to indicate the square on the board.

Cheers,
Evan

Abraham Getzler

unread,
Nov 8, 2017, 1:01:12 PM11/8/17
to MIT App Inventor Forum
Just because the board appears to your eyes as a two dimensional table
does not mean you are forced to store it as a table.

board coordinates as single text values.

Consider alternate ways to represent the game state internally.
Some might ease the coding considerably.

For example, you start the game with 32 pieces, 16 white and 16 black.
That could be a collection of 32 item lists, each describing a different attribute of that piece:
  • its location, i.e. "d2" (""if off the board)
  • its type, i.e. "P"
  • its color, i.e. "W",
  • its image (♙) or codepoint (U+2659) or html (♙),
That set of 32 pieces is fixed through the game, but the states of those pieces will change.

These 4 parallel lists would work well with the list index-of and select blocks,
for all your needs, in one dimension.

It would answer questions like:
  • which piece is at d4?  (Find the index in location of "d4".  If > 0, then select type, color, image at that index.)
ABG

Will Casey

unread,
Nov 8, 2017, 6:38:27 PM11/8/17
to MIT App Inventor Forum
Thank you for the advice. It definitely seems to be both an easier and better way to do it. I'll give it a try.
Reply all
Reply to author
Forward
0 new messages