I broke line checking btw

This commit is contained in:
Andrew Haynes
2026-02-26 02:32:26 -05:00
parent aba76408f8
commit d4803eb156
4 changed files with 95 additions and 65 deletions
+19
View File
@@ -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
+8 -18
View File
@@ -1,21 +1,11 @@
#pragma once #ifndef ENGINE_HPP
#define ENGINE_HPP
#include <array>
#include <string>
#include <ncurses.h> #include <ncurses.h>
class Engine { extern char buf[9];
public:
Engine();
~Engine() { endwin(); }
void run();
private: void run();
char p1 { 'x' }, p2 { 'o' }, temp { p1 }; bool check_line(char id);
std::string status; bool update_status();
std::array<char, 9> view; bool process_input();
void draw_display() const; #endif
bool check_line(char id) const;
bool update_status();
bool process_input();
};
+64 -44
View File
@@ -1,73 +1,93 @@
#include <engine.hpp> #include <unistd.h>
#include <algorithm> #include <algorithm>
#include <chrono> #include <chrono>
#include <thread> #include <ctime>
#include <engine.hpp>
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(); initscr();
cbreak(); cbreak();
noecho(); noecho();
curs_set(0); curs_set(0);
nodelay(stdscr, true); nodelay(stdscr, true);
view.fill(' '); do
} {
void Engine::run() {
bool game_over;
do {
game_over = update_status(); 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(); refresh();
} while(!game_over && process_input()); } while (!game_over && process_input());
endwin();
mvprintw(7, 0, "Press any key to quit.");
nodelay(stdscr, false);
getch();
} }
void Engine::draw_display() const { bool
clear(); update_status()
mvprintw(0, 0, " %c | %c | %c ", view[0], view[1], view[2]); {
mvprintw(1, 0, " - + - + - "); if (check_line(p1) || check_line(p2))
mvprintw(2, 0, " %c | %c | %c ", view[3], view[4], view[5]); {
mvprintw(3, 0, " - + - + - "); (check_line(p1)
mvprintw(4, 0, " %c | %c | %c ", view[6], view[7], view[8]); ? (void)strncpy(status, "Game Over! Player 1 wins!",
mvprintw(6, 0, "%s", status.c_str()); 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; return false;
} }
bool Engine::process_input() { bool
process_input()
{
int ch, index; int ch, index;
bool hit = false; bool hit = false;
while((ch = getch()) != ERR) { while ((ch = getch()) != ERR)
{
hit = true; hit = true;
if(ch == 'q') { return false; } if (ch == 'q') return false;
if(ch < '1' || ch > '9') continue; if (ch < '1' || ch > '9') continue;
index = ch - '1'; index = ch - '1';
if(view[index] != ' ') continue; if (buf[index] != ' ') continue;
view[index] = temp; buf[index] = temp;
temp = (temp == p1) ? p2 : p1; temp = (temp == p1) ? p2 : p1;
} }
if(!hit) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); }
return true; return true;
} }
constexpr std::array<std::array<int, 3>, 8> wins = {{ constexpr int wins[8][3] = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 },
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 }
{0, 4, 8}, {2, 4, 6} };
}};
bool Engine::check_line(char id) const { bool
for(const auto& i : wins) { check_line(const char id)
if(view[i[0]] == id && view[i[1]] == id && view[i[2]] == id) { return true; } {
for (const auto& i : wins)
{
return buf[i[0]] == id && buf[i[1]] == id && buf[i[2]] == id;
} }
return false; return false;
} }
+4 -3
View File
@@ -1,6 +1,7 @@
#include <engine.hpp> #include <engine.hpp>
int main() { int
Engine engine; main()
engine.run(); {
run();
} }