Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LICZBA_KOLUMN 8
- #define LICZBA_WIERSZY 8
- char plansza[LICZBA_KOLUMN][LICZBA_WIERSZY+1];
- int ostatniWiersz[8];
- void zainicjujPlansze()
- {
- for(int i = 0; i < LICZBA_KOLUMN; i++)
- {
- for(int j = 0; j < LICZBA_WIERSZY; j++)
- {
- plansza[i][j] = '-';
- }
- }
- char literka = 'a';
- for(int i = 0; i < LICZBA_KOLUMN;i++)
- {
- plansza[i][LICZBA_WIERSZY] = literka++;
- }
- for(int i = 0; i < LICZBA_KOLUMN;i++)
- {
- ostatniWiersz[i] = 8;
- }
- }
- void wyswietlPlansze()
- {
- for(int j = 0; j < LICZBA_WIERSZY+1; j++)
- {
- for(int i = 0; i < LICZBA_KOLUMN; i++)
- {
- printf("%c",plansza[i][j]);
- }
- puts("");
- }
- }
- int wygranaPionowo()
- {
- for(int i = 0; i < LICZBA_KOLUMN;i++)
- {
- for(int j = 0; j < LICZBA_WIERSZY-4;j++)
- {
- if(plansza[i][j] == '1' && plansza[i][j+1] == '1' && plansza[i][j+2] == '1'
- && plansza[i][j+3] == '1' && plansza[i][j+4] == '1')
- {
- return 1;
- }
- if(plansza[i][j] == '2' && plansza[i][j+1] == '2' && plansza[i][j+2] == '2'
- && plansza[i][j+3] == '2' && plansza[i][j+4] == '2')
- {
- return 1;
- }
- }
- }
- return 0;
- }
- int wygranaPoziomo()
- {
- for(int j = 0; j < LICZBA_WIERSZY;j++)
- {
- for(int i = 0; i < LICZBA_KOLUMN-4;i++)
- {
- if(plansza[i][j] == '1' && plansza[i+1][j] == '1' && plansza[i+2][j] == '1'
- && plansza[i+3][j] == '1' && plansza[i+4][j] == '1')
- {
- return 1;
- }
- if(plansza[i][j] == '2' && plansza[i+1][j] == '2' && plansza[i+2][j] == '2'
- && plansza[i+3][j] == '2' && plansza[i+4][j] == '2')
- {
- return 1;
- }
- }
- }
- return 0;
- }
- int wygranaSkosnie()
- {
- for(int i = 0; i < LICZBA_KOLUMN-4;i++)
- {
- for(int j = 0; j < LICZBA_WIERSZY-4;j++)
- {
- if(plansza[i][j] == '1' && plansza[i+1][j+1] == '1' && plansza[i+2][j+2] == '1'
- && plansza[i+3][j+3] == '1' && plansza[i+4][j+4] == '1')
- {
- return 1;
- }
- if(plansza[i][j] == '2' && plansza[i+1][j+1] == '2' && plansza[i+2][j+2] == '2'
- && plansza[i+3][j+3] == '2' && plansza[i+4][j+4] == '2')
- {
- return 1;
- }
- }
- }
- for(int i = LICZBA_KOLUMN-1; i > 3;i--)
- {
- for(int j = 0; j < LICZBA_WIERSZY-4;j++)
- {
- if(plansza[i][j] == '1' && plansza[i-1][j+1] == '1' && plansza[i-2][j+2] == '1'
- && plansza[i-3][j+3] == '1' && plansza[i-4][j+4] == '1')
- {
- return 1;
- }
- if(plansza[i][j] == '2' && plansza[i-1][j+1] == '2' && plansza[i-2][j+2] == '2'
- && plansza[i-3][j+3] == '2' && plansza[i-4][j+4] == '2')
- {
- return 1;
- }
- }
- }
- return 0;
- }
- int wczytajDane()
- {
- char wejscie;
- char temp[2];
- int kolumna;
- int flag =0;
- do
- {
- flag = scanf("%s",temp);
- if(flag == EOF)
- exit(0);
- if((int)strlen(temp)!=1)
- {
- continue;
- }
- wejscie = temp[0];
- if(wejscie == '=')
- exit(0);
- kolumna = wejscie - 97;
- }while(kolumna < 0 || kolumna > 7 || ostatniWiersz[kolumna] == 0);
- return kolumna;
- }
- void uaktualnijPlansze(int kolumna, int czyTuraPierwszego)
- {
- ostatniWiersz[kolumna]--;
- if(czyTuraPierwszego)
- {
- plansza[kolumna][ostatniWiersz[kolumna]] = '1';
- }
- else
- {
- plansza[kolumna][ostatniWiersz[kolumna]] = '2';
- }
- }
- int main()
- {
- zainicjujPlansze();
- int kolumna;
- int czyTuraPierwszego = 1;
- while(1)
- {
- wyswietlPlansze();
- kolumna = wczytajDane();
- uaktualnijPlansze(kolumna,czyTuraPierwszego);
- czyTuraPierwszego = !czyTuraPierwszego;
- if(wygranaPionowo() || wygranaPoziomo() || wygranaSkosnie())
- {
- wyswietlPlansze();
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement