Advertisement
Kenthris

Untitled

Apr 29th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.69 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;
  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;
  24. char *wincon;
  25. // time_t t;
  26. //----------------epalitheusi gia to an thelei o xristis na paiksei me tis prokathorismens diastaseis------
  27. srand(time(NULL));
  28. do
  29. {
  30. printf("Do you want to play by defaut dimensions(10X10)?(yes/no) ");
  31. scanf("%s", yesno);
  32. yes=strcmp(yesno,"yes");
  33. no=strcmp(yesno,"no");
  34. if ((yes==0)||(no==0))
  35. {
  36. flag=0;
  37. }
  38. if (flag==1)
  39. {
  40. printf("\nWrong input.Please choose between default dimensions(yes) and custom dimensions(no) ");
  41. }
  42. }while (flag!=0);
  43. //---------------epalitheusi gia to oti oi custom diastaseis tha einai megaliteris i ises tou 5------
  44. if (no==0)
  45. {
  46. do
  47. {
  48. printf("\nChoose your own dimensions ");
  49. scanf("%d", &dimensions);
  50. if (dimensions<5)
  51. {
  52. printf("\nWrong input.Please choose a number >=5 ");
  53. }
  54. }while(dimensions<5);
  55. }
  56. //--------------------paixtes-------------------------
  57. //--------------------arithmos paixton-----------------
  58. do
  59. {
  60. printf("\nChoose the number of players (2 or 4) ");
  61. scanf("%d",&players);
  62. if (!((players==2)||(players==4)))
  63. {
  64. printf("\nWrong input.Please choose between 2 or 4 ");
  65. }
  66. }while(!((players==2)||(players==4)));
  67. //-------------------onoma kai typos paixton-----------
  68. nam=(char*)malloc(players*101);
  69. for (i=0;i<players;i++)
  70. {
  71. nam[i]='\0';
  72. }
  73. printf("\nEnter the name and the type(user or computer) of each player.Keep in mind that there should be at least one user ");
  74. type=(int*)malloc(players);
  75. symbol=(char*)malloc(players);
  76. for (i=0;i<players;i++)
  77. {
  78. symbol[i]='0';
  79. type[i]=1;
  80. }
  81. max=0;
  82. i=0;
  83. while((nam[i]!='\0')&&(i<101))
  84. {
  85. max++;
  86. i++;
  87. }
  88. length=0;
  89. for (i=101;i<(players*101);i=i+101)
  90. {
  91. while(nam[i]!='\0')
  92. {
  93. length++;
  94. i++;
  95. }
  96. if (length>max)
  97. {
  98. max=length;
  99. }
  100. i=i-length;
  101. length=0;
  102. }
  103. max=max+1;
  104. name=(char**)malloc(players*sizeof(char*));
  105. for (i=0;i<players;i++)
  106. {
  107. name[i]=(char*)malloc(max*sizeof(char));
  108. }
  109. for (i=0;i<players;i++)
  110. {
  111. for (j=0;j<max;j++)
  112. {
  113. name[i][j]=nam[j+(i*101)];
  114. }
  115. }
  116. for (i=0;i<players;i++)
  117. {
  118. printf("\nEnter the name of player number %d ",i+1);
  119. scanf("%s",&nam[i*101]);
  120. printf("\nEnter the type of player number %d ",i+1);
  121. flag=1;
  122. do
  123. {
  124. scanf("%s", typ);
  125. user=strcmp(typ,"user");
  126. computer=strcmp(typ,"computer");
  127. if ((user==0)||(computer==0))
  128. {
  129. flag=0;
  130. }
  131. if (flag==1)
  132. {
  133. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  134. }
  135. }while (flag!=0);
  136. printf("\nEnter the symbol of player number %d .You can choose between (#,*,&,@) ",i+1);
  137. flag=1;
  138. do
  139. {
  140. if (flag==2)
  141. {
  142. flag=1;
  143. }
  144. scanf("%s",&sym);
  145. if ((sym=='#')||(sym=='@')||(sym=='&')||(sym=='*'))
  146. {
  147. flag=0;
  148. for (j=0;j<players;j++)
  149. {
  150. if (sym==symbol[j])
  151. {
  152. flag=2;
  153. printf("Another player already has this symbol taken.Please choose another one (#,*,&,@) ");
  154. }
  155. }
  156. }
  157. if (flag==1)
  158. {
  159. printf("\nWrong input.Please choose one of your options (#,*,&,@) ");
  160. }
  161. if ((flag!=2)&&(flag!=1))
  162. {
  163. flag=0;
  164. symbol[i]=sym;
  165. }
  166. }while (flag!=0);
  167. if (user==0)
  168. {
  169. users++;
  170. type[i]=0;
  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. printf(" is a ");
  211. if (type[i]==0)
  212. {
  213. printf("user");
  214. }
  215. if (type[i]==1)
  216. {
  217. printf("computer");
  218. }
  219. printf(" and his symbol is %c ",symbol[i]);
  220. printf("\n");
  221. }
  222. flag=1;
  223. do
  224. {
  225. printf("\nDo you want the map to be populated by obstacles?(yes/no) ");
  226. scanf("%s", yesno);
  227. yes=strcmp(yesno,"yes");
  228. no=strcmp(yesno,"no");
  229. if ((yes==0)||(no==0))
  230. {
  231. flag=0;
  232. }
  233. if (flag==1)
  234. {
  235. printf("\nWrong input.Choose between (yes) and (no) ");
  236. }
  237. }while (flag!=0);
  238. sumi=dimensions+2;
  239. sumj=dimensions+3;
  240. array=(char**)malloc((sumi)*sizeof(char*));
  241. for (i=0;i<sumi;i++)
  242. {
  243. array[i]=(char*)malloc((sumj)*sizeof(char));
  244. }
  245.  
  246. for (i = 0; i < sumi; i++)
  247. {
  248. if ((i > 1) && (i < 11))
  249. {
  250. array[i][0] = i - 1 + '0';
  251. array[i][1] = ' ';
  252. array[i][2] = '|';
  253. for (j = 3; j < sumj; j++)
  254. {
  255. array[i][j] = '.';
  256. }
  257. }
  258. else
  259. {
  260. array[i][0] = ((i -1)/10) + '0';
  261. array[i][1] = ((i -1)%10) + '0';
  262. array[i][2] = '|';
  263. for (j = 3; j < sumj; j++)
  264. {
  265. array[i][j] = '.';
  266. }
  267. }
  268. if (i == 0)
  269. {
  270. array[i][2] = '|';
  271. for (j = 3; j < sumj; j++)
  272. {
  273. array[i][j] = j - 3 + 'a';
  274. }
  275. }
  276. if (i == 1)
  277. {
  278. for (j = 0; j < sumj; j++)
  279. {
  280. array[i][j] = '-';
  281. }
  282. }
  283. }
  284. array[0][0]=' ';
  285. array[0][1]=' ';
  286. array[0][2]=' ';
  287.  
  288.  
  289. //direction becomes 0 (up), 1 (left) or 2 (right) through the use of rand(). Keep this comment in the code
  290. //it is later used to check a direction's corresponding starting point for availability to put a player on
  291. printf("1\n");
  292. sp = (dimensions/2) + 1;
  293. wincon=(char*)malloc(players*sizeof(char));
  294. //malloc an array the size of players called wincon. it will save the characters u, l, r and d
  295. //which will be the opposite of the player's direction when he is being given a starting point
  296. //malloc an array named pcoord which will be players x 2, where player coordinates will be saved
  297. //initialize all of pcoord to be dimensions + 1
  298. printf("2\n");
  299. pcoord=(int**)malloc(players*sizeof(int*));
  300. printf("3\n");
  301. for (i=0;i<players;i++)
  302. {
  303. pcoord[i]=(int*)malloc(2*sizeof(int));
  304. printf("4\n");
  305. }
  306. for (i=0;i<players;i++)
  307. {
  308. printf("5\n");
  309. wincon[i]='!';
  310. for (j=0;j<2;j++)
  311. {
  312. printf("6\n");
  313. pcoord[i][j]=dimensions+7;
  314. }
  315. }
  316. do
  317. {
  318. printf("7\n");
  319. player=rand()%4;
  320. printf("7.1\n");
  321. if(pcoord[player][0] == (dimensions + 7))
  322. { //this if checks if the player has already had his starting point set
  323. printf("8\n");
  324. if((type[player] == 0) && (userflag == 1))
  325. { //this if checks for the first randomly chosen player
  326. printf("9\n");
  327. pcoord[player][0] = sumi-1;
  328. printf("9.1\n");
  329. pcoord[player][1] = sp;
  330. printf("9.2\n");
  331. wincon[player] = 'u';
  332. printf("9.3\n");
  333. array[sumi-1][sp+1] = symbol[player];
  334. printf("9.4\n");
  335. userflag = 0;
  336. }
  337. else
  338. {
  339. if (players==2)
  340. {
  341. pcoord[player][0]=2;
  342. pcoord[player][1]=sp+1;
  343. wincon[player]='d';
  344. array[2][sp+1]=symbol[player];
  345. }
  346. if (players==4)
  347. {
  348. printf("10\n");
  349. direction = rand()%3;
  350. //randomly check a direction
  351. //check for direction's availability. we could potentially use a small array, but i did it the hard way
  352. //make the check in a loop. might need a new variable
  353. if ((direction == 0) && (array[2][sp] == '.'))
  354. { //0 is for starting position up (array[2][sp])
  355. printf("11\n");
  356. printf("12\n");
  357. pcoord[player][0] = 2;
  358. pcoord[player][1] = sp;
  359. wincon[player] = 'd';
  360. array[2][sp+1] = symbol[player];
  361. }
  362. else if((direction == 1) && (array[sp][3] == '.'))
  363. { //1 is for starting position left (array[sp][3])
  364. printf("14\n");
  365. printf("15\n");
  366. pcoord[player][0] = sp;
  367. pcoord[player][1] = 3;
  368. wincon[player] = 'r';
  369. array[sp][3] = symbol[player];
  370. }
  371. else if((direction == 2) && (array[sp][sumj-1] == '.'))
  372. {
  373. printf("17\n");
  374. //2 is for starting position right (array[sp][sumj])
  375. //could have gone for a simple else, but put the check in as a precaution
  376. pcoord[player][0] = sp;
  377. pcoord[player][1] = sumj-1;
  378. wincon[player] = 'l';
  379. array[sp][sumj-1] = symbol[player];
  380. }
  381. }
  382. }
  383. counter++; //now that a new player has had his starting point set, the counter can increment
  384. }
  385. }while (counter < players);
  386. for (i = 0; i < sumi; i++)
  387. {
  388. printf("\n");
  389. for (j=0;j<sumj;j++)
  390. {
  391. printf("%c ",array[i][j]);
  392. }
  393. }
  394. free(name);
  395. free(nam);
  396. free(type);
  397. free(symbol);
  398. free(pcoord);
  399. free(wincon);
  400. return 0;
  401. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement