From 5bee0ef2c5ec85fff36760cf793616243470be32 Mon Sep 17 00:00:00 2001 From: Andrew Haynes Date: Thu, 26 Feb 2026 13:23:58 -0500 Subject: [PATCH] Fully commited; converted to raw c instead of c++ --- CMakeLists.txt | 3 -- include/engine.h | 10 ---- src/engine.c | 120 ----------------------------------------------- src/main.c | 115 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 113 insertions(+), 135 deletions(-) delete mode 100644 include/engine.h delete mode 100644 src/engine.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d1303e..7e87daf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,11 +7,8 @@ project(tic_tac_toe) set(SOURCES 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}) diff --git a/include/engine.h b/include/engine.h deleted file mode 100644 index f97d661..0000000 --- a/include/engine.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef ENGINE_HPP -#define ENGINE_HPP - -extern char buf[9]; - -void run(); -bool check_line(char id); -bool update_status(); -bool process_input(); -#endif diff --git a/src/engine.c b/src/engine.c deleted file mode 100644 index 1872dc6..0000000 --- a/src/engine.c +++ /dev/null @@ -1,120 +0,0 @@ -#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/main.c b/src/main.c index 9b00078..5c303bb 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,118 @@ -#include +#include +#include +#include +#include +#include + +const char p1 = 'x', p2 = 'o'; +char temp = p1; +char status[32]; +char buf[9]; +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; +} + +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); + } +} + +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; +} int main() { - 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); }