This commit is contained in:
DarthMooMancer
2025-11-07 15:16:16 -05:00
parent 438b029381
commit 85e66c0beb
2 changed files with 36 additions and 39 deletions
+10 -11
View File
@@ -1,21 +1,21 @@
#ifndef ELEMENTS_HPP
#define ELEMENTS_HPP
#include <array>
#include <map>
constexpr int ROW = 5;
constexpr int COL = 5;
struct Window {
Window();
std::array<std::array<char, COL>, ROW> view;
void draw_display();
struct vec2 {
int x, y;
vec2(int x, int y) : x(x), y(y) {}
auto operator <=> (const vec2&) const = default;
};
struct vec2 {
vec2(int x, int y) : x(x), y(y) {}
int x;
int y;
struct Window {
Window();
std::map<vec2, char> view;
void draw_display();
};
class Engine {
@@ -25,13 +25,12 @@ public:
void run();
private:
Window win;
struct Player {
Player(char id) : id(id) {};
char id;
};
Window win;
Player player1 { 'x' };
Player player2 { 'o' };
Player *temp { &player1 };
+26 -28
View File
@@ -1,27 +1,27 @@
#include <chrono>
#include <algorithm>
#include <engine.hpp>
#include <chrono>
#include <iostream>
#include <ncurses.h>
#include <thread>
#include <ranges>
Window::Window() {
for(auto &val : view) { val.fill('.'); }
for(int i : std::ranges::views::iota(0, ROW)) {
for(int j : std::ranges::views::iota(0, COL)) {
if(i % 2 == 0 && j % 2 == 0) { view[i][j] = ' '; }
}
}
}
Window::Window() : view {
{ { 0, 0 }, ' ' }, { { 2, 0 }, ' ' }, { { 4, 0 }, ' ' },
{ { 0, 2 }, ' ' }, { { 2, 2 }, ' ' }, { { 4, 2 }, ' ' },
{ { 0, 4 }, ' ' }, { { 2, 4 }, ' ' }, { { 4, 4 }, ' ' }
} {}
void Window::draw_display() {
char c;
std::cout << "\033[H" << std::flush; // Clear screen
for(int i : std::ranges::views::iota(0, ROW)) {
for(int j : std::ranges::views::iota(0, COL)) {
if(i % 2 == 0 && j % 2 == 0) { std::cout << view[i][j] << " "; }
else if(i % 2 == 0) { std::cout << "| "; }
else if(i % 2 != 0 && j % 2 == 0) { std::cout << "- "; }
else if(i % 2 != 0 && j % 2 != 0) { std::cout << "+ "; }
if(i % 2 == 0 && j % 2 == 0) { c = view[{i, j}]; }
else if(i % 2 == 0) { c = '|'; }
else if(j % 2 == 0) { c = '-'; }
else { c = '+'; }
std::cout << c << " ";
}
std::cout << "\r\n";
}
@@ -47,7 +47,6 @@ void Engine::run() {
}
bool Engine::exit_code() {
int index = 0;
if(check_line(player1.id)) {
std::cout << "Game Over! Player 1 wins!" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
@@ -57,16 +56,15 @@ bool Engine::exit_code() {
std::this_thread::sleep_for(std::chrono::seconds(2));
return false;
}
for(auto &row : win.view) {
for(auto val : row) {
if(val != ' ') index++;
}
}
if(index == 9) {
int filled = std::ranges::count_if(win.view, [](const auto &pair) {
return pair.second != ' ';
});
if(filled == 9) {
std::cout << "Game Over! Its a tie!" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(2));
return false;
} else index = 0;
}
return true;
}
@@ -77,8 +75,8 @@ bool Engine::process_input() {
if(ch >= '1' && ch <= '9') {
index = ch - '1';
x = (index % 3) * 2, y = (index / 3) * 2;
if(win.view[y][x] != ' ') continue;
win.view[y][x] = temp->id;
if(win.view[{y, x}] != ' ') continue;
win.view[{y, x}] = temp->id;
if(temp->id == 'x') { temp = &player2; }
else if(temp->id == 'o') { temp = &player1; }
}
@@ -89,20 +87,20 @@ bool Engine::process_input() {
bool Engine::check_line(char id) {
for(int i : std::ranges::views::iota(0, COL)) {
if(COL % 2 != 0) continue;
if(win.view[0][i] == id && win.view[2][i] == id && win.view[4][i] == id) {
if(win.view[{0, i}] == id && win.view[{2, i}] == id && win.view[{4, i}] == id) {
return true;
}
}
for(int i : std::ranges::views::iota(0, ROW)) {
if(ROW % 2 != 0) continue;
if(win.view[i][0] == id && win.view[i][2] == id && win.view[i][4] == id) {
if(win.view[{i, 0}] == id && win.view[{i, 2}] == id && win.view[{i, 4}] == id) {
return true;
}
}
auto same_symbol = [&](vec2 a, vec2 b) {
return win.view[a.y][a.x] == id &&
win.view[2][2] == id &&
win.view[b.y][b.x] == id;
return win.view[{a.y, a.x}] == id &&
win.view[{2, 2}] == id &&
win.view[{b.y, b.x}] == id;
};
return same_symbol({ 0, 0 }, { 4, 4 }) || same_symbol({ 4, 0 }, { 0, 4 });
}