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
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
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.
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.
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.
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)
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