Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1. /* Project 1
  2. Angelos Georgios Drapaniotis AM: 2022201600046
  3. Anastasia Vorri AM:2025201000060
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include <ctype.h>
  10. #define MIN 3
  11. #define MAX 24
  12.  
  13. int getCoords(char str[20], int arg);
  14. void bomb(int a, int b, char** level,int N,int M, int *score);
  15. void hammer(int a, int b, char** level, char c,int N,int M, int *score);
  16. void gravity(char **level, int type,int N,int M);
  17.  
  18. int main()
  19. {
  20. int i,j, k, N, M, flag, x, y, exit, c=0, levelcomplete, pista=1, temp;
  21. int *score;
  22. char str[20];
  23. char **level;
  24. //diastaseis ths pistas
  25. do{
  26. printf("Dwse arithmo diastasewn.\n");
  27. scanf("%d%d",&N,&M);
  28. if(N<MIN||N>MAX||M<MIN||M>MAX)
  29. printf("\n[!] Oi dunates distaseis einai megaluteres tou %d kai mikroteres tou %d.\n\n",MIN,MAX);
  30. }while(N<MIN||N>MAX||M<MIN||M>MAX);
  31. level = (char**) malloc(N * sizeof(char*));
  32. score = (int*)malloc(sizeof(int));
  33. exit = 0;
  34. str[8] = 0;
  35. srand(time(NULL));
  36. // xrwmata ston pinaka
  37. for(i=0;i<N;i++){
  38. level[i] = (char*) malloc(M * sizeof(char));
  39. for(j=0;j<M;j++){
  40. level[i][j] = rand()%9 + 49;
  41. }
  42. }
  43. if(level==NULL||score==NULL){
  44. exit = 1;
  45. printf("H KATAXWRHSH MNHMHS APETYXE\n");
  46. }
  47. *score = 0;
  48. do{
  49. // emfanish pistas
  50. if(exit==1)
  51. continue;
  52. levelcomplete = 0;
  53. printf(" ");
  54. for(i=0;i<M;i++){
  55. printf("%d ",i+1); // sthles
  56. }
  57. printf("\n");
  58. printf(" --");
  59. if(M>9)
  60. printf("-");
  61. for(i=0;i<M;i++){
  62. if(i>9){
  63. printf("-");
  64. }
  65. printf("--");
  66. }
  67. printf("\n");
  68. for(i=0;i<N;i++) // grammes
  69. {
  70. if(i<9){
  71. printf(" ");
  72. }
  73. printf("%d|", i+1);
  74. for(j=0;j<M;j++)
  75. {
  76. if(j<9){
  77. printf(" %c",level[i][j]);
  78. }else
  79. printf(" %c",level[i][j]);
  80. }
  81. printf("\n");
  82. }
  83.  
  84. flag = 0;
  85. do{
  86. // elegxos entolhs pou dwthike apo xrhsth
  87. if(flag==1)
  88. {
  89. printf("\n[!]Kati phge strava, prospathise pali!\n");
  90. }
  91. // Emfanizete to score, to epipedo kai dialegei o xrhsths thn kinhsh tou
  92. printf("\n\nPista #%d, Score: %d\nDialekse thn epomenh kinhsh sou.\n", pista, *score);
  93. if(c==0)
  94. while ((c = getchar()) != '\n' && c != EOF);
  95. c=1;
  96. fgets(str, 20, stdin);
  97. printf("\n\n");
  98. for(i=0;i<20;i++){
  99. if(str[i]=='\n')
  100. str[i]='\0';
  101. }
  102.  
  103. /*
  104. Typoi Entolwn:
  105.  
  106. h A,B
  107. b A,B
  108. pr
  109. pl
  110. a A,B C(1-9)
  111. x
  112.  
  113. */
  114. //elegxei na einai swsth h entolh
  115. flag = 0;
  116. if((str[0]=='h'||str[0]=='b'||str[0]=='a')&&str[1]==' '){
  117. i = 1;
  118. do{
  119. i++;
  120. if(str[i]<48||str[i]>57){
  121. flag = 1;
  122. }
  123.  
  124. }while(flag==0);
  125. if(str[i]==','){
  126. flag = 0;
  127. }
  128. if(flag==0){
  129. do{
  130. i++;
  131. if(str[i]<48||str[i]>57){
  132. flag = 1;
  133. }
  134.  
  135. }while(flag==0);
  136. }
  137. if(str[i]=='\0'&&(str[0]=='h'||str[0]=='b')){
  138. flag = 0; // entolh typou h/b A,B
  139. x = getCoords(str, 1);
  140. y = getCoords(str, 2);
  141. if(x > 0 && x <= N && y > 0 && y <= M ){
  142. if(str[0]=='b')
  143. {
  144. bomb(x-1,y-1,level, N, M, score);
  145. gravity(level,0,N,M);
  146. }else if(str[0]=='h'){
  147. hammer(x-1,y-1,level,level[x-1][y-1], N, M, score);
  148. gravity(level,0,N,M);
  149. }
  150. }else{
  151. flag = 1;
  152. }
  153. }
  154. else if(str[i]=' '&&(str[i+1]>=49&&str[i+1]<=57)){
  155. flag = 0; //entolh typou a A,B C(1-9)
  156. x = getCoords(str, 1);
  157. y = getCoords(str, 2);
  158. if(x > 0 && x <= N && y > 0 && y <= M ){
  159. k=0;
  160. while(str[k]!='\0'){
  161. k++;
  162. }
  163. if(level[x-1][y-1]=='.'){
  164. level[x-1][y-1]=str[k-1];
  165. gravity(level,0,N,M);
  166. }
  167. else{
  168. printf("\n[!]To plakidio prepei na einai keno.\n");
  169. flag=1;
  170. }
  171. }
  172. else
  173. flag = 1;
  174. }
  175. }
  176. // push right/push left
  177. else if(str[0]=='p'){
  178. if(str[1]=='r'&&str[2]=='\0')
  179. {
  180. flag = 0; // entolh pr
  181. gravity(level,1,N,M);
  182. }
  183.  
  184. else if(str[1]=='l'&&str[2]=='\0')
  185. {
  186. flag = 0; // entolh pr
  187. gravity(level,2,N,M);
  188. }
  189. }
  190. // entolh x
  191. else if((str[0]=='x'||str[0]=='X')&&str[1]=='\0')
  192. {
  193. flag = 0;
  194. exit = 1;
  195. printf("\n\nHello darkness, my old friend\nIve come to talk with you again\nBecause a vision softly creeping\nLeft its seeds while I was sleeping\nAnd the vision that was planted in my brain\nStill remains\nWithin the sound of silence\n\nIn restless dreams I walked alone\nNarrow streets of cobblestone\nNeath the halo of a street lamp\nI turned my collar to the cold and damp\nWhen my eyes were stabbed by the flash of a neon light\nThat split the night\nAnd touched the sound of silence\n\nAnd in the naked light I saw\nTen thousand people, maybe more\nPeople talking without speaking\nPeople hearing without listening\nPeople writing songs that voices never share\nNo one dared\nDisturb the sound of silence\n\nFools said I, You do not know\nSilence like a cancer grows\nHear my words that I might teach you\nTake my arms that I might reach you\nBut my words like silent raindrops fell\nAnd echoed in the wells of silence\n\nAnd the people bowed and prayed\nTo the neon god they made\nAnd the sign flashed out its warning\nIn the words that it was forming\n\nAnd the sign said, The words of the prophets\nAre written on the subway walls\nAnd tenement halls\nAnd whispered in the sounds of silence\n\n");
  196. }
  197. else
  198. flag = 1;
  199.  
  200. }while(flag == 1);
  201. levelcomplete = 1;
  202. i=0;
  203. do{
  204. j=0;
  205. do{
  206. if(level[i][j]!='.')
  207. levelcomplete = 0;
  208. j++;
  209. }while(j<M&&levelcomplete==1);
  210. i++;
  211. }while(i<N&&levelcomplete==1);
  212. if(levelcomplete==1){
  213. free(level);// apodesmeush mnhmhs
  214. printf("\n\nNikhses thn pista megethous: %d X %d!!!\n",N,M);
  215. if(N<MAX&&M<MAX){
  216. N++;
  217. M++;
  218. printf("As anevasoume ligo thn duskolia me mia pista megethous: %d X %d\n\n",N,M);// megethos pistas ginete +1
  219. level = (char**) malloc(N * sizeof(char*));
  220. for(i=0;i<N;i++){
  221. level[i] = (char*) malloc(M * sizeof(char));
  222. for(j=0;j<M;j++){
  223. level[i][j] = rand()%9 + 49;
  224. }
  225. }
  226. }
  227. else{
  228. printf("Termatises to paixnidi sthn megaluterh pista WOW EISAI TOSO KALOOOOOS!!!!!\n");
  229. exit=1;
  230. }
  231. pista++;
  232. }
  233.  
  234. }while(exit == 0);
  235. printf("\n\nPerasmenes Pistes: %d\n\nScore: %d\n\n", pista-1, *score);// emfanish teliko score
  236. free(level);// apodesmeush mnhmhs
  237. free(score);// apodesmeush mnhmhs
  238.  
  239. return 0;
  240. }
  241.  
  242.  
  243.  
  244. void bomb(int a, int b, char** level,int N,int M, int *score){
  245.  
  246. if(level[a][b] != '.'){
  247. level[a][b] = '.';
  248. *score+=10;
  249. }
  250. if((a-1>=0)&&b-1>=0&&level[a-1][b-1] != '.'){
  251. level[a-1][b-1] = '.';
  252. *score+=10;
  253. }
  254. if((a+1<=N-1)&&(b+1<=M-1)&&level[a+1][b+1] != '.'){
  255. level[a+1][b+1] = '.';
  256. *score+=10;
  257. }
  258. if((a-1>=0)&&b+1<=M-1&&level[a-1][b+1] != '.'){
  259. level[a-1][b+1] = '.';
  260. *score+=10;
  261. }
  262. if((a+1<=N-1)&&b-1>=0&&level[a+1][b-1] != '.'){
  263. level[a+1][b-1] = '.';
  264. *score+=10;
  265. }
  266. if(b-1>=0&&level[a][b-1] != '.'){
  267. level[a][b-1] = '.';
  268. *score+=10;
  269. }
  270. if(b+1<=M-1&&level[a][b+1] != '.'){
  271. level[a][b+1] = '.';
  272. *score+=10;
  273. }
  274. if(a-1>=0&&level[a-1][b] != '.'){
  275. level[a-1][b] = '.';
  276. *score+=10;
  277. }
  278. if(a+1<=N-1&&level[a+1][b] != '.'){
  279. level[a+1][b] = '.';
  280. *score+=10;
  281. }
  282. printf("\n");
  283. }
  284.  
  285. void hammer(int a, int b, char** level,char c,int N,int M,int *score){
  286.  
  287. if(level[a][b] != '.'){
  288. level[a][b] = '.';
  289. *score+=30;
  290. if(a-1>=0)
  291. if(level[a-1][b] == c)
  292. {
  293. hammer(a-1,b,level,c,N,M,score);
  294. }
  295. if(a+1<=N-1)
  296. if(level[a+1][b] == c)
  297. {
  298. hammer(a+1,b,level,c,N,M,score);
  299. }
  300. if(b-1>=0)
  301. if(level[a][b-1] == c)
  302. {
  303. hammer(a,b-1,level,c,N,M,score);
  304. }
  305. if(b+1<=M-1)
  306. if(level[a][b+1] == c)
  307. {
  308. hammer(a,b+1,level,c,N,M,score);
  309. }
  310. }
  311. }
  312.  
  313.  
  314. void gravity(char **level, int type,int N,int M)
  315. {
  316. int i,j,k,max;
  317. if(N>M)
  318. max = N;
  319. else
  320. max = M;
  321. for(k=0;k<max+1;k++){
  322. for(i=0;i<N;i++)
  323. {
  324. for(j=0;j<M;j++)
  325. {
  326. if(type==0&&i+1<=N-1)
  327. {
  328.  
  329. if(level[i+1][j] =='.')//gravity
  330. {
  331. level[i+1][j] = level[i][j];
  332. level[i][j] = '.';
  333. }
  334. }
  335. else if(type==1&&j+1<=M-1)
  336. {
  337.  
  338. if(level[i][j+1] =='.')//pr/pl
  339. {
  340. level[i][j+1] = level[i][j];
  341. level[i][j] = '.';
  342. }
  343. }
  344. else if(type==2&&j-1>=0)
  345. {
  346.  
  347. if(level[i][j-1] =='.')//pr/pl
  348. {
  349. level[i][j-1] = level[i][j];
  350. level[i][j] = '.';
  351. }
  352. }
  353. }
  354. }
  355.  
  356. }
  357.  
  358. }
  359.  
  360. int getCoords(char str[20], int arg){//vriskei suntetagmenes kai tis metatrepei apo xarakthres ascii se arithmous
  361. int i,m,j, coord;
  362. char stop='\0';
  363. i = -1;
  364. m = 0;
  365. coord = 0;
  366. if(str[0]=='a')
  367. stop=' ';
  368. if(arg == 1){
  369. do{
  370. i++;
  371. }while(str[i]!=',');
  372. do{
  373. i--;
  374. if(m==0)//metatroph apo xarakthra se arithmo mesw ascii
  375. coord +=(str[i]-48);
  376. else if(m==1)
  377. coord += (str[i]-48)*10;
  378. m++;
  379. }while(str[i-1]!=' ');
  380. }
  381. else{
  382. do{
  383. i++;
  384. }while(str[i]!=stop); //stop: '\0' or ' '
  385. if(stop ==' '){
  386. do{
  387. i++;
  388. }while(str[i]!=1); //stop: ' '
  389.  
  390. }
  391. do{
  392. i--;
  393. if(m==0)
  394. coord +=(str[i]-48);
  395. else if(m==1)
  396. coord += (str[i]-48)*10;
  397. m++;
  398. }while(str[i-1]!=',');
  399. }
  400.  
  401. return coord;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement