Simulating captured pieces
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Simulating captured pieces
Hey everyone.
I'm trying to create a game called Fanorona, and it has a strange way of capturing pieces. Basically, if a black piece moves into an open space, then all the white pieces in the direction it moved in(I.E upwards in a straight line, left or right in a straight line or diagonally)will be captured as long as no other black piece is in the way. So far the game can handle pieces moving into blank spaces, but nothing beyond that. I've got a 2D array with records that stores the position of each piece regarding the column and row it's in, and I think I could use this to possibly get the capturing working for horizontal and vertical movements at least, but so far haven't had any success.
I'm wondering if anyone here could help point me in the right direction with this and help get the ball rolling. Thanks.
I'm trying to create a game called Fanorona, and it has a strange way of capturing pieces. Basically, if a black piece moves into an open space, then all the white pieces in the direction it moved in(I.E upwards in a straight line, left or right in a straight line or diagonally)will be captured as long as no other black piece is in the way. So far the game can handle pieces moving into blank spaces, but nothing beyond that. I've got a 2D array with records that stores the position of each piece regarding the column and row it's in, and I think I could use this to possibly get the capturing working for horizontal and vertical movements at least, but so far haven't had any success.
I'm wondering if anyone here could help point me in the right direction with this and help get the ball rolling. Thanks.
-
- VIP Livecode Opensource Backer
- Posts: 7230
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Simulating captured pieces
You'll need to loop through all the elements of the array and see which ones match the current horizontal or vertical coordinates (depending on which axis you're dealing with.) If the element's coordinate matches, check whether the direction is correct (the coordinate is greater than or less than the starting point) and if that matches, look at its color. If that matches, take action.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Simulating captured pieces
Thanks for the reply, but it's not actual movement I'm using here, so I'm not sure if that would still apply; for example, if a black piece is selected and then a blank one, the blank one changes skin to black, while the black one swaps to black, so it's not actual movement but it looks like it. I use custom properties to change these, so if the 'ispieceblack' changes from false to true and the 'ispieceblank' changes to false, then the skin changes from blank to black etc. I'm preferably looking for a way to extend the custom property/skin changes from just one piece to several pieces in a move.
Re: Simulating captured pieces
You must know which direction the piece moved, from the change in co-ordinates (array row & column) differential.
You are switching between black state and blank state for start position, for example (2,2) and blank state and black state for new position, eg (2,3). StartX can therefore be 2 and StartY can be 2. CurrentX can be 2 and CurrentY can be 3. CurrentX-StartX = 0 so you can have DeltaX = 0. CurrentY-StartY = 1 so you can have DeltaY = 1. Repeat a loop, adding DeltaX and DeltaY to the CurrentX and CurrentY co-ordinates, checking the state of the new CurrentX and CurrentY location and turning white to black, until you reach another co-ordinate with a black state, or the edge of the board. DeltaX and DeltaY can both be non-zero, which will test the direction diagonally. One or other of the Deltas can be negative, which will test the direction left or up opposed to right or down.
You are switching between black state and blank state for start position, for example (2,2) and blank state and black state for new position, eg (2,3). StartX can therefore be 2 and StartY can be 2. CurrentX can be 2 and CurrentY can be 3. CurrentX-StartX = 0 so you can have DeltaX = 0. CurrentY-StartY = 1 so you can have DeltaY = 1. Repeat a loop, adding DeltaX and DeltaY to the CurrentX and CurrentY co-ordinates, checking the state of the new CurrentX and CurrentY location and turning white to black, until you reach another co-ordinate with a black state, or the edge of the board. DeltaX and DeltaY can both be non-zero, which will test the direction diagonally. One or other of the Deltas can be negative, which will test the direction left or up opposed to right or down.
-
- VIP Livecode Opensource Backer
- Posts: 7230
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Simulating captured pieces
SparkOut's description is more detailed than mine and may give you a better idea. I wasn't talking about movement, only about determining which squares meet the criteria for change. When I said "take action", in this case, the action would be to change the custom property's color setting.
Basically you need to loop through the elements of your array and determine which locations are appropriate for change. SparkOut's description tells you more specifically how to do that.
Basically you need to loop through the elements of your array and determine which locations are appropriate for change. SparkOut's description tells you more specifically how to do that.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Simulating captured pieces
Thanks for the replies. I'll try your suggestions and see what happens from there. I've not used code like this before so I apologize if I'm acting like a noobie.
I feel I should have uploaded this from the beginning, but here's how the 2Darray I mentioned for my "Reset" button is structured:
I feel I should have uploaded this from the beginning, but here's how the 2Darray I mentioned for my "Reset" button is structured:
Code: Select all
on boardReset
put 1 into turn
put empty into var_piece_1
put empty into var_piece_2
put "Black Piece.png" into fanorona_board[1][1][name_]
put "black" into fanorona_board[1][1][colour]
put 1 into fanorona_board[1][1][column]
put 1 into fanorona_board[1][1][row]
put "Black Piece.png" into fanorona_board[1][2][name_]
put "black" into fanorona_board[1][2][colour]
put 2 into fanorona_board[1][2][column]
put 1 into fanorona_board[1][2][row]
--continue this for all 25 pieces
set the icon of button "button 1" to fanorona_board[1][1][name_]
set the ispieceblack of button "button 1" to true
set the ispieceblank of button "button 1" to false
set the ispiecewhite of button "button 1" to false
--continue this for the remaining pieces, resetting the custom properties and therefore the skins as well.
]
Re: Simulating captured pieces
Thanks again for the suggestions. I've produced some code to try and implement it. It's not worked so far, so I'd really appreciate it if anyone could help me refine this and iron out the faults?
Code: Select all
if the ispieceblack //status of a piece as black of var_piece_1 //the first piece clicked and the one to be moved is true and the ispieceblank of var_piece_2 //the 'space' for var_piece_1 to 'move' to is true then
repeat until var_colour = "Black"
put fanorona_board[3][2][row] into startX // [3][2] and [3][3] represent specific pieces and are stand ins until I can change it to work with var-piece 1 and 2
put fanorona_board[3][2][column] into startY
put fanorona_board[3][3][row] into currentX
put fanorona_board[3][3][column] into currentY
subtract currentX from startX
put the result into deltaX
subtract currentY from startY
put the result into deltaY
if currentX + deltaX > currentX or currentY + deltaY >currentY or currentX + deltaX < currentX or currentY + deltaY <currentY then
put currentX + deltaX into currentX
put currentY + deltaY into currentY
put fanorona_board[currentX][currentY][colour] into var_colour
if var_colour = "White" then
set the ispiecewhite of fanorona_board[currentX][currentY] to false
set the ispieceblack of fanorona_board[currentX][currentY] to true
end if
end if
end repeat
end if
Re: Simulating captured pieces
Hi Mokima,
looks like you are trying to set custom properties of VARIABLES? Won't work!
Only Livecode objects can have custom properties!
Best
Klaus
Code: Select all
...
set the ispiecewhite of fanorona_board[currentX][currentY] to false
set the ispieceblack of fanorona_board[currentX][currentY] to true
...
Only Livecode objects can have custom properties!
Best
Klaus
Re: Simulating captured pieces
Oh, another erm.. inconvenience
...
subtract currentX from startX
put the result into deltaX
...
Subtraction MAY have a result, but probably not what you will exspect!
Livecode isn't THAT english-like
Look up "result" in the dictionary!
This will work, but also OVERWRITE the value of startX:
...
subtract currentX from startX
put startX into deltaX
...
If you need to keep the actual value of startX for later use or whatever, you should do something like this:
...
put startX - currentX into deltaX
...
get the picture?
Best
Klaus
...
subtract currentX from startX
put the result into deltaX
...
Subtraction MAY have a result, but probably not what you will exspect!
Livecode isn't THAT english-like
Look up "result" in the dictionary!
This will work, but also OVERWRITE the value of startX:
...
subtract currentX from startX
put startX into deltaX
...
If you need to keep the actual value of startX for later use or whatever, you should do something like this:
...
put startX - currentX into deltaX
...
get the picture?
Best
Klaus
Re: Simulating captured pieces
Hey Klaus, thanks for your quick reply. I've fixed the errors you pointed out, but when I try to 'move' a black piece to an open space, the program still stops responding altogether. I'm not sure what about it is causing the program to crash.
-
- VIP Livecode Opensource Backer
- Posts: 7230
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Simulating captured pieces
Another problem is here:
You can't intersperse comments and code like that. Change it to:
and then put your comments on a different line, or after the "then". The functional code of that line has to end with "then". Everything after the first comment marker "//" will be ignored.
Code: Select all
if the ispieceblack //status of a piece as black of var_piece_1 //the first piece clicked and the one to be moved is true and the ispieceblank of var_piece_2 //the 'space' for var_piece_1 to 'move' to is true then
Code: Select all
if the ispieceblack is true and the ispieceblank of var_piece_2 is true then
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Simulating captured pieces
Thanks for the quick reply Jacque. In my program, that isn't how it's displayed, I simply put the comments in to make it absolutely clear what the variables/custom properties were to avoid any confusion. I'm sorry if it came across the wrong way.
Re: Simulating captured pieces
In case anyone would need it, here's the code I'm working with to try and get the capturing working, but causes livecode to stop responding.
Code: Select all
on capturingmove
repeat until fanorona_board[currentX][currentY][name_] = "Black Piece.png"
put fanorona_board[var_piece_1][row] into startX
put fanorona_board[var_piece_1][column] into startY
put fanorona_board[var_piece_2][row] into currentX
put fanorona_board[var_piece_2][column] into currentY
put startX - currentX into deltaX
put startY - currentY into deltaY
if currentX + deltaX > currentX or currentY + deltaY >currentY or currentX + deltaX < currentX or currentY + deltaY <currentY then
put currentX + deltaX into currentX
put currentY + deltaY into currentY
if fanorona_board[currentX][currentY][name_] = "White Piece.png" then
put "Blank.png" into fanorona_board[currentX][currentY][name_]
set the icon of fanorona_board[currentX][currentY] to fanorona_board[currentX][currentY][name_]
put 1 into arr_int_white
put arr_int_white into field "Whites"
else
if fanorona_board[currentX][currentY][name_] = "Blank.png" then
exit to top
else
end if
end if
end if
end repeat
end capturingmove
Re: Simulating captured pieces
Hi Mokima,
1. are you sure that -> fanorona_board[currentX][currentY][name_] is REALLY "Black Piece.png" at some time?
Sounds like the script gets stuck in the repeat loop, if LC stops reponding!
Using REPEAT UNTIL XYZ IS in fact a dangerous thing
2. Please hit the TAB key from time to time when in the script editor and see hwo the current handler will nicely INDENT!
See below for your script, but indented and thus WAY better readable
3. Some more general hints:
If you do not have an ELSE case in your IF THEN, just leave out the ELSE!
If the keys of your array are NOT variables, use QUOTES around them!
Not sure if this is the case here, but anyway:
Best
Klaus
1. are you sure that -> fanorona_board[currentX][currentY][name_] is REALLY "Black Piece.png" at some time?
Sounds like the script gets stuck in the repeat loop, if LC stops reponding!
Using REPEAT UNTIL XYZ IS in fact a dangerous thing
2. Please hit the TAB key from time to time when in the script editor and see hwo the current handler will nicely INDENT!
See below for your script, but indented and thus WAY better readable
3. Some more general hints:
If you do not have an ELSE case in your IF THEN, just leave out the ELSE!
Code: Select all
...
if fanorona_board[currentX][currentY][name_] = "Blank.png" then
exit to top
## else
end if
...
Not sure if this is the case here, but anyway:
Code: Select all
...
put fanorona_board[var_piece_1]["row"] into startX
put fanorona_board[var_piece_1]["column"] into startY
put fanorona_board[var_piece_2]["row"] into currentX
...
Klaus
Code: Select all
on capturingmove
repeat until fanorona_board[currentX][currentY][name_] = "Black Piece.png"
put fanorona_board[var_piece_1][row] into startX
put fanorona_board[var_piece_1][column] into startY
put fanorona_board[var_piece_2][row] into currentX
put fanorona_board[var_piece_2][column] into currentY
put startX - currentX into deltaX
put startY - currentY into deltaY
if currentX + deltaX > currentX or currentY + deltaY >currentY or currentX + deltaX < currentX or currentY + deltaY <currentY then
put currentX + deltaX into currentX
put currentY + deltaY into currentY
if fanorona_board[currentX][currentY][name_] = "White Piece.png" then
put "Blank.png" into fanorona_board[currentX][currentY][name_]
set the icon of fanorona_board[currentX][currentY] to fanorona_board[currentX][currentY][name_]
put 1 into arr_int_white
put arr_int_white into field "Whites"
else
if fanorona_board[currentX][currentY][name_] = "Blank.png" then
exit to top
end if
end if
end if
end repeat
end capturingmove
Re: Simulating captured pieces
Thanks for your quick reply Klaus. Yes, Fanorona_board[currentX][currentY][name_] should at some point become Black Piece.png. The name of the blank button in the 2D array will be 'Blank.png' for example, while White and Black ones are 'White Piece.png' and 'Black Piece.png' respectively, so sooner or later it will find a piece with the name "Black Piece.png". I'll make sure to indent from now on too, thanks! And yes, the keys of the array are variables that hold each element(Row holds the row the piece is in, colour the colour etc.) within them.