Analysis of Algorithm for Eating Snakes (2)

zhaozj2021-02-17  50

Analysis of Algorithm for Eating Snakes (2)

James @ www.chenshen.com

The following focuses on several methods in the WORM class: l Public void setdirection (byte direction) This method is used to change the direction of the greedy snake, only 90 degrees. Look at the implementation code below:

IF ((Direction! = CurrentDirection) &&! needupdate) {

// Remove the last element in the list (the head of the snake)

Wormlink SL = (Wormlink) Worm.lastelement ();

INT x = sl.getendx ();

INT Y = sl.getendy ();

// Different movement direction coordinates are different

Switch (direction) {

Case Up: // When this paragraph moves up

IF (CurrentDirection! = DOWN) {

Y--; Needupdate = true;

Break;

Case Down: // When this paragraph moves down

IF (CurrentDirection! = Up) {

Y ; Needupdate = true;

Break;

Case Left: // When this paragraph moves to the left

IF (CurrentDirection! = Right) {

x -; needupdate = true;

Break;

Case Right: // When this paragraph moves to the right

IF (CurrentDirection! = Left) {

x ; Needupdate = true;

Break;

// Need to update when the change direction

IF (NeeduPdate == true) {

Worm.AddeElement (New Wormlink (x, y, 0, direction);

CurrentDirection = Direction;}}

l Public Void Update (Graphics G) This function is updated to eat snake status. Each update increases the head, and the tail is reduced. If it eats the food tail segment, it will not be reduced. It seems like a whole snake.

// increase the greed of the snake head

HEAD = (WORMLINK) WORM.lastelement ();

Head.incadelength ();

// If you don't eat food, the tail is reduced.

IF (! Haseten) {

Wormlink tail;

Tail = (Wormlink) Worm.FirstErayment ();

Int tailx = tail.getx ();

Int taily = tail.gety ();

// If the length of the tail is 0 is 0

Tail.Decreaseledth ();

IF (tail.getlength () == 0) {

Worm.RemoveElement (tail);

// Tail reduction

g.setColor (WormPit.Rase_COLOUR);

Drawlink (G, Tailx, TAILY, TAILX, TAILY, 1);

} else {

// If you eat food, you don't delete the tail.

Haseten = false;

Needupdate = false;

/ / Confirm that it is in the boundary

IF (! Wormpit.isinbounds (Head.Getendx (), Head.Getendy ())) {

// If you don't, you will die the Throw New WormException ("over the Edge");

Headx = (byte) Head.Getendx ();

Heady = (byte) Head.getendy ();

/ / The head of the greedy snake increases

g.setcolor (WormPit.Draw_colour);

Drawlink (G, Headx, Heady, Headx, Heady, 1);

// Judgment whether you eat yourself

For (int i = 0; i

SL = (Wormlink) Worm.elementat (i);

IF (sl.contains (headx, heady)) {

Throw New WormException ("You ATE YourSelf");}}

l Void Drawlink (Graphics G, INT X1, INT Y1, INT X2, INT Y2, INT LEN) This function is used to draw a section of the snake, a complete snake is a piece of composite.

/ / Convert the length into pixel length

Len * = Wormpit.cell_size;

// (x1 == x2) Description This section is vertical

IF (x1 == x2) {

/ / Turn the X1 into pixel length

X1 * = Wormpit.cell_size;

/ / (Y2

IF (Y2

/ / Exchange the head, the left side of the left and transferred into pixels

Y1 = Y2 * WORPIT.CELL_SIZE;

} else {

/ / Turn Y1 into pixels

Y1 * = Wormpit.cell_size;}

G.FillRect (x1, y1, wormpit.cell_size, len);

} else {

// This is a horizontal section

Y1 * = Wormpit.cell_size;

IF (x2

/ / Exchange the head, the left side of the left and transferred into pixels

X1 = x2 * Wormpit.cell_size;

} else {

X1 * = Wormpit.cell_size;}

G.FillRect (x1, y1, len, wormpit.cell_size);}

l Public Void Paint (Graphics G) draws a complete greedy snake

Wormlink SL;

INT X1, X2, Y1, Y2;

Int Len;

For (int i = 0; i

// Take out each paragraph, then draw this, even a complete snake

SL = (Wormlink) Worm.elementat (i);

X1 = sl.getx (); x2 = sl.getendx ();

Y1 = sl.gety (); y2 = sl.getendy ();

Len = sl.getlength ();

Drawlink (G, X1, Y1, X2, Y2, LEN);

About the Author:

Shen Yachang, senior programmer, scjp www.chenshen.com jinashen@benq.com

August 10, 2003

转载请注明原文地址:https://www.9cbs.com/read-29693.html

New Post(0)