Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Esercizio "Il Prato del Coniglio"
- * Avebdo un prato ( matrice di boolean di grandezza letta in input ) in cui esiste un coniglio che si ciba delle carote sparse in tutto il prato. Nella matrice le carote corrispondono al TRUE mentre l'assenza di carote corrisponde al FALSE. Il coniglio parte dal centro e si può spostare di una sola cella verticale o orizzontale.
- * Il coniglio ha una vita di 100 punti. Ad ogni salto perde un punto vita e ad ogni carota mangiata ne guadagna 10 punti.
- * Non può saltare al di fuori dei bordi.
- * Quanto resta in vita il coniglio prima che si finiscano le carote e muoia di fame???
- *
- * \\\\\\\\\\ Realizzato da Trupia Ludovico \\\\\\\\\\\\\
- */
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <cstdio>
- #ifdef WIN_32
- #define PULISCI system("cls")
- #else
- #define PULISCI system("clear")
- #endif
- #define PREMI { cin.ignore(); }
- using namespace std;
- int main ()
- {
- srand ( time ( NULL ) ) ;
- int n ;
- cout << "###################### \n##By#Trupia#Ludovico## \n\n\n\n Benvenuti in questo Programma" << endl << endl;
- cout << " Inserisci la grandezza del prato \\\\ " ;
- cin >> n ;
- if ( n <= 1 )
- {
- cout << endl << endl << "Il Cogniglio Non puo saltare, quindi nemmeno morire... forever Alone..." << endl << endl;
- return 0;
- }
- bool prato [ n ][ n ] , vivo = true ;
- int centro = ((n - 1)/2) , rabbX = centro , rabbY = centro , salta = 11, vita = 100 , salti = 0 ;
- for ( int i = 0 ; i < n ; i++ )
- {
- for ( int j = 0 ; j < n ; j++ )
- {
- prato [ i ] [ j ] = rand () % 2 ;
- }
- }
- cout << endl << endl;
- while ( vivo )
- {
- // INIZIO STAMPAGGIO MATRICE
- for ( int i = 0 ; i < n ; i++ )
- {
- cout << endl << endl;
- for ( int j = 0 ; j < n ; j++ )
- {
- cout << "|";
- if ( i == rabbY && j == rabbX )
- {
- cout << "#";
- }
- else
- {
- if ( prato [ i ][ j ] )
- {
- cout << "X";
- }
- else
- {
- cout << "O";
- }
- }
- cout << "| ";
- }
- }
- cout << endl << endl;
- // FINE STAMPAGGIO MATRICE
- cout << " Premere INVIO per continuare \\\\\\\\ ";
- PREMI;
- while ( salta > 10 )
- {
- salta = rand() % 4 + 1;
- salti++;
- switch ( salta )
- {
- case ( 1 ) : // Il Coniglio salta in alto!
- rabbY -= 1;
- if ( rabbY < 0 )
- {
- rabbY += 1;
- salta = 11;
- }
- else
- {
- vita--;
- }
- break;
- case ( 2 ) : // Il Coniglio salta a sinistra!
- rabbX -= 1;
- if ( rabbX < 0 )
- {
- rabbX += 1;
- salta = 11;
- }
- else
- {
- vita--;
- }
- break;
- case ( 3 ) : // IL Coniglio salta a destra!
- rabbX += 1;
- if ( rabbX == n || rabbX > n )
- {
- rabbX -= 1;
- salta = 11;
- }
- else
- {
- vita--;
- }
- break;
- case ( 4 ) : // Il Coniglio salta in basso!
- rabbY += 1;
- if ( rabbY == n || rabbY > n )
- {
- rabbY -= 1;
- salta = 11;
- }
- else
- {
- vita--;
- }
- break;
- }
- }
- salta = 11;
- if ( prato [rabbY][rabbX] )
- {
- prato [rabbY][rabbX] = false;
- vita += 10;
- }
- cout << endl << "Vita -> " << vita << endl ;
- if ( vita == 0 )
- {
- vivo = !vivo;
- }
- }
- cout << endl << endl << "Il Cogniglio e' morto!!! Dopo " << salti << " salti " << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement