Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //PRIMA PARTE
- #include "compito.h"
- #include <iostream>
- using namespace std;
- #include <string.h>
- // costruttore di default
- PuzzleBobble::PuzzleBobble(){
- inr=0;
- for (int i=0;i<R;i++){
- for (int j=0;j<C;j++){
- mat [i][j]=' ';
- }
- }
- }
- //ridefinizione operatore di uscita come funzione globale
- ostream& operator << (ostream& stream , const PuzzleBobble& pb){
- for (int i=0;i<C+2;i++)
- stream<< "=";
- stream << endl;
- for (int i=0;i<R;i++)
- {
- stream << "|";
- for (int j=0;j<C;j++){
- stream << pb.mat[i][j] ;
- }
- stream << "|" << endl;
- }
- for (int i=0;i<C+2;i++)
- stream<< "=";
- stream << endl;
- return stream;
- }
- PuzzleBobble& PuzzleBobble::fire (int c, const char colore){
- // controllo sulla validità dell input
- if (colore==NULL || (colore!='R' && colore !='G' && colore !='Y' && colore !='B'))
- return *this;
- if (c<0 || c>R-1)
- return *this;
- //popsizionamento bolla nella prima casella vuota della colonna desiderata
- for (int j=inr;j<R;j++){
- if (mat[j][c]==' '){
- mat[j][c]=colore;
- break;
- }
- }
- // se la colonna è piena ritorna lascia invariata
- //ritornando l'istanza per riferimento rendo possibile la concatenazione
- return *this;
- }
- PuzzleBobble::operator int(){
- int conta=0;
- int max=0;
- // per ogi colonna conta il numero di elementi
- //e lo confronta con quello delle altre colonne
- //trovando il numero massimo di bolle contenute in una riga
- for (int i=inr;i<C;i++){
- conta=0;
- for (int j=0;j<R;j++){
- if (mat[j][i]!=' ')
- conta++;
- if (conta>=max)
- max=conta;
- }
- }
- return max;
- }
- PuzzleBobble& PuzzleBobble:: scroll(){
- for (int i=R-1;i>=inr-1;i--){
- for (int j=0;j<C;j++){
- if (i+1<10)
- mat[i+1][j]=mat[i][j];
- }
- }
- for (int j=0;j<6;j++)
- mat[0][j]='=';
- inr++;
- return *this;
- }
- void PuzzleBobble::compact(){
- for (int i=R-1;i>=inr-1;i--){
- for (int j=0;j<C;j++){
- //CORTOCIRCUITO
- if (i-1>=0 && mat[i-1][j]==' '){
- mat[i-1][j]=mat[i][j];
- mat[i][j]=' ';
- }
- }
- }
- return;
- }
- //CATERINA BRUCHI
- // fine file
- //seconda parte
- #include "compito.h"
- #include <iostream>
- using namespace std;
- #include <string.h>
- // costruttore di default
- PuzzleBobble::PuzzleBobble(){
- inr=0;
- for (int i=0;i<R;i++){
- for (int j=0;j<C;j++){
- mat [i][j]=' ';
- }
- }
- }
- //ridefinizione operatore di uscita come funzione globale
- ostream& operator << (ostream& stream , const PuzzleBobble& pb){
- for (int i=0;i<C+2;i++)
- stream<< "=";
- stream << endl;
- for (int i=0;i<R;i++)
- {
- stream << "|";
- for (int j=0;j<C;j++){
- stream << pb.mat[i][j] ;
- }
- stream << "|" << endl;
- }
- for (int i=0;i<C+2;i++)
- stream<< "=";
- stream << endl;
- return stream;
- }
- PuzzleBobble& PuzzleBobble::fire (int c, const char colore){
- // controllo sulla validità dell input
- if (colore==NULL || (colore!='R' && colore !='G' && colore !='Y' && colore !='B'))
- return *this;
- if (c<0 || c>R-1)
- return *this;
- //popsizionamento bolla nella prima casella vuota della colonna desiderata
- for (int j=inr;j<R;j++){
- if (mat[j][c]==' '){
- mat[j][c]=colore;
- break;
- }
- }
- // se la colonna è piena ritorna lascia invariata
- //ritornando l'istanza per riferimento rendo possibile la concatenazione
- return *this;
- }
- PuzzleBobble::operator int(){
- int conta=0;
- int max=0;
- // per ogi colonna conta il numero di elementi
- //e lo confronta con quello delle altre colonne
- //trovando il numero massimo di bolle contenute in una riga
- for (int i=inr;i<C;i++){
- conta=0;
- for (int j=0;j<R;j++){
- if (mat[j][i]!=' ')
- conta++;
- if (conta>=max)
- max=conta;
- }
- }
- return max;
- }
- PuzzleBobble& PuzzleBobble:: scroll(){
- for (int i=R-1;i>=inr-1;i--){
- for (int j=0;j<C;j++){
- if (i+1<10)
- mat[i+1][j]=mat[i][j];
- }
- }
- for (int j=0;j<6;j++)
- mat[0][j]='=';
- inr++;
- return *this;
- }
- void PuzzleBobble::compact(){
- for (int i=R-1;i>=inr-1;i--){
- for (int j=0;j<C;j++){
- //CORTOCIRCUITO
- if (i-1>=0 && mat[i-1][j]==' '){
- mat[i-1][j]=mat[i][j];
- mat[i][j]=' ';
- }
- }
- }
- return;
- }
- //CATERINA BRUCHI
- // fine file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement