Commit 51a81155 authored by Konrad McClure's avatar Konrad McClure
Browse files

Added main menu, currently can select 2048 (successfully) and Snake (nothing there)

parent 8f728c2e
......@@ -11,7 +11,7 @@ void Display::number (int value) {
}
void Display::showGame(const vector<vector<int>>& board, const int& score, const int& highScore) {
cout << setw(64) << "For help press: h" << endl << endl;
cout << endl << setw(64) << "For help press: h" << endl << endl;
showScore(score, highScore, board.size());
for (const vector<int>& colums: board ) {
for(int value: colums){
......
......@@ -43,6 +43,8 @@ void Game2048::Shift(char input){
case 'd':
movement.right(*board, score);
break;
default:
break;
}
}
......
......@@ -6,7 +6,7 @@ C2048GameState C2048GameState::m_2048GameState;
void C2048GameState::Init()
{
cout << " 2048GameState Init" << endl;
// cout << " 2048GameState Init" << endl;
int highScore = 1000;
game2048 = new Game2048(4, highScore);
game2048->testInit();
......@@ -15,24 +15,24 @@ void C2048GameState::Init()
void C2048GameState::Pause()
{
cout << " 2048GameState Pause" << endl;
//cout << " 2048GameState Pause" << endl;
}
void C2048GameState::Resume()
{
cout << " 2048GameState Resume" << endl;
//cout << " 2048GameState Resume" << endl;
}
void C2048GameState::Cleanup()
{
cout << " 2048GameState Cleanup" << endl;
//cout << " 2048GameState Cleanup" << endl;
delete game2048;
game2048 = nullptr;
}
void C2048GameState::HandleEvents(CGameEngine* game)
{
cout << " 2048GameState Handle Events" << endl;
// cout << " 2048GameState Handle Events" << endl;
// switch (runtimes)
// {
......@@ -61,11 +61,19 @@ void C2048GameState::HandleEvents(CGameEngine* game)
// break;
// }
input = game->PopInput();
while (game->newInput == false)
{
if (game->newInput)
{
break;
}
}
input = game->input;
game->newInput = false;
if (input == 'q')
{
game->Quit();
game->PopState();
}
runtimes++;
......@@ -73,7 +81,7 @@ void C2048GameState::HandleEvents(CGameEngine* game)
void C2048GameState::Update(CGameEngine* game)
{
cout << " 2048GameState Update" << endl;
// cout << " 2048GameState Update" << endl;
game2048->Shift(input);
if (!game2048->addNumber()) {
......@@ -83,7 +91,9 @@ void C2048GameState::Update(CGameEngine* game)
void C2048GameState::Draw(CGameEngine* game)
{
cout << " 2048GameState Draw" << endl;
// cout << " 2048GameState Draw" << endl;
system("stty cooked");
game2048->Draw();
system("stty raw");
}
......@@ -92,8 +92,9 @@ void CGameEngine::Update()
void CGameEngine::Draw()
{
system("stty cooked");
cout << "\e[2J\e[0;0H";
states.back()->Draw(this);
system("stty raw");
// system("stty raw");
}
void CGameEngine::PushInput(char input)
......
......@@ -31,6 +31,8 @@ class CGameEngine
void PushInput(char input);
char PopInput();
bool newInput = false;
char input;
private:
// Gamestate Stack
vector<CGameState*> states;
......
#include <iostream>
#include "MainMenuState.h"
// #include "TestState2.h"
// #include "TestState3.h"
#include "2048GameState.h"
#include "SnakeState.h"
#include <string>
#include <fstream>
#include <streambuf>
using namespace std;
CMainMenuState CMainMenuState::m_MainMenuState;
void CMainMenuState::Init()
{
cout << " MainMenuState Init" << endl;
// Load in main menu title
ifstream fs("mainmenu.txt");
fs.seekg(0, ios::end);
menuTitle.reserve(fs.tellg());
fs.seekg(0, ios::beg);
menuTitle.assign((istreambuf_iterator<char>(fs)),
istreambuf_iterator<char>());
// Add menu options
menuOptions.push_back("2048");
menuOptions.push_back("Snake");
menuOptions.push_back("Quit");
stateName = "MainMenuState";
input = ' ';
Draw(nullptr);
}
void CMainMenuState::Pause()
{
cout << " MainMenuState Pause" << endl;
}
void CMainMenuState::Resume()
{
cout << " MainMenuState Resume" << endl;
input = 0;
menuPos = 0;
}
void CMainMenuState::Cleanup()
{
cout << " MainMenuState Cleanup" << endl;
}
void CMainMenuState::HandleEvents(CGameEngine* game)
{
cout << " MainMenuState Handle Events" << endl;
while (game->newInput == false)
{
input = ' ';
}
input = game->input;
game->newInput = false;
}
void CMainMenuState::Update(CGameEngine* game)
{
cout << " MainMenuState Update" << endl;
switch(input)
{
case 'w':
menuPos--;
break;
case 's':
menuPos++;
break;
case 'd':
{
switch (menuPos)
{
case 0: //2048
game->PushState(C2048GameState::Instance());
break;
case 1: //Snake
game->PushState(CSnakeState::Instance());
break;
case 2: //Quit
game->Quit();
break;
}
break;
}
default:
break;
}
if (menuPos < 0)
{
menuPos = 0;
}
game->ChangeState(C2048GameState::Instance());
if (menuPos >= menuOptions.size())
{
menuPos = menuOptions.size() - 1;
}
}
void CMainMenuState::Draw(CGameEngine* game)
{
cout << " MainMenuState Draw" << endl;
system("stty cooked");
cout << menuTitle << endl;
for (int i = 0; i < menuOptions.size(); i++)
{
if (i == menuPos)
{
cout << " * " << menuOptions.at(i) << endl;
}
else
{
cout << " " << menuOptions.at(i) << endl;
}
}
system("stty raw");
}
......@@ -3,6 +3,7 @@
#include "GameState.h"
#include <iostream>
#include <string>
class CMainMenuState : public CGameState
{
......@@ -24,7 +25,10 @@ class CMainMenuState : public CGameState
private:
static CMainMenuState m_MainMenuState;
int runtimes = 0;
char input;
int menuPos;
string menuTitle;
vector<string> menuOptions;
};
#endif
\ No newline at end of file
g++ -std=c++11 -pthread *.cpp ../2048/*.cpp ../Testings/Snake/ReadInput.cpp
./a.out
\ No newline at end of file
#include "GameEngine.h"
#include "TestState1.h"
#include "../Testings/Nelson's YEET/ReadInput.h"
#include "MainMenuState.h"
#include "../Testings/Snake/ReadInput.h"
#include <thread>
#include <iostream>
// Primary Loop to be threaded
void MainLoop(CGameEngine game);
void MainLoop(CGameEngine *game);
void MainInput(CGameEngine *game);
int main()
{
CGameEngine game;
CGameEngine* game = new CGameEngine;
game.Init();
game->Init();
game.ChangeState(CTestState1::Instance());
game->ChangeState(CMainMenuState::Instance());
// while (game.isRunning())
// {
......@@ -22,30 +24,34 @@ int main()
// }
thread gameloop(MainLoop, game);
thread inputloop(MainInput, game);
gameloop.join();
inputloop.join();
game.Cleanup();
game->Cleanup();
return 0;
}
// Primary Loop to be threaded
void MainLoop(CGameEngine game)
void MainLoop(CGameEngine* game)
{
while (game.isRunning())
while (game->isRunning())
{
game.HandleEvents();
game.Update();
game.Draw();
game->HandleEvents();
game->Update();
game->Draw();
}
}
void MainInput(CGameEngine game)
void MainInput(CGameEngine* game)
{
ReadInput reader;
while (game.isRunning())
while (game->isRunning())
{
game.PushInput(reader.collectInput());
// game.PushInput(reader.collectInput());
game->input = reader.collectInput();
game->newInput = true;
}
}
\ No newline at end of file
=============================================================
|| _____ _____ _ ||
|| / ____| / ____| | | ||
|| | | __ __ _ _ __ ___ ___| | ___ _ __ | |_ ___ _ __ ||
|| | | |_ |/ _` | '_ ` _ \ / _ \ | / _ \ '_ \| __/ _ \ '__| ||
|| | |__| | (_| | | | | | | __/ |___| __/ | | | || __/ | ||
|| \_____|\__,_|_| |_| |_|\___|\_____\___|_| |_|\__\___|_| ||
|| ||
=============================================================
\ No newline at end of file
#include <iostream>
#include <stdio.h>
#include "ReadInput.h"
using namespace std;
char ReadInput::collectInput (){
system("stty raw");
......
Supports Markdown
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