1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void randGenDiamond ( char grid5x5 [5][5] , char diamond );
void displayGame ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , int &score );
int main ()
{
srand(time(0));
const int gridSize = 5;
char grid5x5 [5][5] = {{'*','*','*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','*'},{'*','*','*','*','*'}};
char player = '&';
char diamond = '$';
char bomb = '%';
int score = 0;
grid5x5 [2][2] = player;
randGenDiamond ( grid5x5 , diamond );
displayGame ( grid5x5 , gridSize , player , diamond , bomb , score );
}
void randGenDiamond ( char grid5x5 [5][5] , char diamond )
{
int x = rand() % 5;
int y = rand() % 5;
int m = rand() % 5;
int n = rand() % 5;
if ( grid5x5 [x][y] != grid5x5[2][2] && grid5x5 [m][n] != grid5x5 [2][2] && x != m && y != n )
{
grid5x5 [x][y] = diamond;
grid5x5 [m][n] = diamond;
}
else
{
grid5x5 [x+1][y-1] = diamond;
grid5x5 [x-1][y+1] = diamond;
}
}
void displayGame ( char grid5x5 [5][5] , int gridSize , char player , char diamond , char bomb , int &score )
{
cout << endl;
cout << "\n ======== Bomber Game ========\n\n\n";
for ( int x = 0 ; x < gridSize ; x++ )
{
cout << " ";
for ( int y = 0 ; y < gridSize ; y++ )
{
cout << " " << grid5x5 [x][y];
}
cout << endl << endl;
}
cout << "\n -----------------------------\n";
cout << " Score: " << score;
cout << "\n -----------------------------\n\n";
}
|