feat: added check_line for player win detection

This commit is contained in:
DarthMooMancer
2025-09-02 13:38:40 -04:00
parent 18773da6c4
commit 9546b47d82
6 changed files with 47 additions and 14 deletions
+8 -1
View File
@@ -1,6 +1,11 @@
#ifndef ELEMENTS_HPP
#define ELEMENTS_HPP
struct Player;
struct Point;
struct Tile;
struct Border;
struct Point {
Point();
Point(int row, int col) : m_row(row), m_col(col) {};
@@ -11,9 +16,11 @@ struct Point {
};
struct Player {
Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn) {};
Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn), m_win(false) {};
char m_symbol;
bool m_turn;
bool m_win;
void check_line(Tile (&tile_list)[9]);
};
struct Tile {
+1 -1
View File
@@ -3,7 +3,7 @@
#include "elements.hpp"
struct Input {
int m_getch = 0;
int _getch = 0;
void get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two);
};
+20 -4
View File
@@ -15,7 +15,23 @@ void Point::to_string() {
std::cout << "(" << m_row << ", " << m_col << ")" << std::endl;
}
// Tile::Tile(int row, int col) {
// m_pos.assign(row, col);
// m_symbol = ' ';
// }
void Player::check_line(Tile (&tile_list)[9]) {
for(int i = 0; i <= 6; i += 3) {
if(tile_list[i].m_symbol == tile_list[i+1].m_symbol && tile_list[i+1].m_symbol == tile_list[i+2].m_symbol) {
if(tile_list[i].m_symbol == m_symbol) {
m_win = true;
}
}
}
if(tile_list[0].m_symbol == tile_list[4].m_symbol && tile_list[4].m_symbol == tile_list[8].m_symbol) {
if(tile_list[0].m_symbol == m_symbol) {
m_win = true;
}
}
if(tile_list[2].m_symbol == tile_list[4].m_symbol && tile_list[4].m_symbol == tile_list[6].m_symbol) {
if(tile_list[2].m_symbol == m_symbol) {
m_win = true;
}
}
}
+7 -7
View File
@@ -17,22 +17,22 @@ void Input::get_input(bool &running, Tile (&tile_list)[9], Player &player_one, P
cbreak();
noecho();
m_getch = getch();
_getch = getch();
endwin();
if(m_getch == 113) {
if(_getch == 113) {
running = false;
}
if(!tile_list[m_getch - 49]._delete) {
if(!tile_list[_getch - 49]._delete) {
if(player_one.m_turn) {
tile_list[m_getch - 49].m_symbol = player_one.m_symbol;
tile_list[_getch - 49].m_symbol = player_one.m_symbol;
_switch_turn(2, player_one, player_two);
} else if(player_two.m_turn) {
tile_list[m_getch - 49].m_symbol = player_two.m_symbol;
tile_list[_getch - 49].m_symbol = player_two.m_symbol;
_switch_turn(1, player_one, player_two);
}
tile_list[m_getch - 49]._delete = true;
m_getch = 48;
tile_list[_getch - 49]._delete = true;
_getch = 48;
}
}
}
+10
View File
@@ -26,8 +26,18 @@ int main() {
};
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(tile_list), std::ref(player_one), std::ref(player_two));
while (running) {
if(player_one.m_win) {
running = false;
std::cout << "Player 1 wins!" << std::endl;
} else if(player_two.m_win) {
running = false;
std::cout << "Player 2 wins!" << std::endl;
}
window.update_display(tile_list, border_list);
player_one.check_line(tile_list);
player_two.check_line(tile_list);
}
input_thread.join();
+1 -1
View File
@@ -5,7 +5,7 @@
void Window::update_display(Tile (&tile_list)[9], Border (&border_list)[16]) {
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
// Modify board
for(int i = 0; i < 9; i++) {
m_board[tile_list[i].m_pos.m_row][tile_list[i].m_pos.m_col] = tile_list[i].m_symbol;
}