feat: added input (player_one and player_two now can place on board)
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
#include <iostream>
|
||||
#include "elements.hpp"
|
||||
|
||||
Point::Point() {
|
||||
m_row = 0;
|
||||
m_col = 0;
|
||||
}
|
||||
|
||||
void Point::assign(int row, int col) {
|
||||
m_row = row;
|
||||
m_col = col;
|
||||
}
|
||||
|
||||
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 = ' ';
|
||||
// }
|
||||
|
||||
+26
-2
@@ -1,7 +1,17 @@
|
||||
#include "input.hpp"
|
||||
#include <ncurses.h>
|
||||
|
||||
void Input::get_input(bool &running) {
|
||||
void _switch_turn(int favor, Player &player_one, Player &player_two) {
|
||||
if(favor == 1) {
|
||||
player_one.m_turn = true;
|
||||
player_two.m_turn = false;
|
||||
} else if(favor == 2) {
|
||||
player_one.m_turn = false;
|
||||
player_two.m_turn = true;
|
||||
}
|
||||
}
|
||||
|
||||
void Input::get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two) {
|
||||
while(running) {
|
||||
initscr();
|
||||
cbreak();
|
||||
@@ -10,8 +20,22 @@ void Input::get_input(bool &running) {
|
||||
m_getch = getch();
|
||||
|
||||
endwin();
|
||||
if(m_getch == 'q') {
|
||||
if(m_getch == 113) {
|
||||
running = false;
|
||||
} if(player_one.m_turn) {
|
||||
if(!tile_list[m_getch - 48]._delete) {
|
||||
tile_list[m_getch - 48].m_symbol = player_one.m_symbol;
|
||||
m_getch = 48;
|
||||
_switch_turn(2, player_one, player_two);
|
||||
tile_list[m_getch - 48]._delete = true;
|
||||
}
|
||||
} else if(player_two.m_turn) {
|
||||
if(!tile_list[m_getch - 48]._delete) {
|
||||
tile_list[m_getch - 48].m_symbol = player_two.m_symbol;
|
||||
m_getch = 48;
|
||||
_switch_turn(1, player_one, player_two);
|
||||
tile_list[m_getch - 48]._delete = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+19
-2
@@ -2,15 +2,32 @@
|
||||
#include <thread>
|
||||
#include "window.hpp"
|
||||
#include "input.hpp"
|
||||
#include "elements.hpp"
|
||||
#include "globals.hpp"
|
||||
|
||||
int main() {
|
||||
Input input;
|
||||
Window window;
|
||||
Player player_one('x', true);
|
||||
Player player_two('o', false);
|
||||
bool running = true;
|
||||
std::thread input_thread(&Input::get_input, &input, std::ref(running));
|
||||
|
||||
Tile tile_list[9] = {
|
||||
Tile(0, 0), Tile(0, 2), Tile(0, 4),
|
||||
Tile(2, 0), Tile(2, 2), Tile(2, 4),
|
||||
Tile(4, 0), Tile(4, 2), Tile(4, 4)
|
||||
};
|
||||
Border border_list[16] = {
|
||||
Border(0, 1, '|'), Border(0, 3, '|'),
|
||||
Border(1, 0, '-'), Border(1, 1, '+'), Border(1, 2, '-'), Border(1, 3, '+'), Border(1, 4, '-'),
|
||||
Border(2, 1, '|'), Border(2, 3, '|'),
|
||||
Border(3, 0, '-'), Border(3, 1, '+'), Border(3, 2, '-'), Border(3, 3, '+'), Border(3, 4, '-'),
|
||||
Border(4, 1, '|'), Border(4, 3, '|'),
|
||||
};
|
||||
|
||||
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) {
|
||||
std::cout << "Hello World" << std::endl;
|
||||
window.update_display(tile_list, border_list);
|
||||
}
|
||||
|
||||
input_thread.join();
|
||||
|
||||
+7
-1
@@ -3,9 +3,15 @@
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
void Window::update_display() {
|
||||
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;
|
||||
}
|
||||
for(int i = 0; i < 16; i++) {
|
||||
m_board[border_list[i].m_pos.m_row][border_list[i].m_pos.m_col] = border_list[i].m_symbol;
|
||||
}
|
||||
|
||||
// Refresh and draw screen
|
||||
std::cout << "\033[H" << std::flush;
|
||||
|
||||
Reference in New Issue
Block a user