void GameScreen::refreshAll() { clearAll(); if(!m_game.hasStarted()) m_placementUI.resetAvailability(); for(int i = 0; i < BOARD_SIZE; ++i) { for(int j = 0; j < BOARD_SIZE; ++j) { sf::Vector2i s(i,j); PiecePtr p = m_game[toSquare(s)]; if (p != nullptr) { m_pieces[p] = PieceSprite(p); m_pieces[p].moveOnSquare(s); m_pieces[p].freeze(m_game.isFrozen(toSquare(s))); //if the piece is frozen, show it if(!m_game.hasStarted() && (p->getColor() == m_game.getActivePlayer())) //if in placement phase, and if the piece belongs to the current player updatePieceAvailability(p->getType());//updating the availability in placement UI } } } updateNbMoves(); if(!m_game.hasStarted()) m_placementUI.setPlayer(m_game.getActivePlayer()); }
void GameScreen::place(sf::Vector2i s) { if(BoardAlignedSprite::isOnBoard(s)) //there is a piece to be placed, and the square to place it in is valid { PiecePtr p = m_game[toSquare(s)]; PieceType oldPieceType = NB_PIECES; if (p != nullptr) oldPieceType = p->getType(); remove(s); //removing any piece that would happen to be here if(m_game.place(m_selectedType, toSquare(s))) //if the placement was a success { m_pieces[m_game[toSquare(s)]] = PieceSprite(m_game[toSquare(s)]); m_pieces[m_game[toSquare(s)]].moveOnSquare(s); updatePieceAvailability(m_selectedType);//updating the availability in placement UI if(oldPieceType != NB_PIECES) updatePieceAvailability(oldPieceType); //id a piece was replaced, update its availability as well } else if (oldPieceType != NB_PIECES) //the placement has failed : we put back the piece that was there before { m_game.place(oldPieceType, toSquare(s)); m_pieces[m_game[toSquare(s)]] = PieceSprite(m_game[toSquare(s)]); m_pieces[m_game[toSquare(s)]].moveOnSquare(s); } } }
void Board::makeMove(Move move) { PiecePtr tmp = _squares[move.src].getPiece(); if(tmp->getType() == POWN){ tmp->moved(); } _squares[move.dest].putPiece(tmp); _squares[move.src].gonePiece(); }
BitCache::PiecePtr BitCache::FetchNewPiece() { if (cache_piece_.size() > max_cache_pieces_) { CachePiece::iterator it = GetOldestPiece(); if (it != cache_piece_.end()) { PiecePtr result = it->second; cache_piece_.erase(it); result->Clear(); return result; } } return PiecePtr(new BitPiece(piece_length_)); }
bool GameScreen::select(sf::Vector2i s) { if(m_selectedPiece != NULL_SQUARE) //if a piece is selected { if(m_selectedTarget != NULL_SQUARE) //if an enemy piece is selected { if(!m_highlighter.isHighlighted(s)) //square isn't selectable unselect(); else //square is selectable { bool res = m_game.displace(toSquare(m_selectedPiece), toSquare(s), toSquare(m_selectedTarget), false); unselect(); return res; } } else //no enemy piece selected { if(!m_highlighter.isHighlighted(s)) //square isn't selectable unselect(); else //square is selectable { PiecePtr p = m_game[toSquare(s)]; if (p == nullptr) //no piece here { bool res = m_game.move(toSquare(m_selectedPiece), toSquare(s), false); unselect(); return res; } else selectTarget(s); } } } else //no piece is selected { PiecePtr p = m_game[toSquare(s)]; if (p != nullptr && p->getColor() == m_game.getActivePlayer()) //there is a piece and it can be selected selectPiece(s); } return false; }