Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6c0054b
Rewrite how the board is stored and moves are made
razvanfilea Nov 20, 2019
1c41cd3
Remove 'ToList' MoveGen template arg, make some fixes
razvanfilea Nov 22, 2019
2bfdff3
Fix a critical error with Board::doMove()
razvanfilea Nov 24, 2019
9db8df9
Fix getPhase always returning ENDGAME
razvanfilea Nov 24, 2019
8fd4e8c
Improve the Evaluation function performance
razvanfilea Nov 24, 2019
bee2ee1
Fix Board::hasValidState and update Pawn PSQT
razvanfilea Nov 26, 2019
68d9853
Switch to the previous Multi Threading model
razvanfilea Nov 28, 2019
7154003
Remove KING_DANGER MoveGen
razvanfilea Nov 28, 2019
eb1963c
Add age attribute to the TranspositionTable
razvanfilea Nov 29, 2019
2da9711
Fix crash when generating legal moves
razvanfilea Nov 30, 2019
f922ceb
Rewrite how the UI processes positions
razvanfilea Nov 30, 2019
220b4d7
Rename NegaMax class to Search
razvanfilea Nov 30, 2019
ee721fd
Work on the ThreadPool
razvanfilea Nov 30, 2019
9a9e5ac
Solve a bug with Castling and disable EnPassant for now
razvanfilea Nov 30, 2019
faba2c2
Add Generated Nodes to Stats
razvanfilea Dec 1, 2019
d73bb5c
Remake the Settings Screen
razvanfilea Dec 1, 2019
9b46c3e
Clean ThreadSafeQueue and ThreadPool
razvanfilea Dec 1, 2019
d5d5ae0
Revert to the old Pawn PSQT
razvanfilea Dec 8, 2019
1b60b38
Re-enable EnPassant
razvanfilea Dec 9, 2019
c0be29b
Add Platform Specific Implementations for BSF and BSR
razvanfilea Dec 10, 2019
8a64bbc
Remove StackVector
razvanfilea Dec 10, 2019
7889cfa
Fix compile errors
razvanfilea Dec 10, 2019
e35ab03
Remake the Pawn Evaluation add Connected Bonus
razvanfilea Dec 11, 2019
459400d
Merge Rays.h into Bitboard.h
razvanfilea Dec 11, 2019
9be2e38
Fix Pawn Move Generation from last commit
razvanfilea Dec 12, 2019
9a37891
Remove Light Theme, add Rook on Queen File Evaluation
razvanfilea Dec 15, 2019
b81a38c
Rewrite Piece class to only use 1 byte
razvanfilea Dec 15, 2019
3a122a9
New icon
razvanfilea Dec 15, 2019
99a93b5
Release 1.0
razvanfilea Dec 15, 2019
b8651a6
Fix issues with the Pull Request
razvanfilea Dec 15, 2019
8c8a8d9
Delete MoveOrdering.h
razvanfilea Dec 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove 'ToList' MoveGen template arg, make some fixes
  • Loading branch information
razvanfilea committed Nov 22, 2019
commit 1c41cd3cb0f09392f67e1ba432dc6a13b8cacdaf
12 changes: 6 additions & 6 deletions ChessAndroid/app/src/main/cpp/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ JavaVM *jvm = nullptr;
jobject gameManagerInstance;

