For each ImageSprite in List... how do I capture opposite color sprites in chess game?

Hi everyone, I'm new to forum and I searched a lot before asking here.
I have this situation: a chessboard with two groups of sprites, the 16 Whites and the 16 Blacks. The aim of this chessboard is only putting the pieces on the board and study the moves, freely moving the pieces on the board without constraints for the actual permitted movement of each pieces (like in the real chessboard when you don't know the rules you can move every piece wherever you want, ignoring for instance that the Pown can move only one cell at once). So it's quite easy, I managed to set Touch Down, Touch up and Drag for every Sprite and also they perfectly snap to the grid so all is fine but... of course I would like that a captured piece could automatically go in the side area. The desired behaviour would be:
for each black sprite, when it takes the place of a white one, that white sprite will go to the side area and viceversa. I can't use the collide function because when you drag a pieces you certainly will touch all the sprites in your path. So I obtained this desired behaviour coding each of the 32 sprites capturing each of the 16 opponents (when the black rock touch up, if its coordinates are equal to the white bishop, then the black rock snap to the position and the white bishop goes to the side area and so on for each sprite).

What I ask you is: is there a way to change my code in DRY (don't repeat yourself)? thank you!!!!
Romina from Italy

1 Like

Looks already nice :+1:

Changed the name of the sprites a bit

wR17,wN18, ... - w17, w18, ...

1 Like

WOW!
I'll let you know if I'm able to make it work! Thank you a lot
Romina

1 Like

:blush: :smiley:

1 Like

Here is a project with drag and dropped Sprites and snap to grid, with sprite cannibalism avoidance, using DRY.

Coincidentally, last night I was working on adding pawn promotion to a button based Chess board (no rules yet.)
chess_board_buttons.aia (16.2 KB)

2 Likes

Amazing, thank you guys, I'll be back soon for a feedback

1 Like

Hello MintArchit, this could be the most difficult answer of my life because I'm not a programmer and maybe I can't make me understood... but first of all thank you for having flipped my view of the problem. I was tring to send to the trash a random white piece (how to call a SpriteNumber.MoveTo if Number is a variable?) instead of focusing on the Sprite1.MoveTo the trash when a list of Sprites takes its position.
Now the question is: have I to insert your clean code inside a parent procedure so that it will be clear that the black piece b1 is the one intended to be sent to the trash? I mean... what if is the black b1 dragged and touched up in the white piece position? In that case I don't want it to be trashed. How to distinguish who is captured and who is the capturer? Thank you for any kind reply

And for ABG: I tried to import your .aia file but I can't see the code inside appinventor. Thank you for sharing anyway EDITED Now I see the code in the other screen!!! Sorry and thank you, I will study it

Bye Romina

1 Like
  • You need to have a way to look up what piece (if any) is on a square given the square coordinates. (I use Button.Text for my app, you might use Sprite Picture)
  • You also need to keep 2 variables:
    • which square did this move start at?
    • which square did this move end at?
  • You will need to remember which piece you are moving until you land on the new square, and dispose of the captured piece before you land fully on the square.
1 Like

Great! I will try and let you know. So grateful. I will share my .aia when it will be a little better looking LOL

1 Like

Here I am after two days of trying to figure out.
This is what I realized (referred to MintArchit QuickSketch): the active sprite b1 (black Tower), when released (es. b1.TouchUp), can't be sended to the Trash when a random wNumber stand in the same position because b1 arrives there and it is the capturer. On the other hand I don't know which Event regarding a standing piece could call a MoveTo_trash_myself procedure (if this Event exists you can avoid to read further and please let me know which is).

Let's see the opposite situation (see .aia attached) in which the white tower w17 captures any black piece in its same position when w17.Touch.Up (in my .aia w17 is the only one that snap to the grid, the other white pieces can move but don't snap in one precise position, I haven't coded it yet).
I tried both "for_each_item_in_BlackList" and "for_each_number_from_1_to_16" to send any black sprite of the selected list to the trash if their positions match with w17 position but it doesn't work. I attach my .aia StudioChessboard.aia (179.8 KB) where you can find the w17.TouchUp well visible in the middle (see the picture).
I don't expect a solution, I am sadly convinced to code each sprite combination, but if you guys whant to lose your time with my experiments please let me know your findings.

1 Like

I see you fell into the trap of making individual event blocks for your sprites.
That leaves a big mess to clean up when you have to change your logic.
Using generic events, parametrized procedures, and lists of sprites by piece type lets you consolidate your logic into one place, making coding easier.

chess_board_buttons.aia (16.2 KB)

1 Like

OMG! That's a lot of material to study :smiley:
but I'm committed and eager to learn! Thank you so much, I'll be back soon with a perfect project to share :slight_smile:

1 Like

Dear @ABG,
I owe to you my deeper understanding of anyImageSprite method and now I'm so prowd of my project. But I can't go further due to my lack of knowledge base. It is so difficult!

Above of all I want to thank you for your coding "grammar", I appreciated the way your code is self-explaining and my next step is to insert all those bookmarks inside my code.

As for my result, I let you discover yourself attaching my .aia file. You will notice that white pieces can "eat" black ones but the b16 Pown, the only one I left without instruction for capturing white pieces. I let it that way to understand better the behaviour of my code. In fact I'm not able to set the anyImageSprite.TouchUp method for both the events (white capuring blacks and viceversa) but I'm fine now, I can re-start from this little result.
Thank you all! Bye bye Romina StudioChessboard_1_0.aia (182.6 KB)

There's no shame in restarting with a new approach, as long as you have learned something.

Here's an interesting computer chess article for you ...


also

2 Likes

Hi everyone, I want you to know how precious was your help because my little app is now in GooglePlay, ad-free and in development. So exciting!
I don't understand each phase, they ask me for beta release, alpha release, production release, testers.... but step by step I learn and improve.

This whole story started with me searching for a free app to study chess on my own without automatic moves and above of all ad-free, so I hope that it could be of some help for someone.

If anyone want to be a tester I will appreciate any suggestion. I don't know if I can post the link of the Google Play Store here so let me know if you can find my app by a simple search on the store: mystudiochessboard.
Bye bye Romina

2 Likes


It looks nice, and as advertised, it imposes no rules.

Does it have a way to promote pawns to Queens?

Ty @ABG but no. I'm not so good at coding but I'll try to improve and to implement that feature!

1 Like