Advertisement
Kenthris

Untitled

Apr 29th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.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,co1,co2,co3,co4,newflag,spot;
  19. char yesno[4],*nam,**name,**array,typ[9],*symbol,sym;
  20. int i,players,j;
  21. int sp, player,**pcoord;
  22. int counter = 0;
  23. int userflag = 1;
  24. int direction;
  25. char *wincon;
  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 ");
  54. }
  55. }while(dimensions<5);
  56. }
  57. obnum = ((dimensions - 1)/2);//sets the number of obstacles to be placed when the board is initialized
  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. for (j=0;j<max;j++)
  114. {
  115. name[i][j]=nam[j+(i*101)];
  116. }
  117. }
  118. for (i=0;i<players;i++)
  119. {
  120. printf("\nEnter the name of player number %d ",i+1);
  121. scanf("%s",&nam[i*101]);
  122. printf("\nEnter the type of player number %d ",i+1);
  123. flag=1;
  124. do
  125. {
  126. scanf("%s", typ);
  127. user=strcmp(typ,"user");
  128. computer=strcmp(typ,"computer");
  129. if ((user==0)||(computer==0))
  130. {
  131. flag=0;
  132. }
  133. if (flag==1)
  134. {
  135. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  136. }
  137. }while (flag!=0);
  138. printf("\nEnter the symbol of player number %d .You can choose between (#,*,&,@) ",i+1);
  139. flag=1;
  140. do
  141. {
  142. if (flag==2)
  143. {
  144. flag=1;
  145. }
  146. scanf("%s",&sym);
  147. if ((sym=='#')||(sym=='@')||(sym=='&')||(sym=='*'))
  148. {
  149. flag=0;
  150. for (j=0;j<players;j++)
  151. {
  152. if (sym==symbol[j])
  153. {
  154. flag=2;
  155. printf("Another player already has this symbol taken.Please choose another one (#,*,&,@) ");
  156. }
  157. }
  158. }
  159. if (flag==1)
  160. {
  161. printf("\nWrong input.Please choose one of your options (#,*,&,@) ");
  162. }
  163. if ((flag!=2)&&(flag!=1))
  164. {
  165. flag=0;
  166. symbol[i]=sym;
  167. }
  168. }while (flag!=0);
  169. if (user==0)
  170. {
  171. users++;
  172. type[i]=0;
  173. }
  174. }
  175. if (users==0)
  176. {
  177. do
  178. {
  179. printf("There must be at least on user.Please re-enter the type of each player,making sure that there is one user! ");
  180. flag=1;
  181. for (i=0;i<players;i++)
  182. {
  183. printf("\n Enter the type of player number %d ",i+1);
  184. do
  185. {
  186.  
  187. scanf("%s", typ);
  188. user=strcmp(typ,"user");
  189. computer=strcmp(typ,"computer");
  190. if ((user==0)||(computer==0))
  191. {
  192. flag=0;
  193. }
  194. else
  195. {
  196. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  197. }
  198. }while (flag!=0);
  199.  
  200. }
  201. }while(users==0);
  202. }
  203. for (i=0;i<players;i++)
  204. {
  205. j=0;
  206. printf("\nThe player with the name ");
  207. while((name[i][j]!='\0')&&(j<max))
  208. {
  209. printf("%c",name[i][j]);
  210. j++;
  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. for (i=0;i<sumi;i++)
  244. {
  245. array[i]=(char*)malloc((sumj)*sizeof(char));
  246. }
  247.  
  248. for (i = 0; i < sumi; i++)
  249. {
  250. if ((i > 1) && (i < 11))
  251. {
  252. array[i][0] = i - 1 + '0';
  253. array[i][1] = ' ';
  254. array[i][2] = '|';
  255. for (j = 3; j < sumj; j++)
  256. {
  257. array[i][j] = '.';
  258. }
  259. }
  260. else
  261. {
  262. array[i][0] = ((i -1)/10) + '0';
  263. array[i][1] = ((i -1)%10) + '0';
  264. array[i][2] = '|';
  265. for (j = 3; j < sumj; j++)
  266. {
  267. array[i][j] = '.';
  268. }
  269. }
  270. if (i == 0)
  271. {
  272. array[i][2] = '|';
  273. for (j = 3; j < sumj; j++)
  274. {
  275. array[i][j] = j - 3 + 'a';
  276. }
  277. }
  278. if (i == 1)
  279. {
  280. for (j = 0; j < sumj; j++)
  281. {
  282. array[i][j] = '-';
  283. }
  284. }
  285. }
  286. array[0][0]=' ';
  287. array[0][1]=' ';
  288. array[0][2]=' ';
  289.  
  290.  
  291. //direction becomes 0 (up), 1 (left) or 2 (right) through the use of rand(). Keep this comment in the code
  292. //it is later used to check a direction's corresponding starting point for availability to put a player on
  293. sp = (dimensions/2) + 1;
  294. wincon=(char*)malloc(players*sizeof(char));
  295. //malloc an array the size of players called wincon. it will save the characters u, l, r and d
  296. //which will be the opposite of the player's direction when he is being given a starting point
  297. //malloc an array named pcoord which will be players x 2, where player coordinates will be saved
  298. //initialize all of pcoord to be dimensions + 1
  299. pcoord=(int**)malloc(players*sizeof(int*));
  300. for (i=0;i<players;i++)
  301. {
  302. pcoord[i]=(int*)malloc(2*sizeof(int));
  303. }
  304. for (i=0;i<players;i++)
  305. {
  306. wincon[i]='!';
  307. for (j=0;j<2;j++)
  308. {
  309. pcoord[i][j]=(dimensions+7);
  310. }
  311. }
  312. do
  313. {
  314. player=rand()%players;
  315. if(pcoord[player][0] == (dimensions + 7))
  316. { //this if checks if the player has already had his starting point set
  317. if((type[player] == 0) && (userflag == 1))
  318. { //this if checks for the first randomly chosen player
  319. pcoord[player][0] = sumi-1;
  320. pcoord[player][1] = sp+1;
  321. wincon[player] = 'u';
  322. array[sumi-1][sp+1] = symbol[player];
  323. userflag = 0;
  324. counter++; //now that a new player has had his starting point set, the counter can increment
  325. }
  326. else
  327. {
  328. if (players==2)
  329. {
  330. pcoord[player][0]=2;
  331. pcoord[player][1]=sp+1;
  332. wincon[player]='d';
  333. array[2][sp+1]=symbol[player];
  334. counter++;
  335. }
  336. if (players==4)
  337. {
  338. direction = rand()%3;
  339. //randomly check a direction
  340. //check for direction's availability. we could potentially use a small array, but i did it the hard way
  341. //make the check in a loop. might need a new variable
  342. if ((direction == 0) && (array[2][sp] == '.'))
  343. { //0 is for starting position up (array[2][sp])
  344. pcoord[player][0] = 2;
  345. pcoord[player][1] = sp;
  346. wincon[player] = 'd';
  347. array[2][sp+1] = symbol[player];
  348. counter++; //now that a new player has had his starting point set, the counter can increment
  349. }
  350. else if((direction == 1) && (array[sp][3] == '.'))
  351. { //1 is for starting position left (array[sp][3])
  352. pcoord[player][0] = sp;
  353. pcoord[player][1] = 3;
  354. wincon[player] = 'r';
  355. array[sp][3] = symbol[player];
  356. counter++; //now that a new player has had his starting point set, the counter can increment
  357. }
  358. else if((direction == 2) && (array[sp][sumj-1] == '.'))
  359. {
  360. //2 is for starting position right (array[sp][sumj])
  361. //could have gone for a simple else, but put the check in as a precaution
  362. pcoord[player][0] = sp;
  363. pcoord[player][1] = sumj-1;
  364. wincon[player] = 'l';
  365. array[sp][sumj-1] = symbol[player];
  366. counter++; //now that a new player has had his starting point set, the counter can increment
  367. }
  368. }
  369. }
  370. }
  371. }while (counter < players);
  372. if (yes == 0){
  373. do{
  374. newflag=1;//used to confirm that the spot for the first piece of the obstacle is surrounded by at least one more dot
  375. do{
  376. do{
  377. co1 = (rand() % dimensions) + 2;
  378. co2 = (rand() % dimensions) + 3;
  379. }while (array[co1][co2] != '.');
  380. spot = rand() % 4;
  381. if((spot == 0) && (array[co1+1][co2] == '.')){
  382. co3=co1+1;
  383. co4=co2;
  384. newflag=0;
  385. }
  386. else if((spot == 1) && (array[co1-1][co2] == '.')){
  387. co3=co1-1;
  388. co4=co2;
  389. newflag=0;
  390. }
  391. else if((spot == 2) && (array[co1][co2+1] == '.')){
  392. co3=co1;
  393. co4=co2+1;
  394. newflag=0;
  395. }
  396. else if((spot == 3) && (array[co1][co2-1] == '.')){
  397. co3=co1;
  398. co4=co2-1;
  399. newflag=0;
  400. }
  401. }while(newflag!=0);
  402. array[co1][co2] = 'X';//there was a random } here. keep this in mind...
  403. array[co3][co4] = 'X';
  404. obnum--;
  405. }while(obnum > 0);
  406. }
  407. for (i = 0; i < sumi; i++)
  408. {
  409. printf("\n");
  410. for (j=0;j<sumj;j++)
  411. {
  412. printf("%c ",array[i][j]);
  413. }
  414. }
  415. for (i=0;i<players;i++)
  416. {
  417. free (name[i]);
  418. free(pcoord[i]);
  419. }
  420. for (i = 0; i < sumi; i++)
  421. {
  422. free (array[i]);
  423. }
  424. free(nam);
  425. free(type);
  426. free(symbol);
  427. free(wincon);
  428. free(array);
  429. free(name);
  430. free(pcoord);
  431. return 0;
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement