From 93f067a93e5b0412fc6dcf0d46db2b903b524fc3 Mon Sep 17 00:00:00 2001 From: DarthMooMancer <107283522+DarthMooMancer@users.noreply.github.com> Date: Thu, 13 Nov 2025 02:51:20 -0500 Subject: [PATCH] a bunch of mini optimizations --- include/engine.hpp | 26 ++++++-------------- src/engine.cpp | 60 ++++++++++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/include/engine.hpp b/include/engine.hpp index ff38fdd..ce4e663 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -1,39 +1,27 @@ -#ifndef ENGINE_HPP -#define ENGINE_HPP +#pragma once #include -#include #include -constexpr int ROW = 3; -constexpr int COL = 3; - struct Window { Window(); - std::array view; + std::array 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 diff --git a/src/engine.cpp b/src/engine.cpp index 7518de7..0642e6c 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1,7 +1,8 @@ -#include #include +#include +#include #include -#include +#include #include 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') { - index = ch - '1'; - if(win.view[index] == ' ') { win.view[index] = temp->id; } - temp = (temp == &player1) ? &player2 : &player1; + if(ch < '1' || ch > '9') continue; + index = ch - '1'; + 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, 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;