Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <time.h>
- #include <windows.h>
- void generate(short int* map)
- {
- for (short int i = 0; i != 100; i++)
- {
- int randomiser = rand()%4;
- if (randomiser == 0) map[i] = 1;
- if (randomiser != 0) map[i] = 0;
- }
- map[94] = 2;
- map[84] = 0;
- }
- void output(short int* map, int score)
- {
- system("cls");
- short int string = 0;
- for (short int i = 0; i != 100; i++)
- {
- if (i / 10 > string)
- {
- string++;
- printf("\n");
- }
- if (map[i] == 0) printf(" ");
- if (map[i] == 1) printf("Z");
- if (map[i] == 2) printf("X");
- if (i == 49)
- {
- printf(" Score: %d", score);
- }
- }
- //printf("\n\n\n\n");
- }
- int lose(short int* map)
- {
- for (short int i = 99; i != 89; i--)
- {
- if (map[i] == 2 && map[i-10] == 1)
- {
- return 1;
- }
- }
- return 0;
- }
- int keyPressed(int key)
- {
- return (GetAsyncKeyState(key) && 0x8000 != 0);
- }
- int move(short int* map, int score)
- {
- int alreadyMoved = 0;
- if (keyPressed(VK_LEFT))
- {
- for (short int i = 99; i != 89; i--)
- {
- if (map[i] == 2 && map[i-1] != 1 && i % 10 != 0 && alreadyMoved == 0)
- {
- map[i]=0;
- map[i-1]=2;
- alreadyMoved = 1;
- }
- }
- output(map, score);
- return 0;
- }
- if (keyPressed(VK_RIGHT))
- {
- for (short int i = 99; i != 89; i--)
- {
- if (map[i] == 2 && map[i+1] != 1 && i % 10 != 9 && alreadyMoved == 0)
- {
- map[i]=0;
- map[i+1]=2;
- alreadyMoved = 1;
- }
- }
- score = score + score / 100 + 10;
- output(map, score);
- return 0;
- }
- if (keyPressed(VK_UP))
- {
- return 1;
- }
- }
- void tick(short int* map)
- {
- int position;
- for (short int i = 99; i != 89; i--)
- {
- if (map[i] == 2)
- {
- position = i;
- }
- }
- for (short int i = 99; i != 9; i--)
- {
- map[i] = map[i-10];
- }
- for(short int i = 0; i != 10; i++)
- {
- int randomiser = rand()%4;
- if (randomiser == 0) map[i] = 1;
- else map[i] = 0;
- }
- map[position] = 2;
- }
- int main() {
- short int lost = 0;
- short int map[100];
- int score = 0;
- generate(map);
- output(map, score);
- srand(time(NULL));
- do
- {
- int clockStart, clockEnd, doTick;
- clockStart=clock();
- clockEnd = clockStart + 500;
- do
- {
- clockStart=clock();
- move(map, score);
- } while(clockStart < clockEnd);
- score = score + score / 100 + 10;
- lost=lose(map);
- tick(map);
- output(map, score);
- } while (lost == 0);
- system("cls");
- printf("YOU LOST");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement