Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 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. //----------------arxikes diastaseis tamplo -----------------
  16. int dimensions=10,yes,no,flag=1,max,length,*type,users=0, user, computer,sumi,sumj,obnum;
  17. char yesno[4],*nam,**name,**array,typ[9],*symbol,sym;
  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("\nEnter 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. symbol=(char*)malloc(players);
  68. for (i=0;i<players;i++)
  69. {
  70. symbol[i]='0';
  71. type[i]=1;
  72. }
  73. max=0;
  74. i=0;
  75. while((nam[i]!='\0')&&(i<101))
  76. {
  77. max++;
  78. i++;
  79. }
  80. length=0;
  81. for (i=101;i<(players*101);i=i+101)
  82. {
  83. while(nam[i]!='\0')
  84. {
  85. length++;
  86. i++;
  87. }
  88. if (length>max)
  89. {
  90. max=length;
  91. }
  92. i=i-length;
  93. length=0;
  94. }
  95. max=max+1;
  96. name=(char**)malloc(players*sizeof(char*));
  97. for (i=0;i<players;i++)
  98. {
  99. name[i]=(char*)malloc(max*sizeof(char));
  100. }
  101. for (i=0;i<players;i++)
  102. {
  103. for (j=0;j<max;j++)
  104. {
  105. name[i][j]=nam[j+(i*101)];
  106. }
  107. }
  108. for (i=0;i<players;i++)
  109. {
  110. printf("\nEnter the name of player number %d ",i+1);
  111. scanf("%s",&nam[i*101]);
  112. printf("\nEnter the type of player number %d ",i+1);
  113. flag=1;
  114. do
  115. {
  116. scanf("%s", typ);
  117. user=strcmp(typ,"user");
  118. computer=strcmp(typ,"computer");
  119. if ((user==0)||(computer==0))
  120. {
  121. flag=0;
  122. }
  123. if (flag==1)
  124. {
  125. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  126. }
  127. }while (flag!=0);
  128. printf("\nEnter the symbol of player number %d .You can choose between (#,*,&,@) ",i+1);
  129. flag=1;
  130. do
  131. {
  132. scanf("%s",&sym);
  133. if ((sym=='#')||(sym=='@')||(sym=='&')||(sym=='*'))
  134. {
  135. flag=0;
  136. for (j=0;j<players;j++)
  137. {
  138. printf("%c",symbol[j]);
  139. printf("%c \n",sym);
  140. printf("%d\n",j);
  141. if (sym==symbol[j])
  142. {
  143. printf("peos\n");
  144. flag=2;
  145. printf("Another player already has this symbol taken.Please choose another one");
  146. }
  147. }
  148. }
  149. if (flag==1)
  150. {
  151. printf("\nWrong input.Please choose one of your options (#,*,&,@) ");
  152. }
  153. if ((flag!=2)&&(flag!=1))
  154. {
  155. flag=0;
  156. symbol[i]=sym;
  157. }
  158. }while (flag!=0);
  159. if (user==0)
  160. {
  161. users++;
  162. type[i]=0;
  163. }
  164.  
  165. }
  166. if (users==0)
  167. {
  168. do
  169. {
  170. printf("There must be at least on user.Please re-enter the type of each player,making sure that there is one user! ");
  171. flag=1;
  172. for (i=0;i<players;i++)
  173. {
  174. printf("\n Enter the type of player number %d ",i+1);
  175. do
  176. {
  177.  
  178. scanf("%s", typ);
  179. user=strcmp(typ,"user");
  180. computer=strcmp(typ,"computer");
  181. if ((user==0)||(computer==0))
  182. {
  183. flag=0;
  184. }
  185. else
  186. {
  187. printf("\n Wrong input.Please choose one of your options (user,computer) ");
  188. }
  189. }while (flag!=0);
  190.  
  191. }
  192. }while(users==0);
  193. }
  194.  
  195. for (i=0;i<players;i++)
  196. {
  197. j=0;
  198. printf("\nThe player with the name ");
  199. while((name[i][j]!='\0')&&(j<max))
  200. {
  201. printf("%c",name[i][j]);
  202. j++;
  203. }
  204. printf(" is a ");
  205. if (type[i]==0)
  206. {
  207. printf("user");
  208. }
  209. if (type[i]==1)
  210. {
  211. printf("computer");
  212. }
  213. printf(" and his symbol is %c ",symbol[i]);
  214. printf("\n");
  215. }
  216.  
  217. flag=1;
  218. do
  219. {
  220. printf("\nDo you want the map to be populated by obstacles?(yes/no) ");
  221. scanf("%s", yesno);
  222. yes=strcmp(yesno,"yes");
  223. no=strcmp(yesno,"no");
  224. if ((yes==0)||(no==0))
  225. {
  226. flag=0;
  227. }
  228. if (flag==1)
  229. {
  230. printf("\nWrong input.Choose between (yes) and (no) ");
  231. }
  232. }while (flag!=0);
  233.  
  234. sumi=dimensions+2;
  235. sumj=dimensions+3;
  236. array=(char**)malloc((sumi)*sizeof(char*));
  237. for (i=0;i<sumi;i++)
  238. {
  239. array[i]=(char*)malloc((sumj)*sizeof(char));
  240. }
  241.  
  242. for (i = 0; i < sumi; i++)
  243. {
  244. if ((i > 1) && (i < 11))
  245. {
  246. array[i][0] = i - 1 + '0';
  247. array[i][1] = ' ';
  248. array[i][2] = '|';
  249. for (j = 3; j < sumj; j++)
  250. {
  251. array[i][j] = '.';
  252. }
  253. }
  254. else
  255. {
  256. array[i][0] = ((i -1)/10) + '0';
  257. array[i][1] = ((i -1)%10) + '0';
  258. array[i][2] = '|';
  259. for (j = 3; j < sumj; j++)
  260. {
  261. array[i][j] = '.';
  262. }
  263. }
  264. if (i == 0)
  265. {
  266. array[i][2] = '|';
  267. for (j = 3; j < sumj; j++)
  268. {
  269. array[i][j] = j - 3 + 'a';
  270. }
  271. }
  272. if (i == 1)
  273. {
  274. for (j = 0; j < sumj; j++)
  275. {
  276. array[i][j] = '-';
  277. }
  278. }
  279. }
  280. array[0][0]=' ';
  281. array[0][1]=' ';
  282. array[0][2]=' ';
  283. // for (i = 0; i < sumi; i++)
  284. // {
  285. // printf("\n");
  286. // for (j=0;j<sumj;j++)
  287. // {
  288. // printf("%c ",array[i][j]);
  289. // }
  290. // }
  291. free(name);
  292. free(nam);
  293. free(type);
  294. free(symbol);
  295. return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement