removing window struct
This commit is contained in:
+2
-7
@@ -3,12 +3,6 @@
|
|||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct Window {
|
|
||||||
Window();
|
|
||||||
std::array<char, 9> view;
|
|
||||||
void draw_display() const;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
public:
|
public:
|
||||||
Engine();
|
Engine();
|
||||||
@@ -17,7 +11,8 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::string status;
|
std::string status;
|
||||||
Window win;
|
std::array<char, 9> view;
|
||||||
|
void draw_display() const;
|
||||||
char p1 { 'x' };
|
char p1 { 'x' };
|
||||||
char p2 { 'o' };
|
char p2 { 'o' };
|
||||||
char temp { p1 };
|
char temp { p1 };
|
||||||
|
|||||||
+15
-18
@@ -5,25 +5,13 @@
|
|||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
Window::Window() {
|
|
||||||
view.fill(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
void Window::draw_display() const {
|
|
||||||
clear();
|
|
||||||
mvprintw(0, 0, " %c | %c | %c ", view[0], view[1], view[2]);
|
|
||||||
mvprintw(1, 0, " - + - + - ");
|
|
||||||
mvprintw(2, 0, " %c | %c | %c ", view[3], view[4], view[5]);
|
|
||||||
mvprintw(3, 0, " - + - + - ");
|
|
||||||
mvprintw(4, 0, " %c | %c | %c ", view[6], view[7], view[8]);
|
|
||||||
}
|
|
||||||
|
|
||||||
Engine::Engine() {
|
Engine::Engine() {
|
||||||
initscr();
|
initscr();
|
||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
nodelay(stdscr, true);
|
nodelay(stdscr, true);
|
||||||
|
view.fill(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() { endwin(); }
|
Engine::~Engine() { endwin(); }
|
||||||
@@ -32,7 +20,7 @@ void Engine::run() {
|
|||||||
while (process_input()) {
|
while (process_input()) {
|
||||||
bool game_over = check_game_over();
|
bool game_over = check_game_over();
|
||||||
if(!game_over) { status = "Current Player: " + std::string(1, temp) + "\n"; }
|
if(!game_over) { status = "Current Player: " + std::string(1, temp) + "\n"; }
|
||||||
win.draw_display();
|
draw_display();
|
||||||
mvprintw(6, 0, status.c_str());
|
mvprintw(6, 0, status.c_str());
|
||||||
refresh();
|
refresh();
|
||||||
if(game_over) { break; }
|
if(game_over) { break; }
|
||||||
@@ -43,10 +31,19 @@ void Engine::run() {
|
|||||||
getch();
|
getch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Engine::draw_display() const {
|
||||||
|
clear();
|
||||||
|
mvprintw(0, 0, " %c | %c | %c ", view[0], view[1], view[2]);
|
||||||
|
mvprintw(1, 0, " - + - + - ");
|
||||||
|
mvprintw(2, 0, " %c | %c | %c ", view[3], view[4], view[5]);
|
||||||
|
mvprintw(3, 0, " - + - + - ");
|
||||||
|
mvprintw(4, 0, " %c | %c | %c ", view[6], view[7], view[8]);
|
||||||
|
}
|
||||||
|
|
||||||
bool Engine::check_game_over() {
|
bool Engine::check_game_over() {
|
||||||
if(check_line(p1)) { return status = "Game Over! Player 1 wins!", true; }
|
if(check_line(p1)) { return status = "Game Over! Player 1 wins!", true; }
|
||||||
if(check_line(p2)) { return status = "Game Over! Player 2 wins!", true; }
|
if(check_line(p2)) { return status = "Game Over! Player 2 wins!", true; }
|
||||||
if(std::ranges::count(win.view, ' ') == 0) {
|
if(std::ranges::count(view, ' ') == 0) {
|
||||||
return status = "Game Over! It's a tie!", true;
|
return status = "Game Over! It's a tie!", true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,8 +58,8 @@ bool Engine::process_input() {
|
|||||||
if(ch == 'q') { return false; }
|
if(ch == 'q') { return false; }
|
||||||
if(ch < '1' || ch > '9') continue;
|
if(ch < '1' || ch > '9') continue;
|
||||||
index = ch - '1';
|
index = ch - '1';
|
||||||
if(win.view[index] == ' ') {
|
if(view[index] == ' ') {
|
||||||
win.view[index] = temp;
|
view[index] = temp;
|
||||||
temp = (temp == p1) ? p2 : p1;
|
temp = (temp == p1) ? p2 : p1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,7 +74,7 @@ bool Engine::check_line(char id) const {
|
|||||||
{0, 4, 8}, {2, 4, 6}
|
{0, 4, 8}, {2, 4, 6}
|
||||||
}};
|
}};
|
||||||
for(const auto& i : wins) {
|
for(const auto& i : wins) {
|
||||||
if(win.view[i[0]] == id && win.view[i[1]] == id && win.view[i[2]] == id) {
|
if(view[i[0]] == id && view[i[1]] == id && view[i[2]] == id) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user