From 3e855635f65216ed4138299e37fd6f2b7f00ea83 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 26 Feb 2026 13:13:36 -0500 Subject: [PATCH] Fully commited; converted to raw c instead of c++ --- CMakeLists.txt | 12 ++-- include/{engine.hpp => engine.h} | 1 - src/engine.c | 120 +++++++++++++++++++++++++++++++ src/engine.cpp | 93 ------------------------ src/{main.cpp => main.c} | 2 +- 5 files changed, 127 insertions(+), 101 deletions(-) rename include/{engine.hpp => engine.h} (87%) create mode 100644 src/engine.c delete mode 100644 src/engine.cpp rename src/{main.cpp => main.c} (52%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e5797e..6d1303e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,17 @@ cmake_minimum_required(VERSION 3.16) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -set(CMAKE_CXX_STANDARD 23) -set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_C_STANDARD 23) +set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) project(tic_tac_toe) set(SOURCES - src/main.cpp - src/engine.cpp + src/main.c + src/engine.c ) add_executable(tic_tac_toe ${SOURCES}) target_include_directories(tic_tac_toe PRIVATE include) -find_package(Curses REQUIRED) -target_link_libraries(tic_tac_toe ${CURSES_LIBRARIES}) +# find_package(Curses REQUIRED) +# target_link_libraries(tic_tac_toe ${CURSES_LIBRARIES}) diff --git a/include/engine.hpp b/include/engine.h similarity index 87% rename from include/engine.hpp rename to include/engine.h index 20ffccd..f97d661 100644 --- a/include/engine.hpp +++ b/include/engine.h @@ -1,6 +1,5 @@ #ifndef ENGINE_HPP #define ENGINE_HPP -#include extern char buf[9]; diff --git a/src/engine.c b/src/engine.c new file mode 100644 index 0000000..1872dc6 --- /dev/null +++ b/src/engine.c @@ -0,0 +1,120 @@ +#include +#include +#include +#include +#include +#include + +const char p1 = 'x', p2 = 'o'; +char temp = p1; +char status[32]; +char buf[9]; + +void +set_raw_mode(bool enable) +{ + static struct termios oldt, newt; + if (enable) + { + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + tcsetattr(STDIN_FILENO, TCSANOW, &newt); + } + else + { + tcsetattr(STDIN_FILENO, TCSANOW, &oldt); + } +} + +void +run() +{ + bool game_over; + for (int i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) buf[i] = ' '; + + set_raw_mode(true); + printf("\033[?25l"); + do + { + game_over = update_status(); + printf("\033[H\033[J"); + printf(" %c | %c | %c\n", buf[0], buf[1], buf[2]); + printf(" - + - + -\n"); + printf(" %c | %c | %c\n", buf[3], buf[4], buf[5]); + printf(" - + - + -\n"); + printf(" %c | %c | %c\n", buf[6], buf[7], buf[8]); + printf("%s", status); + fflush(stdout); + } while (!game_over && process_input()); + printf("Press any key to quit.\n"); + + char dummy; + read(STDIN_FILENO, &dummy, 1); + printf("\033[?25h"); + set_raw_mode(false); +} + +bool +update_status() +{ + auto p1_check = check_line(p1); + auto p2_check = check_line(p2); + if (p1_check || p2_check) + { + (p1_check ? (void)strncpy(status, "Game Over! Player 1 wins!\n", + sizeof(status)) + : (void)strncpy(status, "Game Over! Player 2 wins!\n", + sizeof(status))); + return true; + } + int count = 0; + for (int i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) + { + if (buf[i] != ' ') count++; + } + if (count == sizeof(buf) / sizeof(buf[0])) + { + (void)strncpy(status, "Game Over! It's a tie!\n", + sizeof(status)); + return true; + } + snprintf(status, sizeof(status), "Current Player: %c\n", temp); + + return false; +} + +bool +process_input() +{ + char ch; + while (read(STDIN_FILENO, &ch, 1)) + { + if (ch == 'q') return false; + if (ch < '1' || ch > '9') continue; + int index = ch - '1'; + if (buf[index] != ' ') continue; + buf[index] = temp; + temp = (temp == p1) ? p2 : p1; + return true; + } + return true; +} + +static const 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 +check_line(const char id) +{ + for (int i = 0; i < sizeof(wins) / sizeof(wins[0]); i++) + { + if ((buf[wins[i][0]] == id && buf[wins[i][1]] == id && + buf[wins[i][2]] == id)) + { + return true; + } + } + return false; +} diff --git a/src/engine.cpp b/src/engine.cpp deleted file mode 100644 index fa6df6b..0000000 --- a/src/engine.cpp +++ /dev/null @@ -1,93 +0,0 @@ -#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 = ' '; - - initscr(); - cbreak(); - noecho(); - curs_set(0); - nodelay(stdscr, true); - do - { - game_over = update_status(); - 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()); - endwin(); -} - -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)); - - return false; -} - -bool -process_input() -{ - int ch, index; - bool hit = false; - while ((ch = getch()) != ERR) - { - hit = true; - if (ch == 'q') return false; - if (ch < '1' || ch > '9') continue; - index = ch - '1'; - if (buf[index] != ' ') continue; - buf[index] = temp; - temp = (temp == p1) ? p2 : p1; - } - return true; -} - -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 -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.c similarity index 52% rename from src/main.cpp rename to src/main.c index f845612..9b00078 100644 --- a/src/main.cpp +++ b/src/main.c @@ -1,4 +1,4 @@ -#include +#include int main()