I broke line checking btw
This commit is contained in:
@@ -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
@@ -1,21 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#ifndef ENGINE_HPP
|
||||
#define ENGINE_HPP
|
||||
#include <ncurses.h>
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
Engine();
|
||||
~Engine() { endwin(); }
|
||||
void run();
|
||||
extern char buf[9];
|
||||
|
||||
private:
|
||||
char p1 { 'x' }, p2 { 'o' }, temp { p1 };
|
||||
std::string status;
|
||||
std::array<char, 9> view;
|
||||
void draw_display() const;
|
||||
bool check_line(char id) const;
|
||||
bool update_status();
|
||||
bool process_input();
|
||||
};
|
||||
void run();
|
||||
bool check_line(char id);
|
||||
bool update_status();
|
||||
bool process_input();
|
||||
#endif
|
||||
|
||||
+63
-43
@@ -1,73 +1,93 @@
|
||||
#include <engine.hpp>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#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();
|
||||
cbreak();
|
||||
noecho();
|
||||
curs_set(0);
|
||||
nodelay(stdscr, true);
|
||||
view.fill(' ');
|
||||
}
|
||||
|
||||
void Engine::run() {
|
||||
bool game_over;
|
||||
do {
|
||||
do
|
||||
{
|
||||
game_over = update_status();
|
||||
draw_display();
|
||||
refresh();
|
||||
} while(!game_over && process_input());
|
||||
|
||||
mvprintw(7, 0, "Press any key to quit.");
|
||||
nodelay(stdscr, false);
|
||||
getch();
|
||||
}
|
||||
|
||||
void Engine::draw_display() const {
|
||||
clear();
|
||||
mvprintw(0, 0, " %c | %c | %c ", view[0], view[1], view[2]);
|
||||
mvprintw(0, 0, " %c | %c | %c ", buf[0], buf[1], buf[2]);
|
||||
mvprintw(1, 0, " - + - + - ");
|
||||
mvprintw(2, 0, " %c | %c | %c ", view[3], view[4], view[5]);
|
||||
mvprintw(2, 0, " %c | %c | %c ", buf[3], buf[4], buf[5]);
|
||||
mvprintw(3, 0, " - + - + - ");
|
||||
mvprintw(4, 0, " %c | %c | %c ", view[6], view[7], view[8]);
|
||||
mvprintw(6, 0, "%s", status.c_str());
|
||||
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 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";
|
||||
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 Engine::process_input() {
|
||||
bool
|
||||
process_input()
|
||||
{
|
||||
int ch, index;
|
||||
bool hit = false;
|
||||
while((ch = getch()) != ERR) {
|
||||
while ((ch = getch()) != ERR)
|
||||
{
|
||||
hit = true;
|
||||
if(ch == 'q') { return false; }
|
||||
if(ch < '1' || ch > '9') continue;
|
||||
if (ch == 'q') return false;
|
||||
if (ch < '1' || ch > '9') continue;
|
||||
index = ch - '1';
|
||||
if(view[index] != ' ') continue;
|
||||
view[index] = temp;
|
||||
if (buf[index] != ' ') continue;
|
||||
buf[index] = temp;
|
||||
temp = (temp == p1) ? p2 : p1;
|
||||
}
|
||||
if(!hit) { std::this_thread::sleep_for(std::chrono::milliseconds(150)); }
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr std::array<std::array<int, 3>, 8> wins = {{
|
||||
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
|
||||
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
|
||||
{0, 4, 8}, {2, 4, 6}
|
||||
}};
|
||||
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 Engine::check_line(char id) const {
|
||||
for(const auto& i : wins) {
|
||||
if(view[i[0]] == id && view[i[1]] == id && view[i[2]] == id) { return true; }
|
||||
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;
|
||||
}
|
||||
|
||||
+4
-3
@@ -1,6 +1,7 @@
|
||||
#include <engine.hpp>
|
||||
|
||||
int main() {
|
||||
Engine engine;
|
||||
engine.run();
|
||||
int
|
||||
main()
|
||||
{
|
||||
run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user