From d4803eb15686fc2b8b69e80dd2dc72b8464e1b28 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 26 Feb 2026 02:32:26 -0500 Subject: [PATCH] I broke line checking btw --- .clang-format | 19 ++++++++ include/engine.hpp | 26 ++++------- src/engine.cpp | 108 +++++++++++++++++++++++++++------------------ src/main.cpp | 7 +-- 4 files changed, 95 insertions(+), 65 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..369e701 --- /dev/null +++ b/.clang-format @@ -0,0 +1,19 @@ +--- +BasedOnStyle: Google +Standard: Latest +UseTab: Always +TabWidth: 8 +IndentWidth: 8 +BreakBeforeBraces: Allman +SpaceBeforeCpp11BracedList: true +SeparateDefinitionBlocks: Always +Cpp11BracedListStyle: false +BreakAfterReturnType: TopLevel +AlignTrailingComments: + Kind: Always +ReflowComments: Always +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortLoopsOnASingleLine: true +IndentAccessModifiers: false # Indent access specifiers by one level +AccessModifierOffset: -4 # Optional: adjust offset relative to the class indentation level diff --git a/include/engine.hpp b/include/engine.hpp index 552a7e6..20ffccd 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -1,21 +1,11 @@ -#pragma once - -#include -#include +#ifndef ENGINE_HPP +#define ENGINE_HPP #include -class Engine { -public: - Engine(); - ~Engine() { endwin(); } - void run(); +extern char buf[9]; -private: - char p1 { 'x' }, p2 { 'o' }, temp { p1 }; - std::string status; - std::array view; - void draw_display() const; - bool check_line(char id) const; - bool update_status(); - bool process_input(); -}; +void run(); +bool check_line(char id); +bool update_status(); +bool process_input(); +#endif diff --git a/src/engine.cpp b/src/engine.cpp index 74a494e..fa6df6b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -1,73 +1,93 @@ -#include +#include + #include #include -#include +#include +#include + +const char p1 { 'x' }, p2 { 'o' }; +char temp { p1 }; +char status[32]; +char buf[9]; + +void +run() +{ + bool game_over; + for (auto& i : buf) i = ' '; -Engine::Engine() { initscr(); cbreak(); noecho(); curs_set(0); nodelay(stdscr, true); - view.fill(' '); -} - -void Engine::run() { - bool game_over; - do { + do + { game_over = update_status(); - draw_display(); + clear(); + mvprintw(0, 0, " %c | %c | %c ", buf[0], buf[1], buf[2]); + mvprintw(1, 0, " - + - + - "); + mvprintw(2, 0, " %c | %c | %c ", buf[3], buf[4], buf[5]); + mvprintw(3, 0, " - + - + - "); + mvprintw(4, 0, " %c | %c | %c ", buf[6], buf[7], buf[8]); + mvprintw(6, 0, "%s", status); refresh(); - } while(!game_over && process_input()); - - mvprintw(7, 0, "Press any key to quit."); - nodelay(stdscr, false); - getch(); + } while (!game_over && process_input()); + endwin(); } -void Engine::draw_display() const { - 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]); - mvprintw(6, 0, "%s", status.c_str()); -} +bool +update_status() +{ + if (check_line(p1) || check_line(p2)) + { + (check_line(p1) + ? (void)strncpy(status, "Game Over! Player 1 wins!", + sizeof(status)) + : (void)strncpy(status, "Game Over! Player 2 wins!", + sizeof(status))); + return true; + } + if (std::ranges::count(buf, ' ') == 0) + { + (void)strncpy(status, "Game Over! It's a tie!", sizeof(status)); + return true; + } + (void)strncpy(status, std::format("Current Player: {}\n", temp).c_str(), + sizeof(status)); -bool Engine::update_status() { - 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(view, ' ') == 0) { return status = "Game Over! It's a tie!", true; } - status = "Current Player: " + std::string(1, temp) + "\n"; return false; } -bool Engine::process_input() { +bool +process_input() +{ int ch, index; bool hit = false; - while((ch = getch()) != ERR) { + while ((ch = getch()) != ERR) + { hit = true; - if(ch == 'q') { return false; } - if(ch < '1' || ch > '9') continue; + if (ch == 'q') return false; + if (ch < '1' || ch > '9') continue; index = ch - '1'; - if(view[index] != ' ') continue; - view[index] = temp; + if (buf[index] != ' ') continue; + buf[index] = temp; temp = (temp == p1) ? p2 : p1; } - if(!hit) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); } return true; } -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} -}}; +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 } +}; -bool Engine::check_line(char id) const { - for(const auto& i : wins) { - if(view[i[0]] == id && view[i[1]] == id && view[i[2]] == id) { return true; } +bool +check_line(const char id) +{ + for (const auto& i : wins) + { + return buf[i[0]] == id && buf[i[1]] == id && buf[i[2]] == id; } return false; } diff --git a/src/main.cpp b/src/main.cpp index 89a9239..f845612 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include -int main() { - Engine engine; - engine.run(); +int +main() +{ + run(); }