Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.66 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,sum,blanks=0,obcounter,p;
  19. char yesno[4],*nam,**name,**array,**possible,typ[9],*symbol,sym,set;
  20. int i,players,j,l;
  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. //-----------------arxikopoisi enos pinaka nam,ston opoio tha perasoume ola ta onomata----------
  71. for (i=0;i<players;i++)
  72. {
  73. nam[i]='\0';
  74. }
  75. printf("\nEnter the name and the type(user or computer) of each player.Keep in mind that there should be at least one user ");
  76. type=(int*)malloc(players);
  77. symbol=(char*)malloc(players);
  78. //---------arxikopoioume ton pinaka symbol,vazontas '0' se kathe thesi
  79. //----------arxikopoioume ton pinaka type me 1,theorontas oti arxika einai oloi computers
  80. for (i=0;i<players;i++)
  81. {
  82. symbol[i]='0';
  83. type[i]=1;
  84. }
  85. for (i=0;i<players;i++)
  86. {
  87. printf("\nEnter the name of player number %d ",i+1);
  88. scanf("%s",&nam[i*101]);
  89. printf("\nEnter the type of player number %d ",i+1);
  90. flag=1;
  91. do
  92. {
  93. scanf("%s", typ);
  94. user=strcmp(typ,"user");
  95. computer=strcmp(typ,"computer");
  96. if ((user==0)||(computer==0))
  97. {
  98. flag=0;
  99. }
  100. if (flag==1)
  101. {
  102. printf("\nWrong input.Please choose one of your options (user,computer) ");
  103. }
  104. }while (flag!=0);
  105.  
  106. printf("\nEnter the symbol of player number %d .You can choose between (#,*,&,@) ",i+1);
  107. flag=1;
  108. do
  109. {
  110. if (flag==2)
  111. {
  112. flag=1;
  113. }
  114. scanf("%s",&sym);
  115. if ((sym=='#')||(sym=='@')||(sym=='&')||(sym=='*'))
  116. {
  117. flag=0;
  118. for (j=0;j<players;j++)
  119. {
  120. if (sym==symbol[j])
  121. {
  122. flag=2;
  123. printf("Another player already has this symbol taken.Please choose another one (#,*,&,@) ");
  124. }
  125. }
  126. }
  127. if (flag==1)
  128. {
  129. printf("\nWrong input.Please choose one of your options (#,*,&,@) ");
  130. }
  131. if ((flag!=2)&&(flag!=1))
  132. {
  133. flag=0;
  134. symbol[i]=sym;
  135. }
  136. }while (flag!=0);
  137. if (user==0)
  138. {
  139. users++;
  140. type[i]=0;
  141. }
  142. }
  143. //-------------elegxos gia enan toulaxiston paixti--------------
  144. if (users==0)
  145. {
  146. do
  147. {
  148. printf("There must be at least on user.Please re-enter the type of each player,making sure that there is one user! ");
  149. flag=1;
  150. for (i=0;i<players;i++)
  151. {
  152. user=1;
  153. computer=1;
  154. printf("\nEnter the type of player number %d ",i+1);
  155. do
  156. {
  157.  
  158. scanf("%s", typ);
  159. user=strcmp(typ,"user");
  160. computer=strcmp(typ,"computer");
  161. if ((user==0)||(computer==0))
  162. {
  163. flag=0;
  164. }
  165. else
  166. {
  167. printf("\nWrong input.Please choose one of your options (user,computer) ");
  168. }
  169. }while (flag!=0);
  170. if(user==0)
  171. {
  172. users++;
  173. type[i]=0;
  174. }
  175. }
  176.  
  177. }while(users==0);
  178. }
  179. max=0;
  180. i=0;
  181. while((nam[i]!='\0')&&(i<101))
  182. {
  183. max++;
  184. i++;
  185. }
  186. length=0;
  187. for (i=101;i<(players*101);i=i+101)
  188. {
  189. while(nam[i]!='\0')
  190. {
  191. length++;
  192. i++;
  193. }
  194. if (length>max)
  195. {
  196. max=length;
  197. }
  198. i=i-length;
  199. length=0;
  200. }
  201. max=max+1;
  202. name=(char**)malloc(players*sizeof(char*));
  203. for (i=0;i<players;i++)
  204. {
  205. name[i]=(char*)malloc(max*sizeof(char));
  206. }
  207. for (j=0;j<max;j++)
  208. {
  209. name[i][j]=nam[j+(i*101)];
  210. }
  211. for (i=0;i<players;i++)
  212. {
  213. j=0;
  214. printf("\nThe player with the name ");
  215. while((name[i][j]!='\0')&&(j<max))
  216. {
  217. printf("%c",name[i][j]);
  218. j++;
  219. }
  220.  
  221. printf(" is a ");
  222. if (type[i]==0)
  223. {
  224. printf("user");
  225. }
  226. if (type[i]==1)
  227. {
  228. printf("computer");
  229. }
  230. printf(" and his symbol is %c ",symbol[i]);
  231. printf("\n");
  232. }
  233. flag=1;
  234. do
  235. {
  236. printf("\nDo you want the map to be populated by obstacles?(yes/no) ");
  237. scanf("%s", yesno);
  238. yes=strcmp(yesno,"yes");
  239. no=strcmp(yesno,"no");
  240. if ((yes==0)||(no==0))
  241. {
  242. flag=0;
  243. }
  244. if (flag==1)
  245. {
  246. printf("\nWrong input.Choose between (yes) and (no) ");
  247. }
  248. }while (flag!=0);
  249. sumi=dimensions+2;
  250. sumj=dimensions+3;
  251. array=(char**)malloc((sumi)*sizeof(char*));
  252. possible=(char**)malloc((sumi)*sizeof(char*));
  253. for (i=0;i<sumi;i++)
  254. {
  255. array[i]=(char*)malloc((sumj)*sizeof(char));
  256. possible[i]=(char*)malloc((sumj)*sizeof(char));
  257.  
  258. }
  259.  
  260. for (i = 0; i < sumi; i++)
  261. {
  262. if ((i > 1) && (i < 11))
  263. {
  264. array[i][0] = i - 1 + '0';
  265. array[i][1] = ' ';
  266. array[i][2] = '|';
  267. for (j = 3; j < sumj; j++)
  268. {
  269. array[i][j] = '.';
  270. }
  271. }
  272. else
  273. {
  274. array[i][0] = ((i -1)/10) + '0';
  275. array[i][1] = ((i -1)%10) + '0';
  276. array[i][2] = '|';
  277. for (j = 3; j < sumj; j++)
  278. {
  279. array[i][j] = '.';
  280. }
  281. }
  282. if (i == 0)
  283. {
  284. array[i][2] = '|';
  285. for (j = 3; j < sumj; j++)
  286. {
  287. array[i][j] = j - 3 + 'a';
  288. }
  289. }
  290. if (i == 1)
  291. {
  292. for (j = 0; j < sumj; j++)
  293. {
  294. array[i][j] = '-';
  295. }
  296. }
  297. }
  298. array[0][0]=' ';
  299. array[0][1]=' ';
  300. array[0][2]=' ';
  301.  
  302. obnum = ((dimensions - 1)/2);
  303.  
  304. //after do{}while (counter < players);
  305. if (yes == 0){
  306. do{
  307. newflag=1;//used to confirm that the spot for the first piece of the obstacle is surrounded by at least one more dot
  308. do{
  309. do{
  310. co1 = (rand() % dimensions) + 2;
  311. co2 = (rand() % dimensions) + 3;
  312. }while (array[co1][co2] != '.');
  313. spot = rand() % 4;
  314. if((spot == 0) && (array[co1+1][co2] == '.')){
  315. co3=co1+1;
  316. co4=co2;
  317. newflag=0;
  318. }
  319. else if((spot == 1) && (array[co1-1][co2] == '.')){
  320. co3=co1-1;
  321. co4=co2;
  322. newflag=0;
  323. }
  324. else if((spot == 2) && (array[co1][co2+1] == '.')){
  325. co3=co1;
  326. co4=co2+1;
  327. newflag=0;
  328. }
  329. else if((spot == 3) && (array[co1][co2-1] == '.')){
  330. co3=co1;
  331. co4=co2-1;
  332. newflag=0;
  333. }
  334. }while(newflag!=0);
  335. array[co1][co2] = 'X';//there was a random } here. keep this in mind...
  336. array[co3][co4] = 'X';
  337. obnum--;
  338. }while(obnum > 0);
  339. }
  340. //direction becomes 0 (up), 1 (left) or 2 (right) through the use of rand(). Keep this comment in the code
  341. //it is later used to check a direction's corresponding starting point for availability to put a player on
  342. sp = (dimensions/2) + 1;
  343. wincon=(int*)malloc(players*sizeof(int));
  344. //malloc an array the size of players called wincon. it will save the characters u, l, r and d
  345. //which will be the opposite of the player's direction when he is being given a starting point
  346. //malloc an array named pcoord which will be players x 2, where player coordinates will be saved
  347. //initialize all of pcoord to be dimensions + 1
  348. pcoord=(int**)malloc(players*sizeof(int*));
  349. for (i=0;i<players;i++)
  350. {
  351. pcoord[i]=(int*)malloc(2*sizeof(int));
  352. }
  353. for (i=0;i<players;i++)
  354. {
  355. wincon[i]=5;
  356. for (j=0;j<2;j++)
  357. {
  358. pcoord[i][j]=(dimensions+7);
  359. }
  360. }
  361. do
  362. {
  363. player=rand()%players;
  364. if(pcoord[player][0] == (dimensions + 7))
  365. { //this if checks if the player has already had his starting point set
  366. if((type[player] == 0) && (userflag == 1))
  367. { //this if checks for the first randomly chosen player
  368. pcoord[player][0] = sumi-1;
  369. pcoord[player][1] = sp+1;
  370. wincon[player] = 1;
  371. array[sumi-1][sp+1] = symbol[player];
  372. userflag = 0;
  373. counter++; //now that a new player has had his starting point set, the counter can increment
  374. }
  375. else
  376. {
  377. if (players==2)
  378. {
  379. pcoord[player][0]=2;
  380. pcoord[player][1]=sp+1;
  381. wincon[player]=3;
  382. array[2][sp+1]=symbol[player];
  383. counter++;
  384. }
  385. if (players==4)
  386. {
  387. direction = rand()%3;
  388. //randomly check a direction
  389. //check for direction's availability. we could potentially use a small array, but i did it the hard way
  390. //make the check in a loop. might need a new variable
  391. if ((direction == 0) && (array[2][sp] == '.'))
  392. { //0 is for starting position up (array[2][sp])
  393. pcoord[player][0] = 2;
  394. pcoord[player][1] = sp;
  395. wincon[player] = 3;
  396. array[2][sp+1] = symbol[player];
  397. counter++; //now that a new player has had his starting point set, the counter can increment
  398. }
  399. else if((direction == 1) && (array[sp][3] == '.'))
  400. { //1 is for starting position left (array[sp][3])
  401. pcoord[player][0] = sp;
  402. pcoord[player][1] = 3;
  403. wincon[player] = 2;
  404. array[sp][3] = symbol[player];
  405. counter++; //now that a new player has had his starting point set, the counter can increment
  406. }
  407. else if((direction == 2) && (array[sp][sumj-1] == '.'))
  408. {
  409. //2 is for starting position right (array[sp][sumj])
  410. //could have gone for a simple else, but put the check in as a precaution
  411. pcoord[player][0] = sp;
  412. pcoord[player][1] = sumj-1;
  413. wincon[player] = 4;
  414. array[sp][sumj-1] = symbol[player];
  415. counter++; //now that a new player has had his starting point set, the counter can increment
  416. }
  417. }
  418. }
  419. }
  420. }while (counter < players);
  421. for (i = 0; i < sumi; i++)
  422. {
  423. printf("\n");
  424. for (j=0;j<sumj;j++)
  425. {
  426. printf("%c ",array[i][j]);
  427. }
  428. }
  429. // for(i=0;i<players;i++)
  430. // {
  431. // exit=1;
  432. // help=1;
  433. // if (players==2)
  434. // {
  435. // if ((wincon[i]==1)||(wincon[i]==3))
  436. // {
  437. // j=0;
  438. // while((name[i][j]!='\0')&&(j<max))
  439. // {
  440. // printf("\n%d",wincon[i]);
  441. // j++;
  442. // }
  443. // do
  444. // {
  445. // 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')");
  446. // scanf("%s", move);
  447. // exit=strcmp(move,"exit");
  448. // help=strcmp(move,"help");
  449. // flag=1;
  450. // if (!(exit==0)||(help==0))
  451. // {
  452. // target=strlen(move);
  453. // match=0;
  454. // j=0;
  455. // while((j<target)&&(flag==1))
  456. // {
  457. // if (j==0)
  458. // {
  459. // for (k = 3; k < sumj; k++)
  460. // {
  461. // if (array[0][k]!=move[j])
  462. // {
  463. // match++;
  464. // }
  465. // else
  466. // {
  467. // a1=0;
  468. // }
  469. // }
  470. //
  471. // }
  472. // else
  473. // {
  474. // //an o proigoumenos einai arithmos
  475. // if(a1==1)
  476. // {
  477. // for (k = 3; k < sumj; k++)
  478. // {
  479. // if (array[0][k]!=move[j])
  480. // {
  481. // match++;
  482. // }
  483. // else
  484. // {
  485. // a1=0;
  486. // }
  487. // }
  488. // for (k=2;k<sumi;k++)
  489. // {
  490. // if(array[k][1]!=move[j])
  491. // {
  492. // match++;
  493. // }
  494. // else
  495. // {
  496. // a1=1;
  497. // }
  498. // }
  499. // }
  500. // else
  501. // {
  502. // for(k=2;k<sumi;k++)
  503. // {
  504. // if(array[k][0]!=move[j])
  505. // {
  506. // match++;
  507. // }
  508. // else
  509. // {
  510. // a1=1;
  511. // }
  512. // }
  513. // }
  514. // }
  515. // j++;
  516. // }
  517. //
  518. // }
  519. // if (match!=0)
  520. // {
  521. // printf("\n Wrong input.");
  522. // }
  523. // }while (match!=0);
  524. // }
  525. // }
  526. // if (players==4)
  527. // {
  528. // if (wincon[i]==i+1)
  529. // {
  530. // j=0;
  531. // while((name[i][j]!='\0')&&(j<max))
  532. // {
  533. // printf("\n%d",wincon[i]);
  534. // j++;
  535. // }
  536. // do
  537. // {
  538. // 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')");
  539. // scanf("%s", move);
  540. // exit=strcmp(move,"exit");
  541. // help=strcmp(move,"help");
  542. //// flag=1;
  543. // if (!(exit==0)||(help==0))
  544. // {
  545. // target=strlen(move);
  546. // match=0;
  547. // j=0;
  548. // while((j<target)&&(flag==1))
  549. // {
  550. // if (j==0)
  551. // {
  552. // for (k = 3; k < sumj; k++)
  553. // {
  554. // if (array[0][k]!=move[j])
  555. // {
  556. // match++;
  557. // }
  558. // else
  559. // {
  560. // a1=0;
  561. // }
  562. // }
  563. //
  564. // }
  565. // else
  566. // {
  567. // //an o proigoumenos einai arithmos
  568. // if(a1==1)
  569. // {
  570. // for (k = 3; k < sumj; k++)
  571. // {
  572. // if (array[0][k]!=move[j])
  573. // {
  574. // match++;
  575. // }
  576. // else
  577. // {
  578. // a1=0;
  579. // }
  580. // }
  581. // for (k=2;k<sumi;k++)
  582. // {
  583. // if(array[k][1]!=move[j])
  584. // {
  585. // match++;
  586. // }
  587. // else
  588. // {
  589. // a1=1;
  590. // }
  591. // }
  592. // }
  593. // else
  594. // {
  595. // for(k=2;k<sumi;k++)
  596. // {
  597. // if(possible[k][0]!=move[j])
  598. // {
  599. // match++;
  600. // }
  601. // else
  602. // {
  603. // a1=1;
  604. // }
  605. // }
  606. // }
  607. // }
  608. // j++;
  609. // }
  610. //
  611. // }
  612. // if (match!=0)
  613. // {
  614. // printf("\n Wrong input.");
  615. // }
  616. // }while (match!=0);
  617. // }
  618. // }
  619. // }
  620. blanks=0;
  621. for (j=2;i<sumj;j++)
  622. {
  623. for (l=0;l<sumi;l++)
  624. {
  625. possible[l][j]=array[l][j];
  626. }
  627. i=3;
  628. if (possible[i][j]=='.')
  629. {
  630. k=j;
  631. sum=0;
  632. blanks++;
  633.  
  634. do
  635. {
  636. if (possible[i][k+1]=='.')
  637. {
  638. sum++;
  639. obcounter=0;
  640. if (possible[i][k+1]!='.')
  641. {
  642. obcounter++;
  643. }
  644. if (possible[i+1][k]!='.')
  645. {
  646. obcounter++;
  647. }
  648. if (possible[i-1][k]!='.')
  649. {
  650. obcounter++;
  651. }
  652. if (obcounter==3)
  653. {
  654. possible[i][k+1]='X';
  655. sum--;
  656. }
  657. else
  658. {
  659. set=possible[i][k+1];
  660. possible[i][k+1]=possible[i][k];
  661. possible[i][k]=set;
  662. k=k+1;
  663. }
  664.  
  665. }
  666. else
  667. {
  668.  
  669. if (possible[i+1][k]=='.')
  670. {
  671. sum++;
  672. obcounter=0;
  673. if (possible[i+1][k+1]!='.')
  674. {
  675. obcounter++;
  676. }
  677. if (possible[i+2][k+1]!='.')
  678. {
  679. obcounter++;
  680. }
  681. if (possible[i+1][k-1]!='.')
  682. {
  683. obcounter++;
  684. }
  685. if (obcounter==3)
  686. {
  687. sum--;
  688. possible[i+1][k]='X';
  689. }
  690. else
  691. {
  692. set=possible[i+1][k];
  693. possible[i+1][k]=possible[i][k];
  694. possible[i][k]=set;
  695. i=i+1;
  696. }
  697. }
  698. else
  699. {
  700. if (possible[i-1][k]=='.')
  701. {
  702. sum++;
  703. obcounter=0;
  704. if (possible[i-1][k-1]!='.')
  705. {
  706. obcounter++;
  707. }
  708. if (possible[i-2][k-1]!='.')
  709. {
  710. obcounter++;
  711. }
  712. if (possible[i-1][k+1]!='.')
  713. {
  714. obcounter++;
  715. }
  716. if (obcounter==3)
  717. {
  718. sum--;
  719. possible[i-1][k]='X';
  720. }
  721. else
  722. {
  723. set=possible[i-1][k];
  724. possible[i-1][k]=possible[i][k];
  725. possible[i][k]=set;
  726. i=i-1;
  727. }
  728. }
  729. else
  730. {
  731. if (possible[i][k-1]=='.')
  732. {
  733. obcounter=0;
  734. sum++;
  735. if (possible[i][k-2] !='.')
  736. {
  737. obcounter++;
  738. }
  739. if (possible[i+1][k-1]!='.')
  740. {
  741. obcounter++;
  742. }
  743. if (possible[i-1][k-1] !='.')
  744. {
  745. obcounter++;
  746. }
  747. if (obcounter==3)
  748. {
  749. sum--;
  750. possible[i][k-1]='X';
  751. }
  752. else
  753. {
  754. set=possible[i][k-1];
  755. possible[i][k-1]=possible[i][k];
  756. possible[i][k]=set;
  757. k=k-1;
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }
  764. while(i!=sumi-1);
  765. printf("\n%d",sum);
  766. }
  767.  
  768. }
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782. for (i=0;i<players;i++)
  783. {
  784. free (name[i]);
  785. free(pcoord[i]);
  786. }
  787. for (i = 0; i < sumi; i++)
  788. {
  789. free (array[i]);
  790. }
  791. free(nam);
  792. free(type);
  793. free(symbol);
  794. free(wincon);
  795. free(array);
  796. free(name);
  797. free(pcoord);
  798. return 0;
  799. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement