removing the map
This commit is contained in:
+4
-10
@@ -1,7 +1,6 @@
|
|||||||
#ifndef ELEMENTS_HPP
|
#ifndef ELEMENTS_HPP
|
||||||
#define ELEMENTS_HPP
|
#define ELEMENTS_HPP
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <array>
|
#include <array>
|
||||||
|
|
||||||
constexpr int ROW = 5;
|
constexpr int ROW = 5;
|
||||||
@@ -26,21 +25,16 @@ public:
|
|||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Window> win;
|
Window win;
|
||||||
|
|
||||||
struct Player {
|
struct Player {
|
||||||
Player(char id) : id(id) {};
|
Player(char id) : id(id) {};
|
||||||
char id;
|
char id;
|
||||||
};
|
};
|
||||||
|
|
||||||
const std::array<vec2, 9> map;
|
Player player1 { 'x' };
|
||||||
|
Player player2 { 'o' };
|
||||||
struct GameState {
|
Player *temp { &player1 };
|
||||||
Player player1;
|
|
||||||
Player player2;
|
|
||||||
Player *temp;
|
|
||||||
GameState() : player1('x'), player2('o'), temp { &player1 } {};
|
|
||||||
} state;
|
|
||||||
bool process_input();
|
bool process_input();
|
||||||
bool exit_code();
|
bool exit_code();
|
||||||
bool check_line(char id);
|
bool check_line(char id);
|
||||||
|
|||||||
+17
-24
@@ -1,7 +1,6 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <engine.hpp>
|
#include <engine.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memory>
|
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
@@ -28,26 +27,20 @@ void Window::draw_display() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::Engine() :
|
Engine::Engine() {
|
||||||
map {
|
|
||||||
vec2 { 0, 0 }, vec2 { 2, 0 }, vec2 { 4, 0 },
|
|
||||||
vec2 { 0, 2 }, vec2 { 2, 2 }, vec2 { 4, 2 },
|
|
||||||
vec2 { 0, 4 }, vec2 { 2, 4 }, vec2 { 4, 4 }
|
|
||||||
} {
|
|
||||||
initscr();
|
initscr();
|
||||||
cbreak();
|
cbreak();
|
||||||
noecho();
|
noecho();
|
||||||
curs_set(0);
|
curs_set(0);
|
||||||
nodelay(stdscr, true);
|
nodelay(stdscr, true);
|
||||||
win = std::make_unique<Window>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine() {}
|
Engine::~Engine() {}
|
||||||
|
|
||||||
void Engine::run() {
|
void Engine::run() {
|
||||||
while (exit_code() && process_input()) {
|
while (exit_code() && process_input()) {
|
||||||
win->draw_display();
|
win.draw_display();
|
||||||
std::cout << "Current Player: " << state.temp->id << "\n";
|
std::cout << "Current Player: " << temp->id << "\n";
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds((int) (150)));
|
std::this_thread::sleep_for(std::chrono::milliseconds((int) (150)));
|
||||||
}
|
}
|
||||||
endwin();
|
endwin();
|
||||||
@@ -55,16 +48,16 @@ void Engine::run() {
|
|||||||
|
|
||||||
bool Engine::exit_code() {
|
bool Engine::exit_code() {
|
||||||
int index = 0;
|
int index = 0;
|
||||||
if(check_line(state.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));
|
||||||
return false;
|
return false;
|
||||||
} else if(check_line(state.player2.id)) {
|
} else if(check_line(player2.id)) {
|
||||||
std::cout << "Game Over! Player 2 wins!" << std::endl;
|
std::cout << "Game Over! Player 2 wins!" << std::endl;
|
||||||
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) {
|
for(auto &row : win.view) {
|
||||||
for(auto val : row) {
|
for(auto val : row) {
|
||||||
if(val != ' ') index++;
|
if(val != ' ') index++;
|
||||||
}
|
}
|
||||||
@@ -78,16 +71,16 @@ bool Engine::exit_code() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Engine::process_input() {
|
bool Engine::process_input() {
|
||||||
int ch, index;
|
int ch, index, x, y;
|
||||||
while((ch = getch()) != ERR) {
|
while((ch = getch()) != ERR) {
|
||||||
if(ch == 'q') { return false; }
|
if(ch == 'q') { return false; }
|
||||||
if(ch >= '1' && ch <= '9') {
|
if(ch >= '1' && ch <= '9') {
|
||||||
index = ch - '1';
|
index = ch - '1';
|
||||||
if(win->view[map[index].y][map[index].x] == ' ') {
|
x = (index % 3) * 2, y = (index / 3) * 2;
|
||||||
win->view[map[index].y][map[index].x] = state.temp->id;
|
if(win.view[y][x] != ' ') continue;
|
||||||
if(state.temp->id == 'x') { state.temp = &state.player2; }
|
win.view[y][x] = temp->id;
|
||||||
else if(state.temp->id == 'o') { state.temp = &state.player1; }
|
if(temp->id == 'x') { temp = &player2; }
|
||||||
}
|
else if(temp->id == 'o') { temp = &player1; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -96,20 +89,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