2015-12-27 14:23:04 +00:00
|
|
|
#include <iostream>
|
2015-12-27 15:34:17 +00:00
|
|
|
#include <random>
|
|
|
|
#include <time.h>
|
2015-12-28 16:10:11 +00:00
|
|
|
#include <conio.h>
|
2015-12-27 15:34:17 +00:00
|
|
|
|
2015-12-27 14:23:04 +00:00
|
|
|
#define X_MAX 80
|
|
|
|
#define Y_MAX 23
|
2015-12-27 15:34:17 +00:00
|
|
|
#define ROOMS 8
|
|
|
|
#define ROOM_SIZE 7
|
2015-12-27 14:23:04 +00:00
|
|
|
|
2015-12-27 15:46:41 +00:00
|
|
|
#define WALL 0xB2
|
|
|
|
#define EMPTY 0xFF
|
2015-12-28 16:10:11 +00:00
|
|
|
#define PLAYER 0x02
|
2015-12-27 15:46:41 +00:00
|
|
|
|
2015-12-27 14:23:04 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2015-12-28 16:10:11 +00:00
|
|
|
void move(unsigned char map[X_MAX][Y_MAX], int player[2])
|
|
|
|
{
|
|
|
|
int player_x = player[0];
|
|
|
|
int player_y = player[1];
|
|
|
|
bool waiting = true;
|
|
|
|
while(waiting)
|
|
|
|
{
|
|
|
|
if(getch() == 224)
|
|
|
|
{
|
|
|
|
switch(getch())
|
|
|
|
{
|
|
|
|
case 72: //Freccia su
|
|
|
|
if(map[player_x][player_y-1] == EMPTY)
|
|
|
|
{
|
|
|
|
map[player_x][player_y] = EMPTY;
|
|
|
|
map[player_x][player_y-1] = PLAYER;
|
|
|
|
player_y--;
|
|
|
|
waiting = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 80: //Freccia giù
|
|
|
|
if(map[player_x][player_y+1] == EMPTY)
|
|
|
|
{
|
|
|
|
map[player_x][player_y] = EMPTY;
|
|
|
|
map[player_x][player_y+1] = PLAYER;
|
|
|
|
player_y++;
|
|
|
|
waiting = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 75: //Freccia sinistra
|
|
|
|
if(map[player_x-1][player_y] == EMPTY)
|
|
|
|
{
|
|
|
|
map[player_x][player_y] = EMPTY;
|
|
|
|
map[player_x-1][player_y] = PLAYER;
|
|
|
|
player_x--;
|
|
|
|
waiting = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 77: //Freccia destra
|
|
|
|
if(map[player_x+1][player_y] == EMPTY)
|
|
|
|
{
|
|
|
|
map[player_x][player_y] = EMPTY;
|
|
|
|
map[player_x+1][player_y] = PLAYER;
|
|
|
|
player_x++;
|
|
|
|
waiting = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
player[0] = player_x;
|
|
|
|
player[1] = player_y;
|
|
|
|
}
|
2015-12-27 14:23:04 +00:00
|
|
|
//Aggiorna la console con la situazione corrente del gioco.
|
2015-12-28 16:10:11 +00:00
|
|
|
void draw(unsigned char map[X_MAX][Y_MAX])
|
2015-12-27 14:23:04 +00:00
|
|
|
{
|
|
|
|
for(int y=0; y<Y_MAX; y++)
|
|
|
|
{
|
|
|
|
for(int x=0; x<X_MAX; x++)
|
|
|
|
{
|
|
|
|
cout << map[x][y];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:31:40 +00:00
|
|
|
//Funzioni per la generazione della mappa
|
2015-12-27 14:23:04 +00:00
|
|
|
//Inizializza la mappa con spazi vuoti
|
2015-12-28 16:10:11 +00:00
|
|
|
void init(unsigned char map[X_MAX][Y_MAX])
|
2015-12-27 14:23:04 +00:00
|
|
|
{
|
|
|
|
for(int y=0; y<Y_MAX; y++)
|
|
|
|
{
|
|
|
|
for(int x=0; x<X_MAX; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][y] = WALL;
|
2015-12-27 14:23:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:31:40 +00:00
|
|
|
//Crea una stanza quadrata
|
2015-12-28 16:10:11 +00:00
|
|
|
void room(unsigned char map[X_MAX][Y_MAX], int start_x, int start_y, int end_x, int end_y)
|
2015-12-27 14:31:40 +00:00
|
|
|
{
|
|
|
|
for(int y=start_y; y<=end_y; y++)
|
|
|
|
{
|
|
|
|
for(int x=start_x; x<=end_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][y] = EMPTY;
|
2015-12-27 14:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:52:42 +00:00
|
|
|
//Crea un corridoio che connetta due punti
|
2015-12-28 16:10:11 +00:00
|
|
|
void corridor(unsigned char map[X_MAX][Y_MAX], int start_x, int start_y, int end_x, int end_y, bool verticale)
|
2015-12-27 14:52:42 +00:00
|
|
|
{
|
|
|
|
if(verticale)
|
|
|
|
{
|
|
|
|
if(start_y > end_y)
|
|
|
|
{
|
|
|
|
for(int y=end_y; y<=start_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[start_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int y=start_y; y<=end_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[start_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(start_x > end_x)
|
|
|
|
{
|
|
|
|
for(int x=end_x; x<=start_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][end_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int x=start_x; x<=end_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][end_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(start_x > end_x)
|
|
|
|
{
|
|
|
|
for(int x=end_x; x<=start_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][start_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int x=start_x; x<=end_x; x++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[x][start_y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(start_y > end_y)
|
|
|
|
{
|
|
|
|
for(int y=end_y; y<=start_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[end_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int y=start_y; y<=end_y; y++)
|
|
|
|
{
|
2015-12-27 15:46:41 +00:00
|
|
|
map[end_x][y] = EMPTY;
|
2015-12-27 14:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 14:31:40 +00:00
|
|
|
|
2015-12-28 16:10:11 +00:00
|
|
|
void generate(unsigned char map[X_MAX][Y_MAX])
|
2015-12-27 15:34:17 +00:00
|
|
|
{
|
|
|
|
int corridor_x;
|
|
|
|
int corridor_y;
|
|
|
|
for(int r=0; r<ROOMS; r++)
|
|
|
|
{
|
|
|
|
int size_x = rand() % ROOM_SIZE + 1;
|
|
|
|
int size_y = rand() % ROOM_SIZE + 1;
|
2015-12-27 20:40:59 +00:00
|
|
|
int start_x = rand() % (X_MAX - size_x - 2) + 1;
|
|
|
|
int start_y = rand() % (Y_MAX - size_y - 2) + 1;
|
2015-12-27 15:34:17 +00:00
|
|
|
room(map, start_x, start_y, start_x + size_x, start_y + size_y);
|
|
|
|
if(r > 0)
|
|
|
|
{
|
|
|
|
int link_x = rand() % size_x + 1 + start_x;
|
|
|
|
int link_y = rand() % size_y + 1 + start_y;
|
|
|
|
corridor(map, link_x, link_y, corridor_x, corridor_y, rand() % 2);
|
|
|
|
}
|
|
|
|
corridor_x = rand() % size_x + start_x;
|
|
|
|
corridor_y = rand() % size_y + start_y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 14:23:04 +00:00
|
|
|
int main()
|
|
|
|
{
|
2015-12-28 16:10:11 +00:00
|
|
|
unsigned char map[X_MAX][Y_MAX]; //Mappa del gioco
|
|
|
|
int player[2]; //Coordinate di posizione del giocatore per trovarlo più in fretta
|
|
|
|
srand(0); //TODO: Rendere il seed modificabile
|
2015-12-27 14:23:04 +00:00
|
|
|
init(map);
|
2015-12-27 15:34:17 +00:00
|
|
|
generate(map);
|
2015-12-27 14:23:04 +00:00
|
|
|
draw(map);
|
2015-12-28 16:10:11 +00:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
move(map, player);
|
|
|
|
draw(map);
|
|
|
|
}
|
2015-12-27 14:23:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|