/** * checks if the peasant can produce * produce the unit and returns 0 if succesful and 1 if not * checks if the peasant can produce */ static int produceUnit(int x1, int y1, int x2, int y2, enum unitType type) { // only on the neighbouring field if(!isNeighbour(x1, y1, x2, y2)) return 1; // is it inside the board if(!insideBoard(x1, y1, globalBoardSize)) return 1; if(!insideBoard(x2, y2, globalBoardSize)) return 1; // no unit or it is not a peasant on (x1, y1) UnitsList peasant = findUnit(x1, y1); if (peasant == NULL || peasant->type != PEASANT) return 1; // has the unit moved last two turns if (actualTurnNumber - peasant->lastMove < 3) return 1; UnitsList secondUnit = findUnit(x2, y2); // is there any other unit on (x2, y2) if(secondUnit != NULL) return 1; addUnit(x2, y2, type); // updates the last move of the peasant peasant->lastMove = actualTurnNumber; return 0; }
/** * moves the unit and returns 0 * returns 1 if not possible */ int move(int x1, int y1, int x2, int y2) { // moves only to the neighbouring if(!isNeighbour(x1, y1, x2, y2)) return 1; // inside board if(!insideBoard(x1, y1, globalBoardSize)) return 1; if(!insideBoard(x2, y2, globalBoardSize)) return 1; UnitsList unitFirst = findUnit(x1, y1); // first unit does not exist if (unitFirst == NULL) return 1; // has the unit moved already if (unitFirst->lastMove == actualTurnNumber) return 1; // does the player move his own unit if (unitFirst->player != actualPlayer) return 1; UnitsList unitSecond = findUnit(x2, y2); // moving onto the own unit if(unitSecond != NULL && unitSecond->player == actualPlayer) return 1; // moving on the enemy unit or the empty field if (x1 <= 10 && y1 <= 10) { topleft[unitFirst->x][unitFirst->y] = '.'; } unitFirst->x = x2; unitFirst->y = y2; unitFirst->lastMove = actualTurnNumber; // unit first is on the field for sure int winner = 1; // starts the fight if there is an enemy unit if (unitSecond != NULL) winner = walka(unitFirst, unitSecond); if (x2 <= 10 && y2 <= 10) { if (winner == 0) topleft[x2][y2] = '.'; else if (winner == 1) topleft[x2][y2] = mark(unitFirst->type, unitFirst->player); else topleft[x2][y2] = mark(unitSecond->type, unitSecond->player); } return 0; }
bool Board::squareIs(int x, int y, SquareState squareState) { if (!insideBoard(x, y)) { return false; } return tBoard[x][y] == squareState; }
SquareState Board::getSquare(unsigned x, unsigned y) { if (!insideBoard(x, y)) { std::string error = "Board::getSquare : The position (" + to_string(x) + ", " + to_string(y) + ") is out of range. The size of the board is " + to_string(tSize); throw error; } return tBoard[x][y]; }
/** * Players initialization * returns 1 in case of error */ int init(int boardSize, int turnNumber, int player, int x1, int y1, int x2, int y2) { // checking which init is it if (liczbaInitow > 0) return 1; // distatnce between kings in the maximum norm at least 8 if (abs(x1 - x2) < 8 && abs(y1 - y2) < 8) return 1; // board size > 8 if (boardSize <= 8) return 1; // player number 1 or 2 if (player != 1 && player != 2) return 1; // turn number positive if (turnNumber < 1) return 1; // are the initial positions correct if(!insideBoard(x1, y1, boardSize)) return 1; if(!insideBoard(x1 + 3, y1, boardSize)) return 1; if(!insideBoard(x2, y2, boardSize)) return 1; if(!insideBoard(x2 + 3, y2, boardSize)) return 1; globalBoardSize = boardSize; gameTurnNumber = turnNumber; actualPlayer = 1; firstDistribution(x1, y1); actualPlayer = 2; firstDistribution(x2, y2); actualPlayer = 1; liczbaInitow++; return 0; }