#include #include #define SZER 20 #define WYS 16 enum podlogi { P_TLO, P_PODLOGA, P_PRZYCISK, P_OBNIZONA_SCIANA }; enum obiekty { O_BRAK, O_SKRZYNIA, O_SCIANA, O_BOHATER }; struct Pole { UWORD podloga; UBYTE obiekt, numer; }; struct Plansza { struct Pole plansza[WYS][SZER]; struct Pole *bohater; ULONG status; /* Status przycisków */ ULONG cel; /* Docelowy status */ }; /* Otwórz zasoby systemowe, rozpocznij grę, zakończ */ main() { static struct Plansza plansza = { 0 }; wczytajPlansze(&plansza); gra(&plansza); return(0); } wczytajPlansze(struct Plansza *p) { WORD i, j; for (j = 0; j < SZER; j++) { p->plansza[0][j].obiekt = O_SCIANA; p->plansza[WYS - 1][j].obiekt = O_SCIANA; } for (i = 1; i < WYS - 1; i++) { p->plansza[i][0].obiekt = O_SCIANA; for (j = 1; j < SZER - 1; j++) p->plansza[i][j].podloga = P_PODLOGA; p->plansza[i][SZER - 1].obiekt = O_SCIANA; } p->plansza[2][2].obiekt = O_SKRZYNIA; p->plansza[3][3].podloga = P_PRZYCISK; p->plansza[3][3].numer = 0; p->bohater = &p->plansza[1][1]; p->status = 0L; p->cel = 0x00000001; return(TRUE); } /* Rozpocznij grę */ gra(struct Plansza *p) { WORD dx = 0, dy = 0; struct Pole *d; while (p->status != p->cel) { printf("Pozycja: %d %d\n", (p->bohater - p->plansza) % SZER, (p->bohater - p->plansza) / SZER); odczytaj(&dx, &dy); d = p->bohater + (dy * SZER) + dx; printf("Ruch %d %d\n", dx, dy); ruch(p, p->bohater, d); } return(TRUE); } /* Odczytaj kierunek ruchu */ odczytaj(WORD *dx, WORD *dy) { int x, y; printf("Podaj kierunek:"); fflush(stdin); scanf("%d %d", &x, &y); *dx = x; *dy = y; return(TRUE); } /* Wykonaj ruch bohaterem */ ruch(struct Plansza *p, struct Pole *a, struct Pole *b) { struct Pole *c = b + (b - a); int wynik = TRUE; if (b->obiekt == O_SKRZYNIA) wynik = popchnij(p, a, b, c); else if (b->obiekt == O_SCIANA) wynik = FALSE; if (wynik) { printf("Bohater poruszył się\n"); b->obiekt = a->obiekt; a->obiekt = O_BRAK; p->bohater = b; } return(wynik); } /* Spróbuj popchnąć skrzynię */ popchnij(struct Plansza *p, struct Pole *a, struct Pole *b, struct Pole *c) { int wynik = TRUE; if (c->obiekt != O_BRAK) wynik = FALSE; if (wynik) { printf("Poruszono skrzynią\n"); c->obiekt = b->obiekt; b->obiekt = O_BRAK; if (b->podloga == P_PRZYCISK) { p->status &= ~(1 << b->numer); printf("Zwolniono przycisk ($%lx)\n", p->status); } if (c->podloga == P_PRZYCISK) { p->status |= 1 << b->numer; printf("Naciśnięto przycisk ($%lx)\n", p->status); } } return(wynik); }