-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from Nircek/dzielenie_na_pliki
Podział na pliki (#42)
- Loading branch information
Showing
9 changed files
with
473 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "getch.h" | ||
/* | ||
MIT License | ||
Copyright (c) 2018 Nircek | ||
Permission is hereby granted free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
// code from https://gist.github.com/Nircek/c8b104456e6c68068866425072a0863f | ||
// under MIT license | ||
#include <iostream> | ||
#ifndef _WIN32 | ||
#include <unistd.h> | ||
#include <termios.h> | ||
char getch(){ | ||
// src: https://stackoverflow.com/a/16361724/6732111 | ||
char buf=0; | ||
struct termios old={0}; | ||
std::cout.flush(); | ||
if(tcgetattr(0, &old)<0) | ||
throw "tcsetattr()"; | ||
old.c_lflag&=~ICANON; | ||
old.c_lflag&=~ECHO; | ||
old.c_cc[VMIN]=1; | ||
old.c_cc[VTIME]=0; | ||
if(tcsetattr(0, TCSANOW, &old)<0) | ||
throw "tcsetattr ICANON"; | ||
if(read(0,&buf,1)<0) | ||
throw "read()"; | ||
old.c_lflag|=ICANON; | ||
old.c_lflag|=ECHO; | ||
if(tcsetattr(0, TCSADRAIN, &old)<0) | ||
throw "tcsetattr ~ICANON"; | ||
return buf; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef _GH_NIRCEK_LABIRYNT_GETCH_H_ | ||
#define _GH_NIRCEK_LABIRYNT_GETCH_H_ | ||
|
||
#ifdef _WIN32 | ||
#include <conio.h> | ||
#else | ||
char getch(); | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
#include "labirynt.h" | ||
|
||
// zmienne globalne | ||
string mapy="maps"; //folder, w którym są mapy | ||
string file_name; //ścieżka do mapy | ||
string prev_file; //nazwa poprzedniej mapy | ||
size_t SIZE=0; | ||
string *bufor = NULL; | ||
vector<string> mapa; | ||
char user_ch='X', end_ch='O'; | ||
COORDS player_coords; | ||
COORDS end_coords; | ||
|
||
void getMap() | ||
{ | ||
std::fstream file; | ||
file.open(file_name.c_str(), ios::in); | ||
if(file.good()==false) | ||
{ | ||
throw "File does not exist"; | ||
|
||
} | ||
mapa.clear(); | ||
SIZE = 0; | ||
string line; | ||
while (getline(file, line)) { | ||
mapa.push_back(line); | ||
++SIZE; | ||
} | ||
|
||
file.close(); | ||
} | ||
|
||
bool isExist(COORDS p) { // sprawdza czy dane pole jest na mapie | ||
if(p.y>=SIZE)return false; | ||
return mapa[p.y].size()>=p.x; | ||
} | ||
|
||
bool isWall(COORDS c) // czy na podanych kordach nie ma spacji | ||
{ | ||
if(!isExist(c))return true; | ||
if (mapa[c.y][c.x] == ' ') //sprawdza czy space | ||
return false; //zwraca, czy jest, czy nie | ||
else return true; | ||
} | ||
COORDS randomCoords(bool notWall) { | ||
COORDS c; | ||
do { | ||
c.y = rand() % SIZE; | ||
size_t s = mapa[c.y].size(); | ||
if(s) // s != 0 | ||
c.x = rand() % s; | ||
}while((!isExist(c))||(notWall&&isWall(c))); | ||
return c; | ||
} | ||
void beginingcoords() { // bierze poczontkowe koordy i wrzuca do zmiennej | ||
player_coords = randomCoords(); | ||
} | ||
void targetcoords() { // bierze koordy celu/wyjścia i wrzuca do zmiennej | ||
int rozmiar ,rzmr,sm=SIZE,sn=SIZE ; | ||
do{ | ||
end_coords = randomCoords(); | ||
rozmiar = sqrt(2*pow(mapa[0].size(),2))/2; | ||
rzmr = sqrt(pow(player_coords.x-end_coords.x,2)+pow(player_coords.y-end_coords.y,2)); | ||
cout << rzmr << " "<< rozmiar << endl; | ||
}while (rzmr<rozmiar); | ||
} | ||
|
||
char get(COORDS c) { | ||
if(!isExist(c)) return '?'; | ||
return mapa[c.y][c.x]; | ||
} | ||
void set(COORDS c, char ch) { | ||
if(!isExist(c)) return; | ||
mapa[c.y][c.x] = ch; | ||
} | ||
|
||
// generuje mapę o podanych w parametrach wymiarach oraz zapisuje ją w zmiennej mapa | ||
void generateMap(size_t width, size_t height) { | ||
//src: https://gist.github.com/Nircek/7e1ee37e0bbc30f7ab554c633209a8d4/60b41682b6561b848b7d34ae338c9e8a9ca7ba6e#file-mazes-py-L75 | ||
SIZE = height; | ||
for(int i=0;i<height;++i) | ||
mapa.push_back(string(width, '#')); | ||
COORDS c = randomCoords(false); | ||
set(c,' '); | ||
int it = 0; | ||
while(1) { | ||
c = randomCoords(false); | ||
int n = 0; | ||
int ni = 0; | ||
for(int dx=-1;dx<=1;++dx) | ||
for(int dy=-1;dy<=1;++dy) { | ||
if(dx==0&&dy==0) | ||
continue; | ||
COORDS g = {c.x+dx,c.y+dy}; | ||
if(get(g) == ' ') { | ||
++n; | ||
if(dx==0||dy==0) | ||
++ni; | ||
} | ||
} | ||
if(ni==1&&n<3) { | ||
if(get(c)!=' ') | ||
it=0; | ||
set(c,' '); | ||
} else { | ||
++it; | ||
if(it>width*height*3) | ||
break; | ||
} | ||
} | ||
} | ||
|
||
char getEvent() | ||
|
||
{ | ||
return getch(); | ||
}// pobierz znak | ||
void doEvent(char c) { | ||
c = toupper(c); | ||
COORDS n=player_coords; | ||
switch(c) { | ||
case 'W': --n.y; break; | ||
case 'S': ++n.y; break; | ||
case 'A': --n.x; break; | ||
case 'D': ++n.x; break; | ||
} | ||
if(!isWall(n)) | ||
player_coords = n; | ||
|
||
}// wykonaj operację przypisaną do danego znaku (np WSAD) | ||
char drawOnBufor(COORDS C, char c) { // narysuj na x i y znak c i zwróć poprzednie co tam było | ||
char oc = bufor[C.y][C.x]; | ||
bufor[C.y][C.x] = c; | ||
return oc; | ||
} | ||
void refreshBufor() { // załaduj mapę do bufora i nanieś na nie usera i wyjście | ||
if(!bufor) delete[] bufor; | ||
bufor = new string[SIZE]; | ||
for(size_t i=0; i<SIZE; ++i) | ||
bufor[i] = mapa[i]; | ||
drawOnBufor(player_coords, user_ch); | ||
drawOnBufor(end_coords, end_ch); | ||
} | ||
void viewBufor() { // wypisz bufor na ekran | ||
if(system(NULL)) // sprawdzanie czy konsola dostępna | ||
if(system("CLS")) // sprawdzanie czy komenda CLS zadziałała | ||
if(system("clear")) | ||
cout << string(0xFF, '\n'); | ||
for(size_t i=0;i<SIZE;++i) { | ||
for(size_t j=0;j<bufor[i].size();++j) { | ||
if(player_coords.x==j&&player_coords.y==i) | ||
setColor(RED, BG); | ||
if(end_coords.x==j&&end_coords.y==i) | ||
setColor(GREEN, BG); | ||
cout << bufor[i][j]; | ||
setColor(BLACK, BG); | ||
} | ||
cout << '\n'; | ||
} | ||
} | ||
bool isEnd() {// czy jesteśmy na kordach wyjścia | ||
if ((player_coords.x== end_coords.x) && (player_coords.y==end_coords.y)) | ||
return true; | ||
else return false; | ||
} | ||
#include "pojemnik.h" | ||
void animate() { | ||
cout<<"Udało Ci się rozegrac mape \'"<<prev_file<<"\'.\n\nKlijnij dowolny przycisk, zeby zagrac w kolejna..."; | ||
getch(); | ||
} | ||
bool doEnd(bool animation) { // wykonaj animację wygranej i przerzuć do następnego pliku | ||
|
||
size_t index; | ||
if(animation) | ||
animate(); | ||
pojemnik p1; | ||
struct dirent * plik; | ||
DIR * sciezka; | ||
|
||
if(( sciezka = opendir( mapy.c_str() ) ) ) { | ||
while(( plik = readdir( sciezka ) ) ) | ||
if(string(plik->d_name).size()>=4) | ||
if(string(plik->d_name).substr(string(plik->d_name).size()-4) == string(".map")) | ||
p1.push( plik->d_name ); | ||
closedir( sciezka ); | ||
} | ||
else | ||
throw "Nie udalo sie wylistowac katalogow"; | ||
|
||
p1.sort(); | ||
|
||
index = p1.search(prev_file); | ||
prev_file = p1.get(index+1); | ||
file_name = mapy+"/"+prev_file; | ||
if(prev_file == string (":")) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void loop() { | ||
beginingcoords(); | ||
targetcoords(); | ||
while(1) { | ||
refreshBufor(); | ||
viewBufor(); | ||
doEvent(getEvent()); | ||
if(isEnd()) | ||
break; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#ifndef _GH_NIRCEK_LABIRYNT_LABIRYNT_H_ | ||
#define _GH_NIRCEK_LABIRYNT_LABIRYNT_H_ | ||
|
||
#include <iostream> | ||
#include <cstdlib> /* srand, rand */ | ||
#include <dirent.h> | ||
#include <fstream> | ||
#include <cmath> | ||
#include <vector> | ||
#include "getch.h" | ||
#include "color.h" | ||
|
||
using namespace std; | ||
|
||
//struktury | ||
typedef struct{int x; int y;} COORDS; | ||
|
||
// zmienne globalne | ||
extern string mapy; //folder, w którym są mapy | ||
extern string file_name; //ścieżka do mapy | ||
extern string prev_file; //nazwa poprzedniej mapy | ||
extern size_t SIZE; | ||
extern string *bufor; | ||
extern vector<string> mapa; | ||
extern char user_ch, end_ch; | ||
extern COORDS player_coords; | ||
extern COORDS end_coords; | ||
|
||
// procedury | ||
void getMap(); | ||
bool isExist(COORDS); // sprawdza czy dane pole jest na mapie | ||
bool isWall(COORDS); // czy na podanych kordach nie ma spacji | ||
COORDS randomCoords(bool notWall=true); | ||
void beginingcoords(); // bierze poczontkowe koordy i wrzuca do zmiennej | ||
void targetcoords(); // bierze koordy celu/wyjścia i wrzuca do zmiennej | ||
char get(COORDS); | ||
void set(COORDS, char); | ||
void generateMap(size_t width=SIZE, size_t height=SIZE); // generuje mapę o podanych w parametrach wymiarach oraz zapisuje ją w zmiennej mapa | ||
char getEvent(); //pobierz znak | ||
void doEvent(char); // wykonaj operację przypisaną do danego znaku (np WSAD) | ||
char drawOnBufor(COORDS, char); // narysuj na x i y znak c i zwróć poprzednie co tam było | ||
void refreshBufor(); // załaduj mapę do bufora i nanieś na nie usera i wyjście | ||
void viewBufor(); // wypisz bufor na ekran | ||
bool isEnd(); // czy jesteśmy na kordach wyjścia | ||
void animate(); | ||
bool doEnd(bool animation=true); // wykonaj animację wygranej i przerzuć do następnego pliku | ||
void loop(); | ||
#endif | ||
|
Oops, something went wrong.