Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. //PRIMA PARTE
  2.  
  3. #include "compito.h"
  4. #include <iostream>
  5. using namespace std;
  6. #include <string.h>
  7.  
  8. // costruttore di default
  9. PuzzleBobble::PuzzleBobble(){
  10. inr=0;
  11. for (int i=0;i<R;i++){
  12. for (int j=0;j<C;j++){
  13. mat [i][j]=' ';
  14. }
  15. }
  16. }
  17.  
  18.  
  19. //ridefinizione operatore di uscita come funzione globale
  20. ostream& operator << (ostream& stream , const PuzzleBobble& pb){
  21. for (int i=0;i<C+2;i++)
  22. stream<< "=";
  23. stream << endl;
  24.  
  25. for (int i=0;i<R;i++)
  26. {
  27. stream << "|";
  28. for (int j=0;j<C;j++){
  29. stream << pb.mat[i][j] ;
  30.  
  31. }
  32. stream << "|" << endl;
  33. }
  34.  
  35. for (int i=0;i<C+2;i++)
  36. stream<< "=";
  37. stream << endl;
  38.  
  39.  
  40. return stream;
  41. }
  42.  
  43.  
  44. PuzzleBobble& PuzzleBobble::fire (int c, const char colore){
  45. // controllo sulla validità dell input
  46. if (colore==NULL || (colore!='R' && colore !='G' && colore !='Y' && colore !='B'))
  47. return *this;
  48. if (c<0 || c>R-1)
  49. return *this;
  50.  
  51. //popsizionamento bolla nella prima casella vuota della colonna desiderata
  52. for (int j=inr;j<R;j++){
  53. if (mat[j][c]==' '){
  54. mat[j][c]=colore;
  55. break;
  56. }
  57.  
  58. }
  59. // se la colonna è piena ritorna lascia invariata
  60. //ritornando l'istanza per riferimento rendo possibile la concatenazione
  61. return *this;
  62.  
  63. }
  64.  
  65.  
  66. PuzzleBobble::operator int(){
  67. int conta=0;
  68. int max=0;
  69.  
  70. // per ogi colonna conta il numero di elementi
  71. //e lo confronta con quello delle altre colonne
  72. //trovando il numero massimo di bolle contenute in una riga
  73. for (int i=inr;i<C;i++){
  74. conta=0;
  75. for (int j=0;j<R;j++){
  76. if (mat[j][i]!=' ')
  77. conta++;
  78. if (conta>=max)
  79. max=conta;
  80.  
  81. }
  82. }
  83. return max;
  84. }
  85.  
  86.  
  87. PuzzleBobble& PuzzleBobble:: scroll(){
  88.  
  89. for (int i=R-1;i>=inr-1;i--){
  90. for (int j=0;j<C;j++){
  91. if (i+1<10)
  92. mat[i+1][j]=mat[i][j];
  93. }
  94. }
  95.  
  96. for (int j=0;j<6;j++)
  97. mat[0][j]='=';
  98.  
  99. inr++;
  100.  
  101. return *this;
  102.  
  103.  
  104. }
  105.  
  106. void PuzzleBobble::compact(){
  107.  
  108.  
  109. for (int i=R-1;i>=inr-1;i--){
  110. for (int j=0;j<C;j++){
  111. //CORTOCIRCUITO
  112. if (i-1>=0 && mat[i-1][j]==' '){
  113. mat[i-1][j]=mat[i][j];
  114. mat[i][j]=' ';
  115. }
  116.  
  117. }
  118. }
  119.  
  120. return;
  121.  
  122.  
  123. }
  124.  
  125.  
  126. //CATERINA BRUCHI
  127. // fine file
  128. //seconda parte
  129.  
  130. #include "compito.h"
  131. #include <iostream>
  132. using namespace std;
  133. #include <string.h>
  134.  
  135. // costruttore di default
  136. PuzzleBobble::PuzzleBobble(){
  137. inr=0;
  138. for (int i=0;i<R;i++){
  139. for (int j=0;j<C;j++){
  140. mat [i][j]=' ';
  141. }
  142. }
  143. }
  144.  
  145.  
  146. //ridefinizione operatore di uscita come funzione globale
  147. ostream& operator << (ostream& stream , const PuzzleBobble& pb){
  148. for (int i=0;i<C+2;i++)
  149. stream<< "=";
  150. stream << endl;
  151.  
  152. for (int i=0;i<R;i++)
  153. {
  154. stream << "|";
  155. for (int j=0;j<C;j++){
  156. stream << pb.mat[i][j] ;
  157.  
  158. }
  159. stream << "|" << endl;
  160. }
  161.  
  162. for (int i=0;i<C+2;i++)
  163. stream<< "=";
  164. stream << endl;
  165.  
  166.  
  167. return stream;
  168. }
  169.  
  170.  
  171. PuzzleBobble& PuzzleBobble::fire (int c, const char colore){
  172. // controllo sulla validità dell input
  173. if (colore==NULL || (colore!='R' && colore !='G' && colore !='Y' && colore !='B'))
  174. return *this;
  175. if (c<0 || c>R-1)
  176. return *this;
  177.  
  178. //popsizionamento bolla nella prima casella vuota della colonna desiderata
  179. for (int j=inr;j<R;j++){
  180. if (mat[j][c]==' '){
  181. mat[j][c]=colore;
  182. break;
  183. }
  184.  
  185. }
  186. // se la colonna è piena ritorna lascia invariata
  187. //ritornando l'istanza per riferimento rendo possibile la concatenazione
  188. return *this;
  189.  
  190. }
  191.  
  192.  
  193. PuzzleBobble::operator int(){
  194. int conta=0;
  195. int max=0;
  196.  
  197. // per ogi colonna conta il numero di elementi
  198. //e lo confronta con quello delle altre colonne
  199. //trovando il numero massimo di bolle contenute in una riga
  200. for (int i=inr;i<C;i++){
  201. conta=0;
  202. for (int j=0;j<R;j++){
  203. if (mat[j][i]!=' ')
  204. conta++;
  205. if (conta>=max)
  206. max=conta;
  207.  
  208. }
  209. }
  210. return max;
  211. }
  212.  
  213.  
  214. PuzzleBobble& PuzzleBobble:: scroll(){
  215.  
  216. for (int i=R-1;i>=inr-1;i--){
  217. for (int j=0;j<C;j++){
  218. if (i+1<10)
  219. mat[i+1][j]=mat[i][j];
  220. }
  221. }
  222.  
  223. for (int j=0;j<6;j++)
  224. mat[0][j]='=';
  225.  
  226. inr++;
  227.  
  228. return *this;
  229.  
  230.  
  231. }
  232.  
  233. void PuzzleBobble::compact(){
  234.  
  235.  
  236. for (int i=R-1;i>=inr-1;i--){
  237. for (int j=0;j<C;j++){
  238. //CORTOCIRCUITO
  239. if (i-1>=0 && mat[i-1][j]==' '){
  240. mat[i-1][j]=mat[i][j];
  241. mat[i][j]=' ';
  242. }
  243.  
  244. }
  245. }
  246.  
  247. return;
  248.  
  249.  
  250. }
  251.  
  252.  
  253. //CATERINA BRUCHI
  254. // fine file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement