Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //POP_2019_12_10_projekt_1_Topolska_Martyna_IBM_1_180002.cpp Microsoft Visual Studio Community 2017 Wersja 15.8.6
- #include "pch.h"
- #include <iostream>
- #include <cstdlib>
- #include <time.h>
- #include <cstdio>
- #include <iostream>
- using namespace std;
- const int path = 0;
- const int wall = 1;
- const int treasure = 2;
- const int player = 3;
- void copy_elem(int part[][5], int map[][20], int mapx, int mapy)
- {
- int x = mapx * 5;
- int y = mapy * 5;
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- map[y + i][x + j] = part[i][j];
- }
- }
- }
- void randomize_treasure(int map[][20])
- {
- srand(time(0));
- for (int i = 0; i < 5; i++)
- {
- int a, b;
- do {
- a = rand() % 10;
- b = rand() % 20;
- } while (map[a][b] != 0);
- map[a][b] = treasure;
- }
- }
- void randomize_player(int map[][20], int* playerx, int* playery)
- {
- srand(time(0));
- int a, b;
- do {
- a = rand() % 10;
- b = rand() % 20;
- } while (map[a][b] != path); //dopóki wylosowane miejsce nie będzie ścieżką
- map[a][b] = player;
- *playerx = a;
- *playery = b;
- }
- void draw(int map[][20], int *playerx, int *playery, bool visibility, int counter, int treasures_left)
- {
- system("CLS");
- cout << "Witaj w poszukiwaczu skarbow!" << endl << "Oto zasady: " << endl << "Aby sie poruszac uzywaj klawiatury numerycznej. 8 - w gore, 5 - w dol, 4 - w lewo, 6 - w prawo" << endl << "Aby wylaczyc ograniczone widzenie, wcisnij 0." << endl << "POWODZENIA!" << endl << endl;
- cout << endl << "Liczba twoich ruchow: " << counter << endl;
- cout << "Skarbow do zebrania pozostalo: " << treasures_left << endl;
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 20; j++)
- {
- if (visibility || (i >= *playerx - 2 && i <= *playerx + 2 && j >= *playery - 2 && j <= *playery + 2)) //jeżeli włączona widoczność, rysuj całą mapę, w przeciwnym wypadku otoczenie
- {
- switch (map[i][j])
- {
- case 0:
- printf("%c", (char)32);
- break;
- case 1:
- printf("%c", (char)219);
- break;
- case 2:
- printf("%c", (char)158);
- break;
- case 3:
- printf("%c", (char)79);
- break;
- }
- }
- else
- {
- printf("%c", (char)178);
- }
- }
- printf("\n");
- }
- }
- void move(int map[][20], int *playerx, int *playery, int movex, int movey, int *counter, int *treasures_left)
- {
- if (*playerx + movex >= 0 && *playery + movey >= 0 && *playerx + movex <= 9 && *playery + movey <= 19) //czy gracz po poruszeniu się nie natrafi na krawędź
- {
- int next_tile = map[*playerx + movex][*playery + movey];
- if (next_tile == treasure)
- {
- (*treasures_left)--;
- }
- if (next_tile == treasure || next_tile == path)
- {
- map[*playerx][*playery] = path;
- map[*playerx + movex][*playery + movey] = player;
- *playerx += movex;
- *playery += movey;
- (*counter)++;
- }
- }
- }
- void get_input(int map[][20], int *playerx, int *playery, int *counter, int *treasures_left, bool *visibility)
- {
- char input;
- cin.read(&input, 1);
- switch (input)
- {
- case 56: //góra
- move(map, playerx, playery, -1, 0, counter, treasures_left);
- break;
- case 53: //dół
- move(map, playerx, playery, 1, 0, counter, treasures_left);
- break;
- case 52: //lewo
- move(map, playerx, playery, 0, -1, counter, treasures_left);
- break;
- case 54: //prawo
- move(map, playerx, playery, 0, 1, counter, treasures_left);
- break;
- case 48: //widoczność
- *visibility = !*visibility;
- break;
- }
- }
- void game_loop(int map[][20], int *playerx, int *playery)
- {
- int counter = 0;
- bool visibility = false;
- int treasures_left = 5;
- while (treasures_left > 0)
- {
- draw(map, playerx, playery, visibility, counter, treasures_left);
- get_input(map, playerx, playery, &counter, &treasures_left, &visibility);
- }
- draw(map, playerx, playery, visibility, counter, treasures_left);
- printf("Gratulacje, zebrano wszystkie skarby!");
- }
- int main()
- {
- srand(time(0));
- int map[10][20];
- int playerx, playery;
- int part[6][5][5] =
- {
- {
- {1,1,0,1,1},
- {1,0,0,1,1},
- {0,0,1,0,0},
- {1,0,0,0,1},
- {1,1,0,1,1},
- },
- {
- {1,1,0,0,1},
- {0,1,1,0,1},
- {0,0,0,0,0},
- {1,0,0,0,1},
- {1,1,0,0,1},
- },
- {
- {0,0,0,0,0},
- {0,0,1,0,0},
- {0,0,1,1,0},
- {1,0,1,1,1},
- {1,0,0,0,0},
- },
- {
- {1,1,0,0,0},
- {1,0,0,1,0},
- {0,0,1,0,0},
- {0,1,0,0,1},
- {1,0,0,1,1},
- },
- {
- {0,0,0,1,1},
- {1,0,0,0,1},
- {0,0,1,0,0},
- {0,0,1,0,0},
- {0,0,0,0,1},
- },
- {
- {1,1,0,1,1},
- {1,1,0,1,1},
- {0,0,0,0,0},
- {1,1,0,1,1},
- {1,1,0,1,1}
- },
- };
- for (int i = 0; i < 2; i++)
- {
- for (int j = 0; j < 4; j++)
- {
- copy_elem(part[rand() % 6], map, j, i); //wybieranie losowej czesci mapy
- }
- }
- randomize_treasure(map);
- randomize_player(map, &playerx, &playery);
- game_loop(map, &playerx, &playery);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement