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
+35 -50
View File
@@ -1,8 +1,6 @@
#include <engine.hpp>
#include <algorithm>
#include <cctype>
#include <engine.hpp>
#include <chrono>
#include <format>
#include <iostream>
#include <thread>
@@ -11,12 +9,14 @@ Window::Window() {
}
void Window::draw_display() const {
std::cout << "\033[H" << std::format(
"{} | {} | {}\r\n- + - + -\r\n{} | {} | {}\r\n- + - + -\r\n{} | {} | {}\r\n",
view.at(0), view.at(1), view.at(2),
view.at(3), view.at(4), view.at(5),
view.at(6), view.at(7), view.at(8)
) << std::flush;
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;
}
Engine::Engine() {
@@ -28,65 +28,50 @@ Engine::Engine() {
}
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();
std::cout << "Current Player: " << temp->id << "\r\n";
std::this_thread::sleep_for(std::chrono::milliseconds((int) (150)));
std::cout << status;
if(game_over) { break; }
std::this_thread::sleep_for(std::chrono::milliseconds(150));
}
}
bool Engine::exit_code() {
if(check_line(player1.id)) {
std::cout << "Game Over! Player 1 wins!\n";
std::this_thread::sleep_for(std::chrono::seconds(2));
return false;
} 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;
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;
}
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 false;
}
bool Engine::process_input() {
int ch, index, x, y;
int ch, index;
while((ch = getch()) != ERR) {
if(ch == 'q') { return false; }
if(ch >= '1' && ch <= '9') {
index = ch - '1';
if(win.view.at(index) != ' ') continue;
win.view.at(index) = temp->id;
if(temp->id == 'x') { temp = &player2; }
else if(temp->id == 'o') { temp = &player1; }
if(win.view[index] == ' ') { win.view[index] = temp->id; }
temp = (temp == &player1) ? &player2 : &player1;
}
}
return true;
}
bool Engine::check_line(char id) {
for(int i = 0; i < COL; i++) {
if(win.view.at(i) == id && win.view.at(i + 3) == id && win.view.at(i + 6) == id) {
return true;
}
}
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;
bool Engine::check_line(char id) const {
static constexpr int wins[8][3] = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}
};
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;
}