Skip to content

Commit

Permalink
Now has multiple files and better documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroBrTe committed Nov 26, 2023
1 parent ee2c2e7 commit b5961b7
Show file tree
Hide file tree
Showing 5 changed files with 453 additions and 204 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ cmake_minimum_required(VERSION 3.26)
project(MoveChar)

set(CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_FLAGS "-static-libgcc -static-libstdc++")

add_executable(MoveChar main.cpp)
add_executable(MoveChar main.cpp
game/PushBlocks.cpp
game/PushBlocks.h
)
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ Q - Exit the game

## About
It is just a basic game to learn c++ basics.
You can use the header file to create your own implemetation of the game.

The code for this project is pretty bad, so feel free to contribute to the project.
246 changes: 246 additions & 0 deletions game/PushBlocks.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
#include "PushBlocks.h"


PushBlocks::PushBlocks(int max_x, int max_y) {
map_x = max_x;
map_y = max_y;
}



void PushBlocks::loadGame() {
map = {};
box_cor = {};
finish_cor = {};

createMap();
initMap();

loadAll();
}

void PushBlocks::displayMap() {
for (int line = 0; line < map_y; line++) {
for (int space = 0; space < map_x; space++) {
std::cout << map[line][space];
}
std::cout << "\n";
}
}

void PushBlocks::loadAll() {
int finish_index = getFinishIndex(last_char_x, last_char_y);

if (finish_index != -1) {
map[last_char_y][last_char_x] = finish_c;
} else {
map[last_char_y][last_char_x] = blank_c;
}

std::vector<int> indexBoxToErase;

for (const std::vector<int> &box_cord : box_cor) {
int finish_index_from_box = getFinishIndex(box_cord[0], box_cord[1]);

if (finish_index_from_box != -1) {
int box_index = getBoxIndex(box_cord[0], box_cord[1]);
indexBoxToErase.push_back(box_index);
} else {
map[box_cord[1]][box_cord[0]] = box_c;
}
}

for (const int &box_index : indexBoxToErase) {
box_cor.erase(box_cor.begin() + box_index);
}

for (const std::vector<int> &finish_cord : finish_cor) {
map[finish_cord[1]][finish_cord[0]] = finish_c;
}

map[char_y][char_x] = char_c;

if (box_cor.empty()) {
upLevel();
has_win = true;

} else {
has_win = false;
}
}

void PushBlocks::moveUp() {
if (char_y - 1 < 0) {
return;
}

int boxIndex = getBoxIndex(char_x, char_y - 1);
if (boxIndex != -1) {
if (box_cor[boxIndex][1] - 1 >= 0 && getBoxIndex(box_cor[boxIndex][0], box_cor[boxIndex][1] - 1) == -1) {
--box_cor[boxIndex][1];
} else {
return;
}
}

last_char_y = char_y;
last_char_x = char_x;

char_y--;
loadAll();
}

void PushBlocks::moveDown() {
if (char_y + 1 >= map_y) {
return;
}

int boxIndex = getBoxIndex(char_x, char_y + 1);
if (boxIndex != -1) {
if (box_cor[boxIndex][1] + 1 < map_y && getBoxIndex(box_cor[boxIndex][0], box_cor[boxIndex][1] + 1) == -1) {
++box_cor[boxIndex][1];
} else {
return;
}
}

last_char_y = char_y;
last_char_x = char_x;

char_y++;
loadAll();
}

void PushBlocks::moveRight() {
if (char_x + 1 >= map_x) {
return;
}

int boxIndex = getBoxIndex(char_x + 1, char_y);
if (boxIndex != -1) {
if (box_cor[boxIndex][0] + 1 < map_x && getBoxIndex(box_cor[boxIndex][0] + 1, box_cor[boxIndex][1]) == -1) {
++box_cor[boxIndex][0];
} else {
return;
}
}

last_char_y = char_y;
last_char_x = char_x;

char_x++;
loadAll();
}

void PushBlocks::moveLeft() {
if (char_x - 1 < 0) {
return;
}

int boxIndex = getBoxIndex(char_x - 1, char_y);
if (boxIndex != -1) {
if (box_cor[boxIndex][0] - 1 >= 0 && getBoxIndex(box_cor[boxIndex][0] - 1, box_cor[boxIndex][1]) == -1) {
--box_cor[boxIndex][0];
} else {
return;
}
}

last_char_y = char_y;
last_char_x = char_x;

char_x--;
loadAll();
}

int PushBlocks::getBoxIndex(int x, int y) {
int i = 0;
for (const std::vector<int> &box_cord : box_cor) {
if (x == box_cord[0] && y == box_cord[1]) {
return i;
}
i++;
}
return -1;
}

int PushBlocks::getFinishIndex(int x, int y) {
int i = 0;
for (const std::vector<int> &finish_cord : finish_cor) {
if (x == finish_cord[0] && y == finish_cord[1]) {
return i;
}
i++;
}
return -1;
}

int PushBlocks::get_level() {
return level;
}
std::vector<std::vector<std::string>> PushBlocks::get_map(){
return map;
}

bool PushBlocks::win() {
return has_win;
}

void PushBlocks::createMap() {
std::vector<std::string> temp_x = {};

for (int x = 0; x < map_x; x++) {
temp_x.push_back(blank_c);
}
for (int y = 0; y < map_y; y++) {
map.push_back(temp_x);
}
}

void PushBlocks::initMap() {
char_x = getRandom(0, map_x - 1);
char_y = getRandom(0, map_y - 1);

last_char_x = char_x;
last_char_y = char_y;

initBoxes();
initFinishPositions();
}

void PushBlocks::initBoxes() {
for (int i = 0; i < sqrt(level) * level/4; i++) {
int box_x, box_y;

do {
box_x = getRandom(1, map_x - 2);
box_y = getRandom(1, map_y - 2);
} while ((box_x == char_x && box_y == char_y) || (getBoxIndex(box_x, box_y) != -1));

box_cor.push_back({box_x, box_y});
}
}

void PushBlocks::initFinishPositions() {
int finish_x, finish_y;

do {
finish_x = getRandom(0, map_x - 1);
finish_y = getRandom(0, map_y - 1);
} while ((finish_x == char_x && finish_y == char_y) || (getBoxIndex(char_x, char_y) != -1));

finish_cor.push_back({finish_x, finish_y});
}

void PushBlocks::upLevel() {
++level;
}

int PushBlocks::getRandom(int min, int max) {
std::random_device rd;
std::uniform_int_distribution<int> dist(min, max);
int random_number = dist(rd);

return random_number;
}

Loading

0 comments on commit b5961b7

Please sign in to comment.