feat: move with collision but doesnt work
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
#define ELEMENTS_HPP
|
#define ELEMENTS_HPP
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
#include "globals.hpp"
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
Point() : m_row(0), m_col(0), m_symbol(2), m_direction('n'), m_collide(false) {};
|
Point() : m_row(0), m_col(0), m_symbol(2), m_direction('n'), m_collide(false) {};
|
||||||
@@ -19,6 +21,7 @@ struct Point {
|
|||||||
struct PointCollection {
|
struct PointCollection {
|
||||||
std::vector<Point> m_point_list;
|
std::vector<Point> m_point_list;
|
||||||
void move();
|
void move();
|
||||||
|
void move(Point* m_board[ROW][COL]);
|
||||||
void set_direction(char direction);
|
void set_direction(char direction);
|
||||||
void check_collision();
|
void check_collision();
|
||||||
};
|
};
|
||||||
|
|||||||
+2
-1
@@ -2,10 +2,11 @@
|
|||||||
#define INPUT_HPP
|
#define INPUT_HPP
|
||||||
|
|
||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
|
#include "globals.hpp"
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
int _getch;
|
int _getch;
|
||||||
void get_input(bool &running, PointCollection &collection);
|
void get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+2
-1
@@ -6,7 +6,8 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
struct Window {
|
struct Window {
|
||||||
std::array<std::array<int, COL>, ROW> m_board = {};
|
// std::array<std::array<int, COL>, ROW> m_board = {};
|
||||||
|
Point* m_board[ROW][COL] = { nullptr };
|
||||||
void draw_window(PointCollection &collection);
|
void draw_window(PointCollection &collection);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+63
-52
@@ -1,5 +1,4 @@
|
|||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
#include "globals.hpp"
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -26,58 +25,70 @@ void PointCollection::set_direction(char direction) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void PointCollection::move() {
|
void PointCollection::move(Point* m_board[ROW][COL]) {
|
||||||
for(int i = m_point_list.size() - 1; i >= 0; i--) {
|
for(int i = m_point_list.size() - 1; i >= 0; i--) {
|
||||||
if(m_point_list[i].m_direction == 'u') {
|
Point &p = m_point_list[i];
|
||||||
if(m_point_list[i].m_row <= 0) continue;
|
|
||||||
m_point_list[i].assign(m_point_list[i].m_row - 1, m_point_list[i].m_col);
|
int cur_row = p.m_row;
|
||||||
} else if(m_point_list[i].m_direction == 'd') {
|
int cur_col = p.m_col;
|
||||||
if(m_point_list[i].m_row >= ROW - 1) continue;
|
int new_row = cur_row;
|
||||||
m_point_list[i].assign(m_point_list[i].m_row + 1, m_point_list[i].m_col);
|
int new_col = cur_col;
|
||||||
} else if(m_point_list[i].m_direction == 'l') {
|
|
||||||
if(m_point_list[i].m_col <= 0) continue;
|
if(p.m_direction == 'u') {
|
||||||
m_point_list[i].assign(m_point_list[i].m_row, m_point_list[i].m_col - 1);
|
Point *target = m_board[p.m_row - 1][p.m_col];
|
||||||
} else if(m_point_list[i].m_direction == 'r') {
|
if(target == nullptr && p.m_row >= 0) {
|
||||||
if(m_point_list[i].m_col >= COL - 1) continue;
|
new_row--;
|
||||||
m_point_list[i].assign(m_point_list[i].m_row, m_point_list[i].m_col + 1);
|
}
|
||||||
|
else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol
|
||||||
|
&& !target->m_collide) {
|
||||||
|
target->m_symbol *= 2;
|
||||||
|
target->m_collide = true;
|
||||||
|
m_board[cur_row][cur_col] = nullptr;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else if(p.m_direction == 'd') {
|
||||||
|
Point *target = m_board[p.m_row + 1][p.m_col];
|
||||||
|
|
||||||
|
if(target == nullptr && p.m_row < ROW) {
|
||||||
|
new_row++;
|
||||||
|
}
|
||||||
|
else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol
|
||||||
|
&& !target->m_collide) {
|
||||||
|
target->m_symbol *= 2;
|
||||||
|
target->m_collide = true;
|
||||||
|
m_board[cur_row][cur_col] = nullptr;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if(p.m_direction == 'l') {
|
||||||
|
Point *target = m_board[p.m_row][p.m_col - 1];
|
||||||
|
|
||||||
|
if(target == nullptr && p.m_col >= 0) {
|
||||||
|
new_col--;
|
||||||
|
}
|
||||||
|
else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol
|
||||||
|
&& !target->m_collide) {
|
||||||
|
target->m_symbol *= 2;
|
||||||
|
target->m_collide = true;
|
||||||
|
m_board[cur_row][cur_col] = nullptr;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else if(p.m_direction == 'r') {
|
||||||
|
Point *target = m_board[p.m_row][p.m_col + 1];
|
||||||
|
|
||||||
|
if(target == nullptr && p.m_col < ROW) {
|
||||||
|
new_col++;
|
||||||
|
}
|
||||||
|
else if(target->m_symbol == m_board[cur_row][cur_col]->m_symbol
|
||||||
|
&& !target->m_collide) {
|
||||||
|
target->m_symbol *= 2;
|
||||||
|
target->m_collide = true;
|
||||||
|
m_board[cur_row][cur_col] = nullptr;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_board[p.m_row][p.m_col] = nullptr;
|
||||||
|
p.assign(new_row, new_col);
|
||||||
|
m_board[new_row][new_col] = &p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool _check_collision(Point &a, Point &b) {
|
|
||||||
// if(a.m_row == b.m_row && a.m_col == b.m_col && a.m_symbol == b.m_symbol) {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
// return false;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// void PointCollection::check_collision() {
|
|
||||||
// for(int i = m_point_list.size() - 1; i >= 0; i--) {
|
|
||||||
// for(int a = m_point_list.size() - 1; a >= 0; a--) {
|
|
||||||
// if (i == a) continue;
|
|
||||||
// _check_collision(m_point_list[i], m_point_list[a]);
|
|
||||||
// if(m_point_list[i].m_row == m_point_list[a].m_row) {
|
|
||||||
// if(m_point_list[i].m_col == m_point_list[a].m_col) {
|
|
||||||
// if (m_point_list[i].m_symbol == m_point_list[a].m_symbol) {
|
|
||||||
// if(m_point_list[i].m_direction == 'u') {
|
|
||||||
// if(!m_point_list[a].m_collide) {
|
|
||||||
// m_point_list[a].m_collide = true;
|
|
||||||
// m_point_list[a].m_symbol *= 2;
|
|
||||||
// m_point_list.erase(m_point_list.begin() + i);
|
|
||||||
// }
|
|
||||||
// else {
|
|
||||||
// m_point_list[i].m_row++;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if(m_point_list[i].m_direction == 'd') {
|
|
||||||
// }
|
|
||||||
// if(m_point_list[i].m_direction == 'l') {
|
|
||||||
// }
|
|
||||||
// if(m_point_list[i].m_direction == 'r') {
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
+21
-1
@@ -1,7 +1,7 @@
|
|||||||
#include "input.hpp"
|
#include "input.hpp"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
void Input::get_input(bool &running, PointCollection &collection) {
|
void Input::get_input(bool &running, PointCollection &collection, Point* board[ROW][COL]) {
|
||||||
while (running) {
|
while (running) {
|
||||||
initscr();
|
initscr();
|
||||||
cbreak();
|
cbreak();
|
||||||
@@ -12,24 +12,44 @@ void Input::get_input(bool &running, PointCollection &collection) {
|
|||||||
|
|
||||||
if(_getch == 65) {
|
if(_getch == 65) {
|
||||||
_getch = 0;
|
_getch = 0;
|
||||||
|
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
|
||||||
|
if(!collection.m_point_list[i].m_collide) {
|
||||||
|
collection.m_point_list[i].m_collide = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
collection.set_direction('u');
|
collection.set_direction('u');
|
||||||
collection.m_point_list.push_back(Point());
|
collection.m_point_list.push_back(Point());
|
||||||
collection.m_point_list.back().assign();
|
collection.m_point_list.back().assign();
|
||||||
}
|
}
|
||||||
else if(_getch == 66) {
|
else if(_getch == 66) {
|
||||||
_getch = 0;
|
_getch = 0;
|
||||||
|
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
|
||||||
|
if(!collection.m_point_list[i].m_collide) {
|
||||||
|
collection.m_point_list[i].m_collide = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
collection.set_direction('d');
|
collection.set_direction('d');
|
||||||
collection.m_point_list.push_back(Point());
|
collection.m_point_list.push_back(Point());
|
||||||
collection.m_point_list.back().assign();
|
collection.m_point_list.back().assign();
|
||||||
}
|
}
|
||||||
else if(_getch == 67) {
|
else if(_getch == 67) {
|
||||||
_getch = 0;
|
_getch = 0;
|
||||||
|
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
|
||||||
|
if(!collection.m_point_list[i].m_collide) {
|
||||||
|
collection.m_point_list[i].m_collide = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
collection.set_direction('r');
|
collection.set_direction('r');
|
||||||
collection.m_point_list.push_back(Point());
|
collection.m_point_list.push_back(Point());
|
||||||
collection.m_point_list.back().assign();
|
collection.m_point_list.back().assign();
|
||||||
}
|
}
|
||||||
else if(_getch == 68) {
|
else if(_getch == 68) {
|
||||||
_getch = 0;
|
_getch = 0;
|
||||||
|
for(int i = 0; i < collection.m_point_list.size() - 1; i++) {
|
||||||
|
if(!collection.m_point_list[i].m_collide) {
|
||||||
|
collection.m_point_list[i].m_collide = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
collection.set_direction('l');
|
collection.set_direction('l');
|
||||||
collection.m_point_list.push_back(Point());
|
collection.m_point_list.push_back(Point());
|
||||||
collection.m_point_list.back().assign();
|
collection.m_point_list.back().assign();
|
||||||
|
|||||||
+28
-13
@@ -11,13 +11,29 @@ bool _check_collision(Point &a, Point &b) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _increment_point(Point &p) {
|
||||||
|
p.m_symbol *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
void _handle_point(PointCollection &collection, int index, Point &a, Point &b) {
|
void _handle_point(PointCollection &collection, int index, Point &a, Point &b) {
|
||||||
if(a.m_symbol == b.m_symbol && !b.m_collide) {
|
if(a.m_symbol == b.m_symbol && !b.m_collide) {
|
||||||
collection.m_point_list.erase(collection.m_point_list.begin() + index);
|
collection.m_point_list.erase(collection.m_point_list.begin() + index);
|
||||||
|
_increment_point(b);
|
||||||
b.m_collide = true;
|
b.m_collide = true;
|
||||||
}
|
}
|
||||||
if(!b.m_collide) {
|
else if(b.m_collide) {
|
||||||
// set a to be one behind
|
if(a.m_symbol == 'u' && b.m_symbol == 'u') {
|
||||||
|
a.assign(b.m_row - 1, a.m_col);
|
||||||
|
}
|
||||||
|
if(a.m_symbol == 'd' && b.m_symbol == 'd') {
|
||||||
|
a.assign(b.m_row + 1, a.m_col);
|
||||||
|
}
|
||||||
|
if(a.m_symbol == 'l' && b.m_symbol == 'l') {
|
||||||
|
a.assign(b.m_row, a.m_col + 1);
|
||||||
|
}
|
||||||
|
if(a.m_symbol == 'r' && b.m_symbol == 'r') {
|
||||||
|
a.assign(b.m_row, a.m_col - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,24 +42,23 @@ int main() {
|
|||||||
Window window;
|
Window window;
|
||||||
PointCollection collection;
|
PointCollection collection;
|
||||||
bool running = true;
|
bool running = true;
|
||||||
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection));
|
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(collection), std::ref(window.m_board));
|
||||||
|
|
||||||
collection.m_point_list.push_back(Point());
|
collection.m_point_list.push_back(Point());
|
||||||
collection.m_point_list.back().assign();
|
collection.m_point_list.back().assign();
|
||||||
|
|
||||||
while (running) {
|
while (running) {
|
||||||
// collection.check_collision();
|
// collection.check_collision();
|
||||||
collection.move();
|
collection.move(window.m_board);
|
||||||
window.draw_window(collection);
|
window.draw_window(collection);
|
||||||
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
|
// for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
|
||||||
for(int a = collection.m_point_list.size() - 1; a >= 0; a--) {
|
// for(int a = collection.m_point_list.size() - 1; a >= 0; a--) {
|
||||||
if (i == a) continue;
|
// if (i == a) break;
|
||||||
if(_check_collision(collection.m_point_list[i], collection.m_point_list[a])) {
|
// if(_check_collision(collection.m_point_list[i], collection.m_point_list[a])) {
|
||||||
_handle_point(collection, i, collection.m_point_list[i], collection.m_point_list[a]);
|
// _handle_point(collection, i, collection.m_point_list[i], collection.m_point_list[a]);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
std::cout << collection.m_point_list.size() << "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
input_thread.join();
|
input_thread.join();
|
||||||
|
|||||||
+4
-5
@@ -1,19 +1,18 @@
|
|||||||
#include "window.hpp"
|
#include "window.hpp"
|
||||||
#include <array>
|
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
void Window::draw_window(PointCollection &collection) {
|
void Window::draw_window(PointCollection &collection) {
|
||||||
for(std::array<int, COL> &row : m_board) { row.fill(0); }
|
for(Point** row = m_board[0]; row != m_board[0] + 16; row++) { *row = nullptr; }
|
||||||
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
|
for(int i = collection.m_point_list.size() - 1; i >= 0; i--) {
|
||||||
m_board[collection.m_point_list[i].m_row][collection.m_point_list[i].m_col] = collection.m_point_list[i].m_symbol;
|
m_board[collection.m_point_list[i].m_row][collection.m_point_list[i].m_col] = &collection.m_point_list[i];
|
||||||
}
|
}
|
||||||
std::cout << "\033[H" << std::flush;
|
std::cout << "\033[H" << std::flush;
|
||||||
for(int i = 0; i < ROW; i++) {
|
for(int i = 0; i < ROW; i++) {
|
||||||
for(int j = 0; j < COL; j++) {
|
for(int j = 0; j < COL; j++) {
|
||||||
if(m_board[i][j] == 0) std::cout << std::setw(4) << '.';
|
if(m_board[i][j] == nullptr) std::cout << std::setw(4) << '.';
|
||||||
else std::cout << std::setw(4) << m_board[i][j];
|
else std::cout << std::setw(4) << m_board[i][j]->m_symbol;
|
||||||
}
|
}
|
||||||
std::cout << "\r\n";
|
std::cout << "\r\n";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user