objective c - How to traverse peg through all the squares in a Monopoly type Game in iOS -


how traverse peg through squares in monopoly type board game ?

i have written function movepegbutton on click moves peg destination position gets random generated number.

this have written far move peg button.

(ibaction)movepegbutton:(id)sender {     self.pegdestinationpositionindex = self.pegcurrentpositionindex + self.randomnumber;     if (self.pegdestinationpositionindex > [self.boardcordinatesarray count] - 1)     {          self.pegdestinationpositionindex = self.pegdestinationpositionindex - [self.boardcordinatesarray count];     }     [self animatepeg];     self.pegcurrentpositionindex = self.pegdestinationpositionindex; } 

this have written animate peg.

(void)animatepeg {     int destinationxcord = [[[self.boardcordinatesarray objectatindex:self.pegdestinationpositionindex] objectforkey:@"x"]intvalue];     int destinationycord = [[[self.boardcordinatesarray objectatindex:self.pegdestinationpositionindex] objectforkey:@"y"]intvalue];     [uiview animatewithduration:1.0 animations:^{             self.peg.center = cgpointmake(destinationxcord, destinationycord);     }]; } 

so far peg moves correctly destination square not traversing through squares in way, e.g. if 8x8 square, first run dice rolls 6, peg moves correctly 6th square, second run dice rolls 5, peg moves correctly destination square directly jumps on position diagonally, doesn't traverse squares has in way.

i stuck here, how should ??

so peg supposed walk in square, ei. follow edge of board, instead cuts corners, right?

if so, answer straight forward. make sure peg follows squares lain on board, must make move every square in path source square destination square, otherwise cut corner , go straight destination square.

if player rolls 4, , standing on square 9 , must reach square 13. instead of making peg move directly square 13, force peg go first 10, 11, 12 , @ last square 13.
of course if squares aligned in square manor, is; 4 edges, optimization done, , split path if corner in between source square , destination square.
, of course optimization applied though squares not aligned in square manor, long follows path, , know 'corners' are.

edit:
the described 3 cases
try , test paint skills , try , visualize problem @ hand, have created within paint.

what doing in presented code case 1. doing single animation when moving peg. want case 2, here function -(void)animatepeg; should recognize destination square more 1 square away, , therefore not single move animation, series of move animations, more precise, number of animations indexofdestinationsquare - indexofsourcesquare. in illustrated case, peg should total of eight move animations instead of one.

case 3 shows how process optimized recognizing direction of move, , split when going around corner.

i hope clarifies approach take. approach need extend -(void)animatepeg; function able split single move series of moves, , animate moves one-by-one.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -