optimizations

This commit is contained in:
DarthMooMancer
2025-11-01 18:06:21 -04:00
parent f9d0e9fb01
commit 02ff154b81
8 changed files with 34 additions and 51 deletions
-2
View File
@@ -1,2 +0,0 @@
CompileFlags:
Add: [-Iinclude]
+1 -1
View File
@@ -24,7 +24,7 @@ struct Point {
int get_score(); int get_score();
void determine_index_zero_pos(Point** buffer); void determine_index_zero_pos(Point** buffer);
void move_tail_indicies(Point** buffer); void move_tail_indicies(Point** buffer);
void on_fruit_eaten(Point* (*board)[COL], Point** buffer); void on_fruit_eaten(Point (*board)[COL], Point** buffer);
bool segment_overlap(Point** buffer); bool segment_overlap(Point** buffer);
#endif // ELEMENT_HPP #endif // ELEMENT_HPP
+1 -4
View File
@@ -2,9 +2,6 @@
#define INPUT_HPP #define INPUT_HPP
#include "element.hpp" #include "element.hpp"
struct Input { void get_input(enum Direction &dir, bool &r);
int m_getch = 0;
void get_input(enum Direction &dir, bool &r);
};
#endif // INPUT_HPP #endif // INPUT_HPP
+1 -1
View File
@@ -5,7 +5,7 @@
#include "element.hpp" #include "element.hpp"
struct Window { struct Window {
Point* m_board[ROW][COL] = {}; Point m_board[ROW][COL] = {};
void clear_display(); void clear_display();
void draw_display(int milliseconds); void draw_display(int milliseconds);
void update_display(Point** buffer); void update_display(Point** buffer);
+4 -9
View File
@@ -16,11 +16,7 @@ void Point::set_new_point(int row, int col) {
} }
void determine_index_zero_pos(Point** buffer) { void determine_index_zero_pos(Point** buffer) {
if(buffer[1] == nullptr) {
buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' };
} else {
buffer[1]->set_new_point(buffer[0]->m_row, buffer[0]->m_col); buffer[1]->set_new_point(buffer[0]->m_row, buffer[0]->m_col);
}
if (buffer[0]->m_dir == LEFT) { if (buffer[0]->m_dir == LEFT) {
if (buffer[0]->m_col <= 0) buffer[0]->m_col = COL - 1; if (buffer[0]->m_col <= 0) buffer[0]->m_col = COL - 1;
@@ -49,18 +45,17 @@ void move_tail_indicies(Point** buffer) {
memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*)); memmove(buffer + 4, buffer + 3, (_snake_size - 4) * sizeof(Point*));
buffer[3] = last_point; buffer[3] = last_point;
if(buffer[3] != nullptr) { if(buffer[3] != nullptr) buffer[3]->set_new_point(new_row, new_col);
buffer[3]->set_new_point(new_row, new_col);
}
} }
void on_fruit_eaten(Point* (*board)[COL], Point** buffer) { void on_fruit_eaten(Point (*board)[COL], Point** buffer) {
int row, col; int row, col;
if(buffer[2]->m_row == buffer[0]->m_row && buffer[2]->m_col == buffer[0]->m_col) { if(buffer[2]->m_row == buffer[0]->m_row && buffer[2]->m_col == buffer[0]->m_col) {
do { do {
row = (rand() % ROW); row = (rand() % ROW);
col = (rand() % COL); col = (rand() % COL);
} while (board[row][col] != nullptr); // } while (board[row][col] != nullptr);
} while (board[row][col].m_symbol != '.');
buffer[2]->set_new_point(row, col); buffer[2]->set_new_point(row, col);
memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*)); memmove(buffer + 4, buffer + 3, (_snake_size - 3) * sizeof(Point*));
buffer[3] = new Point { ROW, COL, '@' }; buffer[3] = new Point { ROW, COL, '@' };
+9 -18
View File
@@ -1,7 +1,9 @@
#include "input.hpp" #include "input.hpp"
#include <cstdint>
#include <ncurses.h> #include <ncurses.h>
void Input::get_input(enum Direction &dir, bool &running) { void get_input(enum Direction &dir, bool &running) {
uint16_t m_getch;
while(running) { while(running) {
initscr(); initscr();
cbreak(); cbreak();
@@ -9,22 +11,11 @@ void Input::get_input(enum Direction &dir, bool &running) {
m_getch = getch(); m_getch = getch();
endwin(); endwin();
switch (m_getch) {
case 65: // Up arrow if(m_getch == 65) if(dir != DOWN) dir = UP;
if(dir != DOWN) dir = UP; if(m_getch == 66) if(dir != UP) dir = DOWN;
break; if(m_getch == 67) if(dir != LEFT) dir = RIGHT;
case 66: // Down arrow if(m_getch == 68) if(dir != RIGHT) dir = LEFT;
if(dir != UP) dir = DOWN; if(m_getch == 113) running = false;
break;
case 67: // Right arrow
if(dir != LEFT) dir = RIGHT;
break;
case 68: // Left arrow
if(dir != RIGHT) dir = LEFT;
break;
case 113:
running = false;
break;
}
} }
} }
+6 -8
View File
@@ -8,23 +8,22 @@
int main () { int main () {
std::srand(std::time(0)); std::srand(std::time(0));
Window window; Window window;
Input input;
const int MAX_LEN = (ROW * COL) + 3; const int MAX_LEN = (ROW * COL) + 3;
bool running = true; bool running = true;
Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail Point* buffer[MAX_LEN] = {}; // 2 for head, 1 for fruit, rest for tail
buffer[0] = new Point { ROW / 2, COL / 2, '*' }; // Head buffer[0] = new Point { ROW / 2, COL / 2, '*' }; // Head
buffer[1] = new Point { buffer[0]->m_row, buffer[0]->m_col, '.' };
buffer[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit buffer[2] = new Point { (rand() % ROW), (rand() % COL), 'o' }; // Fruit
std::thread input_thread([&] { input.get_input(buffer[0]->m_dir, running); } ); std::thread input_thread([&] { get_input(buffer[0]->m_dir, running ); } );
while (running) { while (running) {
on_fruit_eaten(window.m_board, buffer); on_fruit_eaten(window.m_board, buffer);
determine_index_zero_pos(buffer); determine_index_zero_pos(buffer);
move_tail_indicies(buffer); move_tail_indicies(buffer);
for(int i = MAX_LEN - 1; i > 3; i--) { if(buffer[MAX_LEN - 1] != nullptr) {
if(buffer[i] == nullptr) break;
std::cout << "You filled the whole board and won! Score: " << get_score() << std::endl; std::cout << "You filled the whole board and won! Score: " << get_score() << std::endl;
running = false; running = false;
} }
@@ -32,14 +31,13 @@ int main () {
std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl; std::cout << "You collided with yourself and died! Score: " << get_score() << std::endl;
running = false; running = false;
} }
window.clear_display(); window.clear_display();
window.update_display(buffer); window.update_display(buffer);
window.draw_display(200); window.draw_display(200);
} }
input_thread.join(); input_thread.join();
for(int i = 0; i < MAX_LEN - 1; i++) { for(auto& p : buffer) delete p;
delete buffer[i];
}
return 0; return 0;
} }
+11 -7
View File
@@ -1,22 +1,26 @@
#include "window.hpp" #include "window.hpp"
#include <algorithm>
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
void Window::clear_display() { void Window::clear_display() {
for(int i = 0; i < ROW; i++) { for(auto &row : m_board) {
for(int j = 0; j < COL; j++) { for(auto &val : row) {
m_board[i][j] = nullptr; val.m_symbol = '.';
} }
} }
} }
void Window::draw_display(int milliseconds) { void Window::draw_display(int milliseconds) {
std::cout << "\033[H" << std::flush; // Clear screen std::cout << "\033[H" << std::flush; // Clear screen
// for(auto row : ROW) {
//
// }
for(int i = 0; i < ROW; i++) { for(int i = 0; i < ROW; i++) {
for(int j = 0; j < ROW; j++) { for(int j = 0; j < COL; j++) {
if(m_board[i][j] == nullptr) { std::cout << ". "; } if(m_board[i][j].m_symbol != '.') { std::cout << m_board[i][j].m_symbol << " "; }
if(m_board[i][j] != nullptr) { std::cout << m_board[i][j]->m_symbol << " "; } else { std::cout << ". "; }
} }
std::cout << "\r\n"; std::cout << "\r\n";
} }
@@ -26,6 +30,6 @@ void Window::draw_display(int milliseconds) {
void Window::update_display(Point** buffer) { void Window::update_display(Point** buffer) {
for(int i = 0; i < ROW * COL; i++) { for(int i = 0; i < ROW * COL; i++) {
if(buffer[i] == nullptr || i == 1 || buffer[i]->m_row > ROW - 1 || buffer[i]->m_col > COL - 1) continue; if(buffer[i] == nullptr || i == 1 || buffer[i]->m_row > ROW - 1 || buffer[i]->m_col > COL - 1) continue;
m_board[buffer[i]->m_row][buffer[i]->m_col] = buffer[i]; m_board[buffer[i]->m_row][buffer[i]->m_col].m_symbol = buffer[i]->m_symbol;
} }
} }