Halli Hallo,
ich bin dabei ein für Linux geschriebenes Roguelike Spiel zu programmieren. Ich versuche nun schon seit 2 tagen zufällig generierte Karten zu erstellen in denen schöne viereckige Räume sind, die dann später mit einem weg verbunden werden. Mein Problem ist das die Räume sich überschneiden oder aneinander lagern und ich begreife nicht warum meine vorkehrungen dagegen in meinem code nicht funktionieren wie sie sollten.
Habt ihr vielleicht eine Idee was ich übersehe oder wo mein denkfehler ist?
Wie die Überschrift schon sagt ist c++ die verwendete Programmiersprache und als IDE benutze ich Code::Blocks falls das von interesse sein sollte (aber eigentlich nicht).
C
#include <iostream>
#include <fstream>
#include "cstdlib"
#include <ctime>
#include <stdio.h>
#include <termios.h>
#include <string>
using namespace std;
int main()
{
const int mapx =22;
const int mapy =82;//69;
const int mapz =10;
char map[mapx][mapy]=
{
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################",
"################################################################################"
};
srand(time(NULL));
// maximale Anzahl der Zimmer
const unsigned int maxRooms=8;
//Variable die hochgezählt wird bis maximale zimmmer erreicht sind
unsigned int roomCounter=0;
char floor='a';
char door='+';
char wall='#';
//Länge des Raumes
int length2;
//Zufällige Position X und Y
int randomPosy;
int randomPosx;
//Breite des Raumes
int width;
//Maximale Position X und Y
int maxrandomPosy;
int maxrandomPosx;
//Hilfsvariablem zum überprüfen der Bedingungen
int checker1=0;
bool check1=false;
int checker2=0;
int check2=false;
int checker3=0;
bool check3=false;
int checker4=0;
bool check4=false;
//Die Schleife soll solange laufen bis die maximalZahl der Räume erreicht ist
while( roomCounter < maxRooms )
{
// Das ist nur zur Hilfe der Numerierung der Räume sozusagen
if(roomCounter == 0)
{
floor = '1';
}
if( roomCounter == 1)
{
floor = '2';
}
if(roomCounter == 2)
{
floor = '3';
}
if( roomCounter == 3)
{
floor = '4' ;
}
if( roomCounter == 4 )
{
floor = '5' ;
}
if( roomCounter == 5 )
{
floor = '6' ;
}
if( roomCounter == 6 )
{
floor = '7' ;
}
if( roomCounter == 7 )
{
floor = '8' ;
}
// er soll solange Zahlen Generieren bis die Stellen die er generiert eine Mauer in der Map sind
do
{
length2 = rand()% 5 + 5;
randomPosy = 1+rand()% 65;
randomPosx = 1+rand()% 14;
width = rand()%3+4;
maxrandomPosy = randomPosy+length2 ;
maxrandomPosx=randomPosx+width;
}
while(map[randomPosx][randomPosy]!=wall &&
map[randomPosx][maxrandomPosy]!=wall &&
map[maxrandomPosx][randomPosy]!=wall &&
map[maxrandomPosx][maxrandomPosy]!=wall);
//So nun überprüfe ich das um das 4-Eck herum mauern sind
//hier überprüfe ich die Oberseite des 4-Ecks
for(int a = randomPosy ; a < maxrandomPosy + 1 ; a++)
{
if(map[ randomPosx-1 ][a]==wall)
{
//wenn an der Stelle eine wand ist soll der zähler hochgezählt werden
++checker1;
}
}
// Wenn nun der Zähler die gleiche Anzahl an Stellen mit
Mauer hat wie die width (ja hätte da die width überprüfen können anstatt
das so kompliziert zu machen)
// dann wird der boolsche checker zu true gesetzt
if(checker1==maxrandomPosy-randomPosy+1)
{
check1=true;
}
// das gleiche Schema wie oben nur hier die rechte Seite des 4-Ecks
for(int b=randomPosx ; b < maxrandomPosx + 1 ; b++ )
{
if(map[b][ maxrandomPosy + 1 ]==wall)
{
++checker2;
}
}
if(checker2 == maxrandomPosx-randomPosx+1 )
{
check2 = true;
}
for(int c = randomPosy ; c < maxrandomPosy + 1 ; c++ )
{
if(map[ maxrandomPosx + 1][code=c] == wall )
{
++checker3;
}
}
if(checker3 == maxrandomPosy-randomPosy+1 )
{
check3=true;
}
for(int d=randomPosx ; d < maxrandomPosx + 1; d++ )
{
if(map[d][randomPosy-1] == wall )
{
++checker4;
}
}
if(checker4 == maxrandomPosx - randomPosx + 1 )
{
check4=true;
}
// Nun wenn um das 4-Eck alles Mauern sind und die Ecken des 4-ecks etc. Mauern sind wird der Raum ausgehöhlt
if( check1 == true && check2 == true && check3 == true && check4 == true )
{
if(map[randomPosx][randomPosy]==wall &&
map[maxrandomPosx][randomPosy]==wall &&
map[randomPosx][maxrandomPosy]==wall &&
map[maxrandomPosx][maxrandomPosy]==wall)
{
if(map[randomPosx-1][randomPosy]==wall &&
map[maxrandomPosx+1][randomPosy]==wall &&
map[randomPosx-1][maxrandomPosy]==wall &&
map[maxrandomPosx+1][maxrandomPosy]==wall)
{
if(map[randomPosx][randomPosy-1]==wall
&& map[maxrandomPosx][randomPosy-1]==wall &&
map[randomPosx][maxrandomPosy+1]==wall &&
map[maxrandomPosx][maxrandomPosy+1]==wall)
{
//Nun wird der Raum ausgehöhlt
while(randomPosy<maxrandomPosy )
{
for(int c=randomPosx; c< maxrandomPosx; c++)
{
map[code=c][randomPosy]=floor;
}
randomPosy++;
}
//wenn ein raum ausgehöhlt wurde die zähler variable um 1 addieren
++roomCounter;
//print map
for(int m=0; m<mapx; m++)
{
cout <<map[m]<<endl;
}
cout<<endl;
}
}
}
}
}
return 0;
}
Alles anzeigen