PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/mastermind/windows/mastermind.c

https://github.com/tdotu/Console-Games
C | 320 lines | 252 code | 29 blank | 39 comment | 44 complexity | 26da9c0ec7c98b3d424ae851888930bf MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. Copyright 2012 Simon Diepold all rights reserved
  3. Mastermind is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. any later version.
  7. Mastermind is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Universe Kernel. If not, see <http://www.gnu.org/licenses/>.
  13. Mastermind ist Freie Software: Sie koennen es unter den Bedingungen
  14. der GNU General Public License, wie von der Free Software Foundation,
  15. Version 3 der Lizenz oder jeder spaeteren
  16. veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
  17. Mastermind wird in der Hoffnung, dass es nuetzlich sein wird, aber
  18. Universe Kernel wird in der Hoffnung, dass es nuetzlich sein wird, aber
  19. OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
  20. Gew‰hrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
  21. Siehe die GNU General Public License fuer weitere Details.
  22. Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
  23. Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
  24. */
  25. /*
  26. authors:
  27. -Simon Diepold aka. Tdotu (Universe Team) simon.diepold@infinitycoding.de
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <conio.h>
  32. #include <windows.h>
  33. #include <winuser.h>
  34. #define richtig 1
  35. #define platz 0
  36. static char computer[8] = {0,0,0,0,0,0,0,0};
  37. static char backup[8];
  38. static char player[8] = {0,0,0,0,0,0,0,0};
  39. static char result[8] = {0,0,0,0,0,0,0,0};
  40. static char rounds[3]={7,5,5};
  41. COORD position;
  42. HANDLE cmdbuffer;
  43. int versuch=0;
  44. int level[3]={4,4,8};
  45. int ziffern;
  46. int schwierigket;
  47. void set_posotion(short x, short y){
  48. position.X=x;
  49. position.Y=y;
  50. SetConsoleCursorPosition(cmdbuffer,position);
  51. }
  52. void random(char* arry){
  53. srand(time(NULL));
  54. while (arry[0]<=0 ||arry[0]>9){
  55. arry[0] = rand()%9+1;
  56. }
  57. while (arry[1] == arry[0] || arry[1] == 0|| arry[1]>9 || arry[1]<=0){
  58. srand(time(NULL));
  59. arry[1] = rand()%9+1;
  60. }
  61. while (arry[2] == 0 || arry[2] == arry[0] || arry[2] == arry[1]|| arry[2]>9 || arry[2]<=0){
  62. srand(time(NULL));
  63. arry[2] = rand()%9+1;
  64. }
  65. while (arry[3] ==0 || arry[3] == arry[0]|| arry[3] == arry[1]|| arry[3] == arry[2] || arry[3]>9 || arry[3]<=0 ){
  66. srand(time(NULL));
  67. arry[3] = rand()%9+1;
  68. }
  69. }
  70. void randomLV2(char* arry){
  71. srand(time(NULL));
  72. arry[0]=rand()%9+1;
  73. arry[1]=rand()%9+1;
  74. arry[2]=rand()%9+1;
  75. arry[3]=rand()%9+1;
  76. }
  77. void randomLV3(char* arry){
  78. srand(time(NULL));
  79. arry[0]=rand()%9+1;
  80. arry[1]=rand()%9+1;
  81. arry[2]=rand()%9+1;
  82. arry[3]=rand()%9+1;
  83. arry[4]=rand()%9+1;
  84. arry[5]=rand()%9+1;
  85. arry[6]=rand()%9+1;
  86. arry[7]=rand()%9+1;
  87. }
  88. void icmd (void){
  89. system("cls");
  90. SetConsoleCP(1252);
  91. position.X=0;
  92. position.Y=0;
  93. cmdbuffer=GetStdHandle(-11);
  94. }
  95. void dump_pc(void){
  96. printf("%d",backup[0]);
  97. printf("%d",backup[1]);
  98. printf("%d",backup[2]);
  99. printf("%d",backup[3]);
  100. if(ziffern==8){
  101. printf("%d",backup[4]);
  102. printf("%d",backup[5]);
  103. printf("%d",backup[6]);
  104. printf("%d\n",backup[7]);
  105. }else{
  106. printf("\n");
  107. }
  108. }
  109. void dump_player(void){
  110. printf("%d",player[0]);
  111. printf("%d",player[1]);
  112. printf("%d",player[2]);
  113. printf("%d",player[3]);
  114. if(ziffern==8){
  115. printf("%d",player[4]);
  116. printf("%d",player[5]);
  117. printf("%d",player[6]);
  118. printf("%d\n",player[7]);
  119. }else{
  120. printf("\n");
  121. }
  122. }
  123. void playerinput(char* arry,int lengh){
  124. int i = 0;
  125. char temp;
  126. while(i<8){
  127. arry[i]=0; i++;
  128. }
  129. i = 0;
  130. while(1){
  131. temp=getch();
  132. if(temp>0x30 && temp<0x3a && i<lengh){
  133. arry[i]=temp;
  134. printf("%c",temp);
  135. i++;
  136. }else{
  137. if(temp=='\b'){
  138. if(i!=0){
  139. set_posotion((i-1),(versuch+1));
  140. printf(" ");
  141. set_posotion((i-1),(versuch+1));
  142. i--;
  143. }
  144. }
  145. else if(temp==0x0d && i==lengh){
  146. break;
  147. }
  148. }
  149. }
  150. i=0;
  151. while(i<4){
  152. arry[i]-=0x30; i++;
  153. }
  154. }
  155. int check (int lengh){
  156. int i=0;
  157. int x=0;
  158. while(i<lengh){
  159. if(player[i]==computer[i]){
  160. result[x]=1;
  161. x++;
  162. player[i]=0;
  163. computer[i]=0;
  164. }
  165. i++;
  166. }
  167. return x;
  168. }
  169. int compare(int lengh){
  170. int i=0;
  171. int y=0;
  172. int x=0;
  173. while(i<lengh){
  174. if(player[i]==0){
  175. i++;
  176. }else{
  177. x=0;
  178. while(x<lengh){
  179. if(player[i]==computer[x]){
  180. y++;
  181. computer[x]=0;
  182. player[i]=0;
  183. break;
  184. }else{
  185. x++;
  186. }
  187. }
  188. }
  189. i++;
  190. }
  191. return y;
  192. }
  193. void xprintf(void){
  194. int i=0;
  195. while(i<ziffern){
  196. printf("X");
  197. i++;
  198. }
  199. printf("\n");
  200. }
  201. int main (void){
  202. icmd();
  203. start:;
  204. schwierigket=0;
  205. //standart frontpage
  206. system("title Mastermind");
  207. printf("Mastermind Programmiert von Simon Diepold\n");
  208. printf("drcke eine belibige Taste...");
  209. getchar();
  210. system("cls");
  211. printf("W„hlen sie eine schwierigkeits Stufe:\n"
  212. "Einfach(1) [4 Stellen, keine gleichen Zahlen,8 Versuche]\n"
  213. "Herausfordernd(2) [4 Stellen, 6 Versuche]\n"
  214. "Schwer(3) [8 Stellen, 6 Versuche]\n"
  215. "Beenden(4) [beendet das Programm]\n"
  216. );
  217. while(1){
  218. schwierigket=getchar();
  219. if(schwierigket==0x31 ||schwierigket==0x32 || schwierigket==0x33 || schwierigket==0x34){
  220. if(schwierigket==0x34){
  221. goto end;
  222. }
  223. schwierigket-=0x30;
  224. ziffern=level[(schwierigket-1)];
  225. break;
  226. }
  227. }
  228. system("cls");
  229. //zufallsgenerator
  230. if(schwierigket==1){
  231. random(backup);
  232. }
  233. else if (schwierigket==2){
  234. randomLV2(backup);
  235. }
  236. else if (schwierigket==3){
  237. randomLV3(backup);
  238. }
  239. xprintf();
  240. versuch = 0;
  241. beginn:;
  242. int i=0;
  243. int x=0;
  244. while(i<8){
  245. result[i]=0; i++;
  246. }
  247. i=0;
  248. while(i<8){
  249. computer[i]=backup[i];
  250. i++;
  251. }
  252. i=0;
  253. playerinput(player,ziffern);
  254. x=check(ziffern);
  255. if (x==ziffern){
  256. printf("\ndu hast gewonnen! Gratulation!");
  257. getch();
  258. system("cls");
  259. goto start;
  260. }else{
  261. x+=compare(ziffern);
  262. set_posotion((ziffern+1),(versuch+1));
  263. i=0;
  264. while(i<x){
  265. result[i]+=0x30;
  266. printf("%c",result[i]);
  267. i++;
  268. }
  269. printf(" \n");
  270. if(versuch==rounds[schwierigket-1]){
  271. printf("\nleider verloren. Die L”sung ist:");
  272. dump_pc();
  273. getch();
  274. system("cls");
  275. goto start;
  276. }
  277. versuch++;
  278. goto beginn;
  279. }
  280. end:;
  281. return 0;
  282. }