Fully commited; converted to raw c instead of c++

This commit is contained in:
Andrew Haynes
2026-02-26 13:23:58 -05:00
parent 3e855635f6
commit 5bee0ef2c5
4 changed files with 113 additions and 135 deletions
-3
View File
@@ -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})
-10
View File
@@ -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
-120
View File
@@ -1,120 +0,0 @@
#include <engine.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
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;
}
+113 -2
View File
@@ -1,7 +1,118 @@
#include <engine.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
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);
}