feat: check_chain checks for horizontal, vertical and diagonal with bug on diagonal if block hasnt gone all the way down yet

This commit is contained in:
DarthMooMancer
2025-09-08 09:09:39 -04:00
parent 51ba052d9a
commit 0f7984e1d0
3 changed files with 43 additions and 24 deletions
+6 -2
View File
@@ -1,15 +1,17 @@
#ifndef ELEMENTS_HPP #ifndef ELEMENTS_HPP
#define ELEMENTS_HPP #define ELEMENTS_HPP
#include "globals.hpp"
#include <vector> #include <vector>
struct Point { struct Point {
Point() : m_row(0), m_col(0), m_id(0) {} Point() : m_row(0), m_col(0), m_id(0), m_collided(false) {}
Point(int row, int col, int id) : m_row(row), m_col(col), m_id(id) {} Point(int row, int col, int id) : m_row(row), m_col(col), m_id(id), m_collided(false) {}
int m_row; int m_row;
int m_col; int m_col;
int m_id; int m_id;
char m_symbol; char m_symbol;
bool m_collided;
void to_string(); void to_string();
void assign(int row, int col); void assign(int row, int col);
void move(); void move();
@@ -21,6 +23,8 @@ struct Player {
char m_symbol; char m_symbol;
bool m_turn; bool m_turn;
void input_tile(int col, std::vector<Point> &points); void input_tile(int col, std::vector<Point> &points);
// bool check_chain(std::array<std::array<char, COL>, ROW> &m_board);
bool check_chain(std::array<std::array<char, COL>, ROW> &m_board, std::vector<Point> &points_list);
}; };
#endif #endif
+33
View File
@@ -1,5 +1,6 @@
#include "elements.hpp" #include "elements.hpp"
#include "globals.hpp" #include "globals.hpp"
#include <array>
#include <vector> #include <vector>
#include <iostream> #include <iostream>
@@ -24,6 +25,7 @@ void Point::check_collision(std::vector<Point> &points_list) {;
if(points_list[i].m_row != m_row) continue; if(points_list[i].m_row != m_row) continue;
if(points_list[i].m_col != m_col) continue; if(points_list[i].m_col != m_col) continue;
m_row = points_list[i].m_row - 1; m_row = points_list[i].m_row - 1;
m_collided = true;
} }
} }
@@ -32,3 +34,34 @@ void Player::input_tile(int col, std::vector<Point> &points) {
else points.push_back(Point({ 0, col, points.back().m_id + 1 })); else points.push_back(Point({ 0, col, points.back().m_id + 1 }));
points.back().m_symbol = m_symbol; points.back().m_symbol = m_symbol;
} }
bool Player::check_chain(std::array<std::array<char, COL>, ROW> &m_board, std::vector<Point> &points_list) {
for(int i = ROW - 1; i >= 0; i--) {
for(int j = COL - 1; j >= 0; j--) {
if(m_board[i][j] != m_symbol) continue;
if(m_board[i - 1][j] != m_symbol) continue;
if(m_board[i - 2][j] != m_symbol) continue;
if(m_board[i - 3][j] != m_symbol) continue;
return true;
}
}
for(int i = ROW - 1; i >= 0; i--) {
for(int j = COL - 1; j >= 0; j--) {
if(m_board[i][j] != m_symbol) continue;
if(m_board[i][j - 1] != m_symbol) continue;
if(m_board[i][j - 2] != m_symbol) continue;
if(m_board[i][j - 3] != m_symbol) continue;
return true;
}
}
for(int i = ROW - 1; i >= 0; i--) {
for(int j = COL - 1; j >= 0; j--) {
if(m_board[i][j] != m_symbol) continue;
if(m_board[i - 1][j + 1] != m_symbol) continue;
if(m_board[i - 2][j + 2] != m_symbol) continue;
if(m_board[i - 3][j + 3] != m_symbol) continue;
return true;
}
}
return false;
}
+4 -22
View File
@@ -6,42 +6,24 @@
#include "elements.hpp" #include "elements.hpp"
int main() { int main() {
std::vector<Point> points_list = {};
Input input; Input input;
Window window; Window window;
Player one('o', true); Player one('o', true);
Player two('x', false); Player two('x', false);
bool running = true; bool running = true;
std::vector<Point> points_list = {};
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(one), std::ref(two), std::ref(points_list)); std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(one), std::ref(two), std::ref(points_list));
char symbol = ' ';
bool d = true;
bool result = false;
while(running) { while(running) {
window.update_display(points_list); window.update_display(points_list);
for(int i = 0; i < points_list.size(); i++) { for(int i = 0; i < points_list.size(); i++) {
points_list[i].move(); points_list[i].move();
points_list[i].check_collision(points_list); points_list[i].check_collision(points_list);
} }
for(int i = 0; i < ROW - 1; i++) { if(one.check_chain(window.m_board)) {
for(int j = 0; j < COL - 1; j++) { std::cout << "Hello" << std::endl;
if(window.m_board[i][j] == ' ') continue; running = false;
symbol = window.m_board[i][j];
d = true;
if(d) {
for(int k = 1; k <= 3; k++) {
if(window.m_board[i][j + k] != symbol) d = false;
}
if(d) {
result = true;
}
}
}
} }
// if(window.check_chain()) {
// std::cout << "Chain is active" << std::endl;
// }
std::cout << result << std::endl;
} }
input_thread.join(); input_thread.join();