Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 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.  
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. int main(void)
  14. {
  15. //----------------arxikes diastaseis tamplo -----------------
  16. int dimensions=10,yes,no,flag=1,max,length,*type,users=0, user, computer,sumi,sumj;
  17. char yesno[4],*nam,**name,**array,typ[9];
  18. int i,players,j;
  19. //----------------epalitheusi gia to an thelei o xristis na paiksei me tis prokathorismens diastaseis------
  20. do
  21. {
  22. printf("Do you want to play by defaut dimensions(10X10)?(yes/no) ");
  23. scanf("%s", yesno);
  24. yes=strcmp(yesno,"yes");
  25. no=strcmp(yesno,"no");
  26. if ((yes==0)||(no==0))
  27. {
  28. flag=0;
  29. }
  30. if (flag==1)
  31. {
  32. printf("\nWrong input.Please choose between default dimensions(yes) and custom dimensions(no) ");
  33. }
  34. }while (flag!=0);
  35. //---------------epalitheusi gia to oti oi custom diastaseis tha einai megaliteris i ises tou 5------
  36. if (no==0)
  37. {
  38. do
  39. {
  40. printf("\nChoose your own dimensions ");
  41. scanf("%d", &dimensions);
  42. if (dimensions<5)
  43. {
  44. printf("\nWrong input.Please choose a number >=5 ");
  45. }
  46. }while(dimensions<5);
  47. }
  48. //--------------------paixtes-------------------------
  49. //--------------------arithmos paixton-----------------
  50. do
  51. {
  52. printf("\nChoose the number of players (2 or 4) ");
  53. scanf("%d",&players);
  54. if (!((players==2)||(players==4)))
  55. {
  56. printf("\nWrong input.Please choose between 2 or 4 ");
  57. }
  58. }while(!((players==2)||(players==4)));
  59. //-------------------onoma kai typos paixton-----------
  60. nam=(char*)malloc(players*101);
  61. for (i=0;i<players;i++)
  62. {
  63. nam[i]='\0';
  64. }
  65. printf("\n Enter the name and the type(user or computer) of each player.Keep in mind that there should be at least one user ");
  66. type=(int*)malloc(players);
  67. for (i=0;i<players;i++)
  68. {
  69. type[i]=1;
  70. }
  71. for (i=0;i<players;i++)
  72. {
  73. printf("\nEnter the name of player number %d ",i+1);
  74. scanf("%s",&nam[i*101]);
  75. printf("\nEnter the type of player number %d ",i+1);
  76. flag=1;
  77. do
  78. {
  79. scanf("%s", typ);
  80. user=strcmp(typ,"user");
  81. computer=strcmp(typ,"computer");
  82. if ((user==0)||(computer==0))
  83. {
  84. flag=0;
  85. }
  86. if (flag==1)
  87. {
  88. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  89. }
  90. }while (flag!=0);
  91. if (user==0)
  92. {
  93. users++;
  94. type[i]=0;
  95. }
  96. }
  97. if (users==0)
  98. {
  99. do
  100. {
  101. printf("There must be at least on user.Please re-enter the type of each player,making sure that there is one user! ");
  102. flag=1;
  103. for (i=0;i<players;i++)
  104. {
  105. printf("\n Enter the type of player number %d ",i+1);
  106. do
  107. {
  108.  
  109. scanf("%s", typ);
  110. user=strcmp(typ,"user");
  111. computer=strcmp(typ,"computer");
  112. if ((user==0)||(computer==0))
  113. {
  114. flag=0;
  115. }
  116. else
  117. {
  118. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  119. }
  120. }while (flag!=0);
  121.  
  122. }
  123. }while(users==0);
  124. }
  125. max=0;
  126. i=0;
  127. while((nam[i]!='\0')&&(i<101))
  128. {
  129. max++;
  130. i++;
  131. }
  132. length=0;
  133. for (i=101;i<(players*101);i=i+101)
  134. {
  135. while(nam[i]!='\0')
  136. {
  137. length++;
  138. i++;
  139. }
  140. if (length>max)
  141. {
  142. max=length;
  143. }
  144. i=i-length;
  145. length=0;
  146. }
  147. max=max+1;
  148. name=(char**)malloc(players*sizeof(char*));
  149. for (i=0;i<players;i++)
  150. {
  151. name[i]=(char*)malloc(max*sizeof(char));
  152. }
  153. for (i=0;i<players;i++)
  154. {
  155. for (j=0;j<max;j++)
  156. {
  157. name[i][j]=nam[j+(i*101)];
  158. }
  159. }
  160. for (i=0;i<players;i++)
  161. {
  162. j=0;
  163. printf("\nThe player with the name ");
  164. while((name[i][j]!='\0')&&(j<max))
  165. {
  166. printf("%c",name[i][j]);
  167. j++;
  168. }
  169. printf(" is a ");
  170. if (type[i]==0)
  171. {
  172. printf("user");
  173. }
  174. if (type[i]==1)
  175. {
  176. printf("computer");
  177. }
  178. printf("\n");
  179. }
  180.  
  181. flag=1;
  182. do
  183. {
  184. printf("\nDo you want the map to be populated by obstacles?(yes/no) ");
  185. scanf("%s", yesno);
  186. yes=strcmp(yesno,"yes");
  187. no=strcmp(yesno,"no");
  188. if ((yes==0)||(no==0))
  189. {
  190. flag=0;
  191. }
  192. if (flag==1)
  193. {
  194. printf("\nWrong input.Choose between (yes) and (no) ");
  195. }
  196. }while (flag!=0);
  197.  
  198. sumi=dimensions+2;
  199. sumj=dimensions+3;
  200. array=(char**)malloc((sumi)*sizeof(char*));
  201. for (i=0;i<sumi;i++)
  202. {
  203. array[i]=(char*)malloc((sumj)*sizeof(char));
  204. }
  205.  
  206. for (i = 0; i < sumi; i++)
  207. {
  208. if ((i > 1) && (i < 11))
  209. {
  210. array[i][0] = i - 1 + '0';
  211. array[i][1] = ' ';
  212. array[i][2] = '|';
  213. for (j = 3; j < sumj; j++)
  214. {
  215. array[i][j] = '.';
  216. }
  217. }
  218. else
  219. {
  220. array[i][0] = ((i -1)/10) + '0';
  221. array[i][1] = ((i -1)%10) + '0';
  222. array[i][2] = '|';
  223. for (j = 3; j < sumj; j++)
  224. {
  225. array[i][j] = '.';
  226. }
  227. }
  228. if (i == 0)
  229. {
  230. array[i][2] = '|';
  231. for (j = 3; j < sumj; j++)
  232. {
  233. array[i][j] = j - 3 + 'a';
  234. }
  235. }
  236. if (i == 1)
  237. {
  238. for (j = 0; j < sumj; j++)
  239. {
  240. array[i][j] = '-';
  241. }
  242. }
  243. }
  244. array[0][0]=NULL;
  245. array[0][1]=NULL;
  246. array[0][2]=NULL;
  247.  
  248.  
  249. // for (i = 0; i < dimensions + 2 ; i++)
  250. // {
  251. // for (j = 0; j < dimensions + 2; j++)
  252. // {
  253. // array[i][j]='0' ;
  254. // }
  255. // }
  256.  
  257. for (i = 0; i < sumi; i++)
  258. {
  259. printf("\n");
  260. for (j=0;j<sumj;j++)
  261. {
  262. printf("%c ",array[i][j]);
  263. }
  264. }
  265.  
  266.  
  267. free(name);
  268. free(nam);
  269. free(type);
  270. return 0;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement