Wormlink class
The greedy snake is made up of a section. Because it often has some of the section a line forming section, this is a relatively effective way to save the entire snake. [X, y] indicates the coordinates of the section head, and then the head of the segment begins to follow the sections in the direction. (The head of the head and the tail of the snake is not a concept)
The following code segment is the segment attribute in Wormlink:
// segment head coordinates
Private int x, y;
// segment length
PRIVATE INT LEN;
// Mobile direction
Private byte dir;
The following focuses on several important functions:
l Public void DecreaseLength ()
This is a decrease from the head of the segment.
/ / The total length of the first segment is reduced by 1
Len -;
Switch (Dir) {// Different directions are different on the left
Case Worm.Left:
X--; BREAK;
Case Worm.right:
x ; Break;
Case Worm.UP:
Y--; BREAK;
Case Worm.Down:
Y ; Break;
l Public Boolean Contains (int x, int y)
Determine the coordinate [X, Y] is included in the segment
Switch (Dir) {// Different ways to judge the way is different
Case Worm.Left:
Return (Y == THIS.Y) && ((x <= this.x) && (x> = getEndx ())))))))))
Case Worm.right:
Return (Y == THIS.Y) && ((x> = this.x) && (x <= getEndx ())))))));
Case Worm.UP:
Return ((x == this.x) && (Y <= this.y) && (y> = getendy ())))))))
Case Worm.Down:
Return ((x == this.x) && ((y> = this.y) && (y <= getendy ()))))))))
}
l public int getndx ()
The tail X coordinate of this section (the last coordinate of the first grid pointing in the direction), when this paragraph is the head segment of the snake, the first coordinate of the head is the first coordinate.
// Different direction judgment methods are different
IF (DIR == Worm.Left)
Return X-Len;
IF (DIR == Worm.Right)
Return X Len;
Return X;
About the Author:
Shen Yachang, Senior Programmer, SCJP
Www.chenshen.com
Jinashen@benq.com
August 10, 2003