wip
This commit is contained in:
+3
-1
@@ -1,6 +1,7 @@
|
|||||||
#ifndef ENGINE_HPP
|
#ifndef ENGINE_HPP
|
||||||
#define ENGINE_HPP
|
#define ENGINE_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
constexpr int ROW = 11;
|
constexpr int ROW = 11;
|
||||||
@@ -31,7 +32,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
Window win;
|
Window win;
|
||||||
std::vector<vec2> buf;
|
std::vector<vec2> tail;
|
||||||
|
std::string status;
|
||||||
vec2 head { COL / 2, ROW / 2 };
|
vec2 head { COL / 2, ROW / 2 };
|
||||||
vec2 fruit { rand() % COL, rand() % ROW };
|
vec2 fruit { rand() % COL, rand() % ROW };
|
||||||
vec2 prev_head { head.x, head.y };
|
vec2 prev_head { head.x, head.y };
|
||||||
|
|||||||
+16
-18
@@ -2,9 +2,9 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <format>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <iostream>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
@@ -38,9 +38,7 @@ Engine::Engine() {
|
|||||||
nodelay(stdscr, true);
|
nodelay(stdscr, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() {
|
Engine::~Engine() { endwin(); }
|
||||||
endwin();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::run() {
|
void Engine::run() {
|
||||||
while (exit_summary() && process_input()) {
|
while (exit_summary() && process_input()) {
|
||||||
@@ -48,10 +46,14 @@ void Engine::run() {
|
|||||||
determine_index_zero_pos();
|
determine_index_zero_pos();
|
||||||
move_tail_indicies();
|
move_tail_indicies();
|
||||||
|
|
||||||
win.update_display(head, fruit, buf);
|
win.update_display(head, fruit, tail);
|
||||||
win.draw_display();
|
win.draw_display();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200)));
|
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200)));
|
||||||
}
|
}
|
||||||
|
nodelay(stdscr, false);
|
||||||
|
mvprintw(12, 0, status.c_str());
|
||||||
|
refresh();
|
||||||
|
getch();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::process_input() {
|
bool Engine::process_input() {
|
||||||
@@ -67,15 +69,11 @@ bool Engine::process_input() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::exit_summary() {
|
bool Engine::exit_summary() {
|
||||||
if(buf.size() >= ROW * COL) {
|
if(tail.size() >= ROW * COL) {
|
||||||
std::cout << "You filled the whole board and won! Score: " << m_score << "\n";
|
return status = std::format("You filled the whole board and won! Score: {}\n", m_score), false;
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if(std::any_of(buf.begin(), buf.end(), [&](const vec2& segment) { return segment == head; })) {
|
if(std::any_of(tail.begin(), tail.end(), [&](const vec2& segment) { return segment == head; })) {
|
||||||
std::cout << "You collided with yourself and died! Score: " << m_score << "\n";
|
return status = std::format("You collided with yourself and died! Score: {}\n", m_score), false;
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -90,17 +88,17 @@ void Engine::determine_index_zero_pos() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::move_tail_indicies() {
|
void Engine::move_tail_indicies() {
|
||||||
if(buf.empty()) return;
|
if(tail.empty()) return;
|
||||||
buf.pop_back();
|
tail.pop_back();
|
||||||
buf.insert(buf.begin(), prev_head);
|
tail.insert(tail.begin(), prev_head);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::add_tail_and_respawn_fruit() {
|
void Engine::add_tail_and_respawn_fruit() {
|
||||||
if(fruit != head) return;
|
if(fruit != head) return;
|
||||||
vec2 new_pos { 0, 0 };
|
vec2 new_pos { 0, 0 };
|
||||||
do { new_pos.set((rand() % ROW), (rand() % COL)); }
|
do { new_pos.set((rand() % ROW), (rand() % COL)); }
|
||||||
while (new_pos == head || std::any_of(buf.begin(), buf.end(), [&](const vec2& seg) { return seg == new_pos; }));
|
while (new_pos == head || std::any_of(tail.begin(), tail.end(), [&](const vec2& seg) { return seg == new_pos; }));
|
||||||
fruit = new_pos;
|
fruit = new_pos;
|
||||||
buf.insert(buf.begin(), vec2 { ROW, COL });
|
tail.insert(tail.begin(), vec2 { ROW, COL });
|
||||||
m_score++;
|
m_score++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user