a bunch of mini optimizations

This commit is contained in:
DarthMooMancer
2025-11-13 02:04:31 -05:00
parent c1fb582c0e
commit ab0d8532f2
2 changed files with 38 additions and 51 deletions
+3 -1
View File
@@ -3,6 +3,7 @@
#include <array> #include <array>
#include <ncurses.h> #include <ncurses.h>
#include <string>
constexpr int ROW = 3; constexpr int ROW = 3;
constexpr int COL = 3; constexpr int COL = 3;
@@ -25,13 +26,14 @@ private:
const char id; const char id;
}; };
std::string status;
Window win; Window win;
Player player1 { 'x' }; Player player1 { 'x' };
Player player2 { 'o' }; Player player2 { 'o' };
Player *temp { &player1 }; Player *temp { &player1 };
bool process_input(); bool process_input();
bool exit_code(); bool exit_code();
bool check_line(char id); bool check_line(char id) const;
}; };
#endif #endif
+36 -51
View File
@@ -1,8 +1,6 @@
#include <engine.hpp>
#include <algorithm>
#include <cctype> #include <cctype>
#include <engine.hpp>
#include <chrono> #include <chrono>
#include <format>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
@@ -11,12 +9,14 @@ Window::Window() {
} }
void Window::draw_display() const { void Window::draw_display() const {
std::cout << "\033[H" << std::format( std::cout << "\033[H"
"{} | {} | {}\r\n- + - + -\r\n{} | {} | {}\r\n- + - + -\r\n{} | {} | {}\r\n", << view[0] << " | " << view[1] << " | " << view[2]
view.at(0), view.at(1), view.at(2), << "\r\n- + - + -\r\n"
view.at(3), view.at(4), view.at(5), << view[3] << " | " << view[4] << " | " << view[5]
view.at(6), view.at(7), view.at(8) << "\r\n- + - + -\r\n"
) << std::flush; << view[6] << " | " << view[7] << " | " << view[8]
<< "\r\n"
<< std::flush;
} }
Engine::Engine() { Engine::Engine() {
@@ -28,65 +28,50 @@ Engine::Engine() {
} }
void Engine::run() { void Engine::run() {
while (exit_code() && process_input()) { while (process_input()) {
bool game_over = exit_code();
if(!game_over) { status = "Current Player: " + std::string(1, temp->id) + "\n"; }
win.draw_display(); win.draw_display();
std::cout << "Current Player: " << temp->id << "\r\n"; std::cout << status;
std::this_thread::sleep_for(std::chrono::milliseconds((int) (150))); if(game_over) { break; }
std::this_thread::sleep_for(std::chrono::milliseconds(150));
} }
} }
bool Engine::exit_code() { bool Engine::exit_code() {
if(check_line(player1.id)) { if(check_line(player1.id)) { status = "Game Over! Player 1 wins!"; return true; }
std::cout << "Game Over! Player 1 wins!\n"; if(check_line(player2.id)) { status = "Game Over! Player 2 wins!"; return true; }
std::this_thread::sleep_for(std::chrono::seconds(2)); if(std::count(win.view.begin(), win.view.end(), ' ') == 0) {
return false; status = "Game Over! It's a tie!";
} else if(check_line(player2.id)) {
std::cout << "Game Over! Player 2 wins!\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
return false;
}
int filled = std::ranges::count_if(win.view, [](const auto &sym) { return sym != ' '; });
if(filled == ROW * COL) {
std::cout << "Game Over! Its a tie!\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
return false;
}
return true; return true;
} }
return false;
}
bool Engine::process_input() { bool Engine::process_input() {
int ch, index, x, y; int ch, index;
while((ch = getch()) != ERR) { while((ch = getch()) != ERR) {
if(ch == 'q') { return false; } if(ch == 'q') { return false; }
if(ch >= '1' && ch <= '9') { if(ch >= '1' && ch <= '9') {
index = ch - '1'; index = ch - '1';
if(win.view.at(index) != ' ') continue; if(win.view[index] == ' ') { win.view[index] = temp->id; }
win.view.at(index) = temp->id; temp = (temp == &player1) ? &player2 : &player1;
if(temp->id == 'x') { temp = &player2; }
else if(temp->id == 'o') { temp = &player1; }
} }
} }
return true; return true;
} }
bool Engine::check_line(char id) { bool Engine::check_line(char id) const {
for(int i = 0; i < COL; i++) { static constexpr int wins[8][3] = {
if(win.view.at(i) == id && win.view.at(i + 3) == id && win.view.at(i + 6) == id) { {0, 1, 2}, {3, 4, 5}, {6, 7, 8},
return true; {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
} {0, 4, 8}, {2, 4, 6}
}
for(int i = 0; i < ROW * COL; i += ROW) {
if(win.view.at(i) == id && win.view.at(i + 1) == id && win.view.at(i + 2) == id) {
return true;
}
}
auto same_symbol = [&](int first, int last) {
return win.view.at(first) == id &&
win.view.at(4) == id &&
win.view.at(last) == id;
}; };
return same_symbol(0, 8) || same_symbol(2, 6); for(const auto& i : wins) {
if(win.view[i[0]] == id && win.view[i[1]] == id && win.view[i[2]] == id) {
return true;
}
}
return false;
} }