-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameBase.cpp
executable file
·133 lines (122 loc) · 3.2 KB
/
gameBase.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
// gameBase.cpp
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <regex>
#include <iomanip> // std::setw
#include <memory> // shared_ptr
using namespace std;
#include "Lab4.h"
#include "GamePiece.h"
#include "GameBase.h"
GameBase::GameBase()
: dimX_a(), dimY_a(), pos_a(), display_length()
{}
shared_ptr<GameBase> GameBase::instance() {
if (GameBase::ptr != nullptr) {
return GameBase::ptr;
}
throw nullpointer_exception;
}
size_t GameBase::getDisplayLength()
{
size_t maxLength = 0;
for (unsigned int i = 0; i < pos_a.size(); ++i) {
if (maxLength < pos_a[i].display_.length()) {
maxLength = pos_a[i].display_.length();
}
}
return maxLength;
}
int GameBase::play() {
print();
int numTurns = 0;
while (true) {
int result = turn();
numTurns++;
if (done()) {
cout << "Congrats! It took you " << numTurns << " moves to finish." << endl;;
return success;
}
else if (stalemate()) {
cout << "Oh no! There's a stalemate! No valid moves remain. You made " << numTurns << " moves." << endl;
return failure_stalemate;
}
else if (result == failure_invalid_move) {
numTurns--;
cout << "You've made an invalid move!" << endl;
cout << endl;
}
else if (result == success) {
cout << numTurns << " move(s) made" << endl;
}
else {
numTurns--;
cout << "Looks like you quit. You made " << numTurns << " moves." << endl;
save();
return user_request_quit;
}
}
}
void GameBase::checkArguments(int argc, char *argv[]) {
// check that exactly one argument has been passed (i.e. "NineAlmondsGame")
if (GameBase::ptr != nullptr) {
throw overwrite_instance;
}
if (argc > 4 || argc == 1) { // incorrect
throw failure_command_line_arguments;
}
else {
if (!strcmp(argv[gameName], "NineAlmonds")) {
GameBase::ptr = make_shared<NineAlmondsGame>();
}
else if (!strcmp(argv[gameName], "MagicSquare")) {
GameBase::ptr = make_shared<MagicSquareGame>();
}
else if (!strcmp(argv[gameName], "Reversi")) {
if (argc != 4) {
throw failure_command_line_arguments;
}
string name1 = argv[2];
string name2 = argv[3];
GameBase::ptr = make_shared<ReversiGame>(name1, name2);
}
else {
throw incorrect_name;
}
}
}
int GameBase::prompt(unsigned int &x, unsigned int &y) {
while (true) {
cout << "(Type \"quit\" to end the game or enter a valid coordinate \"x,y\")" << endl;
string input;
if (cin >> input) {
if (input == "quit") {
return user_request_quit;
}
}
else {
return failure_invalid_move;
}
// utilized the following: https://solarianprogrammer.com/2011/10/12/cpp-11-regex-tutorial/
regex coordinate("[[:digit:]],[[:digit:]]");
if (!regex_match(input, coordinate)) {
cerr << "Badly formatted input. Does not match \"x,y\". Try again." << endl;
return failure_invalid_move;
}
input.replace(input.find(","), 1, " ");
istringstream i(input);
if (i >> x && i >> y) {
return success;
}
else
{
return failure_cannot_extract_coordinates;
}
}
}
shared_ptr<GameBase> GameBase::ptr = nullptr;