-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
279 lines (248 loc) · 5.76 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "types.h"
#include "gamestate.h"
#include "moves.h"
#include "init.h"
#include "io.h"
#include <iostream>
#include <map>
using namespace std;
enum COMMANDS
{
unrecognized_command, new_game, display_screen, help, quit, move_command, search_command
};
static map<string, COMMANDS> MAP_COMMANDS =
{
{"newgame", new_game}, {"d", display_screen}, {"help", help}, {"quit", quit}, {"move", move_command}, {"search", search_command}
};
static map<char, int> char_suits = {
{'C', CLUBS}, {'S', SPADES}, {'D', DIAMONDS}, {'H', HEARTS}
};
static void Parse_help(vector<string> tokens) {
if (tokens.size() == 1) {
cout << "\nCommands:\n\"help\": Provides help information for commands.\n\"newgame\": Starts a new Klondike game.\n\"d\": Prints current game state.\n\"quit\": Closes game client.\n\"move\": Move card location.\n\n";
return;
}
switch (MAP_COMMANDS[tokens[1]])
{
case (quit):
{
cout << "Closes game client.\n\nquit\n\n";
break;
}
case (help):
{
cout << "Provides help information for commands.\n\nhelp command\n\n command - displays help information on that command.\n\n";
break;
}
case (display_screen):
{
cout << "Prints current game state.\n\nd\n\n";
break;
}
case (new_game):
{
cout << "Starts a new Klondike game.\n\nnewgame\n\n";
break;
}
case (move_command):
{
cout << "Move card location.\n\nmove operation [source] [destination]\n\n";
cout << " operation - type of move to be performed\n";
cout << " T move the stock card to the waste pile or turning over the stock.\n\n";
cout << " pp move a partial pile or card from one pile to another.\n";
cout << " -[source] location and number of partial pile to move.\n (Ex: 23, move 3 cards from the top of pile 2)\n";
cout << " -[destination] location of pile to move to.\n\n";
cout << " sp move a card from the stock to a pile, only destination is needed to be specified.\n";
cout << " -[destination] location of pile to move to.\n\n";
cout << " sf move a card from the stock to a foundation.\n\n";
cout << " pf move a card from a pile to a foundation.\n";
cout << " -[source] location of pile to move the card from. (Ex: 2, move a card from the top of pile 2)\n\n";
cout << " fp move a card from a foundation to a pile.\n";
cout << " -[destination] location of pile to move to.\n\n";
cout << " hint have the computer play a move for you.\n\n";
break;
}
case (unrecognized_command):
{
cout << "Unknown command\n";
break;
}
}
return;
}
void Parse_move(vector<string> tokens, GAMESTATE* gamestate) {
if (tokens.size() == 1) {
cout << "Invalid command size!\n";
}
else if (tokens.at(1) == "T") {
try {
if (!Turn_stock(&gamestate->stock)) {
cout << "Invalid move!\n";
}
else {
++gamestate->moves;
}
}
catch (...) {
cout << "Formatting error!\n";
}
}
else if (tokens.at(1) == "pp") {
try {
int input_index = stoi(tokens.at(2));
int pile_from = (input_index / 10) - 1;
int card_index = input_index % 10;
int pile_to = stoi(tokens.at(3)) - 1;
if (!Pile_to_pile(&gamestate->piles[pile_from], &gamestate->piles[pile_to], card_index)) {
cout << "Invalid move!\n";
}
else {
++gamestate->moves;
}
}
catch (...) {
cout << "Formatting error!\n";
}
}
else if (tokens.at(1) == "sp") {
try {
int pile_to = stoi(tokens.at(2)) - 1;
if (!Stock_to_pile(&gamestate->piles[pile_to], &gamestate->stock)) {
cout << "Invalid move!\n";
}
else {
++gamestate->moves;
}
}
catch (...) {
cout << "Formatting error!\n";
}
}
else if (tokens.at(1) == "sf") {
try {
if (!Stock_to_foundation(gamestate)) {
cout << "Invalid move!\n";
}
else {
++gamestate->moves;
}
}
catch (...) {
cout << "Formatting error!\n";
}
}
else if (tokens.at(1) == "pf") {
try {
int pile = stoi(tokens.at(2)) - 1;
if (!Pile_to_foundation(gamestate, pile)) {
cout << "Invalid move!\n";
}
else {
++gamestate->moves;
}
}
catch (...) {
cout << "Formatting error!\n";
}
}
else if (tokens.at(1) == "fp") {
try {
int pile = stoi(tokens.at(3)) - 1;
if (!Foundation_to_pile(&gamestate->piles[pile], &gamestate->foundations[pile], char_suits[tokens.at(2)[0]])) {
cout << "Invalid move!\n";
}
else {
++gamestate->moves;
}
}
catch (...) {
cout << "Formatting error!\n";
}
}
/*
else if (tokens.at(1) == "hint") {
if (Move_hint(gamestate)) {
++gamestate->moves;
}
else {
cout << "No moves found.\n";
}
}*/
else {
cout << "Invalid Command!\n";
}
return;
}
int main() {
Init_all();
cout << "Welcome to CGK, a command-line game tutorial by Nathanael Lu.\n";
cout << "Type \"help\" for command options.\n";
GAMESTATE gamestate;
bool gamestarted = false;
while (true) {
string input;
// get user / GUI input
if (!getline(cin, input))
{
// continue the loop
break;
}
// make sure input is available
if (!input.length())
{
// continue the loop
continue;
}
//Split the string into tokens to make it easier to work with
vector<string> tokens = split_command(input);
switch (MAP_COMMANDS[tokens[0]])
{
case (quit):
{
return 0;
}
case (help):
{
Parse_help(tokens);
break;
}
case (display_screen):
{
if (gamestarted) {
Print_gamestate(&gamestate);
}
else {
cout << "No game available!\n";
}
break;
}
case (new_game):
{
gamestate = Start_game();
gamestarted = true;
break;
}
case (move_command):
{
if (gamestarted) {
Parse_move(tokens, &gamestate);
}
else {
cout << "No game available!\n";
}
break;
}
case (search_command):
{
//Parse_search(input, &gamestate);
break;
}
case (unrecognized_command):
{
cout << "Unknown command\n";
break;
}
}
}
return 0;
}