Commit 962b4c8a authored by Konrad McClure's avatar Konrad McClure

Added score entering to game over screen

parent 7c6f862c
...@@ -13,6 +13,7 @@ class Game2048 { ...@@ -13,6 +13,7 @@ class Game2048 {
void Shift(char input); void Shift(char input);
void testInit(){*board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};} void testInit(){*board = { {4,0,0,2}, {4,2,0,0}, {4,4,0,0}, {4,4,0,0}};}
bool addNumber(); bool addNumber();
int getScore() {return score;}
......
NAME SCORE NAME SCORE
Mal 54234 Mal 54234
BVB 9240 BVB 9240
BND 6564 ass 532
CDS 6544 WEB 128
NKV 5453 qwe 128
KAL 2434 mop 128
ddd 76
...@@ -13,7 +13,7 @@ void C2048GameOverState::Init() ...@@ -13,7 +13,7 @@ void C2048GameOverState::Init()
if (!isInit) if (!isInit)
{ {
// Load in main menu title // Load in main menu title
ifstream fs("2048ScoreTitle.txt"); ifstream fs("2048GameOverTitle.txt");
fs.seekg(0, ios::end); fs.seekg(0, ios::end);
menuTitle.reserve(fs.tellg()); menuTitle.reserve(fs.tellg());
fs.seekg(0, ios::beg); fs.seekg(0, ios::beg);
...@@ -22,7 +22,7 @@ void C2048GameOverState::Init() ...@@ -22,7 +22,7 @@ void C2048GameOverState::Init()
istreambuf_iterator<char>()); istreambuf_iterator<char>());
// Set name for debugging // Set name for debugging
stateName = "2048score"; stateName = "2048gameover";
// Read in score data // Read in score data
highscores.LoadContainers(); highscores.LoadContainers();
...@@ -32,6 +32,7 @@ void C2048GameOverState::Init() ...@@ -32,6 +32,7 @@ void C2048GameOverState::Init()
// Set first input and draw the menu // Set first input and draw the menu
input = ' '; input = ' ';
scoreSet = false;
Draw(nullptr); Draw(nullptr);
} }
...@@ -52,25 +53,39 @@ void C2048GameOverState::Cleanup() ...@@ -52,25 +53,39 @@ void C2048GameOverState::Cleanup()
void C2048GameOverState::HandleEvents(CGameEngine* game) void C2048GameOverState::HandleEvents(CGameEngine* game)
{ {
while (game->newInput == false)
if (!scoreSet)
{ {
input = ' '; system("stty cooked");
cout << "Press enter to continue" << endl;
game->inputPause = true;
while (game->newInput == false) {}
highscores.userInputNameMakeScore(game->GetScore());
scoreSet = true;
system("stty raw");
game->inputPause = false;
} }
else
{
while (game->newInput == false) {}
input = game->input; input = game->input;
game->newInput = false; game->newInput = false;
}
} }
void C2048GameOverState::Update(CGameEngine* game) void C2048GameOverState::Update(CGameEngine* game)
{ {
switch(input) if (scoreSet)
{ {
case 'x': switch(input)
// Erase Scores {
break; case 'x':
default: game->PopState();
game->PopState(); break;
break; default:
break;
}
} }
} }
...@@ -79,8 +94,13 @@ void C2048GameOverState::Draw(CGameEngine* game) ...@@ -79,8 +94,13 @@ void C2048GameOverState::Draw(CGameEngine* game)
system("stty cooked"); system("stty cooked");
cout << menuTitle << endl; cout << menuTitle << endl;
cout << "\n\n";
highscores.displayScores(); highscores.displayScores();
highscores.updateFile();
system("stty raw"); system("stty raw");
} }
...@@ -19,7 +19,7 @@ class C2048GameOverState : public CGameState ...@@ -19,7 +19,7 @@ class C2048GameOverState : public CGameState
void Update(CGameEngine* game); void Update(CGameEngine* game);
void Draw(CGameEngine* game); void Draw(CGameEngine* game);
static C2048GameOverState* Instance() { return &m_2048ScoreState; } static C2048GameOverState* Instance() { return &m_2048GameOverState; }
protected: protected:
C2048GameOverState() {} C2048GameOverState() {}
...@@ -30,6 +30,7 @@ class C2048GameOverState : public CGameState ...@@ -30,6 +30,7 @@ class C2048GameOverState : public CGameState
string menuTitle; string menuTitle;
SL2048 highscores; SL2048 highscores;
bool isInit = false; bool isInit = false;
bool scoreSet = false;
}; };
......
=======================================================
|| _____ ____ ||
|| / ____| / __ \ ||
|| | | __ __ _ _ __ ___ ___ | | | |_ _____ _ __ ||
|| | | |_ |/ _` | '_ ` _ \ / _ \ | | | \ \ / / _ \ '__| ||
|| | |__| | (_| | | | | | | __/ | |__| |\ V / __/ | ||
|| \_____|\__,_|_| |_| |_|\___| \____/ \_/ \___|_| ||
|| ||
=======================================================
#include <iostream> #include <iostream>
#include "2048GameState.h" #include "2048GameState.h"
#include "2048GameOverState.h"
C2048GameState C2048GameState::m_2048GameState; C2048GameState C2048GameState::m_2048GameState;
...@@ -84,8 +85,11 @@ void C2048GameState::Update(CGameEngine* game) ...@@ -84,8 +85,11 @@ void C2048GameState::Update(CGameEngine* game)
// cout << " 2048GameState Update" << endl; // cout << " 2048GameState Update" << endl;
game2048->Shift(input); game2048->Shift(input);
if (!game2048->addNumber()) { if (!game2048->addNumber())
game->PopState(); {
// Push the score to the engine for temporary keeping
game->HoldScore(game2048->getScore());
game->ChangeState(C2048GameOverState::Instance());
} }
} }
......
...@@ -95,16 +95,4 @@ void CGameEngine::Draw() ...@@ -95,16 +95,4 @@ void CGameEngine::Draw()
cout << "\e[2J\e[0;0H"; cout << "\e[2J\e[0;0H";
states.back()->Draw(this); states.back()->Draw(this);
// system("stty raw"); // system("stty raw");
}
void CGameEngine::PushInput(char input)
{
inputQueue.push(input);
}
char CGameEngine::PopInput()
{
char input = inputQueue.front();
inputQueue.pop();
return input;
} }
\ No newline at end of file
...@@ -31,12 +31,15 @@ class CGameEngine ...@@ -31,12 +31,15 @@ class CGameEngine
void PushInput(char input); void PushInput(char input);
char PopInput(); char PopInput();
void HoldScore(int score) { this->score = score; }
int GetScore() { return score; }
bool newInput = false; bool newInput = false;
char input; char input;
bool inputPause = false;
private: private:
// Gamestate Stack // Gamestate Stack
vector<CGameState*> states; vector<CGameState*> states;
queue<char> inputQueue; int score;
// Currently running bool (false will initiate cleanup) // Currently running bool (false will initiate cleanup)
bool m_running; bool m_running;
......
...@@ -50,8 +50,10 @@ void MainInput(CGameEngine* game) ...@@ -50,8 +50,10 @@ void MainInput(CGameEngine* game)
while (game->isRunning()) while (game->isRunning())
{ {
// game.PushInput(reader.collectInput()); if (!game->inputPause)
game->input = reader.collectInput(); {
game->newInput = true; game->input = reader.collectInput();
game->newInput = true;
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment