a bunch of mini optimizations

This commit is contained in:
DarthMooMancer
2025-11-13 02:51:20 -05:00
parent ab0d8532f2
commit 93f067a93e
2 changed files with 41 additions and 45 deletions
+7 -19
View File
@@ -1,39 +1,27 @@
#ifndef ENGINE_HPP
#define ENGINE_HPP
#pragma once
#include <array>
#include <ncurses.h>
#include <string>
constexpr int ROW = 3;
constexpr int COL = 3;
struct Window {
Window();
std::array<char, ROW * COL> view;
std::array<char, 9> view;
void draw_display() const;
};
class Engine {
public:
Engine();
~Engine() { endwin(); };
~Engine();
void run();
private:
struct Player {
Player(char id) : id(id) {};
const char id;
};
std::string status;
Window win;
Player player1 { 'x' };
Player player2 { 'o' };
Player *temp { &player1 };
char p1 { 'x' };
char p2 { 'o' };
char temp { p1 };
bool process_input();
bool exit_code();
bool check_game_over();
bool check_line(char id) const;
};
#endif
+33 -25
View File
@@ -1,7 +1,8 @@
#include <cctype>
#include <engine.hpp>
#include <algorithm>
#include <array>
#include <chrono>
#include <iostream>
#include <ncurses.h>
#include <thread>
Window::Window() {
@@ -9,14 +10,12 @@ Window::Window() {
}
void Window::draw_display() const {
std::cout << "\033[H"
<< view[0] << " | " << view[1] << " | " << view[2]
<< "\r\n- + - + -\r\n"
<< view[3] << " | " << view[4] << " | " << view[5]
<< "\r\n- + - + -\r\n"
<< view[6] << " | " << view[7] << " | " << view[8]
<< "\r\n"
<< std::flush;
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() {
@@ -27,23 +26,28 @@ Engine::Engine() {
nodelay(stdscr, true);
}
Engine::~Engine() { endwin(); }
void Engine::run() {
while (process_input()) {
bool game_over = exit_code();
if(!game_over) { status = "Current Player: " + std::string(1, temp->id) + "\n"; }
bool game_over = check_game_over();
if(!game_over) { status = "Current Player: " + std::string(1, temp) + "\n"; }
win.draw_display();
std::cout << status;
mvprintw(6, 0, status.c_str());
refresh();
if(game_over) { break; }
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
mvprintw(7, 0, "Press any key to quit.");
nodelay(stdscr, false);
getch();
}
bool Engine::exit_code() {
if(check_line(player1.id)) { status = "Game Over! Player 1 wins!"; return true; }
if(check_line(player2.id)) { status = "Game Over! Player 2 wins!"; return true; }
if(std::count(win.view.begin(), win.view.end(), ' ') == 0) {
status = "Game Over! It's a tie!";
return true;
bool Engine::check_game_over() {
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(std::ranges::count(win.view, ' ') == 0) {
return status = "Game Over! It's a tie!", true;
}
return false;
@@ -51,23 +55,27 @@ bool Engine::exit_code() {
bool Engine::process_input() {
int ch, index;
bool hit = false;
while((ch = getch()) != ERR) {
hit = true;
if(ch == 'q') { return false; }
if(ch >= '1' && ch <= '9') {
if(ch < '1' || ch > '9') continue;
index = ch - '1';
if(win.view[index] == ' ') { win.view[index] = temp->id; }
temp = (temp == &player1) ? &player2 : &player1;
if(win.view[index] == ' ') {
win.view[index] = temp;
temp = (temp == p1) ? p2 : p1;
}
}
if(!hit) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); }
return true;
}
bool Engine::check_line(char id) const {
static constexpr int wins[8][3] = {
static constexpr std::array<std::array<int, 3>, 8> wins = {{
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}
};
}};
for(const auto& i : wins) {
if(win.view[i[0]] == id && win.view[i[1]] == id && win.view[i[2]] == id) {
return true;