feat: input_tile and players

This commit is contained in:
DarthMooMancer
2025-09-05 21:50:49 -04:00
parent 4ce29c2b14
commit 54d78ef6c0
9 changed files with 148 additions and 7 deletions
+30
View File
@@ -0,0 +1,30 @@
#include "elements.hpp"
#include "globals.hpp"
#include <vector>
void Point::assign(int row, int col) {
m_row = row;
m_col = col;
}
void Point::move() {
if(m_row < ROW - 1) {
m_row++;
}
}
void Point::check_collision(std::vector<Point> &points_list) {;
for(int i = 0; i < points_list.size(); i++) {
if(points_list[i].m_id == m_id) continue;
if(points_list[i].m_row != m_row) continue;
if(points_list[i].m_col != m_col) continue;
m_row = points_list[i].m_row - 1;
m_active = false;
}
}
void Player::input_tile(int col, std::vector<Point> &points) {
if(points.size() == 0) points.push_back(Point({ 0, col, 0 }));
else points.push_back(Point({ 0, col, points.back().m_id + 1 }));
points.back().m_symbol = m_symbol;
}
+30 -4
View File
@@ -1,7 +1,8 @@
#include "input.hpp"
#include <iostream>
#include <ncurses.h>
#include "input.hpp"
void Input::get_input(bool &running) {
void Input::get_input(bool &running, Player &one, Player &two, std::vector<Point> &points) {
while(running) {
initscr();
cbreak();
@@ -10,8 +11,33 @@ void Input::get_input(bool &running) {
_getch = getch();
endwin();
if(_getch == 113) {
running = false;
switch (_getch) {
case 49:
one.input_tile(0, points);
break;
case 50:
one.input_tile(1, points);
break;
case 51:
one.input_tile(2, points);
break;
case 52:
one.input_tile(3, points);
break;
case 53:
one.input_tile(4, points);
break;
case 54:
one.input_tile(5, points);
break;
case 55:
one.input_tile(6, points);
break;
case 113:
running = false;
break;
default:
_getch = 0;
}
}
}
+15 -2
View File
@@ -1,14 +1,27 @@
#include <iostream>
#include <thread>
#include <vector>
#include "input.hpp"
#include "window.hpp"
#include "elements.hpp"
int main() {
std::vector<Point> points_list = {};
Input input;
Window window;
Player one('o', true);
Player two('x', false);
bool running = true;
std::thread input_thread(&Input::get_input, &input, std::ref(running));
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(one), std::ref(two), std::ref(points_list));
while(running) {
std::cout << "Hello World" << std::endl;
window.update_display(points_list);
for(int i = 0; i < points_list.size(); i++) {
if(!points_list[i].m_active) continue;
points_list[i].move();
points_list[i].check_collision(points_list);
}
std::cout << points_list.size() << std::endl;
}
input_thread.join();
+20
View File
@@ -0,0 +1,20 @@
#include "window.hpp"
#include <iostream>
#include <chrono>
#include <thread>
void Window::update_display(std::vector<Point> &points_list) {
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
for(int i = 0; i < points_list.size(); i++) {
m_board[points_list[i].m_row][points_list[i].m_col] = '*';
}
std::cout << "\033[H" << std::flush;
for(std::array<char, COL> &row : m_board) {
for(char &col : row) {
std::cout << col << " ";
}
std::cout << "\r\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(200)); // 1000 / fps; 200ms = 5fps
}