Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- int spryt = 5;
- struct plansza{
- int tura;
- int wysokosc;
- int szerokosc;
- int **elementy;
- };
- typedef struct{ //typedef przypisuje pewnej nazwie(tu pos), jakiś typ danych(tu struct, które ma elemnty x i y)
- int x;
- int y;
- }pos;
- void ZwolnijTablice(struct plansza *tab);
- void ZapiszDoPliku(struct plansza StanGry);
- struct plansza WczytajZPliku();
- struct plansza rozruch();
- void wypiszOceny(struct plansza wymiary);
- void Wypisz(struct plansza);
- int sprawdzwygrana(struct plansza StanGry);
- pos Koordynaty();
- int min(int a, int b);
- int max(int a, int b);
- int alpha_beta(struct plansza StanGry, int glebokosc, int alpha, int beta);
- pos max_z_tablicy(struct plansza oceny);
- int main()
- {
- struct plansza StanGry = rozruch();
- int game_over=0;
- Wypisz(StanGry);
- int poprawne;
- pos wybor;
- while(game_over==0){
- game_over = sprawdzwygrana(StanGry);
- printf("%d\n", game_over);
- if(game_over==-1||game_over==1)
- game_over = 0;
- if(game_over==-5)
- {
- printf("Koniec Gry! Wygraly kolka!\n");
- return 0;
- }
- if(game_over==5)
- {
- printf("Koniec Gry! Wygraly krzyzyki!\n");
- return 0;
- }
- if(StanGry.tura==0){
- poprawne = 0;
- do{
- wybor = Koordynaty();
- if(StanGry.elementy[wybor.y][wybor.x]==0)
- poprawne = 1;
- else{
- printf("Bledny ruch. Sprobuj ponownie:\n");
- }
- }while(!poprawne);
- StanGry.elementy[wybor.y][wybor.x]='O';//Tura?'X':'O';//Jeżeli Tura==0 to zapisuje O jak Tura==1 to zapisuje X
- StanGry.tura = 1;
- }
- else
- {
- struct plansza tablicaRuchow = {-1, StanGry.szerokosc, StanGry.wysokosc};
- tablicaRuchow.elementy=calloc(tablicaRuchow.wysokosc, sizeof(int*));
- for(int i=0; i<tablicaRuchow.wysokosc; i++)
- *(tablicaRuchow.elementy+i)=(int*)calloc(tablicaRuchow.szerokosc, sizeof(int));
- for(int y=0; y<StanGry.wysokosc; y++)
- for(int x=0; x<StanGry.szerokosc; x++){
- //printf("%d\n", StanGry.elementy[y][x]);
- if(StanGry.elementy[y][x]!=0){
- tablicaRuchow.elementy[y][x]=-100;
- }
- else{
- StanGry.elementy[y][x]='X';//Tu zmienić znaczek na zmienna żeby można wybierać strone gracza
- StanGry.tura = 0;
- tablicaRuchow.elementy[y][x] = alpha_beta(StanGry, spryt, -90, 90);
- StanGry.elementy[y][x]=0;
- }
- }
- pos najlepszyRuch = max_z_tablicy(tablicaRuchow);
- wypiszOceny(tablicaRuchow);
- StanGry.elementy[najlepszyRuch.y][najlepszyRuch.x] = 'X';
- }
- Wypisz(StanGry);
- ZapiszDoPliku(StanGry);
- }
- ZwolnijTablice(&StanGry);
- }
- void ZwolnijTablice(struct plansza *tab)
- {
- for(int y=0; y<tab->wysokosc; y++)
- free(tab->elementy[y]);
- }
- void ZapiszDoPliku(struct plansza StanGry)
- {
- FILE *plik = fopen("Stan.txt","w+");
- fprintf(plik,"%d x %d %d\n", StanGry.wysokosc, StanGry.szerokosc, StanGry.tura);
- for(int i = 0; i < StanGry.wysokosc; ++i, fprintf(plik,"\n"))
- for(int j = 0; j < StanGry.szerokosc; ++j)
- fprintf(plik,"%d ", StanGry.elementy[i][j]);
- fclose(plik);
- }
- struct plansza WczytajZPliku(){
- FILE *plik = fopen("Stan.txt","r");
- struct plansza StanGry;
- fscanf(plik,"%d x %d", &StanGry.wysokosc, &StanGry.szerokosc, &StanGry.tura);
- printf("%d %d", StanGry.wysokosc, StanGry.szerokosc);
- StanGry.elementy=calloc(StanGry.wysokosc, sizeof(int*));//Tworzy wskaźnik który jest w stanie przechowywać StanGry.wysokosc wskaźników typu char
- for(int i=0; i<StanGry.wysokosc; i++){
- StanGry.elementy[i]=(int*)calloc(StanGry.szerokosc, sizeof(int));
- }
- for(int i = 0; i < StanGry.wysokosc; ++i, fprintf(plik,"\n"))
- for(int j = 0; j < StanGry.szerokosc; ++j)
- fscanf(plik,"%d", StanGry.elementy[i]+j);
- fclose(plik);
- return StanGry;
- }
- struct plansza rozruch()
- {
- printf("Czy chcesz wczytac poprzedni stan gry?[t/n]");
- char wybor;
- int w,s;
- scanf("%c",&wybor);
- struct plansza wymiary;
- if(wybor=='t')
- wymiary = WczytajZPliku();
- else if(wybor=='n'){
- wymiary.tura = 0;
- printf("prosze podac wymiary planszy: \n wyskokosc:");
- scanf("%d", &wymiary.wysokosc);
- printf("\n szerokosc: ");
- scanf("%d",&wymiary.szerokosc);
- printf("\n\n\n\n\n\n");
- wymiary.elementy=calloc(wymiary.wysokosc, sizeof(int*));
- for(int i=0; i<wymiary.wysokosc; i++){
- *(wymiary.elementy+i)=(int*)calloc(wymiary.szerokosc, sizeof(int));
- }
- }
- printf("wymiary: %dx%d\n\n", wymiary.wysokosc, wymiary.szerokosc);
- return wymiary;
- }
- void wypiszOceny(struct plansza wymiary){
- for(int i = 0; i < wymiary.wysokosc; ++i, printf("\n")){
- for(int j = 0; j < wymiary.szerokosc; ++j)
- printf(" %4d |",wymiary.elementy[i][j]);
- printf("\n");
- }
- }
- void Wypisz(struct plansza wymiary){
- for(int odlicz = 0 ; odlicz < wymiary.szerokosc ; ++odlicz)
- printf(" %d ", odlicz);
- printf("\n");
- for(int k = 0 ; k < wymiary.szerokosc ; ++k)
- printf("---|");
- printf("\n");
- for(int i = 0; i < wymiary.wysokosc; ++i, printf("\n"))
- {
- for(int j = 0; j < wymiary.szerokosc; ++j)
- if(wymiary.elementy[i][j])
- if(wymiary.elementy[i][j] == 'X')
- printf(" %c |",wymiary.elementy[i][j]);
- else
- {
- printf(" %c |",wymiary.elementy[i][j]);
- }
- else
- printf(" |");
- printf(" %d\n", i);
- for(int k = 0 ; k < wymiary.szerokosc ; ++k)
- printf("---|");
- }
- }
- pos Koordynaty(){
- int x,y;
- scanf("%d %d", &x, &y);
- return (pos){x,y};
- }
- int sprawdzwygrana(struct plansza StanGry)
- {
- /*
- 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]))
- if(StanGry.elementy[1][1]=='O')
- return -1;
- else
- return 1;
- for(int i=0; i<3; i++)
- 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]))
- if(StanGry.elementy[i][i]=='O')
- return -1;
- else
- return 1;
- return 0;
- */
- for(int y=0; y<StanGry.wysokosc; y++)
- for(int x=0; x<StanGry.szerokosc-4; x++)
- if(StanGry.elementy[y][x]!=0 &&
- StanGry.elementy[y][x]==StanGry.elementy[y][x+1]&&
- StanGry.elementy[y][x]==StanGry.elementy[y][x+2]&&
- StanGry.elementy[y][x]==StanGry.elementy[y][x+3]){
- if(StanGry.elementy[y][x]==StanGry.elementy[y][x+4]){
- if(StanGry.elementy[y][x]=='O')
- return -5;
- else
- return 5;
- }else{
- if(StanGry.elementy[y][x]=='O')
- return -1;
- else
- return 1;
- }
- }
- for(int y=0; y<StanGry.wysokosc-4; y++)
- for(int x=0; x<StanGry.szerokosc; x++)
- if(StanGry.elementy[y][x]!=0 &&
- StanGry.elementy[y][x]==StanGry.elementy[y+1][x]&&
- StanGry.elementy[y][x]==StanGry.elementy[y+2][x]&&
- StanGry.elementy[y][x]==StanGry.elementy[y+3][x]){
- if(StanGry.elementy[y][x]==StanGry.elementy[y+4][x]){
- if(StanGry.elementy[y][x]=='O')
- return -5;
- else
- return 5;
- }else{
- if(StanGry.elementy[y][x]=='O')
- return -1;
- else
- return 1;
- }
- }
- for(int y=0; y<StanGry.wysokosc-4; y++)
- for(int x=0; x<StanGry.szerokosc-4; x++)
- if(StanGry.elementy[y][x]!=0 &&
- StanGry.elementy[y][x]==StanGry.elementy[y+1][x+1]&&
- StanGry.elementy[y][x]==StanGry.elementy[y+2][x+2]&&
- StanGry.elementy[y][x]==StanGry.elementy[y+3][x+3]){
- if(StanGry.elementy[y][x]==StanGry.elementy[y+4][x+4]){
- if(StanGry.elementy[y][x]=='O')
- return -5;
- else
- return 5;
- }else{
- if(StanGry.elementy[y][x]=='O')
- return -1;
- else
- return 1;
- }
- }
- for(int y=0; y<StanGry.wysokosc-4; y++)
- for(int x=StanGry.szerokosc; x>StanGry.szerokosc-4; --x)
- if(StanGry.elementy[y][x]!=0 &&
- StanGry.elementy[y][x]==StanGry.elementy[y+1][x-1]&&
- StanGry.elementy[y][x]==StanGry.elementy[y+2][x-2]&&
- StanGry.elementy[y][x]==StanGry.elementy[y+3][x-3]){
- if(StanGry.elementy[y][x]==StanGry.elementy[y+4][x-4]){
- if(StanGry.elementy[y][x]=='O')
- return -5;
- else
- return 5;
- }else{
- if(StanGry.elementy[y][x]=='O')
- return -1;
- else
- return 1;
- }
- }
- return 0;
- }
- int min(int a, int b){
- if(a>b)
- return b;
- return a;
- }
- int max(int a, int b){
- if(a>b)
- return a;
- return b;
- }
- int alpha_beta(struct plansza StanGry, int glebokosc, int alpha, int beta){
- //Wypisz(StanGry);
- int wynik = sprawdzwygrana(StanGry);
- //printf("%d\n", wynik);
- if(glebokosc==0||wynik==-5||wynik==5){//Przy zmianie sprawdzania wygranej trzeba zmienić warunek
- return wynik;
- }
- if(StanGry.tura==0){
- StanGry.tura = 1;
- for(int y=0; y<StanGry.wysokosc; y++)
- for(int x=0; x<StanGry.szerokosc; x++)
- if(StanGry.elementy[y][x]==0){
- StanGry.elementy[y][x]='O';
- beta = min(beta, alpha_beta(StanGry, glebokosc-1, alpha, beta));
- StanGry.elementy[y][x]=0;
- if(alpha>=beta)
- break;
- }
- StanGry.tura = 0;
- if(glebokosc==spryt)
- printf("alpha - %d, beta - %d\n", alpha, beta);
- //ZwolnijTablice(&StanGry);
- return beta;
- }else{
- StanGry.tura = 0;
- for(int y=0; y<StanGry.wysokosc; y++)
- for(int x=0; x<StanGry.szerokosc; x++)
- if(StanGry.elementy[y][x]==0){
- StanGry.elementy[y][x]='X';
- alpha = max(alpha, alpha_beta(StanGry, glebokosc-1, alpha, beta));
- StanGry.elementy[y][x]=0;
- if(alpha>=beta)
- break;
- }
- StanGry.tura = 1;
- if(glebokosc==spryt)
- printf("alpha - %d, beta - %d\n", alpha, beta);
- //ZwolnijTablice(&StanGry);
- return alpha;
- }
- }
- pos max_z_tablicy(struct plansza oceny){
- pos maxPolozenie = {0,0};
- int maxWartosc = oceny.elementy[0][0];
- for(int i=1; i<oceny.szerokosc*oceny.wysokosc; i++)
- if (maxWartosc<oceny.elementy[i/oceny.szerokosc][i%oceny.szerokosc]){
- maxPolozenie = (pos){.x=i%oceny.szerokosc, .y=i/oceny.szerokosc};
- maxWartosc = oceny.elementy[i/oceny.szerokosc][i%oceny.szerokosc];
- }
- return maxPolozenie;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement