Advertisement
Kenthris

Untitled

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