diff --git a/.cache/clangd/index/fruit.cpp.80CA8E09C6743CC5.idx b/.cache/clangd/index/fruit.cpp.80CA8E09C6743CC5.idx new file mode 100644 index 0000000..86dd2d7 Binary files /dev/null and b/.cache/clangd/index/fruit.cpp.80CA8E09C6743CC5.idx differ diff --git a/.cache/clangd/index/fruit.hpp.B7520D20FB102EAE.idx b/.cache/clangd/index/fruit.hpp.B7520D20FB102EAE.idx new file mode 100644 index 0000000..859faaa Binary files /dev/null and b/.cache/clangd/index/fruit.hpp.B7520D20FB102EAE.idx differ diff --git a/.cache/clangd/index/globals.hpp.3DF8F8245B52D37B.idx b/.cache/clangd/index/globals.hpp.3DF8F8245B52D37B.idx new file mode 100644 index 0000000..0281595 Binary files /dev/null and b/.cache/clangd/index/globals.hpp.3DF8F8245B52D37B.idx differ diff --git a/.cache/clangd/index/input.cpp.540A9D56E66246B1.idx b/.cache/clangd/index/input.cpp.540A9D56E66246B1.idx new file mode 100644 index 0000000..762d74c Binary files /dev/null and b/.cache/clangd/index/input.cpp.540A9D56E66246B1.idx differ diff --git a/.cache/clangd/index/input.hpp.82943EF4CB7EC1AF.idx b/.cache/clangd/index/input.hpp.82943EF4CB7EC1AF.idx new file mode 100644 index 0000000..6db53c5 Binary files /dev/null and b/.cache/clangd/index/input.hpp.82943EF4CB7EC1AF.idx differ diff --git a/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx b/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx new file mode 100644 index 0000000..051f830 Binary files /dev/null and b/.cache/clangd/index/main.cpp.C070F90AD8D66EE4.idx differ diff --git a/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx b/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx new file mode 100644 index 0000000..6cb2fe4 Binary files /dev/null and b/.cache/clangd/index/point.cpp.5A6C80E33F52E8D8.idx differ diff --git a/.cache/clangd/index/point.hpp.1A777AC8437A8313.idx b/.cache/clangd/index/point.hpp.1A777AC8437A8313.idx new file mode 100644 index 0000000..c570bed Binary files /dev/null and b/.cache/clangd/index/point.hpp.1A777AC8437A8313.idx differ diff --git a/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx b/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx new file mode 100644 index 0000000..a6e9199 Binary files /dev/null and b/.cache/clangd/index/snake.cpp.60A7519252B9A7E3.idx differ diff --git a/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx b/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx new file mode 100644 index 0000000..3c41a92 Binary files /dev/null and b/.cache/clangd/index/snake.hpp.8322EDE86440CED8.idx differ diff --git a/.cache/clangd/index/window.cpp.CFB09F5DADCCC8C6.idx b/.cache/clangd/index/window.cpp.CFB09F5DADCCC8C6.idx new file mode 100644 index 0000000..e7d2b64 Binary files /dev/null and b/.cache/clangd/index/window.cpp.CFB09F5DADCCC8C6.idx differ diff --git a/.cache/clangd/index/window.hpp.2E6429FBACC19FAB.idx b/.cache/clangd/index/window.hpp.2E6429FBACC19FAB.idx new file mode 100644 index 0000000..da3ca44 Binary files /dev/null and b/.cache/clangd/index/window.hpp.2E6429FBACC19FAB.idx differ diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..91e78fc --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-Iinclude] diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a86442..2fdbb24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16) project(snake) set(CMAKE_C_STANDARD 23) set(CMAKE_C_STANDARD_REQUIRED ON) -# Enable verbose makefile for better output + set(CMAKE_VERBOSE_MAKEFILE ON) include_directories(include) diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..25eb4b2 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +build/compile_commands.json \ No newline at end of file diff --git a/include/fruit.hpp b/include/fruit.hpp index 86c4f5b..26ce878 100644 --- a/include/fruit.hpp +++ b/include/fruit.hpp @@ -10,5 +10,6 @@ struct Fruit { Point m_point; Fruit(); void create_point(); + void reset(std::array, ROW> &arr); }; #endif // FRUIT_HPP diff --git a/src/fruit.cpp b/src/fruit.cpp index 8a727f6..d4b4680 100644 --- a/src/fruit.cpp +++ b/src/fruit.cpp @@ -1,8 +1,7 @@ -#include #include #include -// #include "util.h" #include "fruit.hpp" +#include "globals.hpp" Fruit::Fruit() { std::srand(std::time(0)); @@ -10,4 +9,11 @@ Fruit::Fruit() { void Fruit::create_point() { m_point = Point {rand() % 9, rand() % 9}; + m_row = m_point.m_row; + m_col = m_point.m_col; +} + +void Fruit::reset(std::array, ROW> &arr) { + m_point.clear(arr); + create_point(); } diff --git a/src/main.cpp b/src/main.cpp index de996df..8ba43ca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -16,16 +15,13 @@ Input input; Fruit fruit; int main() { - // int randomNum = random() % 101; - // - // std::cout << randomNum; - // fruit.create_point(); - // std::cout << fruit.m_point.get_point() << std::endl; + int score = 0; std::array, ROW> &board = window.m_board; bool update = true; int row = 0, col = 0; Point previous {row,col}; + fruit.create_point(); std::thread input_thread(&Input::get_input, &input); while(update) { if (input.m_current_key == 's') snake.set_direction(1); @@ -34,9 +30,15 @@ int main() { if (input.m_current_key == 'd') snake.set_direction(4); if (input.m_current_key == 'q') break; + if(fruit.m_row == row && fruit.m_col == col) { + fruit.reset(board); + ++score; + } std::tie(row, col) = previous.get_point(snake.get_direction(), row, col); board[row][col] = '*'; + board[fruit.m_row][fruit.m_col] = '*'; + std::cout << "Score: " << score << std::endl; previous.clear(board); // Clear old point previous = Point {row, col}; // Set new previous window.update_loop(5); // Update loop to clear board, tick and redisplay board diff --git a/src/point.cpp b/src/point.cpp index 16e5714..b8e9a0f 100644 --- a/src/point.cpp +++ b/src/point.cpp @@ -1,4 +1,4 @@ -#include +#include #include "point.hpp" std::string Point::get_point() const { @@ -25,3 +25,8 @@ std::pair Point::get_point(int direction, int row, int col) { } return {row, col}; } + +/* Create save_point to take old_direction, new_direction, row and col + * Once a tail hits the { row, col }, change old_direction to new_direction + * Tail has own point to manage, game has array of points to manage tail and mantain coords with direction + * */ diff --git a/walkup.sh b/walkup.sh deleted file mode 100755 index 3b8d1a4..0000000 --- a/walkup.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash - -# Start from the current directory -DIR="$(pwd)" - -# Walk up until we find a marker or reach root -while [[ ! -f "$DIR/CMakeLists.txt" && \ - ! -f "$DIR/package.json" && \ - ! -d "$DIR/.git" && \ - "$DIR" != "/" ]]; do - DIR="$(dirname "$DIR")" -done - -# Check if root was reached without finding anything -if [[ "$DIR" == "/" ]]; then - echo "❌ Project root not found." - exit 1 -fi - -echo "✅ Project root found at: $DIR" -