|
@@ -8,8 +8,13 @@ std::array<Vector, 8> Game::neighbours;
|
|
std::array<Vector, 9 * 5> Game::fieldVectors;
|
|
std::array<Vector, 9 * 5> Game::fieldVectors;
|
|
|
|
|
|
Game::Game() : active(-1, -1), mustTake(false) {
|
|
Game::Game() : active(-1, -1), mustTake(false) {
|
|
|
|
+ // sets the basic game up:
|
|
|
|
+ // 0 - start game
|
|
|
|
+ // 1 - player vs ai
|
|
|
|
+ // 0 - player starts
|
|
std::cout << "0\n1\n0\n";
|
|
std::cout << "0\n1\n0\n";
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ // allows for each neighbor iterating
|
|
neighbours[0].set(1, 0);
|
|
neighbours[0].set(1, 0);
|
|
neighbours[1].set(0, 1);
|
|
neighbours[1].set(0, 1);
|
|
neighbours[2].set(-1, 0);
|
|
neighbours[2].set(-1, 0);
|
|
@@ -19,6 +24,7 @@ Game::Game() : active(-1, -1), mustTake(false) {
|
|
neighbours[6].set(1, -1);
|
|
neighbours[6].set(1, -1);
|
|
neighbours[7].set(-1, -1);
|
|
neighbours[7].set(-1, -1);
|
|
|
|
|
|
|
|
+ // allows for each for all fields
|
|
uint index = 0;
|
|
uint index = 0;
|
|
for(uint y = 0; y < 5; y++) {
|
|
for(uint y = 0; y < 5; y++) {
|
|
for(uint x = 0; x < 9; x++) {
|
|
for(uint x = 0; x < 9; x++) {
|
|
@@ -30,19 +36,24 @@ Game::Game() : active(-1, -1), mustTake(false) {
|
|
void Game::readLine() {
|
|
void Game::readLine() {
|
|
String line;
|
|
String line;
|
|
while(true) {
|
|
while(true) {
|
|
|
|
+ // read input by each character
|
|
char c = std::cin.get();
|
|
char c = std::cin.get();
|
|
if(c == '\n') {
|
|
if(c == '\n') {
|
|
|
|
+ // parse the command if the line ended
|
|
if(parseLine(line)) {
|
|
if(parseLine(line)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+ // clear the buffer for the next command
|
|
line.clear();
|
|
line.clear();
|
|
} else if(c != '\r') {
|
|
} else if(c != '\r') {
|
|
|
|
+ // line carriages are ignored
|
|
line.append(c);
|
|
line.append(c);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
bool Game::parseLine(const String& line) {
|
|
bool Game::parseLine(const String& line) {
|
|
|
|
+ // identify when the server expects an answer or a new game field is available
|
|
if(line == " 0 1 2 3 4 5 6 7 8") {
|
|
if(line == " 0 1 2 3 4 5 6 7 8") {
|
|
fields.read();
|
|
fields.read();
|
|
fields.print(active);
|
|
fields.print(active);
|
|
@@ -53,9 +64,11 @@ bool Game::parseLine(const String& line) {
|
|
} else if(line == "Please enter wether you want to Withdraw or Approach [W/A]") {
|
|
} else if(line == "Please enter wether you want to Withdraw or Approach [W/A]") {
|
|
takeStone();
|
|
takeStone();
|
|
} else if(line == "Do you want to continue with your turn [Y/N]?") {
|
|
} else if(line == "Do you want to continue with your turn [Y/N]?") {
|
|
|
|
+ // we always want to continue if possible
|
|
std::cout << "Y\n";
|
|
std::cout << "Y\n";
|
|
} else if(line == "************************Player 1 won!**********************" ||
|
|
} else if(line == "************************Player 1 won!**********************" ||
|
|
line == "************************Player 2 won!**********************") {
|
|
line == "************************Player 2 won!**********************") {
|
|
|
|
+ // stop the game if anybody wins - the server does not do that
|
|
std::cerr << line << "\n";
|
|
std::cerr << line << "\n";
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -67,10 +80,12 @@ bool Game::isInRange(const Vector& v) const {
|
|
}
|
|
}
|
|
|
|
|
|
bool Game::areNeighbors(const Vector& from, const Vector& to) const {
|
|
bool Game::areNeighbors(const Vector& from, const Vector& to) const {
|
|
|
|
+ // fields are not neighbors with themselves
|
|
if(from == to) {
|
|
if(from == to) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
Vector diff = from - to;
|
|
Vector diff = from - to;
|
|
|
|
+ // fields with a bigger distance than one cannot be neighbors
|
|
if(diff.x < -1 || diff.x > 1 || diff.y < -1 || diff.y > 1) {
|
|
if(diff.x < -1 || diff.x > 1 || diff.y < -1 || diff.y > 1) {
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -83,11 +98,14 @@ bool Game::areNeighbors(const Vector& from, const Vector& to) const {
|
|
}
|
|
}
|
|
|
|
|
|
uint Game::removeLine(const Vector& from, const Vector& to, Fields::State state) {
|
|
uint Game::removeLine(const Vector& from, const Vector& to, Fields::State state) {
|
|
|
|
+ // walk along the line and removes opponent stones
|
|
Vector current = to;
|
|
Vector current = to;
|
|
Vector unit = to - from;
|
|
Vector unit = to - from;
|
|
|
|
+ // counter for removed stones
|
|
uint rank = 0;
|
|
uint rank = 0;
|
|
while(true) {
|
|
while(true) {
|
|
current += unit;
|
|
current += unit;
|
|
|
|
+ // stop removing on wrong stones or the end of the game field
|
|
if(!isInRange(current) || !fields.hasState(current, state)) {
|
|
if(!isInRange(current) || !fields.hasState(current, state)) {
|
|
return rank;
|
|
return rank;
|
|
}
|
|
}
|
|
@@ -97,6 +115,7 @@ uint Game::removeLine(const Vector& from, const Vector& to, Fields::State state)
|
|
}
|
|
}
|
|
|
|
|
|
uint Game::getRank(const Vector& from, const Vector& to, Fields::State state) const {
|
|
uint Game::getRank(const Vector& from, const Vector& to, Fields::State state) const {
|
|
|
|
+ // same as removeLine but the removable stones are only counted
|
|
Vector current = to;
|
|
Vector current = to;
|
|
Vector unit = to - from;
|
|
Vector unit = to - from;
|
|
uint rank = 0;
|
|
uint rank = 0;
|