Advertisement
belowzero

Untitled

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