Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.00 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int spryt = 5;
  5. struct plansza{
  6. int tura;
  7. int wysokosc;
  8. int szerokosc;
  9. int **elementy;
  10.  
  11. };
  12.  
  13. typedef struct{ //typedef przypisuje pewnej nazwie(tu pos), jakiś typ danych(tu struct, które ma elemnty x i y)
  14. int x;
  15. int y;
  16. }pos;
  17.  
  18.  
  19. void ZwolnijTablice(struct plansza *tab);
  20. void ZapiszDoPliku(struct plansza StanGry);
  21. struct plansza WczytajZPliku();
  22. struct plansza rozruch();
  23. void wypiszOceny(struct plansza wymiary);
  24. void Wypisz(struct plansza);
  25. int sprawdzwygrana(struct plansza StanGry);
  26. pos Koordynaty();
  27. int min(int a, int b);
  28. int max(int a, int b);
  29. int alpha_beta(struct plansza StanGry, int glebokosc, int alpha, int beta);
  30. pos max_z_tablicy(struct plansza oceny);
  31.  
  32. int main()
  33. {
  34. struct plansza StanGry = rozruch();
  35. int game_over=0;
  36. Wypisz(StanGry);
  37. int poprawne;
  38. pos wybor;
  39.  
  40. while(game_over==0){
  41. game_over = sprawdzwygrana(StanGry);
  42. printf("%d\n", game_over);
  43. if(game_over==-1||game_over==1)
  44. game_over = 0;
  45.  
  46. if(game_over==-5)
  47. {
  48. printf("Koniec Gry! Wygraly kolka!\n");
  49. return 0;
  50. }
  51. if(game_over==5)
  52. {
  53. printf("Koniec Gry! Wygraly krzyzyki!\n");
  54. return 0;
  55. }
  56. if(StanGry.tura==0){
  57. poprawne = 0;
  58. do{
  59. wybor = Koordynaty();
  60. if(StanGry.elementy[wybor.y][wybor.x]==0)
  61. poprawne = 1;
  62. else{
  63. printf("Bledny ruch. Sprobuj ponownie:\n");
  64. }
  65. }while(!poprawne);
  66. StanGry.elementy[wybor.y][wybor.x]='O';//Tura?'X':'O';//Jeżeli Tura==0 to zapisuje O jak Tura==1 to zapisuje X
  67. StanGry.tura = 1;
  68. }
  69. else
  70. {
  71. struct plansza tablicaRuchow = {-1, StanGry.szerokosc, StanGry.wysokosc};
  72. tablicaRuchow.elementy=calloc(tablicaRuchow.wysokosc, sizeof(int*));
  73. for(int i=0; i<tablicaRuchow.wysokosc; i++)
  74. *(tablicaRuchow.elementy+i)=(int*)calloc(tablicaRuchow.szerokosc, sizeof(int));
  75. for(int y=0; y<StanGry.wysokosc; y++)
  76. for(int x=0; x<StanGry.szerokosc; x++){
  77. //printf("%d\n", StanGry.elementy[y][x]);
  78. if(StanGry.elementy[y][x]!=0){
  79. tablicaRuchow.elementy[y][x]=-100;
  80. }
  81. else{
  82. StanGry.elementy[y][x]='X';//Tu zmienić znaczek na zmienna żeby można wybierać strone gracza
  83. StanGry.tura = 0;
  84. tablicaRuchow.elementy[y][x] = alpha_beta(StanGry, spryt, -90, 90);
  85. StanGry.elementy[y][x]=0;
  86. }
  87. }
  88.  
  89. pos najlepszyRuch = max_z_tablicy(tablicaRuchow);
  90. wypiszOceny(tablicaRuchow);
  91. StanGry.elementy[najlepszyRuch.y][najlepszyRuch.x] = 'X';
  92. }
  93. Wypisz(StanGry);
  94. ZapiszDoPliku(StanGry);
  95. }
  96. ZwolnijTablice(&StanGry);
  97. }
  98.  
  99. void ZwolnijTablice(struct plansza *tab)
  100. {
  101. for(int y=0; y<tab->wysokosc; y++)
  102. free(tab->elementy[y]);
  103. }
  104.  
  105. void ZapiszDoPliku(struct plansza StanGry)
  106. {
  107. FILE *plik = fopen("Stan.txt","w+");
  108. fprintf(plik,"%d x %d %d\n", StanGry.wysokosc, StanGry.szerokosc, StanGry.tura);
  109. for(int i = 0; i < StanGry.wysokosc; ++i, fprintf(plik,"\n"))
  110. for(int j = 0; j < StanGry.szerokosc; ++j)
  111. fprintf(plik,"%d ", StanGry.elementy[i][j]);
  112. fclose(plik);
  113. }
  114.  
  115. struct plansza WczytajZPliku(){
  116. FILE *plik = fopen("Stan.txt","r");
  117. struct plansza StanGry;
  118. fscanf(plik,"%d x %d", &StanGry.wysokosc, &StanGry.szerokosc, &StanGry.tura);
  119. printf("%d %d", StanGry.wysokosc, StanGry.szerokosc);
  120. StanGry.elementy=calloc(StanGry.wysokosc, sizeof(int*));//Tworzy wskaźnik który jest w stanie przechowywać StanGry.wysokosc wskaźników typu char
  121. for(int i=0; i<StanGry.wysokosc; i++){
  122. StanGry.elementy[i]=(int*)calloc(StanGry.szerokosc, sizeof(int));
  123. }
  124. for(int i = 0; i < StanGry.wysokosc; ++i, fprintf(plik,"\n"))
  125. for(int j = 0; j < StanGry.szerokosc; ++j)
  126. fscanf(plik,"%d", StanGry.elementy[i]+j);
  127. fclose(plik);
  128. return StanGry;
  129. }
  130.  
  131. struct plansza rozruch()
  132. {
  133. printf("Czy chcesz wczytac poprzedni stan gry?[t/n]");
  134. char wybor;
  135. int w,s;
  136. scanf("%c",&wybor);
  137. struct plansza wymiary;
  138. if(wybor=='t')
  139. wymiary = WczytajZPliku();
  140. else if(wybor=='n'){
  141. wymiary.tura = 0;
  142. printf("prosze podac wymiary planszy: \n wyskokosc:");
  143. scanf("%d", &wymiary.wysokosc);
  144. printf("\n szerokosc: ");
  145. scanf("%d",&wymiary.szerokosc);
  146. printf("\n\n\n\n\n\n");
  147. wymiary.elementy=calloc(wymiary.wysokosc, sizeof(int*));
  148. for(int i=0; i<wymiary.wysokosc; i++){
  149. *(wymiary.elementy+i)=(int*)calloc(wymiary.szerokosc, sizeof(int));
  150. }
  151. }
  152. printf("wymiary: %dx%d\n\n", wymiary.wysokosc, wymiary.szerokosc);
  153. return wymiary;
  154. }
  155.  
  156. void wypiszOceny(struct plansza wymiary){
  157. for(int i = 0; i < wymiary.wysokosc; ++i, printf("\n")){
  158. for(int j = 0; j < wymiary.szerokosc; ++j)
  159. printf(" %4d |",wymiary.elementy[i][j]);
  160. printf("\n");
  161. }
  162.  
  163. }
  164.  
  165. void Wypisz(struct plansza wymiary){
  166. for(int odlicz = 0 ; odlicz < wymiary.szerokosc ; ++odlicz)
  167. printf(" %d ", odlicz);
  168. printf("\n");
  169. for(int k = 0 ; k < wymiary.szerokosc ; ++k)
  170. printf("---|");
  171. printf("\n");
  172. for(int i = 0; i < wymiary.wysokosc; ++i, printf("\n"))
  173. {
  174. for(int j = 0; j < wymiary.szerokosc; ++j)
  175. if(wymiary.elementy[i][j])
  176. if(wymiary.elementy[i][j] == 'X')
  177. printf(" %c |",wymiary.elementy[i][j]);
  178. else
  179. {
  180. printf(" %c |",wymiary.elementy[i][j]);
  181. }
  182. else
  183. printf(" |");
  184. printf(" %d\n", i);
  185. for(int k = 0 ; k < wymiary.szerokosc ; ++k)
  186. printf("---|");
  187. }
  188. }
  189.  
  190. pos Koordynaty(){
  191. int x,y;
  192. scanf("%d %d", &x, &y);
  193. return (pos){x,y};
  194. }
  195.  
  196. int sprawdzwygrana(struct plansza StanGry)
  197. {
  198. /*
  199. if(StanGry.elementy[1][1]!=0&&(StanGry.elementy[0][0]==StanGry.elementy[1][1]&&StanGry.elementy[0][0]==StanGry.elementy[2][2]||StanGry.elementy[0][2]==StanGry.elementy[1][1]&&StanGry.elementy[0][2]==StanGry.elementy[2][0]))
  200. if(StanGry.elementy[1][1]=='O')
  201. return -1;
  202. else
  203. return 1;
  204.  
  205.  
  206. for(int i=0; i<3; i++)
  207. if(StanGry.elementy[i][i]!=0&&(StanGry.elementy[0][i]==StanGry.elementy[1][i]&&StanGry.elementy[0][i]==StanGry.elementy[2][i]||StanGry.elementy[i][0]==StanGry.elementy[i][1]&&StanGry.elementy[i][0]==StanGry.elementy[i][2]))
  208. if(StanGry.elementy[i][i]=='O')
  209. return -1;
  210. else
  211. return 1;
  212. return 0;
  213. */
  214.  
  215. for(int y=0; y<StanGry.wysokosc; y++)
  216. for(int x=0; x<StanGry.szerokosc-4; x++)
  217. if(StanGry.elementy[y][x]!=0 &&
  218. StanGry.elementy[y][x]==StanGry.elementy[y][x+1]&&
  219. StanGry.elementy[y][x]==StanGry.elementy[y][x+2]&&
  220. StanGry.elementy[y][x]==StanGry.elementy[y][x+3]){
  221. if(StanGry.elementy[y][x]==StanGry.elementy[y][x+4]){
  222. if(StanGry.elementy[y][x]=='O')
  223. return -5;
  224. else
  225. return 5;
  226. }else{
  227. if(StanGry.elementy[y][x]=='O')
  228. return -1;
  229. else
  230. return 1;
  231. }
  232. }
  233.  
  234.  
  235. for(int y=0; y<StanGry.wysokosc-4; y++)
  236. for(int x=0; x<StanGry.szerokosc; x++)
  237. if(StanGry.elementy[y][x]!=0 &&
  238. StanGry.elementy[y][x]==StanGry.elementy[y+1][x]&&
  239. StanGry.elementy[y][x]==StanGry.elementy[y+2][x]&&
  240. StanGry.elementy[y][x]==StanGry.elementy[y+3][x]){
  241. if(StanGry.elementy[y][x]==StanGry.elementy[y+4][x]){
  242. if(StanGry.elementy[y][x]=='O')
  243. return -5;
  244. else
  245. return 5;
  246. }else{
  247. if(StanGry.elementy[y][x]=='O')
  248. return -1;
  249. else
  250. return 1;
  251. }
  252. }
  253. for(int y=0; y<StanGry.wysokosc-4; y++)
  254. for(int x=0; x<StanGry.szerokosc-4; x++)
  255. if(StanGry.elementy[y][x]!=0 &&
  256. StanGry.elementy[y][x]==StanGry.elementy[y+1][x+1]&&
  257. StanGry.elementy[y][x]==StanGry.elementy[y+2][x+2]&&
  258. StanGry.elementy[y][x]==StanGry.elementy[y+3][x+3]){
  259. if(StanGry.elementy[y][x]==StanGry.elementy[y+4][x+4]){
  260. if(StanGry.elementy[y][x]=='O')
  261. return -5;
  262. else
  263. return 5;
  264. }else{
  265. if(StanGry.elementy[y][x]=='O')
  266. return -1;
  267. else
  268. return 1;
  269. }
  270. }
  271. for(int y=0; y<StanGry.wysokosc-4; y++)
  272. for(int x=StanGry.szerokosc; x>StanGry.szerokosc-4; --x)
  273. if(StanGry.elementy[y][x]!=0 &&
  274. StanGry.elementy[y][x]==StanGry.elementy[y+1][x-1]&&
  275. StanGry.elementy[y][x]==StanGry.elementy[y+2][x-2]&&
  276. StanGry.elementy[y][x]==StanGry.elementy[y+3][x-3]){
  277. if(StanGry.elementy[y][x]==StanGry.elementy[y+4][x-4]){
  278. if(StanGry.elementy[y][x]=='O')
  279. return -5;
  280. else
  281. return 5;
  282. }else{
  283. if(StanGry.elementy[y][x]=='O')
  284. return -1;
  285. else
  286. return 1;
  287. }
  288. }
  289. return 0;
  290. }
  291.  
  292. int min(int a, int b){
  293. if(a>b)
  294. return b;
  295. return a;
  296. }
  297.  
  298. int max(int a, int b){
  299. if(a>b)
  300. return a;
  301. return b;
  302. }
  303.  
  304. int alpha_beta(struct plansza StanGry, int glebokosc, int alpha, int beta){
  305. //Wypisz(StanGry);
  306. int wynik = sprawdzwygrana(StanGry);
  307. //printf("%d\n", wynik);
  308. if(glebokosc==0||wynik==-5||wynik==5){//Przy zmianie sprawdzania wygranej trzeba zmienić warunek
  309. return wynik;
  310. }
  311.  
  312. if(StanGry.tura==0){
  313. StanGry.tura = 1;
  314. for(int y=0; y<StanGry.wysokosc; y++)
  315. for(int x=0; x<StanGry.szerokosc; x++)
  316. if(StanGry.elementy[y][x]==0){
  317. StanGry.elementy[y][x]='O';
  318. beta = min(beta, alpha_beta(StanGry, glebokosc-1, alpha, beta));
  319. StanGry.elementy[y][x]=0;
  320. if(alpha>=beta)
  321. break;
  322. }
  323. StanGry.tura = 0;
  324. if(glebokosc==spryt)
  325. printf("alpha - %d, beta - %d\n", alpha, beta);
  326. //ZwolnijTablice(&StanGry);
  327. return beta;
  328.  
  329. }else{
  330. StanGry.tura = 0;
  331. for(int y=0; y<StanGry.wysokosc; y++)
  332. for(int x=0; x<StanGry.szerokosc; x++)
  333. if(StanGry.elementy[y][x]==0){
  334. StanGry.elementy[y][x]='X';
  335.  
  336. alpha = max(alpha, alpha_beta(StanGry, glebokosc-1, alpha, beta));
  337. StanGry.elementy[y][x]=0;
  338.  
  339. if(alpha>=beta)
  340. break;
  341. }
  342. StanGry.tura = 1;
  343. if(glebokosc==spryt)
  344. printf("alpha - %d, beta - %d\n", alpha, beta);
  345. //ZwolnijTablice(&StanGry);
  346. return alpha;
  347. }
  348.  
  349. }
  350.  
  351. pos max_z_tablicy(struct plansza oceny){
  352. pos maxPolozenie = {0,0};
  353. int maxWartosc = oceny.elementy[0][0];
  354. for(int i=1; i<oceny.szerokosc*oceny.wysokosc; i++)
  355. if (maxWartosc<oceny.elementy[i/oceny.szerokosc][i%oceny.szerokosc]){
  356. maxPolozenie = (pos){.x=i%oceny.szerokosc, .y=i/oceny.szerokosc};
  357. maxWartosc = oceny.elementy[i/oceny.szerokosc][i%oceny.szerokosc];
  358. }
  359. return maxPolozenie;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement