-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameLogic.cpp
161 lines (131 loc) · 4.88 KB
/
gameLogic.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "gameLogic.h"
#include "JSONParser.h"
#include "Board.h"
#include "Stones.h"
#include <iostream>
#include <string>
namespace gameLogic
{
std::string placeStonePlayer(JSONParser &eraJSON, Board &eraBoard, Stones &eraStones, std::string generatedStone)
{
const short int KEY = 0;
const short int VALUE = 1;
std::string playerChosenString;
std::cout << "placing stone" << std::endl; //mozna odmazat
//getline because std::cin would stop on space otherwise
std::getline(std::cin, playerChosenString);
eraJSON.setInput(playerChosenString);
if (!eraJSON.parse())
{
std::cerr << "JSON parsing error occured, your input is not correct." << std::endl;
exit(EXIT_FAILURE);
}
if (eraJSON.getResult().size() > 2)
{
std::cerr << "More than two key : value JSON inputs, not possible in this application." << std::endl;
exit(EXIT_FAILURE);
}
//check if chosen_stone KEY was entered
std::string stoneValue, fieldValue;
if (std::get<KEY>(eraJSON.getResult().at(0)) == "stone")
stoneValue = std::get<VALUE>(eraJSON.getResult().at(0));
else if (std::get<KEY>(eraJSON.getResult().at(1)) == "stone")
stoneValue = std::get<VALUE>(eraJSON.getResult().at(1));
else
{
std::cerr << "Input: chosen_stone as JSON Key is not correct." << std::endl;
exit(EXIT_FAILURE);
}
//check if field KEY was entered
if (std::get<KEY>(eraJSON.getResult().at(0)) == "field")
fieldValue = std::get<VALUE>(eraJSON.getResult().at(0));
else if (std::get<KEY>(eraJSON.getResult().at(1)) == "field")
fieldValue = std::get<VALUE>(eraJSON.getResult().at(1));
else
{
std::cerr << "Input: Field as JSON Key is not correct." << std::endl;
exit(EXIT_FAILURE);
}
//once more inputed stone (for control) is not the same
if (stoneValue != generatedStone)
{
std::cerr << "Repeated stone is not the same as previous chosen one." << std::endl;
exit(EXIT_FAILURE);
}
//check if input field value is correct one in range of <1;4>
else if (!eraJSON.checkIfFieldString(fieldValue))
{
std::cerr << "Field value is not in range of <1;4>." << std::endl;
exit(EXIT_FAILURE);
}
//check if input field value is free to use
if (!eraBoard.checkIfFieldFree(fieldValue))
{
std::cerr << "Chosen field is not free." << std::endl;
exit(EXIT_FAILURE);
}
//everything is ok
//place chosen stone on chosen field
eraBoard.placeStone(stoneValue, fieldValue);
eraStones.reduceFreeStones(stoneValue);
eraBoard.reduceFreeFields(fieldValue);
//for gameStatus function, it does not check the whole matrix (not necessary)
return fieldValue;
}
std::string placeStoneAI(Board &eraBoard, Stones &eraStones, std::string stoneValue)
{
std::string fieldValue = eraBoard.generateRandomField();
std::cout << "placing stone" << std::endl;
std::cout << "{\"stone\": " << "\"" << stoneValue << "\", \"field\": " << fieldValue << "}\n";
eraBoard.placeStone(stoneValue, fieldValue);
eraStones.reduceFreeStones(stoneValue);
eraBoard.reduceFreeFields(fieldValue);
//for gameStatus function, it does not check the whole matrix (not necessary)
return fieldValue;
}
std::string chooseStonePlayer(JSONParser &eraJSON, Stones &eraStones)
{
std::cout << "giving chosen stone" << std::endl;
std::string playerChosenString;
//getline because std::cin would stop on space otherwise
std::getline(std::cin, playerChosenString);
const short int KEY = 0;
const short int VALUE = 1;
eraJSON.setInput(playerChosenString);
if (!eraJSON.parse())
{
std::cerr << "JSON parsing error occured, your input is not correct." << std::endl;
exit(EXIT_FAILURE);
}
//more than one JSON KEY input, not possible this time
else if (eraJSON.getResult().size() > 1)
{
std::cerr << "More than two key : value JSON inputs, not possible in this application." << std::endl;
exit(EXIT_FAILURE);
}
else if (std::get<KEY>(eraJSON.getResult().at(0)) != "chosen_stone")
{
std::cerr << "Input: chosen_stone as JSON Key is not correct." << std::endl;
exit(EXIT_FAILURE);
}
else if (!eraJSON.checkIfBinaryString(std::get<VALUE>(eraJSON.getResult().at(0))))
{
std::cerr << "Stone value is not binary string." << std::endl;
exit(EXIT_FAILURE);
}
else if (!eraStones.checkIfFreeStone(std::get<VALUE>(eraJSON.getResult().at(0))))
{
std::cerr << "Stone value has already been used. This is not free stone." << std::endl;
exit(EXIT_FAILURE);
}
return (std::get<VALUE>(eraJSON.getResult().at(0)));
}
std::string chooseStoneAI(Stones &eraStones)
{
std::string generatedStone;
std::cout << "giving chosen stone" << std::endl; //mozna odmazat
generatedStone = eraStones.generateRandomStone();
std::cout << "{\"chosen_stone\": \"" << generatedStone << "\"}\n";
return generatedStone;
}
}