fixed bug where the game exits before text is printed
This commit is contained in:
+15
-15
@@ -3,33 +3,33 @@
|
|||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
class Window;
|
|
||||||
|
|
||||||
struct Player {
|
|
||||||
Player(char id) : id(id) {};
|
|
||||||
char id;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct vec2 {
|
|
||||||
vec2(int x, int y) : x(x), y(y) {}
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
};
|
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
public:
|
public:
|
||||||
Engine();
|
Engine();
|
||||||
|
~Engine();
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Window> win;
|
std::unique_ptr<class Window> win;
|
||||||
|
|
||||||
|
struct Player {
|
||||||
|
Player(char id) : id(id) {};
|
||||||
|
char id;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct vec2 {
|
||||||
|
vec2(int x, int y) : x(x), y(y) {}
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
|
||||||
struct GameState {
|
struct GameState {
|
||||||
Player player1;
|
Player player1;
|
||||||
Player player2;
|
Player player2;
|
||||||
Player *temp;
|
Player *temp;
|
||||||
GameState() : player1('x'), player2('o'), temp { &player1 } {};
|
GameState() : player1('x'), player2('o'), temp { &player1 } {};
|
||||||
} state;
|
} state;
|
||||||
void get_current_player();
|
void set_player();
|
||||||
bool process_input();
|
bool process_input();
|
||||||
bool exit_code();
|
bool exit_code();
|
||||||
bool check_line(char id);
|
bool check_line(char id);
|
||||||
|
|||||||
+16
-10
@@ -1,3 +1,4 @@
|
|||||||
|
#include <chrono>
|
||||||
#include <elements.hpp>
|
#include <elements.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <window.hpp>
|
#include <window.hpp>
|
||||||
@@ -15,6 +16,8 @@ Engine::Engine() {
|
|||||||
win = std::make_unique<Window>();
|
win = std::make_unique<Window>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Engine::~Engine() {}
|
||||||
|
|
||||||
void Engine::run() {
|
void Engine::run() {
|
||||||
while (exit_code() && process_input()) {
|
while (exit_code() && process_input()) {
|
||||||
win->draw_display();
|
win->draw_display();
|
||||||
@@ -28,9 +31,11 @@ bool Engine::exit_code() {
|
|||||||
int index = 0;
|
int index = 0;
|
||||||
if(check_line(state.player1.id)) {
|
if(check_line(state.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));
|
||||||
return false;
|
return false;
|
||||||
} else if(check_line(state.player2.id)) {
|
} else if(check_line(state.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));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for(auto &row : win->view) {
|
for(auto &row : win->view) {
|
||||||
@@ -40,6 +45,7 @@ bool Engine::exit_code() {
|
|||||||
}
|
}
|
||||||
if(index == 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));
|
||||||
return false;
|
return false;
|
||||||
} else index = 0;
|
} else index = 0;
|
||||||
return true;
|
return true;
|
||||||
@@ -51,37 +57,37 @@ bool Engine::process_input() {
|
|||||||
if(ch == 113) { return false; }
|
if(ch == 113) { return false; }
|
||||||
if(ch == 49) {
|
if(ch == 49) {
|
||||||
win->view[0][0] = state.temp->id;
|
win->view[0][0] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 50) {
|
} if(ch == 50) {
|
||||||
win->view[0][2] = state.temp->id;
|
win->view[0][2] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 51) {
|
} if(ch == 51) {
|
||||||
win->view[0][4] = state.temp->id;
|
win->view[0][4] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 52) {
|
} if(ch == 52) {
|
||||||
win->view[2][0] = state.temp->id;
|
win->view[2][0] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 53) {
|
} if(ch == 53) {
|
||||||
win->view[2][2] = state.temp->id;
|
win->view[2][2] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 54) {
|
} if(ch == 54) {
|
||||||
win->view[2][4] = state.temp->id;
|
win->view[2][4] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 55) {
|
} if(ch == 55) {
|
||||||
win->view[4][0] = state.temp->id;
|
win->view[4][0] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 56) {
|
} if(ch == 56) {
|
||||||
win->view[4][2] = state.temp->id;
|
win->view[4][2] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
} if(ch == 57) {
|
} if(ch == 57) {
|
||||||
win->view[4][4] = state.temp->id;
|
win->view[4][4] = state.temp->id;
|
||||||
get_current_player();
|
set_player();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::get_current_player() {
|
void Engine::set_player() {
|
||||||
if(state.temp->id == 'x') { state.temp = &state.player2; }
|
if(state.temp->id == 'x') { state.temp = &state.player2; }
|
||||||
else if(state.temp->id == 'o') { state.temp = &state.player1; }
|
else if(state.temp->id == 'o') { state.temp = &state.player1; }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user