refactor: renamed snake to element and moved fruit to element. Removed a lot of unused functions and variables

This commit is contained in:
DarthMooMancer
2025-08-30 01:09:50 -04:00
parent d488f6464a
commit 96e5eb9930
15 changed files with 159 additions and 246 deletions
+78
View File
@@ -0,0 +1,78 @@
#include "element.hpp"
#include <cstdlib>
#include <ctime>
Fruit::Fruit() {
std::srand(std::time(0));
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
}
void Fruit::get_new_pos(std::array<std::array<char, COL>, ROW> &board, Head &head, int &score) {
if(m_point.m_row == head.m_head.m_row && m_point.m_col == head.m_head.m_col) {
m_point.set_new_point((rand() % 9), (rand() % 9), 0);
score++;
}
}
Head::Head() {
m_head.set_new_point(5, 5, 2);
}
void Head::get_new_pos() {
if (m_head.m_direction == -1) {
if (m_head.m_col <= 0) m_head.m_col = COL - 1;
else m_head.m_col--;
} else if (m_head.m_direction == 1) {
if (m_head.m_col >= COL - 1) m_head.m_col = 0;
else m_head.m_col++;
} else if (m_head.m_direction == -2) {
if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
else m_head.m_row--;
} else if (m_head.m_direction == 2) {
if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
else m_head.m_row++;
}
}
Body::Body() {
m_score = 0;
m_collision = false;
}
void Body::collision(Head &head) {
for(int i = 0; i < m_body.size(); i++) {
if(m_body.at(i).m_tail_pos.m_row == head.m_head.m_row) {
if(m_body.at(i).m_tail_pos.m_col == head.m_head.m_col) {
m_collision = true;
}
}
}
}
void Body::add_tail() {
if(m_score >= 1) {
m_score = 0;
m_body.push_back(Tail{});
}
}
void Body::get_new_points(Head &head) {
for (int i = 0; i < m_body.size(); i++) {
m_body.front().m_previous_tail_pos.set_new_point(
head.m_previous_head.m_row,
head.m_previous_head.m_col,
head.m_previous_head.m_direction);
if(i > 0) {
m_body.at(i).m_previous_tail_pos.set_new_point(
m_body.at(i - 1).m_tail_pos.m_row,
m_body.at(i - 1).m_tail_pos.m_col,
m_body.at(i - 1).m_tail_pos.m_direction);
}
}
for (int i = 0; i < m_body.size(); i++) {
m_body.at(i).m_tail_pos.set_new_point(
m_body.at(i).m_previous_tail_pos.m_row,
m_body.at(i).m_previous_tail_pos.m_col,
m_body.at(i).m_previous_tail_pos.m_direction);
}
}
-19
View File
@@ -1,19 +0,0 @@
#include <cstdlib>
#include <ctime>
#include "fruit.hpp"
#include "globals.hpp"
Fruit::Fruit() {
std::srand(std::time(0));
m_point.m_row = (rand() % 9);
m_point.m_col = (rand() % 9);
}
void Fruit::reset(std::array<std::array<char, COL>, ROW> &board, int row, int col, int &score) {
if(m_point.m_row == row && m_point.m_col == col) {
m_point.clear(board);
m_point.m_row = (rand() % 9);
m_point.m_col = (rand() % 9);
score++;
}
}
+7 -9
View File
@@ -1,9 +1,8 @@
#include "input.hpp"
#include <ncurses.h>
void Input::get_input(int &d, bool &c, bool &r) {
bool running = true;
while(running && !(c)) {
void Input::get_input(int &direction, bool &running) {
while(running) {
initscr();
cbreak();
noecho();
@@ -11,16 +10,15 @@ void Input::get_input(int &d, bool &c, bool &r) {
m_getch = getch();
endwin();
if(m_current_key != 1 && (m_getch == 'a' || m_getch == 'D')) {
d = m_current_key = -1;
direction = m_current_key = -1;
} else if(m_current_key != - 1 && (m_getch == 'd' || m_getch == 'C')) {
d = m_current_key = 1;
m_current_key = 1;
direction = m_current_key = 1;
} else if(m_current_key != -2 && (m_getch == 's' || m_getch == 'B')) {
d = m_current_key = 2;
direction = m_current_key = 2;
} else if(m_current_key != 2 && (m_getch == 'w' || m_getch == 'A')) {
d = m_current_key = -2;
direction = m_current_key = -2;
} else if(m_getch == 'q') {
r = running = false;
running = false;
}
}
+15 -17
View File
@@ -1,7 +1,6 @@
#include <thread>
#include "point.hpp"
#include "fruit.hpp"
#include "snake.hpp"
#include <iostream>
#include "element.hpp"
#include "window.hpp"
#include "globals.hpp"
#include "input.hpp"
@@ -13,28 +12,27 @@ int main () {
Body body;
Head head;
bool running = true;
bool collision = false;
std::thread input_thread(
&Input::get_input, &input,
std::ref(head.m_head.m_direction),
std::ref(collision),
std::ref(running));
while (running) {
body.collision(head, collision);
window.update_score(fruit, body);
window.check_exit(running, body, collision);
head.get_new_point(head.m_head.m_direction);
body.collision(head);
body.add_tail();
if(body.m_body.size() >= (ROW * COL)) {
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
running = false;
} else if(body.m_collision) {
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
running = false;
}
head.get_new_pos();
body.get_new_points(head);
head.get_previous_point();
head.m_previous_head.set_new_point(head.m_head.m_row, head.m_head.m_col, head.m_head.m_direction);
fruit.reset(window.m_board, head.m_head.m_row, head.m_head.m_col, window.m_score);
window.m_board[head.m_head.m_row][head.m_head.m_col] = '*';
window.m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
body.draw(window.m_board);
window.update_display(FPS);
body.clear(window.m_board);
head.m_head.clear(window.m_board);
fruit.get_new_pos(window.m_board, head, body.m_score);
window.update_display(fruit, head, body);
}
input_thread.join();
+2 -7
View File
@@ -1,12 +1,7 @@
#include <string>
#include "point.hpp"
std::string Point::to_string() const {
return "(" + std::to_string(m_row) + ", " + std::to_string(m_col) + "), " + std::to_string(m_direction);
}
void Point::clear(std::array<std::array<char, COL>, ROW> &arr) const {
arr[m_row][m_col] = '.';
void Point::clear(std::array<std::array<char, COL>, ROW> &board) const {
board[m_row][m_col] = '.';
}
void Point::set_new_point(int row, int col, int dir) {
-96
View File
@@ -1,96 +0,0 @@
#include "snake.hpp"
Head::Head() {
m_head = Point { 5, 5, 2 };
m_previous_head.set_new_point(m_previous_head.m_row = 0, m_previous_head.m_col = 0, m_previous_head.m_direction = 2);
}
Head::Head(int default_direction, int row, int col) {
m_head = Point { row, col, default_direction };
m_previous_head.set_new_point(m_previous_head.m_row = 0, m_previous_head.m_col = 0, m_previous_head.m_direction = 2);
}
void Head::get_new_point(int direction) {
if (direction == -1) {
if (m_head.m_col <= 0) m_head.m_col = COL - 1;
else m_head.m_col--;
} else if (direction == 1) {
if (m_head.m_col >= COL - 1) m_head.m_col = 0;
else m_head.m_col++;
} else if (direction == -2) {
if (m_head.m_row <= 0) m_head.m_row = ROW - 1;
else m_head.m_row--;
} else if (direction == 2) {
if (m_head.m_row >= ROW - 1) m_head.m_row = 0;
else m_head.m_row++;
} else {
return;
}
}
void Head::get_previous_point() {
m_previous_head.m_row = m_head.m_row;
m_previous_head.m_col = m_head.m_col;
m_previous_head.m_direction = m_head.m_direction;
}
Tail::Tail(int follow_direction, int row, int col) {
m_tail_pos = Point { row, col, follow_direction };
}
void Tail::get_previous_point() {
m_tail_pos.m_row = m_previous_tail_pos.m_row;
m_tail_pos.m_col = m_previous_tail_pos.m_col;
m_tail_pos.m_direction = m_previous_tail_pos.m_direction;
}
void Body::collision(Head &head, bool &collision) {
for(int i = 0; i < m_body.size(); i++) {
if(m_body.at(i).m_tail_pos.m_row == head.m_head.m_row) {
if(m_body.at(i).m_tail_pos.m_col == head.m_head.m_col) {
collision = true;
}
}
}
}
void Body::clear(std::array<std::array<char, COL>, ROW> &arr) {
for(int i = 0; i < m_body.size(); i++) {
m_body.at(i).m_tail_pos.clear(arr);
}
}
void Body::draw(std::array<std::array<char, COL>, ROW> &arr) {
for(int i = 0; i < m_body.size(); i++) {
if(i < m_body.size()) {
arr[m_body.at(i).m_tail_pos.m_row][m_body.at(i).m_tail_pos.m_col] = '0';
}
}
}
void Body::move(Head &head) {
for (int i = 0; i < m_body.size(); i++) {
if(i == 0) {
}
}
}
void Body::get_new_points(Head &head) {
for (int i = 0; i < m_body.size(); i++) {
if(i == 0) {
m_body.at(i).m_previous_tail_pos.set_new_point(
head.m_previous_head.m_row,
head.m_previous_head.m_col,
head.m_previous_head.m_direction);
} else {
m_body.at(i).m_previous_tail_pos.set_new_point(
m_body.at(i - 1).m_tail_pos.m_row,
m_body.at(i - 1).m_tail_pos.m_col,
m_body.at(i - 1).m_tail_pos.m_direction);
}
}
for (int i = 0; i < m_body.size(); i++) {
m_body.at(i).get_previous_point();
}
}
+15 -33
View File
@@ -1,51 +1,33 @@
#include "window.hpp"
#include <iostream>
#include <chrono>
#include <string>
#include <thread>
Window::Window() {
for(std::array<char, COL> &row : m_board) {
row.fill('.');
}
m_score = 0;
m_reset = 0;
m_previous_reset = 0;
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
}
void Window::check_exit(bool &running, Body &body, bool &collision) {
if(body.m_body.size() >= (ROW * COL)) {
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
kill(running);
} else if(collision) {
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
kill(running);
void Window::update_display(Fruit &fruit, Head &head, Body &body) {
// Update symbols of positions
m_board[head.m_head.m_row][head.m_head.m_col] = '*';
m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
for(int i = 0; i < body.m_body.size(); i++) {
m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '0';
}
}
void Window::draw_window() {
std::cout << "\033[H" << std::flush; // Clear screen
// Draw Screen
for(std::array<char, COL> &row : m_board) {
for(char &col : row) {
std::cout << col << " ";
}
std::cout << "\r\n";
}
std::cout << std::flush;
}
void Window::update_score(Fruit &fruit, Body &body) {
if(m_score >= 1) {
m_score = 0;
body.m_body.push_back(Tail{2, 0, 0});
}
}
void Window::kill(bool &running) {
running = false;
}
void Window::update_display(int fps) {
std::cout<<"\033[H" << std::flush;
draw_window();
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
// Clear old spaces
for(int i = 0; i < body.m_body.size(); i++) {
body.m_body.at(i).m_tail_pos.clear(m_board);
}
head.m_head.clear(m_board);
fruit.m_point.clear(m_board);
}