-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
161 lines (134 loc) · 4.76 KB
/
main.cc
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 "util.h"
#include "player.h"
#include "pieces.h"
#include "board.h"
#include "gameio.h"
using namespace std;
int main(int argc, char **argv)
{
using std::cout;
// TODO TEST THIS OUT: cout << "\033[1;31mbold red text\033[0m\n" << endl;
cout << " " << endl;
cout << " #### " << endl;
cout << " ###### _____ _ _ ______ _____ _____ " << endl;
cout << " #### / ____| | | | ____|/ ____/ ____|" << endl;
cout << " ## | | | |__| | |__ | (___| (___ " << endl;
cout << " #### | | | __ | __| \\___ \\\\___ \\ " << endl;
cout << " ## | |____| | | | |____ ____) |___) |" << endl;
cout << " ###### \\_____|_| |_|______|_____/_____/ " << endl;
cout << " ###### " << endl;
cout << " ######## " << endl;
cout << "\nWelcome to Chess - By Imane, Amol & Ethan with <3!\n"
<< endl;
bool useGraphics = true;
bool useText = true;
bool useUnicode = false;
bool debug = false;
bool enhancedChecks = false;
std::vector<std::string> args;
for (int i = 1; i < argc; ++i)
{
args.emplace_back(argv[i]);
}
if (isInVector(args, "-d"))
{
cout << "[INFO] Debug output is enabled." << endl;
debug = true;
}
if (isInVector(args, "-ng"))
{
cout << "[INFO] Graphics display is disabled." << endl;
useGraphics = false;
}
if (isInVector(args, "-nt"))
{
if (useGraphics)
{
cout << "[INFO] Text display is disabled." << endl;
useText = false;
}
else
{
cout << "[WARNING] You have requested for both displays to be disabled. This makes the game unplayable, and thus the text display had been left enabled." << endl;
}
}
if (isInVector(args, "-u")) {
if (useText) {
cout << "[INFO] Unicode display is enabled." << endl;
useUnicode = true;
}
else {
cout << "[WARNING] You have requested for unicode display but have disabled the text display. This will not habe any effect." << endl;
}
}
if (isInVector(args, "-ec")) {
cout << "[INFO] Enhanced checks are enabled." << endl;
enhancedChecks = true;
}
cout << "Commands:\n"
<< "---------" << endl;
cout << "• 'game white-player black-player' starts a game with designated player types." << endl;
cout << "The parameters white-player and black-player can be either [human] or [computer[1-4]]\n"
<< endl;
string curLine; // current line from input
string cmd; // game - resign - player - setup - done
string whitePlayer, blackPlayer;
int whiteLevel, blackLevel; // 0 - 4 (0 for humans)
// create new board after [game] cmd
PlayerType whitePt;
PlayerType blackPt;
while (true)
{
getline(cin, curLine);
// handle fatal read fail
if (cin.fail())
{
outScore(0, 0); // we don't have enough info to create a gameBoard, output dummy values
return 0;
}
stringstream ss{curLine};
ss >> cmd;
// handle read fail
if (ss.fail())
{
cout << INVALID_COMMAND << endl;
continue;
}
const string lowerCmd = toLowerString(cmd);
if (lowerCmd == "game")
{ // try to obtain sufficient info to start the game
ss >> whitePlayer >> blackPlayer;
// handle read fail
if (ss.fail())
{
cout << INVALID_COMMAND << endl;
continue;
}
whitePt = strToPlayer(whitePlayer);
blackPt = strToPlayer(blackPlayer);
// handle invalid input
if (whitePt == PlayerType::NULL_PLR || blackPt == PlayerType::NULL_PLR)
{
cout << INVALID_COMMAND << endl;
continue; // try again -> return to beginning of outer loop
}
// retrieve Player level
whiteLevel = strToComputerLvl(whitePlayer);
blackLevel = strToComputerLvl(blackPlayer);
// we have all the information we need - we never need to return to this loop
// therefore break the loop and start the main game loop
break;
}
else
{
cout << INVALID_COMMAND << endl;
}
}
// create board and commence main gameplay loop
Board gameBoard{whitePt, whiteLevel, blackPt, blackLevel, useGraphics, useText, useUnicode, debug, enhancedChecks};
playGame(gameBoard);
// ctrl-d has been pressed:
const float whiteScore = gameBoard.getScore(Colour::White);
const float blackScore = gameBoard.getScore(Colour::Black);
outScore(whiteScore, blackScore);
}