AKB Forums

Go Back   AKB Forums > Technical sections > Algorithms
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Algorithms The source of algorithms for your project

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Mar 1, 2002, 15:04   #1
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: Yerevan, Armenia
Posts: 7,069
Blog Entries: 15
Rep Power: 10
Reputation: 246
Post Как постpоить лабиpинт?

Originally posted by (Mitya Dorogoj 2:5000/54.4)

FullFill - на солько плотно заполнять лабиpинт (делать ли холлы).

WallShort- на сколько коpоткие должны быть стены 0 - одни колонны.

<blockquote><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">code:</font><hr><pre>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

const int size = 20;
const int fullfill = 100; // in %
const int wallshort= 50; // in %

char m[size+1][size+1];

// Random generator
int r[2][size/2*size/2];
int h; // How many number in array;

void initrandom ()
{
int j=0;
for (int y=2; y<size; y+=2)
for (int x=2; x< size; x+=2)
{
r[0][j] = x; r[1][j] = y; j++;
}
h=j-1;
}

int getrandom(int &x, int &y)
{
int i = random (h);
x = r[0][i]; y = r[1][i];
r[0][i] = r[0][h]; r[1][i] = r[1][h];
return h--;
}

// View labirint on screen
void view()
{
for (int y=0; y<=size; y++)
for (int x=0; x<=size; x++)
{
gotoxy (x*2+1,y+1);
if (m[y][x]==0) cprintf ("..");
if (m[y][x]==1) cprintf ("XX");
}
}

int main(void)
{
printf ("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\L abirint generator");
// Clear labirint
for (int c = 0; c < size*size; c++) ((char *)m)[c] = 0;

// Make border
for (int i = 0; i <= size; i++)
{
m[0][i] = 1; m[size][i] = 1;
m[i][0] = 1; m[i][size] = 1;
}
view ();
initrandom();
int startx, starty;
while (getrandom (startx, starty))
{
if (m[starty][startx]==1) continue;
if (random (100) > fullfill) continue;
int sx=0,sy=0;
do
{
sx=random (3)-1;
sy=random (3)-1;
} while (sx==0 && sy==0 || sx!=0 && sy!=0); //sx==0 and sy==0
while (m[starty][startx]==0)
{
if (random (100) > wallshort)
{m[starty][startx] = 1; break;}
m[starty][startx] = 1;
startx +=sx; starty+=sy;
m[starty][startx] = 1;
startx +=sx; starty+=sy;
}
}
view();
return 0;
}

</pre><hr></blockquote>
__________________
Chat with acid


acid is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 22:01.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link