Fully commited; converted to raw c instead of c++
This commit is contained in:
+6
-6
@@ -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})
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#ifndef ENGINE_HPP
|
||||
#define ENGINE_HPP
|
||||
#include <ncurses.h>
|
||||
|
||||
extern char buf[9];
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#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 = ' ';
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <engine.hpp>
|
||||
#include <engine.h>
|
||||
|
||||
int
|
||||
main()
|
||||
Reference in New Issue
Block a user