A more interesting automatically generates a maze program.

xiaoxiao2021-03-06  68

This is the gvbasic code I just downloaded in Wenxing's PC1000A. It can automatically generate maze, very interesting. To tell the truth, this code is worth mentioning, and its hierarchical structure is really bad. I change it into Win32 programs.

The gvbasic code is as follows: 10 l = 8: mx = 160 / L: my = 80 / L: DIM A (MX 1, My 1): goto 11020 While NOT (x = 2 and y = 1) 30 if A ( X-1, Y) * a (x, y-1) * a (x, y 1) * a (x 1, y) = 0 THEN 7040 for i = 0 to 3: xx = x (i = 0) - (i = 2): yy = y (i = 1) - (i = 3): if a (xx, yy) = s-1 THEN 6050 Next: RETURN60 x = xx: y = yy: s = A (x, y): goto 9070 r = int (RND (1) * 4): xx = x (r = 0) - (r = 2): yy = y (r = 1) - (r = 3) : IF a (xx, yy) <> 0 THEN 7080 x = xx: y = yy: s = s 1: a (x, y) = S90 Wend: Return110 for i = 0 to mx 1: A (i 0) = - 1: a (i, my 1) = - 1: Next: for i = 0 to my 1: a (0, i) = - 1: a (MX 1, I) = - 1: Next120 x = MX-1: Y = my: s = 1: a (x, y) = s: gosub 20130 for = my to 1 step -1: for p = mx to 1 step -1: if A (p, q)> 0 THEN 160140 r = int (RND (1) * 4): PP = p (r = 0) - (r = 2) :: qq = q (r = 1) - (r = 3 : IF A (PP, QQ) <1 THEN 140150 x = P: Y = Q: S = a (PP, QQ) * 2 1000: A (x, y) = s: Gosub 20: f = 1160 Next p, q: if f life f = 0: goto 130200 graph210 for x = 1 to mx: for y = 1 to my220 t = abx (a (x, y) -a (x 1, y): if t <= 1 OR T-1000 = a (x, y) OR T-1000 = A (x 1, y) THEN 230225 LINE X * L, Y * L1, X * L, Y * L230 T = ABX (A (x, y) -A (x, y 1)): IF T <= 1 OR T-1000 = A (x, y) OR TH = A (x, y 1) THEN 240235 LINEX * LL , Y * L, X * L, Y * L240 Next Y, X250 Box 1 , 0, 160, 80: Line L, 0, L L, 0, 0: Line 160-L, 80, 160-LL, 80, 0260 END400 While Not (x = 2 and y = 1) 1000 for i = 1 to 4: For J = 1 to 8: Print A (J, I); "";: Next: Print Inkey $: NEXT: RETURN

The WIN32 code I change is as follows: int setmaze (int ** maze, int s, int x, int y) {Int r, xx, yy; int i; while (x! = 2 || y! = 1) {IF (Maze [Y] * MAZE [Y] [x 1] * MAZE [Y 1] [x] * maze [y-1] [x] == 0) {r = rand () % 4; xx = x (r == 0) - (r == 2); yy = y (r == 1) - (r == 3); while (maze [yy] [xx]! = 0) {R = rand ()% 4; xx = x (r == 0) - (r == 2); yy = y (r == 1) - (r == 3);} x = xx; y = YY; S ; Maze [y] = s;} else {for (i = 0; i <4; i ) {xx = x (i == 0) - (i == 2); yy = y (i == 1) - (i == 3); if (Maze [YY] [xx] == S-1) {x = xx; y = yy; s = maze [y] [x]; }}}}}} Return 0;} int ** cretemaze (int xsize, int = = = xsize-1, y = ysize, s = 1; int R; int i, J;

INT ** MAZE = New Int * [i 2]; for (i = 0; i 0; I -) {for (j = Xsize; j> 0; j -) {IF (Maze [i] [j] <= 0) {r = rand ()% 4; while (maze [i (r == 1) - (r == 3 )] [j (r == 0) - (r == 2)] <1) r = rand ()% 4; x = j; y = i; s = maze [i (r == 1) - R == 3)] [j (r == 0) - (r == 2)] * 2 1000; MAZE [Y] = S; Setmaze (MAZE, S, X, Y); Checkover = False;}}} Return Maze;} int destroymaze (int ** maze, int ysize) {for (int i = 0; i

Return 0;}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {PAINTSTRUCT ps; HDC hdc, hdcEmf; static HENHMETAFILE hemf; RECT rect; static int ** Maze; int t; static int xSize = 20; static int ySize = 16;

Switch (Message) {Case WM_Command: Break; Case WM_Create: Maze = Createmaze; HDCEMF = CREATEENHMETAFILE (NULL, "EMF.EMF", NULL, NULL);

Rectangle (HDCEMF, 0, 0, 20 * Xsize 1, 20 * Ysize 1); for (int i = 1; i

Ordinary maps are divided by blocks, defining a map only needs to define a two-dimensional array, such as I define a map of 8 * 6, to define an array of [6] [8], where 0 Representative obstacles, 1 representative is feasible, but this is not too lazy.

This random maze is still divided in blocks, the difference is that the map is used, and the map of the map has 4 top points, plus the concept of the map boundary, so a map of Xsize * Ysize is (Xsize 2) * (Ysize 2) Points. This is no longer the concept of obstacles in this map, and it becomes a relationship between blocks between blocks.

The basic concept is over, and now I talk about my understanding of this generating algorithm.

I defined two variables static int xsize = 20; static int ysize = 16; to represent the map generated 20 * 16, can change the value. In the Createmaze (int, int) function, first define a Maze [i 2 ] [xsize 2], then assign the initial value, assign the array boundaries to -1, non-boundary assignment 0. Map data intuitive feelings as follows: -1 -1 -1 -1 -1-1 0 0 0 - 1-1 0 0-01-1 0 0 0 -1-1 -1 -1 -1 -1 Note these three variables, int x = xsize-1, y = ysize, s = 1; image point said, X, y indicates that a wcholery in the labyrinous vertex array at this moment, while S represents this is the first step. Then this elves started at the lower right position of the maze, every step of the direction Random number decision (random maze, but to note that the elf is not walking in the maze, but walking in the point of the maze block), if the road can go (ie, the array value is zero), then step, s = S 1 and assign S to a new location, if there is no way around (around the variables around), the variables of the four weeks are looking for the variables of S-1 (the road when it comes, if found, s = S-1, and return a previous location). Otherwise end function. It is also important to note that if the wizard has reached the point coordinate (1, 2), it will end the function. Take a look at the second half of the Createmaze, why Plus such a paragraph, because the original setmaze has given a random route from the vertex to the end, but there is still a closed area on the map, which does not meet the strong attachment of random maze, this code detects those unsuccessful The vertices of assignments, place the elves on these vertices, and then turn out a path. S = maze [μ (r == 1) - (r == 3)] [j (r == 0) - (r == 2)] * 2 1000; representatives related to these unfielded points and existing pathways.

Everything is ready to work, then the most critical drawing. Take a look at WM_Paint: Rectangle (HDCEMF, 0, 0, 20 * xsize 1, 20 * Ysize 1); for (int i = 1; i

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

New Post(0)