Old tune, using SDK to realize maze algorithm

xiaoxiao2021-03-06  62

I have recently read the book of the data structure, and now the textbook is still using the C / C 's algorithm, compile or perform the console mode, if the algorithm of these data structures can be used on the SDK, then the Windows program can be developed. The algorithm program improves learning. Don't look at the cold-ice character in a monotonous console mode to learn the data structure, so you can learn to call Windows API and Windows programming, on the other hand, you can learn the data structure. I hope that I will learn like this. Methods have some help to friends who begin to learn Windows.

This is the labyrinth program (F1 key starting) developed using SDK.

The maze algorithm is still the old road, backtracking and stack implementation, I use a stack implementation. Use a two-way linked table to touch stack, use a Ptrfirst and a PTRLAST and a stack of the stack and the top pointer to define a stack element structure, this The structure saves the location in the labyrinth.

Typedef struct _tagnode {

int NROW;

Int ncolumn;

Struct_tagnode * next;

Struct_tagnode * previou;

} Node;

Define a marker number

Bool bpass [row]; // row and column are labyrinth sizes.

Implementation of the main pseudo code of the maze algorithm.

A. From the beginning of the start position, it is possible to determine if the direction of the ball is feasible. If one direction is feasible, move it in the direction.

The movement is in the stack.

Condition: The forward direction is a wall, then the direction cannot be forwarded.

If the forward and direction is passed, the direction cannot be forwarded.

CANMOVE (Gnrow, Gncolumn, Right))

{

Gncolumn = Movery; // advance

BPASS [gnrow] [gncolumn] = true; // mark passing position

// gnrow, Gncolumn position is in the stack.

}

ELSE IF (Gnrow, Gncolumn, Left) // Right is unlocked, left.

{

Gncolumn = movetleft; // advance

BPASS [gnrow] [gncolumn] = true; // mark passing position

// gnrow, Gncolumn position is in the stack.

}

ELSE IF (Gnrow, Gncolumn, Forward) // Left is unlocked, forward.

{

Gncolumn = MoveForward; // advance

BPASS [gnrow] [gncolumn] = true; // mark passing position

// gnrow, Gncolumn position is in the stack.

}

ELSE IF (Gnrow, Gncolumn, Back) // Forward is not in, backward.

{

Gncolumn = Moveback; // advance

BPASS [gnrow] [gncolumn] = true; // mark passing position

// gnrow, Gncolumn position is in the stack.

}

B. Various directions cannot be feasible, return to the previous position, use the returned operation, return to A.

Else IF (Canmove (Gnrow, Gncolumn, BACK)

{}

A, B continues to repeat until you find the exit, or traverse the maze (stack empty)

IF is the labyrinth export

BSearch = false;

IF stack is empty // No exit,

BSearch = false;

From this algorithm, we can use SDK to implement this maze, but there are several problems must be paid attention to, the first, in pure C / C development (no adjustment API) is, our loop is using while (1) {...} is implemented, but in Windows programming, each Windows program is a message-driven (Event Driven), which is an infinite loop, so that you have to change your thoughts We don't use WHILE (1) {....} to implement a loop, use messages to implement loops, this message is sent by the Windows program, we don't have to add custom messages. :: setTimer (.... ); You can send a WM_TIMER message every fixed time so that we can use this message to implement a loop because it has a message every fixed time, so we can use this message to control the speed of the ball. Use a FLAG To determine if the loop can end without using Break.case WM_Timer:

IF (BStart)

START ();

IF (BSearch)

Search (); // BSearch loop end flag. Find out the exit or stack is empty, bsearch = false.

Return 0;

Second, the second payment problem, in the SDK, the data used is static or global, so it is best to complete the operating place for global data (read and write operation) to be completed in one function, avoid excessive use modification functions In order to make the variables, you can see in the source code. In Search (), only the overall variable Ptrfirst, PTRLAST, GNROW, GNCOLUMN operation, other functions are not responsible for the operation of data. Third issue, resource allocation The problem is released, and for the resource occupied, the memory is released in the WM_DESTROY message. There is only the skill of the learning map, which is simple, as long as you see the source code, you can understand, I am just in Here, how to cause the motion effect of the ball, once the ball is moved, the front position is blocked, so that the process can cause the motion effect. There are two profiles in the source code, write Be as simple as possible, a friend who just learned Windows programming must be helpful. When compiling, just enter the NMAKE command in Console Mode, use it to use, such learning data structure is a very fun process.

Source code http://www.vckbase.com/code/downloadcode.asp?id=2448

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

New Post(0)