02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

49 }else{<br />

50 flip[6] = false;<br />

51 }<br />

52 if (scanBoard(row, col, piece, opponent, -1, 1)){<br />

53 legal = true;<br />

54 flip[7] = true;<br />

55 }else{<br />

56 flip[7] = false;<br />

57 }<br />

58<br />

59 return legal;<br />

60 }<br />

Listing 16.5<br />

As you can see from Listing 16.5 the meat of the legal move checker is the function ‘scanBoard’.<br />

Listing 16.6 shows the function in full. Notice how parameters five and six are used in a repeat<br />

loop checking for the existence of an opponent’s piece. There must be at least one piece so if none<br />

is found the function returns false. If there is an opponent’s piece then the values for ‘incX’ and<br />

‘incY’ are added repeatedly until the condition fails. At this point we check that the end of the<br />

line contains a player’s piece; if this is true then this direction contains a line that is suitable.<br />

1 function scanBoard(row, col, piece, opponent, incX, incY){<br />

2 var x, y, name;<br />

3<br />

4 x = col + incX;<br />

5 y = row + incY;<br />

6 name = "board" + y + "_" + x;<br />

7<br />

8 //Must be at least one<br />

9 if (eval(name)._currentframe != opponent) return false;<br />

10<br />

11 while(eval(name)._currentframe==opponent){<br />

12 x += incX;<br />

13 y += incY;<br />

14 name = "board" + y + "_" + x;<br />

15 }<br />

16<br />

17 //Next piece must be a piece frame<br />

18 if (eval(name)._currentframe == piece) return true;<br />

19<br />

20 return false;<br />

21 }<br />

Listing 16.6<br />

250

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!