const BoardManager::PieceChangeListener listener = [](State state, bool shouldRedraw,
const StackVector<PosPair, 2> &moved)
const std::vector<std::pair<byte, byte>> &moved)
{
JNIEnv *env;
int getEnvStat = jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
Expand All @@ -36,8 +36,8 @@ const BoardManager::PieceChangeListener listener = [](State state, bool shouldRe

for (unsigned i = 0; i < moved.size(); ++i)
{
const Pos &startPos = moved[i].first;
const Pos &destPos = moved[i].second;
const Pos &startPos = Pos(moved[i].first);
const Pos &destPos = Pos(moved[i].second);
jobject obj = env->NewObject(Cache::posPairClass, constructorId,
startPos.x, startPos.y, destPos.x, destPos.y);

Expand Down Expand Up @@ -202,8 +202,8 @@ external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_Native_movePiece(JNIEnv */*pEnv*/, jclass /*type*/,
jbyte selectedX, jbyte selectedY, jbyte destX, jbyte destY)
{
BoardManager::movePiece(Pos(static_cast<byte>(selectedX), static_cast<byte>(selectedY)),
Pos(static_cast<byte>(destX), static_cast<byte>(destY)));
BoardManager::movePiece(Pos(static_cast<byte>(selectedX), static_cast<byte>(selectedY)).toSquare(),
Pos(static_cast<byte>(destX), static_cast<byte>(destY)).toSquare());
}

external JNIEXPORT jint JNICALL
Expand Down Expand Up @@ -256,7 +256,7 @@ static U64 perft(const Board &board, int depth)
external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_Native_perft(JNIEnv */*pEnv*/, jclass /*type*/, jint depth)
{
constexpr std::array perftResults {
constexpr std::array<U64, 7> perftResults {
1, 20, 400, 8902, 197281, 4865609, 119060324
};
constexpr int maxSize = static_cast<int>(perftResults.size());
Expand Down
79 changes: 30 additions & 49 deletions ChessAndroid/app/src/main/cpp/chess/BoardManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void BoardManager::initBoardManager(const PieceChangeListener &listener, const b

s_MovesHistory.clear();
s_MovesHistory.reserve(200);
s_MovesHistory.emplace_back(Pos(), Pos(), s_Board);
s_MovesHistory.emplace_back(0u, 0u, s_Board);

Stats::resetStats();

Expand All @@ -34,86 +34,67 @@ void BoardManager::initBoardManager(const PieceChangeListener &listener, const b
s_WorkerThread = std::thread(moveComputerPlayer, s_Settings);
}

void BoardManager::loadGame(const std::vector<PosPair> &moves, const bool isPlayerWhite)
void BoardManager::loadGame(const std::vector<std::pair<byte, byte>> &moves, const bool isPlayerWhite)
{
s_IsPlayerWhite = isPlayerWhite;

s_Board.initDefaultBoard();

s_MovesHistory.emplace_back(Pos(), Pos(), s_Board);
s_MovesHistory.emplace_back(0u, 0u, s_Board);

for (const PosPair &move : moves)
{
s_Board.doMove(move.first.toSquare(), move.second.toSquare());
s_Board.score = Evaluation::evaluate(s_Board);
s_MovesHistory.emplace_back(move.first, move.second, s_Board);
}
try {
for (const auto &move : moves)
{
s_Board.doMove(move.first, move.second);
s_Board.score = Evaluation::evaluate(s_Board);
s_MovesHistory.emplace_back(move.first, move.second, s_Board);
}
} catch (...) {
// Couldn't load all moves correctly
}

s_Listener(s_Board.state, true, {});
}

Piece::MaxMovesVector BoardManager::getPossibleMoves(const Pos &selectedPos)
StackVector<Pos, 27> BoardManager::getPossibleMoves(const Pos &selectedPos)
{
Piece::MaxMovesVector moves;
StackVector<Pos, 27> moves;

const Piece &piece = s_Board[selectedPos];
const auto possibleMoves = piece.getPossibleMoves(selectedPos.toSquare(), s_Board);
const byte startSq = selectedPos.toSquare();
const Piece &piece = s_Board.getPiece(startSq);
U64 possibleMoves = piece.getPossibleMoves(startSq, s_Board);

for (const Pos &destPos : possibleMoves)
while (possibleMoves)
{
const Piece &destPiece = s_Board[destPos];
const byte destSq = Bitboard::findNextSquare(possibleMoves);
const Piece &destPiece = s_Board.getPiece(destSq);
if (destPiece.type == PieceType::KING)
continue;

Board board = s_Board;
board.doMove(selectedPos.toSquare(), destPos.toSquare());

if (board.state == State::INVALID)
continue;
if (piece.isWhite && (board.state == State::WHITE_IN_CHECK || board.state == State::WINNER_BLACK))
continue;
if (!piece.isWhite && (board.state == State::BLACK_IN_CHECK || board.state == State::WINNER_WHITE))
continue;
board.doMove(startSq, destSq);

int count = 1;

for (const auto &game : BoardManager::getMovesHistory())
{
if (board.colorToMove == game.board.colorToMove &&
board.state == game.board.state &&
board.zKey == game.board.zKey)
count++;

// TODO: Fix this
if (count == 3)
{
board.score = 0;
board.state = State::DRAW;
break;
}
}

moves.emplace_back(destPos);
moves.emplace_back(Pos(destSq));
}

return moves;
}

void BoardManager::movePiece(const Pos &selectedPos, const Pos &destPos, const bool movedByPlayer)
void BoardManager::movePiece(const byte startSq, const byte destSq, const bool movedByPlayer)
{
assert(selectedPos.isValid() && destPos.isValid());
assert(startSq < 64 && destSq < 64);

const byte castledBefore = (s_Board.castlingRights & CASTLED_WHITE) | (s_Board.castlingRights & CASTLED_BLACK);
s_Board.doMove(selectedPos.toSquare(), destPos.toSquare());
s_Board.doMove(startSq, startSq);
const byte castledAfter = (s_Board.castlingRights & CASTLED_WHITE) | (s_Board.castlingRights & CASTLED_BLACK);

s_Board.score = Evaluation::evaluate(s_Board);
s_Board.zKey = Hash::compute(s_Board);

const StackVector<PosPair, 2> piecesMoved{ {selectedPos, destPos} };
const std::vector piecesMoved{ std::make_pair(startSq, destSq) };
const bool shouldRedraw = s_Board.isPromotion || (castledBefore != castledAfter);

s_MovesHistory.emplace_back(selectedPos, destPos, s_Board);
s_MovesHistory.emplace_back(startSq, destSq, s_Board);
s_Listener(s_Board.state, shouldRedraw, piecesMoved);

if (movedByPlayer && (s_Board.state == State::NONE || s_Board.state == State::WHITE_IN_CHECK || s_Board.state == State::BLACK_IN_CHECK))
Expand All @@ -132,7 +113,7 @@ void BoardManager::moveComputerPlayer(const Settings &settings)

Stats::stopTimer();
s_IsWorking = false;
movePiece(bestMove.start, bestMove.dest, false);
movePiece(bestMove.startSq, bestMove.destSq, false);

s_WorkerThread.detach();
}
Expand Down Expand Up @@ -162,7 +143,7 @@ void BoardManager::undoLastMoves()

s_Board = previousBoard;
s_Listener(previousBoard.state, shouldRedraw,
{ { engineMove.dest, engineMove.start }, { playerMove.dest, playerMove.start } });
{ { engineMove.destSq, engineMove.startSq }, { playerMove.destSq, playerMove.startSq } });

// Remove the last two moves from the vector
s_MovesHistory.pop_back();
Expand Down
8 changes: 4 additions & 4 deletions ChessAndroid/app/src/main/cpp/chess/BoardManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Board;
class BoardManager final
{
public:
using PieceChangeListener = std::function<void(State state, bool shouldRedraw, const StackVector<PosPair, 2> &moved)>;
using PieceChangeListener = std::function<void(State state, bool shouldRedraw, const std::vector<std::pair<byte, byte>> &moved)>;

private:
static Settings s_Settings;
Expand All @@ -29,15 +29,15 @@ class BoardManager final

public:
static void initBoardManager(const PieceChangeListener &listener, bool isPlayerWhite = true);
static void loadGame(const std::vector<PosPair> &moves, bool isPlayerWhite);
static void loadGame(const std::vector<std::pair<byte, byte>> &moves, bool isPlayerWhite);
static void undoLastMoves();

static Board &getBoard() { return s_Board; }
static const auto &getMovesHistory() { return s_MovesHistory; }
static bool isWorking() { return s_IsWorking; }
static bool isPlayerWhite() { return s_IsPlayerWhite; }
static Piece::MaxMovesVector getPossibleMoves(const Pos &selectedPos);
static void movePiece(const Pos &selectedPos, const Pos &destPos, bool movedByPlayer = true);
static StackVector<Pos, 27> getPossibleMoves(const Pos &selectedPos);
static void movePiece(byte startSq, byte destSq, bool movedByPlayer = true);
static void setSettings(const Settings &settings) { s_Settings = settings; }

private:
Expand Down
Loading