moving things around
This commit is contained in:
+5
-6
@@ -2,21 +2,20 @@
|
|||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <ncurses.h>
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
public:
|
public:
|
||||||
Engine();
|
Engine();
|
||||||
~Engine();
|
~Engine() { endwin(); }
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
char p1 { 'x' }, p2 { 'o' }, temp { p1 };
|
||||||
std::string status;
|
std::string status;
|
||||||
std::array<char, 9> view;
|
std::array<char, 9> view;
|
||||||
void draw_display() const;
|
void draw_display() const;
|
||||||
char p1 { 'x' };
|
|
||||||
char p2 { 'o' };
|
|
||||||
char temp { p1 };
|
|
||||||
bool process_input();
|
|
||||||
bool check_game_over();
|
|
||||||
bool check_line(char id) const;
|
bool check_line(char id) const;
|
||||||
|
bool update_status();
|
||||||
|
bool process_input();
|
||||||
};
|
};
|
||||||
|
|||||||
+11
-17
@@ -1,8 +1,6 @@
|
|||||||
#include <engine.hpp>
|
#include <engine.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ncurses.h>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
Engine::Engine() {
|
Engine::Engine() {
|
||||||
@@ -14,16 +12,13 @@ Engine::Engine() {
|
|||||||
view.fill(' ');
|
view.fill(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() { endwin(); }
|
|
||||||
|
|
||||||
void Engine::run() {
|
void Engine::run() {
|
||||||
while (process_input()) {
|
bool game_over;
|
||||||
bool game_over = check_game_over();
|
do {
|
||||||
if(!game_over) { status = "Current Player: " + std::string(1, temp) + "\n"; }
|
game_over = update_status();
|
||||||
draw_display();
|
draw_display();
|
||||||
refresh();
|
refresh();
|
||||||
if(game_over) { break; }
|
} while(!game_over && process_input());
|
||||||
}
|
|
||||||
|
|
||||||
mvprintw(7, 0, "Press any key to quit.");
|
mvprintw(7, 0, "Press any key to quit.");
|
||||||
nodelay(stdscr, false);
|
nodelay(stdscr, false);
|
||||||
@@ -40,10 +35,11 @@ void Engine::draw_display() const {
|
|||||||
mvprintw(6, 0, "%s", status.c_str());
|
mvprintw(6, 0, "%s", status.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::check_game_over() {
|
bool Engine::update_status() {
|
||||||
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(view, ' ') == 0) { return status = "Game Over! It's a tie!", true; }
|
if(std::ranges::count(view, ' ') == 0) { return status = "Game Over! It's a tie!", true; }
|
||||||
|
status = "Current Player: " + std::string(1, temp) + "\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,25 +51,23 @@ 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(view[index] == ' ') {
|
if(view[index] != ' ') continue;
|
||||||
view[index] = temp;
|
view[index] = temp;
|
||||||
temp = (temp == p1) ? p2 : p1;
|
temp = (temp == p1) ? p2 : p1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if(!hit) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); }
|
if(!hit) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); }
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::check_line(char id) const {
|
constexpr std::array<std::array<int, 3>, 8> wins = {{
|
||||||
static constexpr std::array<std::array<int, 3>, 8> wins = {{
|
|
||||||
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
|
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
|
||||||
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
|
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
|
||||||
{0, 4, 8}, {2, 4, 6}
|
{0, 4, 8}, {2, 4, 6}
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
bool Engine::check_line(char id) const {
|
||||||
for(const auto& i : wins) {
|
for(const auto& i : wins) {
|
||||||
if(view[i[0]] == id && view[i[1]] == id && view[i[2]] == id) {
|
if(view[i[0]] == id && view[i[1]] == id && view[i[2]] == id) { return true; }
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,5 +3,4 @@
|
|||||||
int main() {
|
int main() {
|
||||||
Engine engine;
|
Engine engine;
|
||||||
engine.run();
|
engine.run();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user