Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <cstdlib>
#include "maze.h"
#include <ctime>
#include <iostream>
#include <cmath>
void map_generator(char *map)
{
for (int y=0; y<heighth; y++)
for (int x=0; x<width; x++)
map[x+y*width]='#';
std::srand(std::time(0));
int dug=0;
while (dug<width/2 && dug<heighth/2)
{
tunnel(map, random_exit(dug), dug);
dug++;
}
}
int random_exit(int dug)
{
int ran=rand()%(width*2+heighth*2-8-dug*8);
if (ran<=width-3) return ran+1;
if (ran<=width+heighth-5) return width*(ran-width+4)-1;
if (ran<=width*2+heighth-7) return width*(heighth-1)+width*2+heighth-ran-6;
return (width*2+heighth*2-8-ran)*width;
}
void tunnel(char *map, int exit, int dug)
{
bool right=true; bool down=true;
int entry=width*heighth-1-exit;
if (dug==0) map[entry]='@';
int a=exit % width;
int near_exit=exit;
if (a==dug) near_exit++;
if (a==width-1-dug) near_exit--;
int b=entry % width;
int near_entry=entry;
if (b==dug) near_entry++;
if (b==width-1-dug) near_entry--;
if (a>b) right=false;
a=(exit-a)/heighth;
if (a==dug) near_exit+=width;
if (a==width-1-dug) near_exit-=width;
b=(entry-b)/heighth;
if (b==dug) near_entry+=width;
if (b==width-1-dug) near_entry-=width;
if (a>b) down=false;
int entry_x=near_entry%width;
int entry_y=floor((near_entry-entry_x)/heighth);
std::cout << entry_x << ' ';
std::cout << entry_y << ' ';
std::cout << dug << '\n';
int risk=0;
event_generator(map, risk, near_entry);
risk=80;
int position=near_exit;
bool moved;
while (position!=near_entry)
{
int x=position%width;
int y=floor((position-x)/heighth);
std::cout << x << ' ';
std::cout << y << ' ';
std::cout << dug << '\n';
moved=false;
if (x==entry_x)
{
if (down)
{
position+=width;
risk=event_generator(map, risk, position);
moved=true;
}
else
{
position-=width;
risk=event_generator(map, risk, position);
moved=true;
}
}
if (y==entry_y && moved==false)
{
if (right)
{
position++;
risk=event_generator(map, risk, position);
moved=true;
}
else
{
position--;
risk=event_generator(map, risk, position);
moved=true;
}
}
if (!down && y==entry_y && moved==false)
if (right)
{
position++;
risk=event_generator(map, risk, position);
moved=true;
}
else
{
position--;
risk=event_generator(map, risk, position);
moved=true;
}
if (moved==false && rand()%2==0)
if (right)
{
position++;
risk=event_generator(map, risk, position);
moved=true;
}
else
{
position--;
risk=event_generator(map, risk, position);
moved=true;
}
else
if (down)
{
position+=width;
risk=event_generator(map, risk, position);
moved=true;
}
else
{
position-=width;
risk=event_generator(map, risk, position);
moved=true;
}
}
}
int event_generator(char *map, int risk, int position)
{
if (rand()%100 < risk)
{
map[position]='M';
return floor(risk/2);
}
map[position]=' ';
return risk+5;
}