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