Simulating captured pieces

Creating Games? Developing something for fun?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Simulating captured pieces

Post by Mokima » Thu Jan 01, 2015 8:44 pm

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Simulating captured pieces

Post by jacque » Fri Jan 02, 2015 8:09 pm

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

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Re: Simulating captured pieces

Post by Mokima » Sat Jan 03, 2015 3:44 pm

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.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Simulating captured pieces

Post by SparkOut » Sat Jan 03, 2015 6:29 pm

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Simulating captured pieces

Post by jacque » Sat Jan 03, 2015 7:57 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Re: Simulating captured pieces

Post by Mokima » Sat Jan 03, 2015 10:06 pm

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:

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

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Re: Simulating captured pieces

Post by Mokima » Sun Jan 04, 2015 5:01 pm

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

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Simulating captured pieces

Post by Klaus » Sun Jan 04, 2015 5:28 pm

Hi Mokima,

Code: Select all

...
set the ispiecewhite of fanorona_board[currentX][currentY] to false
set the ispieceblack of fanorona_board[currentX][currentY] to true
...
looks like you are trying to set custom properties of VARIABLES? Won't work!
Only Livecode objects can have custom properties!

Best

Klaus

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Simulating captured pieces

Post by Klaus » Sun Jan 04, 2015 5:33 pm

Oh, another erm.. inconvenience 8)
...
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 :D
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

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Re: Simulating captured pieces

Post by Mokima » Sun Jan 04, 2015 8:46 pm

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Simulating captured pieces

Post by jacque » Sun Jan 04, 2015 11:18 pm

Another problem is here:

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
You can't intersperse comments and code like that. Change it to:

Code: Select all

if the ispieceblack  is true and the ispieceblank of var_piece_2 is true then
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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Re: Simulating captured pieces

Post by Mokima » Sun Jan 04, 2015 11:31 pm

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.

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Re: Simulating captured pieces

Post by Mokima » Tue Jan 06, 2015 6:32 pm

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

Klaus
Posts: 13821
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Simulating captured pieces

Post by Klaus » Tue Jan 06, 2015 6:50 pm

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 :D

2. Please hit the TAB key from time to time when in the script editor and see hwo the current handler will nicely INDENT! :D
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
...
If the keys of your array are NOT variables, use QUOTES around them!
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
...
Best

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

Mokima
Posts: 10
Joined: Tue Dec 30, 2014 8:08 pm

Re: Simulating captured pieces

Post by Mokima » Tue Jan 06, 2015 7:13 pm

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.

Post Reply

Return to “Games”