wip
This commit is contained in:
+20
-28
@@ -1,8 +1,6 @@
|
||||
#include "element.hpp"
|
||||
#include "globals.hpp"
|
||||
#include <element.hpp>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
#include <ncurses.h>
|
||||
#include <ranges>
|
||||
#include <iostream>
|
||||
@@ -25,9 +23,8 @@ SnakeGame::~SnakeGame() {
|
||||
}
|
||||
|
||||
void SnakeGame::run() {
|
||||
while (exit_summary()) {
|
||||
process_input();
|
||||
add_tail_and_respawn_fruit(win->board);
|
||||
while (exit_summary() && process_input()) {
|
||||
add_tail_and_respawn_fruit(win->view);
|
||||
determine_index_zero_pos();
|
||||
move_tail_indicies();
|
||||
|
||||
@@ -37,27 +34,30 @@ void SnakeGame::run() {
|
||||
}
|
||||
}
|
||||
|
||||
void SnakeGame::process_input() {
|
||||
bool SnakeGame::process_input() {
|
||||
int ch;
|
||||
while((ch = getch()) != ERR) {
|
||||
switch(ch) {
|
||||
case 'w': if(state.m_dir != Direction::DOWN) state.m_dir = Direction::UP; break;
|
||||
case 's': if(state.m_dir != Direction::UP) state.m_dir = Direction::DOWN; break;
|
||||
case 'd': if(state.m_dir != Direction::LEFT) state.m_dir = Direction::RIGHT; break;
|
||||
case 'a': if(state.m_dir != Direction::RIGHT) state.m_dir = Direction::LEFT; break;
|
||||
case 'q': state.running = false; break;
|
||||
case 'w': if(state.dir != Direction::DOWN) state.dir = Direction::UP; break;
|
||||
case 's': if(state.dir != Direction::UP) state.dir = Direction::DOWN; break;
|
||||
case 'd': if(state.dir != Direction::LEFT) state.dir = Direction::RIGHT; break;
|
||||
case 'a': if(state.dir != Direction::RIGHT) state.dir = Direction::LEFT; break;
|
||||
case 'q': return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SnakeGame::exit_summary() {
|
||||
if(!state.running) return false;
|
||||
if(buf.size() >= MAX_LEN - 1) {
|
||||
std::cout << "You filled the whole board and won! Score: " << state.m_score << "\n";
|
||||
return false;
|
||||
} if(segment_overlap()) {
|
||||
std::cout << "You collided with yourself and died! Score: " << state.m_score << "\n";
|
||||
return false;
|
||||
}
|
||||
for(int i : std::ranges::views::iota(0, (int)buf.size())) {
|
||||
if(buf[i] == head) {
|
||||
std::cout << "You collided with yourself and died! Score: " << state.m_score << "\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -65,7 +65,7 @@ bool SnakeGame::exit_summary() {
|
||||
void SnakeGame::determine_index_zero_pos() {
|
||||
prev_head.set(head.x, head.y);
|
||||
|
||||
switch(state.m_dir) {
|
||||
switch(state.dir) {
|
||||
case Direction::LEFT: head.x = (head.x + COL - 1) % COL; break;
|
||||
case Direction::RIGHT: head.x = (head.x + 1) % COL; break;
|
||||
case Direction::UP: head.y = (head.y + ROW - 1) % ROW; break;
|
||||
@@ -79,22 +79,14 @@ void SnakeGame::move_tail_indicies() {
|
||||
buf.insert(buf.begin(), prev_head);
|
||||
}
|
||||
|
||||
void SnakeGame::add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> board) {
|
||||
void SnakeGame::add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> view) {
|
||||
if(fruit != head) return;
|
||||
int row, col;
|
||||
do {
|
||||
row = (rand() % ROW);
|
||||
col = (rand() % COL);
|
||||
} while (board[row][col] != '.');
|
||||
} while (view[row][col] != '.');
|
||||
fruit.set(row, col);
|
||||
buf.insert(buf.begin(), vec { ROW, COL });
|
||||
buf.insert(buf.begin(), vec2 { ROW, COL });
|
||||
state.m_score++;
|
||||
}
|
||||
|
||||
bool SnakeGame::segment_overlap() {
|
||||
if(buf.size() < 1) return false;
|
||||
for(int i : std::ranges::views::iota(0, (int)buf.size())) {
|
||||
if(buf[i] == head) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#include "element.hpp"
|
||||
#include <element.hpp>
|
||||
|
||||
int main () {
|
||||
SnakeGame engine;
|
||||
|
||||
+8
-8
@@ -1,5 +1,5 @@
|
||||
#include "window.hpp"
|
||||
#include "element.hpp"
|
||||
#include <window.hpp>
|
||||
#include <element.hpp>
|
||||
#include <ncurses.h>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
@@ -7,21 +7,21 @@
|
||||
void Window::draw_display() {
|
||||
for(int i : std::ranges::views::iota(0, ROW)) {
|
||||
for(int j : std::ranges::views::iota(0, COL)) {
|
||||
mvaddch(i, j * 2, board[i][j]);
|
||||
mvaddch(i, j * 2, view[i][j]);
|
||||
}
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Window::update_display(vec &head, vec &fruit, std::vector<vec> &buf) {
|
||||
for(auto& row : board) row.fill('.');
|
||||
board[head.y][head.x] = '*';
|
||||
board[fruit.y][fruit.x] = 'o';
|
||||
void Window::update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf) {
|
||||
for(auto& row : view) row.fill('.');
|
||||
view[head.y][head.x] = '*';
|
||||
view[fruit.y][fruit.x] = 'o';
|
||||
|
||||
for(int i : std::ranges::views::iota(0, (int)buf.size())) {
|
||||
const auto &segments = buf.at(i);
|
||||
if(segments.x < COL && segments.y < ROW) {
|
||||
board[segments.y][segments.x] = '~';
|
||||
view[segments.y][segments.x] = '~';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user