feat: added input (player_one and player_two now can place on board)
This commit is contained in:
@@ -8,6 +8,7 @@ set(SOURCES
|
||||
src/main.cpp
|
||||
src/input.cpp
|
||||
src/window.cpp
|
||||
src/elements.cpp
|
||||
)
|
||||
|
||||
add_executable(tic_tac_toe ${SOURCES})
|
||||
|
||||
+27
-1
@@ -1,6 +1,32 @@
|
||||
#ifndef ELEMENTS_HPP
|
||||
#define ELEMENTS_HPP
|
||||
|
||||
// Add code here
|
||||
struct Point {
|
||||
Point();
|
||||
Point(int row, int col) : m_row(row), m_col(col) {};
|
||||
int m_row;
|
||||
int m_col;
|
||||
void assign(int row, int col);
|
||||
void to_string();
|
||||
};
|
||||
|
||||
struct Player {
|
||||
Player(char symbol, bool turn) : m_symbol(symbol), m_turn(turn) {};
|
||||
char m_symbol;
|
||||
bool m_turn;
|
||||
};
|
||||
|
||||
struct Tile {
|
||||
Tile(int row, int col) : m_pos(row, col), m_symbol(' '), _delete(false) {};
|
||||
char m_symbol;
|
||||
Point m_pos;
|
||||
bool _delete;
|
||||
};
|
||||
|
||||
struct Border {
|
||||
Border(int row, int col, char symbol) : m_pos(row, col), m_symbol(symbol) {};
|
||||
char m_symbol;
|
||||
Point m_pos;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
#ifndef GLOBALS_HPP
|
||||
#define GLOBALS_HPP
|
||||
|
||||
constexpr int ROW = 10;
|
||||
constexpr int COL = 10;
|
||||
constexpr int ROW = 5;
|
||||
constexpr int COL = 5;
|
||||
|
||||
#endif
|
||||
|
||||
+3
-3
@@ -1,10 +1,10 @@
|
||||
#ifndef INPUT_HPP
|
||||
#define INPUT_HPP
|
||||
// #include "elements.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
struct Input {
|
||||
char m_getch = ' ';
|
||||
void get_input(bool &running);
|
||||
int m_getch = 0;
|
||||
void get_input(bool &running, Tile (&tile_list)[9], Player &player_one, Player &player_two);
|
||||
};
|
||||
|
||||
#endif // INPUT_HPP
|
||||
|
||||
+2
-2
@@ -3,10 +3,10 @@
|
||||
|
||||
#include <array>
|
||||
#include "globals.hpp"
|
||||
// #include "elements.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
struct Window {
|
||||
std::array<std::array<char, COL>, ROW> m_board = {};
|
||||
void update_display();
|
||||
void update_display(Tile (&tile_list)[9], Border (&border_list)[16]);
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -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