-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameState.h
More file actions
70 lines (59 loc) · 1.66 KB
/
gameState.h
File metadata and controls
70 lines (59 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef GAME_STATE_H
#define GAME_STATE_H
#include "action.h"
#include <string>
#include <unordered_map>
using namespace std;
struct GameState{
bool win = false;
bool gameOver = false;
int score = 0;
Action lastInput = Action::NONE;
// Initiate the 3 status lines
string status1 = "0";
string status2 = "0";
string status3 = "0";
bool appleEatenThisTick = false; // For snake
private:
// Initiate the map that will hold the values
unordered_map<string, int> statusMap;
// Initial command to name the statuses
void bindStatus(const string &name, int slot){
if(slot >= 1 && slot <= 3){
statusMap[name] = slot;
}
}
string &getSlot(const string &name){
int slot = statusMap[name]; // assumes name was bound
if(slot == 1) return status1;
if(slot == 2) return status2;
return status3;
}
public:
friend class GameEngine;
// Setters
void setStatus(const string &name, int v){
getSlot(name) = to_string(v);
}
void setStatus(const string &name, float v){
getSlot(name) = to_string(v);
}
void setStatus(const string &name, bool v){
getSlot(name) = v ? "true" : "false";
}
void setStatus(const string &name, char v){
getSlot(name) = string(1, v);
}
void setStatus(const string &name, const string &v){
getSlot(name) = v;
}
// Helper to find the key for a slot to print it
const string *getStatusKeyForSlot(int slot) const {
for(const auto &p:statusMap){
if(p.second == slot)
return &p.first;
}
return nullptr;
}
};
#endif