Advertisement
belowzero

Untitled

Apr 29th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.09 KB | None | 0 0
  1. //1)lathos me ti malloc,de diavazo sosta onoma kai typo
  2. //2)elegxos gia athroisma xriston(prepei na einai toylaxiston 1)
  3. //3)olo to 3
  4. //4)check gia 26 grammata
  5. //5)apodesmeusi name kai type
  6. //6)den afinoyme tipota ellinika
  7. //7)backslash n stylish changes
  8. //8)na tsekaro an uparxei xoros se kathe malloc
  9. //9)na tsekaro gia yes kai no
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. int main(void)
  15. {
  16.  
  17. //----------------arxikes diastaseis tamplo -----------------
  18. int dimensions=10,yes,no,flag=1,max,length,*type,users=0, user, computer,sumi,sumj,obnum,k,newflag,co1,co2,co3,co4,spot;
  19. char yesno[4],*nam,**name,**array,typ[9],*symbol,sym;
  20. int i,players,j;
  21. int sp, counter=0, player,**pcoord;
  22. int userflag = 1;
  23. int direction,match,*wincon;
  24. int a1=0,exit,target,help;
  25. char move[6];
  26. // time_t t;
  27. //----------------epalitheusi gia to an thelei o xristis na paiksei me tis prokathorismens diastaseis------
  28. srand(time(NULL));
  29. do
  30. {
  31. printf("Do you want to play by defaut dimensions(10X10)?(yes/no) ");
  32. scanf("%s", yesno);
  33. yes=strcmp(yesno,"yes");
  34. no=strcmp(yesno,"no");
  35. if ((yes==0)||(no==0))
  36. {
  37. flag=0;
  38. }
  39. if (flag==1)
  40. {
  41. printf("\nWrong input.Please choose between default dimensions(yes) and custom dimensions(no) ");
  42. }
  43. }while (flag!=0);
  44. //---------------epalitheusi gia to oti oi custom diastaseis tha einai megaliteris i ises tou 5------
  45. if (no==0)
  46. {
  47. do
  48. {
  49. printf("\nChoose your own dimensions ");
  50. scanf("%d", &dimensions);
  51. if (dimensions<5)
  52. {
  53. printf("\nWrong input.Please choose a number >=5 and <=26");
  54. }
  55. }while((dimensions<5)||(dimensions>26));
  56. }
  57. //--------------------paixtes-------------------------
  58. //--------------------arithmos paixton-----------------
  59. do
  60. {
  61. printf("\nChoose the number of players (2 or 4) ");
  62. scanf("%d",&players);
  63. if (!((players==2)||(players==4)))
  64. {
  65. printf("\nWrong input.Please choose between 2 or 4 ");
  66. }
  67. }while(!((players==2)||(players==4)));
  68. //-------------------onoma kai typos paixton-----------
  69. nam=(char*)malloc(players*101);
  70. for (i=0;i<players;i++)
  71. {
  72. nam[i]='\0';
  73. }
  74. printf("\nEnter the name and the type(user or computer) of each player.Keep in mind that there should be at least one user ");
  75. type=(int*)malloc(players);
  76. symbol=(char*)malloc(players);
  77. for (i=0;i<players;i++)
  78. {
  79. symbol[i]='0';
  80. type[i]=1;
  81. }
  82. max=0;
  83. i=0;
  84. while((nam[i]!='\0')&&(i<101))
  85. {
  86. max++;
  87. i++;
  88. }
  89. length=0;
  90. for (i=101;i<(players*101);i=i+101)
  91. {
  92. while(nam[i]!='\0')
  93. {
  94. length++;
  95. i++;
  96. }
  97. if (length>max)
  98. {
  99. max=length;
  100. }
  101. i=i-length;
  102. length=0;
  103. }
  104. max=max+1;
  105. name=(char**)malloc(players*sizeof(char*));
  106. for (i=0;i<players;i++)
  107. {
  108. name[i]=(char*)malloc(max*sizeof(char));
  109. }
  110. for (i=0;i<players;i++)
  111. {
  112. printf("\nEnter the name of player number %d ",i+1);
  113. scanf("%s",&nam[i*101]);
  114. printf("\nEnter the type of player number %d ",i+1);
  115. flag=1;
  116. do
  117. {
  118. scanf("%s", typ);
  119. user=strcmp(typ,"user");
  120. computer=strcmp(typ,"computer");
  121. if ((user==0)||(computer==0))
  122. {
  123. flag=0;
  124. }
  125. if (flag==1)
  126. {
  127. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  128. }
  129. }while (flag!=0);
  130. printf("\nEnter the symbol of player number %d .You can choose between (#,*,&,@) ",i+1);
  131. flag=1;
  132. do
  133. {
  134. if (flag==2)
  135. {
  136. flag=1;
  137. }
  138. scanf("%s",&sym);
  139. if ((sym=='#')||(sym=='@')||(sym=='&')||(sym=='*'))
  140. {
  141. flag=0;
  142. for (j=0;j<players;j++)
  143. {
  144. if (sym==symbol[j])
  145. {
  146. flag=2;
  147. printf("Another player already has this symbol taken.Please choose another one (#,*,&,@) ");
  148. }
  149. }
  150. }
  151. if (flag==1)
  152. {
  153. printf("\nWrong input.Please choose one of your options (#,*,&,@) ");
  154. }
  155. if ((flag!=2)&&(flag!=1))
  156. {
  157. flag=0;
  158. symbol[i]=sym;
  159. }
  160. }while (flag!=0);
  161. if (user==0)
  162. {
  163. users++;
  164. type[i]=0;
  165. }
  166.  
  167. for (j=0;j<max;j++)
  168. {
  169. name[i][j]=nam[j+(i*101)];
  170. }
  171. }
  172.  
  173. if (users==0)
  174. {
  175. do
  176. {
  177. printf("There must be at least on user.Please re-enter the type of each player,making sure that there is one user! ");
  178. flag=1;
  179. for (i=0;i<players;i++)
  180. {
  181. printf("\n Enter the type of player number %d ",i+1);
  182. do
  183. {
  184.  
  185. scanf("%s", typ);
  186. user=strcmp(typ,"user");
  187. computer=strcmp(typ,"computer");
  188. if ((user==0)||(computer==0))
  189. {
  190. flag=0;
  191. }
  192. else
  193. {
  194. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  195. }
  196. }while (flag!=0);
  197.  
  198. }
  199. }while(users==0);
  200. }
  201. for (i=0;i<players;i++)
  202. {
  203. j=0;
  204. printf("\nThe player with the name ");
  205. while((name[i][j]!='\0')&&(j<max))
  206. {
  207. printf("%c",name[i][j]);
  208. j++;
  209. }
  210.  
  211. printf(" is a ");
  212. if (type[i]==0)
  213. {
  214. printf("user");
  215. }
  216. if (type[i]==1)
  217. {
  218. printf("computer");
  219. }
  220. printf(" and his symbol is %c ",symbol[i]);
  221. printf("\n");
  222. }
  223. flag=1;
  224. do
  225. {
  226. printf("\nDo you want the map to be populated by obstacles?(yes/no) ");
  227. scanf("%s", yesno);
  228. yes=strcmp(yesno,"yes");
  229. no=strcmp(yesno,"no");
  230. if ((yes==0)||(no==0))
  231. {
  232. flag=0;
  233. }
  234. if (flag==1)
  235. {
  236. printf("\nWrong input.Choose between (yes) and (no) ");
  237. }
  238. }while (flag!=0);
  239. sumi=dimensions+2;
  240. sumj=dimensions+3;
  241. array=(char**)malloc((sumi)*sizeof(char*));
  242. for (i=0;i<sumi;i++)
  243. {
  244. array[i]=(char*)malloc((sumj)*sizeof(char));
  245. }
  246.  
  247. for (i = 0; i < sumi; i++)
  248. {
  249. if ((i > 1) && (i < 11))
  250. {
  251. array[i][0] = i - 1 + '0';
  252. array[i][1] = ' ';
  253. array[i][2] = '|';
  254. for (j = 3; j < sumj; j++)
  255. {
  256. array[i][j] = '.';
  257. }
  258. }
  259. else
  260. {
  261. array[i][0] = ((i -1)/10) + '0';
  262. array[i][1] = ((i -1)%10) + '0';
  263. array[i][2] = '|';
  264. for (j = 3; j < sumj; j++)
  265. {
  266. array[i][j] = '.';
  267. }
  268. }
  269. if (i == 0)
  270. {
  271. array[i][2] = '|';
  272. for (j = 3; j < sumj; j++)
  273. {
  274. array[i][j] = j - 3 + 'a';
  275. }
  276. }
  277. if (i == 1)
  278. {
  279. for (j = 0; j < sumj; j++)
  280. {
  281. array[i][j] = '-';
  282. }
  283. }
  284. }
  285. array[0][0]=' ';
  286. array[0][1]=' ';
  287. array[0][2]=' ';
  288.  
  289. obnum = ((dimensions - 1)/2);
  290.  
  291. //after do{}while (counter < players);
  292. if (yes == 0){
  293. do{
  294. newflag=1;//used to confirm that the spot for the first piece of the obstacle is surrounded by at least one more dot
  295. do{
  296. do{
  297. co1 = (rand() % dimensions) + 2;
  298. co2 = (rand() % dimensions) + 3;
  299. }while (array[co1][co2] != '.');
  300. spot = rand() % 4;
  301. if((spot == 0) && (array[co1+1][co2] == '.')){
  302. co3=co1+1;
  303. co4=co2;
  304. newflag=0;
  305. }
  306. else if((spot == 1) && (array[co1-1][co2] == '.')){
  307. co3=co1-1;
  308. co4=co2;
  309. newflag=0;
  310. }
  311. else if((spot == 2) && (array[co1][co2+1] == '.')){
  312. co3=co1;
  313. co4=co2+1;
  314. newflag=0;
  315. }
  316. else if((spot == 3) && (array[co1][co2-1] == '.')){
  317. co3=co1;
  318. co4=co2-1;
  319. newflag=0;
  320. }
  321. }while(newflag!=0);
  322. array[co1][co2] = 'X';//there was a random } here. keep this in mind...
  323. array[co3][co4] = 'X';
  324. obnum--;
  325. }while(obnum > 0);
  326. }
  327. //direction becomes 0 (up), 1 (left) or 2 (right) through the use of rand(). Keep this comment in the code
  328. //it is later used to check a direction's corresponding starting point for availability to put a player on
  329. sp = (dimensions/2) + 1;
  330. wincon=(int*)malloc(players*sizeof(int));
  331. //malloc an array the size of players called wincon. it will save the characters u, l, r and d
  332. //which will be the opposite of the player's direction when he is being given a starting point
  333. //malloc an array named pcoord which will be players x 2, where player coordinates will be saved
  334. //initialize all of pcoord to be dimensions + 1
  335. pcoord=(int**)malloc(players*sizeof(int*));
  336. for (i=0;i<players;i++)
  337. {
  338. pcoord[i]=(int*)malloc(2*sizeof(int));
  339. }
  340. for (i=0;i<players;i++)
  341. {
  342. wincon[i]=5;
  343. for (j=0;j<2;j++)
  344. {
  345. pcoord[i][j]=(dimensions+7);
  346. }
  347. }
  348. do
  349. {
  350. player=rand()%players;
  351. if(pcoord[player][0] == (dimensions + 7))
  352. { //this if checks if the player has already had his starting point set
  353. if((type[player] == 0) && (userflag == 1))
  354. { //this if checks for the first randomly chosen player
  355. pcoord[player][0] = sumi-1;
  356. pcoord[player][1] = sp+1;
  357. wincon[player] = 1;
  358. array[sumi-1][sp+1] = symbol[player];
  359. userflag = 0;
  360. counter++; //now that a new player has had his starting point set, the counter can increment
  361. }
  362. else
  363. {
  364. if (players==2)
  365. {
  366. pcoord[player][0]=2;
  367. pcoord[player][1]=sp+1;
  368. wincon[player]=3;
  369. array[2][sp+1]=symbol[player];
  370. counter++;
  371. }
  372. if (players==4)
  373. {
  374. direction = rand()%3;
  375. //randomly check a direction
  376. //check for direction's availability. we could potentially use a small array, but i did it the hard way
  377. //make the check in a loop. might need a new variable
  378. if ((direction == 0) && (array[2][sp] == '.'))
  379. { //0 is for starting position up (array[2][sp])
  380. pcoord[player][0] = 2;
  381. pcoord[player][1] = sp;
  382. wincon[player] = 3;
  383. array[2][sp+1] = symbol[player];
  384. counter++; //now that a new player has had his starting point set, the counter can increment
  385. }
  386. else if((direction == 1) && (array[sp][3] == '.'))
  387. { //1 is for starting position left (array[sp][3])
  388. pcoord[player][0] = sp;
  389. pcoord[player][1] = 3;
  390. wincon[player] = 2;
  391. array[sp][3] = symbol[player];
  392. counter++; //now that a new player has had his starting point set, the counter can increment
  393. }
  394. else if((direction == 2) && (array[sp][sumj-1] == '.'))
  395. {
  396. //2 is for starting position right (array[sp][sumj])
  397. //could have gone for a simple else, but put the check in as a precaution
  398. pcoord[player][0] = sp;
  399. pcoord[player][1] = sumj-1;
  400. wincon[player] = 4;
  401. array[sp][sumj-1] = symbol[player];
  402. counter++; //now that a new player has had his starting point set, the counter can increment
  403. }
  404. }
  405. }
  406. }
  407. }while (counter < players);
  408. for (i = 0; i < sumi; i++)
  409. {
  410. printf("\n");
  411. for (j=0;j<sumj;j++)
  412. {
  413. printf("%c ",array[i][j]);
  414. }
  415. }
  416. for(i=0;i<players;i++)
  417. {
  418. exit=1;
  419. help=1;
  420. if (players==2)
  421. {
  422. if ((wincon[i]==1)||(wincon[i]==3))
  423. {
  424. j=0;
  425. while((name[i][j]!='\0')&&(j<max))
  426. {
  427. printf("\n%d",wincon[i]);
  428. j++;
  429. }
  430. do
  431. {
  432. printf(" is playing.Enter the coordinates of the square you want to move to,or the coordinates of the squares you want to place an obstacle.You can also enter 'help' or 'exit')");
  433. scanf("%s", move);
  434. exit=strcmp(move,"exit");
  435. help=strcmp(move,"help");
  436. flag=1;
  437. if (!(exit==0)||(help==0))
  438. {
  439. target=strlen(move);
  440. match=0;
  441. j=0;
  442. while((j<target)&&(flag==1))
  443. {
  444. if (j==0)
  445. {
  446. for (k = 3; k < sumj; k++)
  447. {
  448. if (array[0][k]!=move[j])
  449. {
  450. match++;
  451. }
  452. else
  453. {
  454. a1=0;
  455. }
  456. }
  457.  
  458. }
  459. else
  460. {
  461. //an o proigoumenos einai arithmos
  462. if(a1==1)
  463. {
  464. for (k = 3; k < sumj; k++)
  465. {
  466. if (array[0][k]!=move[j])
  467. {
  468. match++;
  469. }
  470. else
  471. {
  472. a1=0;
  473. }
  474. }
  475. for (k=2;k<sumi;k++)
  476. {
  477. if(array[k][1]!=move[j])
  478. {
  479. match++;
  480. }
  481. else
  482. {
  483. a1=1;
  484. }
  485. }
  486. }
  487. else
  488. {
  489. for(k=2;k<sumi;k++)
  490. {
  491. if(array[k][0]!=move[j])
  492. {
  493. match++;
  494. }
  495. else
  496. {
  497. a1=1;
  498. }
  499. }
  500. }
  501. }
  502. j++;
  503. }
  504.  
  505. }
  506. if (match!=0)
  507. {
  508. printf("\n Wrong input.");
  509. }
  510. }while (match!=0);
  511. }
  512. }
  513. if (players==4)
  514. {
  515. if (wincon[i]==i+1)
  516. {
  517. j=0;
  518. while((name[i][j]!='\0')&&(j<max))
  519. {
  520. printf("\n%d",wincon[i]);
  521. j++;
  522. }
  523. do
  524. {
  525. printf(" is playing.Enter the coordinates of the square you want to move to,or the coordinates of the squares you want to place an obstacle.You can also enter 'help' or 'exit')");
  526. scanf("%s", move);
  527. exit=strcmp(move,"exit");
  528. help=strcmp(move,"help");
  529. // flag=1;
  530. if (!(exit==0)||(help==0))
  531. {
  532. target=strlen(move);
  533. match=0;
  534. j=0;
  535. while((j<target)&&(flag==1))
  536. {
  537. if (j==0)
  538. {
  539. for (k = 3; k < sumj; k++)
  540. {
  541. if (array[0][k]!=move[j])
  542. {
  543. match++;
  544. }
  545. else
  546. {
  547. a1=0;
  548. }
  549. }
  550.  
  551. }
  552. else
  553. {
  554. //an o proigoumenos einai arithmos
  555. if(a1==1)
  556. {
  557. for (k = 3; k < sumj; k++)
  558. {
  559. if (array[0][k]!=move[j])
  560. {
  561. match++;
  562. }
  563. else
  564. {
  565. a1=0;
  566. }
  567. }
  568. for (k=2;k<sumi;k++)
  569. {
  570. if(array[k][1]!=move[j])
  571. {
  572. match++;
  573. }
  574. else
  575. {
  576. a1=1;
  577. }
  578. }
  579. }
  580. else
  581. {
  582. for(k=2;k<sumi;k++)
  583. {
  584. if(array[k][0]!=move[j])
  585. {
  586. match++;
  587. }
  588. else
  589. {
  590. a1=1;
  591. }
  592. }
  593. }
  594. }
  595. j++;
  596. }
  597.  
  598. }
  599. if (match!=0)
  600. {
  601. printf("\n Wrong input.");
  602. }
  603. }while (match!=0);
  604. }
  605. }
  606. }
  607.  
  608. for (i=0;i<players;i++)
  609. {
  610. free (name[i]);
  611. free(pcoord[i]);
  612. }
  613. for (i = 0; i < sumi; i++)
  614. {
  615. free (array[i]);
  616. }
  617. free(nam);
  618. free(type);
  619. free(symbol);
  620. free(wincon);
  621. free(array);
  622. free(name);
  623. free(pcoord);
  624. return 0;
  625. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement