Crossfire Server, Branches 1.12  R18729
snake.c
Go to the documentation of this file.
1 
2 
8 #include <stdio.h>
9 #include <global.h>
10 #include <time.h>
11 
20 char **make_snake_layout(int xsize, int ysize) {
21  int i, j;
22 
23  /* allocate that array, set it up */
24  char **maze = (char **)calloc(sizeof(char *), xsize);
25  for (i = 0; i < xsize; i++) {
26  maze[i] = (char *)calloc(sizeof(char), ysize);
27  }
28 
29  /* write the outer walls */
30  for (i = 0; i < xsize; i++)
31  maze[i][0] = maze[i][ysize-1] = '#';
32  for (j = 0; j < ysize; j++)
33  maze[0][j] = maze[xsize-1][j] = '#';
34 
35  /* Bail out if the size is too small to make a snake. */
36  if (xsize < 8 || ysize < 8)
37  return maze;
38 
39  /* decide snake orientation--vertical or horizontal , and
40  make the walls and place the doors. */
41 
42  if (RANDOM()%2) { /* vertical orientation */
43  int n_walls = RANDOM()%((xsize-5)/3)+1;
44  int spacing = xsize/(n_walls+1);
45  int orientation = 1;
46 
47  for (i = spacing; i < xsize-3; i += spacing) {
48  if (orientation) {
49  for (j = 1; j < ysize-2; j++) {
50  maze[i][j] = '#';
51  }
52  maze[i][j] = 'D';
53  } else {
54  for (j = 2; j < ysize; j++) {
55  maze[i][j] = '#';
56  }
57  maze[i][1] = 'D';
58  }
59  orientation ^= 1; /* toggle the value of orientation */
60  }
61  } else { /* horizontal orientation */
62  int n_walls = RANDOM()%((ysize-5)/3)+1;
63  int spacing = ysize/(n_walls+1);
64  int orientation = 1;
65 
66  for (i = spacing; i < ysize-3; i += spacing) {
67  if (orientation) {
68  for (j = 1; j < xsize-2; j++) {
69  maze[j][i] = '#';
70  }
71  maze[j][i] = 'D';
72  } else {
73  for (j = 2; j < xsize; j++) {
74  maze[j][i] = '#';
75  }
76  maze[1][i] = 'D';
77  }
78  orientation ^= 1; /* toggle the value of orientation */
79  }
80  }
81 
82  /* place the exit up/down */
83  if (RANDOM()%2) {
84  maze[1][1] = '<';
85  maze[xsize-2][ysize-2] = '>';
86  } else {
87  maze[1][1] = '>';
88  maze[xsize-2][ysize-2] = '<';
89  }
90 
91  return maze;
92 }
char ** make_snake_layout(int xsize, int ysize)
Definition: snake.c:20