Merhaba arkadaşlar abiler,benim rastgele labirent oluşturmakla ilgili bir ödevim var.Ben labirenti oluşturdum tek sıkıntı her programı çalıştırdığımda labirentin rastgele ve çözülebilir olmasını istiyorum.Bunu yapamadım ve sizden düşüncelerinizi yardımlarınızı bekliyorum.
Kodum burda.Ve bu labirenti değiştiremem veya yeniden yazamam bunun üstüne eklemeliyim.
[code]
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
#include <time.h>
using namespace std;
const int KEY_LEFT = 'a', KEY_RIGHT = 'd',KEY_UP = 'w', KEY_DOWN ='s';
const int HEIGHT =25, WIDTH = 40;
bool hasntWon = true;
int hP = 100;
enum DirectionX {Right,Left};
enum DirectionY {Up,Down};
struct Player {
char sprite;
int x;
int y;
};
struct Enemy{
char sprite;
int x;
int y;
DirectionX pastDirX;
DirectionY pastDirY;
};
unsigned char MoveEnemy(Enemy& enemy, unsigned char maze[HEIGHT][WIDTH], int i , int j , string& smove, bool& nc){
if (nc){
bool psbUp = false;
bool psbDown = false;
bool psbRight = false;
bool psbLeft = false;
if (maze[enemy.y][enemy.x+1] != '#' ){
psbRight = true;
}
if (maze[enemy.y][enemy.x-1] != '#' ){
psbLeft = true;
}
if (maze[enemy.y+1][enemy.x] != '#' ){
psbLeft = true;
}
if (maze[enemy.y-1][enemy.x] != '#') {
psbLeft = true;
}
enum Mv {up,right,down,left,NA};
Mv move = NA;
if (smove =="up"){
move = up;
}
else if (smove =="right"){
move = right;
}
else if (smove =="down"){
move = down;
}
else if (smove =="left"){
move = left;
}
else {
move = NA;
}
if (enemy.pastDirX == Right && !psbRight){
if(enemy.pastDirY == Up){
if (psbUp){
move = up;
}
else if (psbDown && move != up){
move = down;
}
else if (psbLeft){
move = left;
}
else {
move = down;
}
}
else{
if(psbDown){
move = down ;
}
else if(psbUp && move != down){
move = up;
}
else if (psbLeft){
move = left;
}
else{
move = up;
}
}
}
else if (enemy.pastDirX == Right && psbRight){
move = right;
}
else if (enemy.pastDirX == Left && !psbLeft){
if(enemy.pastDirY == Up) {
if (psbUp){
move = up;
}
else if(psbDown && move != up){
move = down;
}
else if (psbRight){
move = right;
}
else {
move = down;
}
}
else {
if(psbDown){
move = down;
}
else if (psbUp && move != down){
move = up;
}
else if (psbRight){
move = right;
}
else {
move = up;
}
}
}
else if (enemy.pastDirX == Left && psbLeft){
move = left;
}
else if(enemy.pastDirY == Up && !psbUp){
if (enemy.pastDirX == Right){
if (psbRight){
move = right;
}
else if (psbLeft && move != right){
move = left;
}
else if (psbDown){
move = down;
}
else {
move = left;
}
}
else {
if (psbLeft ){
move = left;
}
else if (psbRight && move != left){
move = right;
}
else if (psbDown){
move = down;
}
else {
move = right;
}
}
}
else if (enemy.pastDirY == Up && psbUp){
move = up;
}
else if (enemy.pastDirY == Down && !psbDown){
if (enemy.pastDirX == Right){
if (psbRight){
move = right;
}
else if (psbLeft && move != right){
move = left;
}
else if (psbUp){
move = up;
}
else {
move = left;
}
}
else {
if (psbLeft){
move = left;
}
else if (psbRight && move != left ){
move = right ;
}
else if (psbRight){
move = up;
}
else {
move = right ;
}
}
}
else if (enemy.pastDirY == Down && psbDown){
move = down;
}
switch (move) {
case up:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirY = Up;
smove = "up";
enemy.y--;
break ;
case right:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirX = Right;
smove = "right";
enemy.x++;
break ;
case down:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirY = Down;
smove = "down";
enemy.y++;
break ;
case left:
maze[enemy.y][enemy.x] = ' ';
enemy.pastDirX = Left;
smove = "left";
enemy.x--;
break ;
case NA:
smove = "NA";
break;
}
}
nc = false;
return maze[i][j];
}
int main()
{
int x,y,h,i,j;
srand(time(NULL));
unsigned char maze [HEIGHT][WIDTH]={{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
{' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ','#','#',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ','#'},
{'#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ','#',' ','#','#','#',' ','#','#','#',' ','#',' ','#','#','#',' ','#',' ','#',' ','#','#',' ','#'},
{'#',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ','#'},
{'#',' ','#','#',' ','#','#','#',' ','#',' ','#',' ','#',' ',' ',' ',' ','#',' ','#','#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ','#'},
{'#',' ','#',' ',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#','#',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ',' ','#'},
{'#',' ','#',' ','#','#',' ','#',' ','#',' ',' ',' ',' ',' ',' ',' ',' ','#',' ','#','#','#',' ','#','#','#','#','#','#','#','#','#',' ','#',' ','#','#',' ','#'},
{'#',' ','#',' ','#',' ',' ','#',' ','#','#','#','#',' ','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','#'},
{'#',' ','#',' ','#','#',' ','#',' ','#',' ',' ',' ',' ','#',' ',' ',' ','#','#','#','#','#',' ','#',' ','#',' ','#',' ','#','#',' ','#','#',' ','#',' ','#','#'},
{'#',' ','#',' ',' ',' ',' ','#',' ','#','#','#','#',' ','#','#','#','#','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#','#',' ',' ','#',' ','#',' ',' ','#'},
{'#',' ','#','#','#','#',' ','#',' ',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ',' ','#',' ','#','#','#','#','#',' ','#'},
{'#',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#',' ','#',' ','#','#','#','#',' ',' ',' ','#',' ','#',' ','#'},
{'#',' ','#','#',' ',' ',' ','#',' ',' ',' ',' ','#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ','#'},
{'#',' ','#',' ',' ','#',' ','#',' ','#','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#',' ','#',' ','#','#','#','#',' ','#',' ','#',' ','#','#','#','#','#'},
{'#',' ','#',' ','#','#',' ','#',' ','#',' ',' ',' ',' ','#',' ','#',' ',' ','#',' ',' ',' ',' ','#',' ',' ',' ',' ',' #',' ','#',' ','#',' ',' ',' ','#',' ','#'},
{'#',' ','#',' ','#',' ',' ','#',' ','#','#','#','#','#','#',' ','#',' ','#','#',' ','#','#','#','#','#','#','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#'},
{'#',' ','#',' ','#',' ','#','#',' ','#',' ',' ',' ',' ',' ',' ','#',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#','#','#','#','#',' ','#',' ','#'},
{'#',' ','#',' ','#',' ',' ','#',' ','#',' ','#','#','#','#','#','#','#',' ','#',' ','#','#','#','#','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ',' ',' ','#'},
{'#',' ','#','#','#','#','#','#',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#'},
{'#',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#'},
{'#',' ','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#','#','#',' ',' ',' ','#',' ','#','#','#',' ','#'},
{'#',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#','#','#',' ','#',' ','#',' ','#',' ','#'},
{'#',' ','#','#',' ','#',' ','#','#','#',' ','#','#','#',' ','#',' ','#',' ','#',' ','#',' ','#',' ','#','#','#',' ','#',' ',' ',' ','#',' ','#',' ','#',' ','#'},
{'#',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ',' ',' ',' ',' ','#',' ',' ',' ','#',' ','#',' ','#',' ',' ',' ',' ',' ','#',' ',' '},
{'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#'},
};
struct Player player;
player.sprite ='@';
player.x = 0;
player.y = 1;
string prevmove1 = "NA";
string prevmove2 = "NA";
while(hasntWon){
if (maze[player.y][player.x] == ' '){
maze[player.y][player.x] = player.sprite;
}
for (y=0;y<HEIGHT ; y++){
cout <<endl;
for (x = 0; x<WIDTH ; x++){
cout << maze[y][x];
}
}
char key = getch();
switch(key){
case KEY_LEFT :
if (maze[player.y][player.x-1] != '#'){
maze[player.y][player.x] = ' ';
player.x--;
}
break;
case KEY_UP :
if (maze[player.y-1][player.x] != '#'){
maze[player.y][player.x] = ' ';
player.y--;
}
break;
case KEY_DOWN :
if (maze[player.y+1][player.x] != '#'){
maze[player.y][player.x] = ' ';
player.y++;
}
break;
case KEY_RIGHT :
if (maze[player.y][player.x+1] != '#'){
maze[player.y][player.x] = ' ';
player.x++;
}
break;
case 'q':
exit(0);
break;
}
bool once1 = true;
bool once2 = true;
bool once3 = true;
bool once4 = true;
if (player.x == WIDTH -1){
cout <<endl<<endl<<"You have found the way out of the maze!" << endl<<endl;
system("PAUSE");
hasntWon = false;
}
system("CLS");
}
}
enum {
W = 36, // width of maze
H = 25 // height of maze
};
enum {
North,
East,
South,
West,
NDir
};
char visited[H][W];
char horz[H][W - 1]; // horizontal E-W paths in the maze
char vert[H - 1][W]; // veritcal N-S paths in the maze
/*
* Fill dir with directions to unvisited cells, return count
*/
int adjacent(int dir[], int x, int y)
{
int ndir = 0;
if (y > 0 && visited[y - 1][x] == 0) dir[ndir++] = North;
if (x < W - 1 && visited[y][x + 1] == 0) dir[ndir++] = East;
if (y < H - 1 && visited[y + 1][x] == 0) dir[ndir++] = South;
if (x > 0 && visited[y][x - 1] == 0) dir[ndir++] = West;
return ndir;
}
/*
* Traverse cells depth first and create paths as you go
*/
void dfs(int x, int y)
{
int dir[NDir];
int ndir;
visited[y][x] = 1;
ndir = adjacent(dir, x, y);
while (ndir) {
int pick = rand() % ndir;
switch (dir[pick]) {
case North: vert[y - 1][x] = 1; dfs(x, y - 1); break;
case East: horz[y][x] = 1; dfs(x + 1, y); break;
case South: vert[y][x] = 1; dfs(x, y + 1); break;
case West: horz[y][x - 1] = 1; dfs(x - 1, y); break;
}
ndir = adjacent(dir, x, y);
}
}
/*
* Print a map of the maze
*/
void map(void)
{
int i, j;
for (i = 0; i < W; i++) {
putchar('_');
putchar('_');
}
putchar('\n');
for (j = 0; j < H; j++) {
putchar('|');
for (i = 0; i < W; i++) {
putchar(j < H - 1 && vert[j][i] ? ' ' : '_');
putchar(i < W - 1 && horz[j][i] ? '_' : '|');
}
putchar('\n');
}
}
[\code ]