QList<QPoint> MineLocker::checkAroundMinesForLock(const QPoint &openedNearMinePos) { QList<QPoint> minesPosForLock; int numberOfOpened = 0; auto aroundPositions = Utility::getAroundPositions(openedNearMinePos); for (auto &aroundPos : aroundPositions) { auto &piece = getPiece(aroundPos); if (piece->isMine()) { if (!piece->isLock()) minesPosForLock << aroundPos; } else { if (piece->isOpen()) ++numberOfOpened; } } if (numberOfOpened != 8 - getPiece(openedNearMinePos)->numberOfAroundMines()) return QList<QPoint>(); for (auto &minePos : minesPosForLock) getPiece(minePos)->lock(); return minesPosForLock; }
void Board::setColors(int c) { getPiece(0, c)->setColor('B'); getPiece(1, c)->setColor('B'); getPiece(6, c)->setColor('W'); getPiece(7, c)->setColor('W'); }
void Board::evaluateMatches() { m_Matches = 0; m_MissingPieces = 0; for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { if (getPiece(i,j) == g_PIECES[NO_PIECE]) { m_MissingPieces++; continue; } if (j > 0) { if (getPiece(i,j-1)->getRight() == getPiece(i,j)->getLeft()) m_Matches++; } if (i > 0) { if (getPiece(i-1,j)->getBottom() == getPiece(i,j)->getTop()) m_Matches++; } } } }
bool ChessBoard::validateKingMove( Move move ) { int dif_x = std::abs( move.to_x - move.from_x ); int dif_y = std::abs( move.to_y - move.from_y ); Piece* king = getPiece( move.from_x, move.from_y ); if( dif_x == 2 && dif_y == 0 ) { Piece* rook; if( move.to_x != 2 ) { rook = getPiece( move.to_x + 1, move.to_y); if( rook != NULL && rook->nrOfMoves == 0 && king->nrOfMoves == 0) { if( checkHorisontalOrVerticalLine( Move( move.from_x, move.from_y, move.to_x+1, move.to_y) ) ) { return true; } } } else { rook = getPiece( move.to_x - 2, move.to_y ); if( rook != NULL && rook->nrOfMoves == 0 && king->nrOfMoves == 0) { if( checkHorisontalOrVerticalLine( Move( move.from_x, move.from_y, move.to_x-2, move.to_y) ) ) { return true; } } } } return (dif_x <= 1 && dif_y <= 1 ); }
/** * @brief Permet de vérifier si la pièce peut bouger, et se déplace si oui * @param[in] colonnneO la colonne d'origine de la pièce * @param[in] ligneO la ligne d'origine de la pièce * @param[in] colonne la colonne où la pièce doit se déplacer * @param[in] ligne la ligne où la pièce doit se déplacer * @param[in] camp le camp de la pièce * @return un booléen pour savoir si le mouvement est possible * et si la pièce a été déplacée * @see bool Stratego::deplacementPossible(char colonneO, int ligneO, * char colonne, int ligne) const */ bool Stratego::mouvement(char colonneO, int ligneO, char colonne, int ligne, int camp) { char piece = getPiece(colonneO, ligneO); if (!estPieceDe(piece, camp) || !estDansTerrain(colonne, ligne)) return (false); if (piece == BOMBE || piece == DRAPEAU || piece == BOMBE+MINUS || piece == DRAPEAU+MINUS) return (false); if (!deplacementPossible(colonneO, ligneO, colonne, ligne)) return (false); char pieceDest = getPiece(colonne, ligne); if (pieceDest != VIDE) { if(!campsOpposes(piece, pieceDest)) return (false); else { combat(colonneO, ligneO, colonne, ligne); return (true); } } else { // Case destination vide put(colonne, ligne, piece); put(colonneO, ligneO, VIDE); } return (true); }
bool ChessBoard::validatePawnMove( Move move ) { Piece* pawn = getPiece( move.from_x, move.from_y ); PieceColor pawn_color = pawn->color; int dif_x = std::abs( move.to_x - move.from_x ); int dif_y = std::abs( move.to_y - move.from_y ); /* check if the pawn is moving the right way*/ if( !validatePawnVerticalDirection( pawn_color, move.to_y, move.from_y ) ) { return false; } if( dif_y == 2 ) { if( move.from_y != 6 && move.from_y != 1 ) return false; if( !checkHorisontalOrVerticalLine( move ) ) return false; if( getPiece( move.to_x, move.to_y ) != NULL ) return false; /* should add ampasang rules */ } else if( dif_y == 1 ) { if( dif_x > 1 ) return false; if( dif_x == 1 ) { if ( getPiece(move.to_x, move.to_y ) == NULL ) { if( moves.size() != 0 ) { Move prevMove = moves.top(); Piece* piece = getPiece( prevMove.to_x, prevMove.to_y ); if( ( piece->type != PAWN) ) { return false; } if( std::abs( prevMove.from_y - prevMove.to_y ) != 2 || prevMove.to_x != prevMove.from_x ) { return false; } if( std::abs( prevMove.to_y - move.to_y ) > 1 ) return false; } } } else { return ( !(getPiece(move.to_x, move.to_y ) != NULL) ); } } else { return false; } return true; }
Cell* getPossibleMovesPawn(ChessBoard* board,ChessPiece* piece, int* movinc)//was originally reference here { Cell pos = getPosition(piece); Cell kingStartpos = getStartPosition(getPieces(getColor(piece),board)[0]); bool pawnMovesUp = kingStartpos.rowum == 0; int rowinc = (pawnMovesUp ? 1 : -1); int cellsFront = (abs(kingStartpos.rowum - pos.rowum) < 2 ? 2 : 1); Cell* moves=(Cell*) malloc(sizeof(Cell)*6); ChessPiece * pieces; int row = pos.rowum + rowinc; int i=0; Cell cell; cell.colum=pos.colum; cell.rowum=row; while(i<cellsFront) { if (row > 7 || row < 0) return moves; cell.rowum=row; pieces = getPiece(pos.colum, row); if (pieces != 0) break; if(!isCheckOnMove((board), piece, pos, cell)) moves[(*movinc)++]=cell; row += rowinc; i++; } //this checks if a pawn can move diagonal if (pos.colum != 0 && pos.rowum + rowinc < 8 && pos.rowum + rowinc > -1) { pieces = getPiece(pos.colum - 1, pos.rowum + rowinc); if (pieces != 0 && getColor(pieces) != getColor(piece) && !isCheckOnMove((board), piece, pos, getPosition(pieces))) { moves[(*movinc)++]=(getPosition(pieces)); } } if (pos.colum != 7 && pos.rowum + rowinc < 8 && pos.rowum + rowinc > -1) { pieces = getPiece(pos.colum + 1, pos.rowum + rowinc); if (pieces != 0 && getColor(pieces) != getColor(piece) && !isCheckOnMove((board), piece, pos, getPosition(pieces))) { moves[(*movinc)++]=(getPosition(pieces)); } } return moves; }
char* Board::writeBoardToFile(char *filename) { string name = filename; string tryName = ""; int fileIndex = 1; char indexBuf[10]; sprintf(indexBuf, "%d", fileIndex); tryName = name + indexBuf + ".lay"; while (fileExists(tryName)) { fileIndex++; sprintf(indexBuf, "%d", fileIndex); tryName = name + indexBuf + ".lay"; } name = tryName; ofstream myfile; myfile.open(name.c_str()); for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { char id [10]; char rotation [10]; if (j > 0) { myfile << "\t"; } //itoa(getPiece(i, j)->getID(), id, 10); sprintf(id, "%d", getPiece(i, j)->getID()); //itoa(getPiece(i, j)->getRotation(), rotation, 10); sprintf(rotation, "%d", getPiece(i, j)->getRotation()); myfile << id; myfile << "\t"; myfile << rotation; } myfile << "\n"; } char matches [10]; char missing [10]; //itoa(m_Matches, matches, 10); sprintf(matches, "%d", m_Matches); //itoa(m_MissingPieces, missing, 10); sprintf(missing, "%d", m_MissingPieces); myfile << "Matches: "; myfile << matches; myfile << "\nMissingPieces: "; myfile << missing; myfile << "\n"; myfile.close(); printf("Writing to: %s\n", name.c_str()); return ""; //name.c_str(); }
void getMovesStraight(ChessBoard* board, ChessPiece* pieces, Cell* moves, bool vertical, bool rightOrUp, int* counter) { Cell ownPos = getPosition(pieces); int col = ownPos.colum; int row = ownPos.rowum; ChessPiece * piece; Cell cell; if (vertical) { int rowInc = (rightOrUp ? 1 : -1); row += rowInc; while((rightOrUp ? (row < 8) : (row > -1))) { piece = getPiece(col, row); cell.colum=col; cell.rowum=row; if (piece == 0 && !isCheckOnMove((board), pieces, ownPos, cell)) { moves[(*counter)++]=cell; } else if (piece != 0) { if (getColor(piece) != getColor(pieces) && !isCheckOnMove((board), pieces, ownPos, cell)) moves[(*counter)++]=cell; return; } row += rowInc; } } else { int colInc = (rightOrUp ? 1 : -1); col += colInc; while((rightOrUp ? (col < 8) : (col > -1))) { piece = getPiece(col, row); cell.rowum=row; cell.colum=col; if (piece == 0 && !isCheckOnMove((board), pieces, ownPos, cell)) { moves[(*counter)++]=cell; } else if (piece != 0) { if (getColor(piece) != getColor(pieces) && !isCheckOnMove(board, pieces, ownPos, cell)) { moves[(*counter)++]=cell; } return; } col += colInc; } } } // end of method getMovesStraight(...)
/** * @brief Déplace une pièce appartenant à colonneO, ligneO sur colonne, ligne * @param[in] colonneO la colonne d'origine de la pièce * @param[in] ligneO la ligne d'origine de la pièce * @param[in] colonne la colonne où la pièce doit se déplacer * @param[in] ligne la ligne où la pièce doit se déplacer * @return un booléen pour savoir si la pièce a bien été déplacée */ bool Stratego::deplacer(char colonneO, int ligneO, char colonne, int ligne) { char piece = getPiece(colonneO, ligneO); if ((estDansCamp(colonneO, ligneO, NORD) && estDansCamp(colonne, ligne,NORD) && estPieceDe(piece, NORD)) || (estDansCamp(colonneO, ligneO, SUD) && estDansCamp(colonne,ligne,SUD) && estPieceDe(piece, SUD) )) { char pieceO = getPiece(colonne, ligne); put(colonne, ligne, piece); put(colonneO, ligneO, pieceO); return (true); } return (false); }
/** @param piece The piece to test. @param x The X coordinate to move the piece to. @param y The Y coordinate to move the piece to. */ bool Board::resultsInCheck(Piece* piece, int x, int y) { bool check = false; Position oldPos = piece->getPos(); Piece* capturedPiece = getPieceAt(x, y); piece->setPos(x, y); // Find the kings position Position kingPos; kingPos = getPiece(KING, piece->getColor())->getPos(); if(capturedPiece != NULL) capturedPiece->setCaptured(true); // Find out if the king gets pinned for(auto iter = mPieces.begin(); iter != mPieces.end(); iter++) { Piece* p = (*iter); if(p->getColor() != piece->getColor() && p->pinning(kingPos.x, kingPos.y) && !p->getCaptured()) { check = true; break; } } if(capturedPiece != NULL) capturedPiece->setCaptured(false); piece->setPos(oldPos); return check; }
std::vector<Piece*> ChessBoard::findThreatheningPieces( Piece* p ) { std::vector<Piece*> pieces; pieces.push_back( NULL ); std::vector<Piece*> toCheck; if( p->color == WHITE ) { toCheck = blackPieces; } else { toCheck = whitePieces; } for( std::vector<Piece*>::iterator it = toCheck.begin(); it != toCheck.end(); ++it ) { if( (*it)->inPlay ) { Move m((*it)->x, (*it)->y, p->x, p->y); bool b = makeMove((*it)->x, (*it)->y, p->x, p->y); if( b ){ goBackAMove(); pieces.push_back( getPiece( (*it)->x, (*it)->y ) ); } } } return pieces; }
bool ChessBoard::checkIfCanMoveTo( PieceColor color, int to_x, int to_y ) { std::vector<Piece*> toCheck; if( color == WHITE ) { toCheck = whitePieces; } else { toCheck = blackPieces; } for( std::vector<Piece*>::iterator it = toCheck.begin(); it != toCheck.end(); ++it ) { if( (*it)->inPlay ) { Move m((*it)->x, (*it)->y, to_x, to_y); Piece* piece = getPiece( (*it)->x, (*it)->y ); if( piece->type == QUEEN ) { std::cout << "Checking if queen on: x: " << piece->x << " y: " <<piece->y << ". can move to x: " << to_x << " y: " << to_y << "\n"; } if( validateMove( m ) ){ return true; } } } if( color == WHITE ) std::cout << "white cant move to: "; else std::cout << "black cant move to: "; std::cout << " x: "<< to_x << " y: " << to_y << "\n"; return false; }
bool ChessBoard::checkHorisontalOrVerticalLine( Move move ) { int dif_x = move.to_x - move.from_x; int dif_y = move.to_y - move.from_y; if( dif_x != 0 && dif_y != 0 ) return false; int dir_x = 0; int dir_y = 0; if( dif_x != 0 ) { dir_x = dif_x/std::abs( dif_x ); } else { dir_y = dif_y/std::abs( dif_y ); } int x = move.from_x + dir_x, y = move.from_y + dir_y; while( x != move.to_x || y != move.to_y ) { if( getPiece( x, y ) != NULL ) return false; x += dir_x; y += dir_y; } return true; }
/** * Prints out the board to standard out. * For testing purposes. */ void printBoard() { int i, j; // fprintf(stderr, "From input:\n"); // // for (i = 0; i < columns - PADDING; i++) { // for (j = 0; j < rows - PADDING; j++) { // printf("%c", pieces[getPiece(j,i)]); // } // } // fprintf(stderr, "\nTop Pieces:\n"); // for (i = 0; i < columns - PADDING; i++) { // printf("%d ", columnHeight[i]); // } // fprintf(stderr, "\n\n"); // for (i = rows - 1; i >= 0; i--) { // for (j = 0; j < columns; j++) { // printf("%c ", pieces[board[i + j * rows]]); // } // printf("\n"); // } // printf("\n\n"); for (i = rows - 7; i >= 0; i--) { for (j = 0; j < boardSize(columns); j++) { fprintf(stderr, "%c ", pieces[getPiece(i,j)]); } fprintf(stderr, "\n"); } fprintf(stderr, "\n\n"); }
bool validMoveKnight(ChessBoard* board, Cell to, ChessPiece* piecen) { if (to.colum > 7 || to.colum < 0 || to.rowum > 7 || to.rowum < 0) return false; Cell ownPos = getPosition(piecen); int i; for (i = 0; i < 4; i++) { if (knightmovePatterns[i][0] + ownPos.colum == to.colum && (knightmovePatterns[i][1] + ownPos.rowum == to.rowum || knightmovePatterns[i][1]*-1 + ownPos.rowum == to.rowum)) { ChessPiece * piece = getPiece(to.colum, to.rowum); Cell cell; cell.colum=to.colum; cell.rowum=to.rowum; if (piece == 0 && isCheckOnMove(board, piecen, ownPos, cell)) { return false; } else if (piece != 0 && (getColor(piece) == getColor(piecen) || isCheckOnMove(board, piecen, ownPos, cell))) { return false; } else return true; } } return false; } // end of method validdMove(ChessBoard & board, Cell to)
const Surroundings Game::getSurroundings(const Position &pos) const { Surroundings Sur; unsigned int x,y; unsigned int i = 0; // Checks surrounding from a piece using grid from -1 -> 1. returns the surroundings of a peice. for (int row =-1; row <=1; ++row) { x = pos.x+row; for (int col =-1; col <=1; ++col) { y=pos.y+col; if( x>=__height || y >=__width) { Sur.array[i] = INACCESSIBLE; i++; } else if(__grid[x*__width+y]== nullptr) { Sur.array[i] = EMPTY; i++; } else { Sur.array[i]=getPiece(x,y)->getType(); i++; } } } Sur.array[4]=SELF; return Sur; }
int checkAddition(std::string in, int inWidth, std::string added, int addedWidth, std::vector<int> previousCounts) { for (int i=0; i<blockHeight; i++) { int leftPiece = getPiece(in, (i+1)*inWidth-1); int rightPiece = getPiece(added, i*addedWidth); if (pieceHasRight(leftPiece) != pieceHasLeft(rightPiece)) { return 1; } // Make sure that we don't have H/8 to the left of D/4 if (leftPiece==8 && rightPiece==4) { return 2; } } // Check piece counts return pieceCountIsValid(added,previousCounts); }
/** * @brief Permet de vérifier qu'un déplacement d'une pièce est possible * @param[in] colonneO la colonne d'origine de la pièce * @param[in] ligneO la ligne d'origine de la pièce * @param[in] colonne la colonne où la pièce doit se déplacer * @param[in] ligne la ligne où la pièce doit se déplacer * @return 0 : non déplaçable, 1 : case libre, 2 : case occupée * @see bool Stratego::deplacer(char colonneO,int ligneO,char colonne,int ligne) */ int Stratego::deplacementPossible(char colonneO, int ligneO, char colonne, int ligne) const { if (!estDansTerrain(colonne, ligne)) return 0; char piece = getPiece(colonne, ligne); if ((colonneO-COLGAUCHE) % 2 == 0 && ( (colonneO == colonne && (ligneO == ligne-1 || ligneO == ligne+1)) || ((colonneO == colonne-1 || colonneO == colonne+1) && (ligneO == ligne || ligneO == ligneO-1)) )) if (piece == VIDE) return 1; else return 2; else if ((colonneO-COLGAUCHE) % 2 == 1 && ((colonneO == colonne && (ligneO == ligne-1 || ligneO == ligne+1)) || ((colonneO == colonne-1 || colonneO == colonne+1) && (ligneO == ligne+1 || ligneO == ligneO)))) if (piece == VIDE) return 1; else return 2; else return 0; }
void redo(Game* game) { int turn = 0; int maxTurn = 0; char cmd[7]; int rst = 0; int debug = 0; int in = 0; Piece* piece; clock_t start = 0; assert(game); maxTurn = game->turn-2; game->turn = 0; resetBoard(game); if (maxTurn <= 0) { printBoard(game); return; } for (turn = 1;turn <= maxTurn;turn++) { rst = getStepFromLog(game, turn, cmd); rst = movePieceNotation(game, cmd); if (cmd[4] > 'A' && cmd[4] < 'R') { piece = getPiece(game, cmd[2]-'a', cmd[3]-'1'); piece->type = cmd[4]; } game->turn = turn; } printBoard(game); }
bool Chunk::checkHash(const SHA1Hash & h) { PieceData::Ptr d = getPiece(0,size,true); if (!d || !d->ok()) return false; else return d->generateHash() == h; }
std::vector<int> getPieceCounts(std::string puzzle) { //int *count = new int[16]; std::vector<int> count = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; for (int i=0; i<puzzle.length(); i++) { int temp = getPiece(puzzle,i); count[temp]++; } return count; }
void Board::setNonPawns(int r) { getPiece(r, 0)->setType('R'); getPiece(r, 1)->setType('N'); getPiece(r, 2)->setType('B'); getPiece(r, 3)->setType('Q'); getPiece(r, 4)->setType('K'); getPiece(r, 5)->setType('B'); getPiece(r, 6)->setType('N'); getPiece(r, 7)->setType('R'); }
//! Can the player intersect the checking piece? bool Board::canIntersectChecker(Color color, Piece* checker) { // Impossible to intersect a Knight if(checker->getType() == KNIGHT) return false; Position kingPos = getPiece(KING, color)->getPos(); // Impossible to intersect something that's one unit close Position diff; diff.x = checker->getPos().x - kingPos.x; diff.y = checker->getPos().y - kingPos.y; if(abs(diff.x) + abs(diff.y) <= 2 && abs(diff.x) <= 1 && abs(diff.y) <= 1) { return false; } else { // Vertically if(diff.x == 0 && diff.y != 0) { for(int i = 0; i < abs(diff.y); i++) { if(canMoveTo(color, kingPos.x, kingPos.y + i*diff.y/abs(diff.y))) { return true; } } } // Horizontally else if(diff.x != 0 && diff.y == 0) { for(int i = 0; i < abs(diff.x); i++) { if(canMoveTo(color, kingPos.x + i*diff.x/abs(diff.x), kingPos.y)) { return true; } } } // Diagonally else if(abs(diff.x) == abs(diff.y) && diff.x != 0) { for(int i = 0; i < abs(diff.x); i++) { if(canMoveTo(color, kingPos.x + i*diff.x/abs(diff.x), kingPos.y + i*diff.y/abs(diff.y))) { return true; } } } } return false; }
int pieceCountIsValid(std::string puzzle, std::vector<int> count) { for (int i=0; i<puzzle.length(); i++) { int temp = getPiece(puzzle,i); count[temp]++; if (count[temp] > numCounts[temp]) { return 3; } } return 0; }
bool Board::checkDeath(Square s) { if(!isTrap(s) || isFree(s)) //if it is not a trap, or no piece is on the trap return false; Color color = getPiece(s)->getColor(); Square adjSquare; for(int i = 0; i < 4; ++i) //checks the 4 directions { adjSquare = s + m_cardinals[i]; if(isValid(adjSquare) && !isFree(adjSquare)) { if(getPiece(adjSquare)->getColor() == color) //an ally is around : the piece can't die return false; } } removePiece(s); return true; }
int pieceCountIsValid(std::string puzzle) { int count [16] = {0}; for (int i=0; i<puzzle.length(); i++) { int temp = getPiece(puzzle,i); count[temp]++; if (count[temp] > numCounts[temp]) { return 3; } } return 0; }
bool Board::isFrozen(Square s) const { if(isFree(s)) return true; //a blank square is considered frozen Color color = getPiece(s)->getColor(); Square adjSquare; bool frozen = false; for(int i = 0; i < 4; ++i) //checks the 4 directions { adjSquare = s + m_cardinals[i]; if(isValid(adjSquare) && !isFree(adjSquare)) { if(getPiece(adjSquare)->getColor() == color) //an ally is around : the piece can't be frozen return false; else //an ennemy is around : frozen if weaker or if already frozen frozen |= (*getPiece(s) < *getPiece(adjSquare)); } } return frozen; }
/** * @brief Permet un combat * @param[in] colAtq la colonne de la pièce attaquante * @param[in] ligAtq la ligne de la pièce attaquante * @param[in] colAdv la colonne de la pièce adverse * @param[in] ligAdv la ligne de la pièce adverse * @see bool Stratego::dejaUnePiece(char colDep, int ligDep, char colDest, * int ligDest) */ void Stratego::combat(char colAtq, int ligAtq, char colAdv, int ligAdv) { char pieceAttaquante = getPiece(colAtq, ligAtq), pieceAdverse = getPiece(colAdv, ligAdv), // Évite un changement de camp de pieceAttaquante pieceAtq = pieceAttaquante; if (estPieceDe(pieceAttaquante, NORD)) pieceAttaquante -= MINUS; if (estPieceDe(pieceAdverse, NORD)) pieceAdverse -= MINUS; if ( ((pieceAttaquante < pieceAdverse) && pieceAdverse != BOMBE) || (pieceAttaquante == DEMINEUR && pieceAdverse == BOMBE) || (pieceAttaquante == ESPION && pieceAdverse == MARECHAL) ) put(colAdv, ligAdv, pieceAtq); else if (pieceAttaquante == pieceAdverse) { put(colAdv, ligAdv, VIDE); } put(colAtq, ligAtq, VIDE); }
/** * @brief Si une case sur le chemin de l'éclaireur est occupée, cette fonction * détremine si un combat est nécesssaire ou si le mouvement est impossible * param[in] colDep la colonne d'origine de la pièce éclaireur * param[in] ligDep la ligne d'origine de la pièce éclaireur * param[in] colDest la colonne où la pièce éclaireur doit se déplacer * param[in] ligDest la ligne où la pièce éclaireur doit se déplacer * @return false : mouvement impossible, true : combat effectué */ bool Stratego::mouvementEclaireur(char colDep, int ligDep, char colDest, int ligDest) { if (!estDansTerrain(colDest, ligDest)) return (false); char pieceDep = getPiece(colDep, ligDep), pieceDest = getPiece(colDest, ligDest); if (pieceDest == VIDE) { put(colDest, ligDest, pieceDep); put(colDep, ligDep, VIDE); return (true); } if (!campsOpposes(pieceDep, pieceDest)) return (false); else { combat(colDep, ligDep, colDest, ligDest); return (true); } }