feat: input_tile and players
This commit is contained in:
@@ -7,6 +7,8 @@ include_directories(include)
|
||||
set(SOURCES
|
||||
src/main.cpp
|
||||
src/input.cpp
|
||||
src/window.cpp
|
||||
src/elements.cpp
|
||||
)
|
||||
|
||||
add_executable(connect_four ${SOURCES})
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef ELEMENTS_HPP
|
||||
#define ELEMENTS_HPP
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct Point {
|
||||
Point() : m_row(0), m_col(0), m_id(0), m_active(true) {}
|
||||
Point(int row, int col, int id) : m_row(row), m_col(col), m_id(id), m_active(true) {}
|
||||
int m_row;
|
||||
int m_col;
|
||||
int m_id;
|
||||
bool m_active;
|
||||
char m_symbol;
|
||||
void assign(int row, int col);
|
||||
void move();
|
||||
void check_collision(std::vector<Point> &points_list);
|
||||
};
|
||||
|
||||
struct Player {
|
||||
Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn) {}
|
||||
char m_symbol;
|
||||
bool m_turn;
|
||||
std::vector<Point> m_points;
|
||||
void input_tile(int col, std::vector<Point> &points);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef GLOBALS_HPP
|
||||
#define GLOBALS_HPP
|
||||
|
||||
constexpr int ROW = 6;
|
||||
constexpr int COL = 7;
|
||||
|
||||
#endif
|
||||
+3
-1
@@ -1,9 +1,11 @@
|
||||
#ifndef INPUT_HPP
|
||||
#define INPUT_HPP
|
||||
|
||||
#include "elements.hpp"
|
||||
|
||||
struct Input {
|
||||
int _getch = 0;
|
||||
void get_input(bool &running);
|
||||
void get_input(bool &running, Player &one, Player &two, std::vector<Point> &points);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef WINDOW_HPP
|
||||
#define WINDOW_HPP
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include "globals.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
struct Window {
|
||||
std::array<std::array<char, COL>, ROW> m_board = {};
|
||||
void update_display(std::vector<Point> &points_list);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -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;
|
||||
}
|
||||
+29
-3
@@ -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) {
|
||||
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
@@ -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();
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user