/* FILE: randField.cpp last change: 23-July-2012 author: Romeo Rizzi * This program generates a random pacman field as for execise 2 "pacman" of the 23-July-2012 exam in Algorithms. * Usage syntax: * > randField out_file m n f p seed (righe, colonne, fantasmi, pillolazzi, random seed generator) */ #include #include #include #include #include using namespace std; const int MAX_M = 1000; const int MAX_N = 1000; int m, n; char cell[MAX_M+2][MAX_N+2]; int RandNumber(int min, int max) { /* returns an integer in [min, max] * see Stroustrup "The c++ Programming Language" 3th edition pg. 685 * for comments on the following manipulation choice. * In particular, considerations on the bad quality of low bits come into account. */ return min + (int) ( (max-min) * (double(rand()) / RAND_MAX) ); } void skip(int &i, int &j) { j++; if(j > n) { j = 1; i++; } if(i > m) { i = 1; } } int main(int argc, char** argv) { srand(time(NULL)); int f = 1, p = 1; m = atoi(argv[2]); n = atoi(argv[3]); if(argc > 4) f = atoi(argv[4]); if(argc > 5) p = atoi(argv[5]); if(argc > 6) srand( atoi(argv[6]) ); assert( f+p < m*n); for(int i = 1; i <= m; i++) for(int j = 1; j <= n; j++) if( RandNumber(1, 6) == 6 ) cell[i][j] = 'W'; else cell[i][j] = 'L'; cell[ RandNumber(1, m) ][ RandNumber(1, n) ] = 'M'; // il nostro pacman for(int count = 1; count <= f; count++) { int i = RandNumber(1, m); int j = RandNumber(1, n); while ( (cell[i][j] != 'L') & (cell[i][j] != 'W') ) skip(i,j); cell[i][j] = 'F'; } for(int count = 1; count <= p; count++) { int i = RandNumber(1, m); int j = RandNumber(1, n); while ( (cell[i][j] != 'L') & (cell[i][j] != 'W') ) skip(i,j); cell[i][j] = 'P'; } ofstream fout(argv[1]); fout << m << " " << n << endl; for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) fout << cell[i][j] << " "; fout << endl; } fout.close(); return 0; }