Concetti Chiave
- The program is a simple console-based game where a player controls an entity on a grid map using arrow keys.
- Players must navigate the map, collecting points by eating dots while avoiding being caught by an enemy.
- Difficulty levels (Easy, Normal, Hard) affect the speed of the enemy's movement.
- The game utilizes a breadth-first search algorithm to determine the enemy's path towards the player.
- Game ends when the enemy catches the player, displaying the player's score upon loss.
#include
#include
#include
#include
#include
using namespace std;
char tmp_map[18][32];
char map[18][32] = {
"+#############################+",
"| |",
"| |",
"|## ########### ## #########|",
"| | |",
"| | |### | | | |",
"| | | | |### | | | |",
"| | #####| | | ## | |",
"| | |### | | |",
"| |##### ### ## |",
"| ###### ####### ###|",
"| |",
"|# ### #### ### #######|",
"| |",
"| |",
"| |",
"| |",
"+#############################+"
};
void ShowMap()
{
for(int i = 0; i
printf("%s\n",map );
}
}
void gotoxy( short x, short y )
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
COORD position = { x, y } ;
SetConsoleCursorPosition( hStdout, position ) ;
}
class entity {
public:
entity( int x, int y ){
this ->x = x;
this ->y = y;
}
void move_x( int p ){
if( map[y][x+p] == ' ' ) x += p;
}
void move_y( int p ){
if( map[y+p][x] == ' ' ) y += p;
}
void move( int p, int q ){
x += p;
y += q;
}
int get_x(){ return x; }
int get_y(){ return y; }
void draw( char p ){
map[x][y] = p;
gotoxy( x, y ); printf( "%c", p );
}
private:
int x;
int y;
};
struct walk {
short walk_count;
short x;
short y;
short back;
};
struct target {
short x;
short y;
};
vector
vector
void AddArray( int x, int y, int wc , int back ){
if( tmp_map[y][x] == ' ' || tmp_map[y][x] == '.' ){
tmp_map[y][x] = '#';
walk tmp;
tmp.x = x;
tmp.y = y;
tmp.walk_count = wc;
tmp.back = back;
BFSArray.push_back( tmp );
}
}
void FindPath( int sx, int sy, int x, int y ){
memcpy( tmp_map, map, sizeof(map) );
BFSArray.clear();
walk tmp;
tmp.x = sx;
tmp.y = sy;
tmp.walk_count = 0;
tmp.back = -1;
BFSArray.push_back( tmp );
int i = 0;
while( i
if( BFSArray.x == x && BFSArray.y == y ){
walk_queue.clear();
target tmp2;
while( BFSArray.walk_count != 0 ){
tmp2.x = BFSArray.x;
tmp2.y = BFSArray.y;
walk_queue.push_back( tmp2 );
i = BFSArray.back;
}
break;
}
AddArray( BFSArray.x+1, BFSArray.y, BFSArray.walk_count+1, i );
AddArray( BFSArray.x-1, BFSArray.y, BFSArray.walk_count+1, i );
AddArray( BFSArray.x, BFSArray.y+1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x, BFSArray.y-1, BFSArray.walk_count+1, i );
/*
AddArray( BFSArray.x+1, BFSArray.y+1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x-1, BFSArray.y+1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x+1, BFSArray.y+1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x+1, BFSArray.y-1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x+1, BFSArray.y-1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x-1, BFSArray.y-1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x-1, BFSArray.y+1, BFSArray.walk_count+1, i );
AddArray( BFSArray.x-1, BFSArray.y-1, BFSArray.walk_count+1, i );
*/
i++;
}
BFSArray.clear();
}
int main()
{
bool running = true;
int x = 15; // hero x
int y = 16; // hero y
int old_x;
int old_y;
int ex = 1;
int ey = 1;
int pts = 0;
printf("Instruzione:\n1.
Usare le freccette per muovere il tuo eroe\n2. Mangia i pallini prodotti dal tuo nemico per ottenere punti\n3. Non farti prendere dal tuo nemico\n\n");printf("H -> Difficile\nN -> Normale\nE -> Facile\n\nInput : ");
char diffi;
int speedmod = 3;
cin >> diffi;
if( diffi == 'N' ){
speedmod = 2;
}else if( diffi == 'H' ){
speedmod = 1;
}
system("cls");
ShowMap();
gotoxy( x, y ); cout
int frame = 0;
FindPath( ex,ey,x,y );
while( running ){
gotoxy( x, y ); cout
old_x = x;
old_y = y;
if ( GetAsyncKeyState( VK_UP ) ){
if( map[y-1][x] == '.' ){ y--; pts++; } else
if( map[y-1][x] == ' ' ) y--;
}
if ( GetAsyncKeyState( VK_DOWN ) ){
if( map[y+1][x] == '.' ){ y++; pts++; } else
if( map[y+1][x] == ' ' ) y++;
}
if ( GetAsyncKeyState( VK_LEFT ) ){
if( map[y][x-1] == '.' ){ x--; pts++; } else
if( map[y][x-1] == ' ' ) x--;
}
if ( GetAsyncKeyState( VK_RIGHT ) ){
if( map[y][x+1] == '.' ){ x++; pts++; } else
if( map[y][x+1] == ' ' ) x++;
}
if( old_x != x || old_y != y ){
FindPath( ex,ey,x,y );
}
gotoxy( x,y ); cout
map[ey][ex] = '.';
gotoxy( ex, ey ); cout
if( frame%speedmod == 0 && walk_queue.size() != 0 ){
ex = walk_queue.back().x;
ey = walk_queue.back().y;
walk_queue.pop_back();
}
gotoxy( ex, ey ); cout
if( ex == x && ey == y ){
break;
}
gotoxy( 32, 18 );
gotoxy( 32, 1 ); cout
Sleep( 100 );
frame++;
}
system("cls");
printf("Hai perso...Il tuo punteggio e' : %i", pts );
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
cin.get();
return 0;
}
Domande da interrogazione
- Qual è lo scopo principale del programma fornito?
- Come si muove l'eroe nel gioco?
- Quali sono le difficoltà disponibili nel gioco e come influenzano il gameplay?
- Come viene calcolato il percorso del nemico verso l'eroe?
- Cosa succede quando il nemico raggiunge l'eroe?
Il programma è un gioco in cui l'utente controlla un eroe che deve muoversi su una mappa, mangiare pallini per ottenere punti e evitare di essere catturato da un nemico.
L'eroe si muove utilizzando le freccette della tastiera, e può spostarsi in spazi vuoti o mangiare pallini per guadagnare punti.
Le difficoltà disponibili sono Facile, Normale e Difficile, e influenzano la velocità del nemico, con Difficile che rende il nemico più veloce.
Il percorso del nemico verso l'eroe viene calcolato utilizzando un algoritmo di ricerca in ampiezza (BFS) per trovare il percorso più breve.
Quando il nemico raggiunge l'eroe, il gioco termina e viene visualizzato il punteggio totale ottenuto dall'utente.