feat: added check_line for player win detection
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
#ifndef ELEMENTS_HPP
|
#ifndef ELEMENTS_HPP
|
||||||
#define ELEMENTS_HPP
|
#define ELEMENTS_HPP
|
||||||
|
|
||||||
|
struct Player;
|
||||||
|
struct Point;
|
||||||
|
struct Tile;
|
||||||
|
struct Border;
|
||||||
|
|
||||||
struct Point {
|
struct Point {
|
||||||
Point();
|
Point();
|
||||||
Point(int row, int col) : m_row(row), m_col(col) {};
|
Point(int row, int col) : m_row(row), m_col(col) {};
|
||||||
@@ -11,9 +16,11 @@ struct Point {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Player {
|
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;
|
char m_symbol;
|
||||||
bool m_turn;
|
bool m_turn;
|
||||||
|
bool m_win;
|
||||||
|
void check_line(Tile (&tile_list)[9]);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Tile {
|
struct Tile {
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
#include "elements.hpp"
|
#include "elements.hpp"
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
int m_getch = 0;
|
int _getch = 0;
|
||||||
void get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two);
|
void get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+20
-4
@@ -15,7 +15,23 @@ void Point::to_string() {
|
|||||||
std::cout << "(" << m_row << ", " << m_col << ")" << std::endl;
|
std::cout << "(" << m_row << ", " << m_col << ")" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tile::Tile(int row, int col) {
|
|
||||||
// m_pos.assign(row, col);
|
void Player::check_line(Tile (&tile_list)[9]) {
|
||||||
// m_symbol = ' ';
|
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
@@ -17,22 +17,22 @@ void Input::get_input(bool &running, Tile (&tile_list)[9], Player &player_one, P
|
|||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
|
|
||||||
m_getch = getch();
|
_getch = getch();
|
||||||
|
|
||||||
endwin();
|
endwin();
|
||||||
if(m_getch == 113) {
|
if(_getch == 113) {
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
if(!tile_list[m_getch - 49]._delete) {
|
if(!tile_list[_getch - 49]._delete) {
|
||||||
if(player_one.m_turn) {
|
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);
|
_switch_turn(2, player_one, player_two);
|
||||||
} else if(player_two.m_turn) {
|
} 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);
|
_switch_turn(1, player_one, player_two);
|
||||||
}
|
}
|
||||||
tile_list[m_getch - 49]._delete = true;
|
tile_list[_getch - 49]._delete = true;
|
||||||
m_getch = 48;
|
_getch = 48;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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));
|
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) {
|
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);
|
window.update_display(tile_list, border_list);
|
||||||
|
player_one.check_line(tile_list);
|
||||||
|
player_two.check_line(tile_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
input_thread.join();
|
input_thread.join();
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
void Window::update_display(Tile (&tile_list)[9], Border (&border_list)[16]) {
|
void Window::update_display(Tile (&tile_list)[9], Border (&border_list)[16]) {
|
||||||
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
||||||
// Modify board
|
|
||||||
for(int i = 0; i < 9; i++) {
|
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;
|
m_board[tile_list[i].m_pos.m_row][tile_list[i].m_pos.m_col] = tile_list[i].m_symbol;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user