Chess engine threefold repetition detection leads to incorrect play - c++

I am trying to implement threefold repetition detection in my chess engine, but the method I use leads to incorrect play. I check for one repetition of the position in the current search space or two repetitions "behind" the root. This approach is used by Stockfish too but it makes my engine weaker (ELO score drops significantly when testing).
Each position in the history of the game has a zobrist hash index.
Threefold repetition detection:
bool Eval::isThreefoldRepetition() const {
const std::deque<GameState>&history=internal_board.getHistory();
int repetitions=0;
uint64_t hash=internal_board.getGameState().zobrist_key;
for(int k=history.size()-2;k>=0;k-=2){
if(history[k].zobrist_key==hash){
if(k>=root)//found repetition in current search space
return true;
//else we need 2 repetitions that happen before the root
repetitions++;
if(repetitions==2)
return true;
}
}
return false;
}
Search:
int Eval::negamax(int depth, int alpha, int beta, Color color) {
if(isThreefoldRepetition())
return threefold_repetition;
uint64_t hash=internal_board.getGameState().zobrist_key;
int alphaOrig=alpha;
Transposition node=TranspositionTable::getInstance().getTransposition(hash);
if(node.getType()!=NodeType::Null &&node.getDepth()>=depth){
if(node.getType()==NodeType::Exact)
return node.getValue();
else if(node.getType()==NodeType::LowerBound)
alpha=std::max(alpha,node.getValue());
else if(node.getType()==NodeType::UpperBound)
beta=std::min(beta,node.getValue());
if(alpha>=beta)
return node.getValue();
}
if (depth == 0) {
return getHeuristicScore(color);
}
std::vector<Move> moves = movegen.getAllMoves();
if (moves.size() == 0) {
if (movegen.isInCheck(color))
return checkmate - depth;//try to delay the checkmate
return stalemate;
}
int best = -infinity;
Move best_move;
setRating(moves);
std::sort(moves.begin(), moves.end(), compare);
for (const Move &move:moves) {
if(!hasTimeLeft()){
premature_stop=true;
break;
}
internal_board.makeMove(move);
int down = -negamax(depth - 1, -beta, -alpha, getOpposite(color));
internal_board.undoLastMove();
if(down>best){
best=down;
best_move=move;
}
alpha = std::max(alpha, best);
if (alpha >= beta)
break;
}
if(!premature_stop) {
//save the position in the transposition table
NodeType node_type;
if (best <= alphaOrig)//did not affect the score
node_type = NodeType::UpperBound;
else if (best >= beta)
node_type = NodeType::LowerBound;
else node_type = NodeType::Exact;
TranspositionTable::getInstance().addEntry(Transposition(node_type, hash, depth, best, best_move));
}
return best;
}
Notice that I can't check for a 'real' threefold repetition because a position will not be repeated thrice in the search space when using transposition tables.
Is my method of evaluation wrong?

Related

Minimax algorithm for tic tac toe doesnt give optimal solution

I am writing 3D tic tac toe game using minimax algorithm with alpha beta pruning, but the algorithm doesnt give optimal solution, it goes and chooses next possible solution from winning states, not taking into concern what is on the board, meaning it wont block my moves.
This is the code:
`
int Game::miniMax(char marker, int depth, int alpha, int beta){
// Initialize best move
bestMove = std::make_tuple(-1, -1, -1);
// If we hit a terminal state (leaf node), return the best score and move
if (isBoardFull() || getBoardState('O')!=0 || depth>5)
return getBoardState('O');
auto allowedMoves = getAllowedMoves();
for (int i = 0; i < allowedMoves.size(); i++){
auto move = allowedMoves[i];
board[std::get<0>(move)][std::get<1>(move)][std::get<2>(move)] = marker;
// Maximizing player's turn
if (marker == 'O'){
int bestScore = INT32_MIN;
int score = miniMax('X', depth + 1, alpha, beta);
// Get the best scoring move
if (bestScore <= score){
bestScore = score - depth * 10;
bestMove = move;
// Check if this branch's best move is worse than the best
// option of a previously search branch. If it is, skip it
alpha = std::max(alpha, bestScore);
board[std::get<0>(move)][std::get<1>(move)][std::get<2>(move)] = '-';
if (beta <= alpha){
break;
}
}
} // Minimizing opponent's turn
else{
int bestScore = INT32_MAX;
int score = miniMax('O', depth + 1, alpha, beta);
if (bestScore >= score){
bestScore = score + depth * 10;
bestMove = move;
// Check if this branch's best move is worse than the best
// option of a previously search branch. If it is, skip it
beta = std::min(beta, bestScore);
board[std::get<0>(move)][std::get<1>(move)][std::get<2>(move)] = '-';
if (beta <= alpha){
break;
}
}
}
board[std::get<0>(move)][std::get<1>(move)][std::get<2>(move)] = '-'; // Undo move
}
if(marker== 'O')
return INT32_MIN;
else
return INT32_MAX;
}
`
What do I need to change to make it work?
I tried other ways to implement minimax, but it doesnt give optimal, or any, solution.
The limit on depth is because it is too slow for bigger depths, but still not giving solution, also increasing the value of the constant that multiplies the depth in the score part is only slowing the program

(Chess) Problem with negamax search missing checkmate

I'm implementing a search algorithm into the search function with Negamax with alpha-beta pruning. However, it often misses forced checkmate.
(Note: "Mate in X" counts whole turns, while "depth" and "move(s)" relies on half moves.)
Example
The position with the following FEN: 1k1r4/pp1b1R2/3q2pp/4p3/2B5/4Q3/PPP2B2/2K5 b - - 0 1 has a Mate in 3 (depth of 5 to the algorithm).
It goes Qd1+, Kxd1, Bg4+, Kc1/Ke1 (Doesn't matter), Rd1#.
It can spot the checkmate from 1 move away, but fails at higher depths.
Possible Causes
It could be a typo, a misused type, or even a complete misunderstanding of the method, as all of it happened before.
Simplified Code
I've make some part of the code code easier to read. (eg. remove std::, turns multiple lines into function).
Shouldn't changes the functionalities though.
Root Call
pieceMove searchBestMove (gameState currentState, int depth) {
//Calls the Negamax search
pieceColor sideToMove = whoseTurnIsIt();
vector<pieceMove> moveList = generateLegalMoves(currentState, sideToMove);
pieceMove bestMove;
signed int bestEval = numeric_limits<signed int>::max();
for (const auto move : moveList) {
signed int evaluation = negaMax(applyMove(currentState, move), numeric_limits<signed int>::min(), numeric_limits<signed int>::max(), depth - 1, 1);
if (evaluation < bestEval) {
bestMove = move;
bestEval = evaluation;
}
}
return bestMove;
}
Search Function
signed int negaMax (gameState currentState, signed int alpha, signed int beta, int depth, int rootDepth) {
//Main Negamax search
//Terminal node
if (depth == 0) {
return evaluates(currentState); //Replace this line with the one below to enable the extended search
//return quiescenceSearch(currentState, alpha, beta);
}
//Mate distance pruning
signed int mateDistScore = numeric_limits<signed int>::max() - rootDepth;
alpha = max(alpha, -mateDistScore);
beta = min(beta, mateDistScore - 1);
if (alpha >= beta) return alpha;
vector<pieceMove> moveList = generateLegalMoves(currentState);
//If no moves are allowed, then it's either checkmate or stalemate
if (moveList.size() == 0) return evaluates(currentState)
orderMoves(currentState, moveList);
for (const auto move : moveList) {
signed int score = -negaMax(applyMove(currentState, move), -beta, -alpha, depth - 1, rootDepth + 1);
if (score >= beta) return beta; //Bata cutoff
alpha = max(score, alpha);
}
return alpha;
}
Extended Search
signed int quiescenceSearch (gameState currentState, signed int alpha, signed int beta) {
//Searches only captures
//Terminal node
int evaluation = evaluates(currentState);
if (evaluation >= beta) return beta;
alpha = max(alpha, evaluation);
vector<pieceMove> moveList = generateCaptureMoves(currentState);
//If no moves are allowed, then it's either checkmate or stalemate
if (moveList.size() == 0) return evaluates(currentState);
orderMoves(currentState, moveList);
for (const auto move : moveList) {
signed int score = -quiescenceSearch(applyMove(currentState, move), -beta, -alpha);
if (score >= beta) return beta; //Bata cutoff
alpha = max(score, alpha);
}
return alpha;
}
I think you need to call the function "quiescenceSearch" when the depth is 0 in "negaMax". Also you need to check for "checks" too in "quiescenceSearch" along with captures since they are not quiet moves. Also Matedistance pruning works only when positions are properly scored(https://www.chessprogramming.org/Mate_Distance_Pruning#Mating_Value). May be checking if your evaluation function is evaluating properly could also help.

Error reconstructing path in a grid using BFS

The problem I am facing is the following:
I have a function based on the BFS search algorithm that I use in a NxM grid, the mission of this function is to return the following Direction from a set of possible Directions = {Up, Down, Left , Right} (No diagonal moves!)to which a player has to move, so that in each "round / frame" where there is a type of item of a game (For example, in this specific case, a bazooka) is closer to the item.
To address the problem, I have created a Map class made of vector <vector <Cell> > where vector is from the standard library and Cell is what the grid is made of and has some consulting methods on what is in one of the NxM cells (if there is a building, an enemy, a Bazooka, etc.)
So, for implementing a solution for this, I made a struct TrackingBFS to reconstruct the path of the BFS search:
struct TrackingBFS {
pair <int,int> anterior;
bool visited;
};
And this is the BFS search implementation:
//Pre: origen is a valid position on the grid where the player is
//Post:Returns a pair of bool and a direction to the closest bazooka. If we have access to a bazooka, then we will return a pair (true,Dir) where Dir is a direction to take to be closer to the bazooka else a pair (false, Dir) where dir is just the same direction as origen.
pair <bool,Dir> direction_to_closest_gun (const Pos& origen) {
//R = board_rows() C = board_cols()
//m = mapa
//sr,sc = origin positions
int sr = origen.i;
int sc = origen.j;
//Variables para mantener tracking de los pasos cogidos
queue <int> rq; //Queue of x-row coordinates
queue <int> cq; //Queue of y-row coordinates
int move_count = 0; //Number of steps
int nodes_left_in_layer = 1; //How many nodes we need to de-queue before doing a step
int nodes_in_next_layer = 0; //How many nodes we add to the expansio of the BFS so we can upgrade nodes_left_in_layer in the next iteration
bool arma_mejor_encontrada = false;
//Visited is a MxN matrix of TrackingBFS that for every i,j stores the following information:
//If we visited the cell at position visited [i][j], then the TrackingBFS struct will be visited = true and last_node = (k,l) where k and l are the components of the LAST cell on the grid we visited in the BFS search.
//Else if we didn't visited the cell at [i][j], the TrackingBFS struct will be visited = true and last_node (-1,-1).
TrackingBFS aux;
aux.last_node = make_pair(-1,-1);
aux.visited = false;
//We create a Matrix of TrackingBFS named visited of NxM filled with the unvisited cells
vector < vector <TrackingBFS> > visited (board_rows(), vector <TrackingBFS> (board_cols(),aux));
//--------------------------------SOLVE---------------------------------
rq.push(sr);
cq.push(sc);
visited[sr][sc].visited = true;
visited[sr][sc].last_node = make_pair(sr,sc);
int xfound;
int yfound;
while (!rq.empty()) {
int r = rq.front();
int c = cq.front();
rq.pop();
cq.pop();
if (mapa[r][c].weapon == Bazooka) {
arma_mejor_encontrada = true;
xfound = r;
yfound = c;
break;
}
//Explore neighbours
Pos p (r,c);
for (Dir d : dirs) {
Pos searching = p + d;
int rr = searching.i;
int cc = searching.j;
//If the position we are searching is out of range or it's been visited before or there is a obstacle then continue
if (!pos_ok(searching) or visited[rr][cc].visited or mapa[rr][cc].type == Building or mapa[rr][cc].resistance != -1 or mapa[rr][cc].id != -1) {
//NADA
}
//Else we add the visited node to the queue, and fill the information on visited[rr][cc] with the node we are coming and mark it as visited
else {
rq.push(rr);
cq.push(cc);
visited[rr][cc].visited = true;
visited[rr][cc].last_node = make_pair (r,c);
nodes_in_next_layer++;
}
}
nodes_left_in_layer--;
if (nodes_left_in_layer == 0) {
nodes_left_in_layer = nodes_in_next_layer;
nodes_in_next_layer = 0;
move_count++;
}
}
//If we found the Bazooka
if (arma_mejor_encontrada) {
//Return the pair (true,next direction of the player at position (sr,sc) to approach the bazooka)
return make_pair(arma_mejor_encontrada, reconstruir_camino(visited,xfound,yfound,sr,sc));
}
else {
//If there is no bazooka we return (false,Up (this second component is meaningless))
return make_pair(arma_mejor_encontrada, Up);
}
}
The reconstruir_camino (recosntruct_path in english) implementation:
//This function is made to reconstruct the path from where we found de bazooka (x,y) to where our player is (ox,oy), whe are only interested in the next position of because this is run each frame, so we are constantly refreshing the state of the map.
Dir reconstruir_camino(const vector < vector <TrackingBFS> >& visited, const int& x, const int& y, const int& ox, const int& oy) {
//In v we save the pair of coordinates of our path, this was done only for debuging and in debug_matriz_tracking(visited) is also only for debuging
vector <pair<int,int>> path;
debug_matriz_tracking(visited);
//
int i = visited[x][y].last_node.first;
int j = visited[x][y].last_node.second;
while (not (i == ox and j == oy)) { //While the next node is not iqual as the original node we started de search (The one where our player is)
path.push_back(make_pair(i,j)); //Just for debuging
i = visited[i][j].last_node.first;
j = visited[i][j].last_node.second;
}
//So now in path we have the start and end nodes of every edge on the path
int siguiente_x = path.back().first;
int siguiente_y = path.back().second;
debug_camino(path);
return direccion_contiguos(ox,oy,siguiente_x,siguiente_y);
}
And direccion_contiguos (contiguous direction / relative direction in english) implementation:
//Returns the RELATIVE direction to go if we want to go from (ox, oy) to (dx, dy) being these two contiguous positions, that is, (dx, dy) is in Up, Down, Left or Right with respect to (ox, oy) IT WORKS FINE, NOTHING WRONG WITH THIS DON'T TOUCH
Dir direccion_contiguos (const int& ox, const int& oy, const int& dx, const int& dy) {
Pos o (ox,oy);
Pos d (dx,dy);
if (o + Up == d) {
return Up;
}
else if (o + Down == d){
return Down;
}
else if (o + Left == d) {
return Left;
}
else if (o + Right == d) {
return Right;
}
return Down;
}
So now in visited, we have the information to reconstruct the path, in fact I debuged it (it's kinda messy i know, sorry), in a visual way so this is what I got for a Player in origen = (7,10) and bazooka at position = (4,11):
[Imgur link of the Visual representation of the Matrix for reconstructing the path from origin to bazooka][1]
To read this image, at the top and left there are the coordinates of every cell of the visited matrix, the ones with green font, are the ones that have been visited, and they store THE NEXT cell/vertex of the path, and the ones with (-1,-1) are the ones that have not been visited by the BFS algorithm and thus they don't have any previous node and are in white.
So, NICE! It seems to work, at least the visited matrix.
My problem is when I debug the vector of edges/directions of the graph/grid, this is what I used in the example of the image:
void debug_camino(const vector <pair<int,int>>& v) {
cerr << "----------------------CAMINO-------------------- DEBUG_CAMINO" << endl;
for (long unsigned int i = 0; i < v.size(); ++i) {
cerr << "(" << v[i].first << "," << v[i].second << "),";
}
cerr << endl;
}
And when I executed the program, this is the path that I got with debug_camino():
If you see the image attached you can see that that's almost the path but not quite yet.
(4,12),(4,13),(4,14),(3,15),(3,16),(4,16),(5,16),(6,16),(7,15),(7,14),(7,13),(7,12),(7,11)
These ones bolded are not real (even valid because they are diagonal moves) reconstructions of the path and I don't really know WHY this is happening, but it's provoking my player to not following right path, and I want to fix the error and I'm kinda desperate because I don't really know where the error is and I've been trying for days :( ! I hope somebody can help me with this. Thanks for reading all this and sorry if the code is in some parts in Spanish or if it's not all that readable.
[1]: https://i.stack.imgur.com/vZ2Go.png
Okay, I actually managed to fix this error, i was overwriting the i variable so that was causing the error.
//This function is made to reconstruct the path from where we found de bazooka (x,y) to where our player is (ox,oy), whe are only interested in the next position of because this is run each frame, so we are constantly refreshing the state of the map.
Dir reconstruir_camino(const vector < vector <TrackingBFS> >& visited, const int& x, const int& y, const int& ox, const int& oy) {
//In v we save the pair of coordinates of our path, this was done only for debuging and in debug_matriz_tracking(visited) is also only for debuging
vector <pair<int,int>> path;
debug_matriz_tracking(visited);
//
int i = visited[x][y].last_node.first;
int j = visited[x][y].last_node.second;
while (not (i == ox and j == oy)) { //While the next node is not iqual as the original node we started de search (The one where our player is)
path.push_back(make_pair(i,j)); //Just for debuging
**i** = visited[i][j].last_node.first;
j = visited[**i**][j].last_node.second;
}
//So now in path we have the start and end nodes of every edge on the path
int siguiente_x = path.back().first;
int siguiente_y = path.back().second;
debug_camino(path);
return direccion_contiguos(ox,oy,siguiente_x,siguiente_y);
}
It's already fixed

Find path in grid if possible using Recursion

Given a grid of size R X C and startPosition and endPosition of a person in a grid composed of zero and one. Now I want to find path from start position to end position, and also trace the marked path on grid by labelling them 2. If path is not possible, I need to tell that path is not possible. So I had written this logic :
vector<vector<int>> solution;
void printSolution(vector<vector<int>> grid)
{
for (int i=0;i<grid.size();i++)
{
for (int j=0;j<grid[i].size();j++)
cout<<grid[i][j]<<" ";
cout<<endl;
}
}
bool isSafe(vector<vector<int>> grid, pair<int,int> position)
{
if(position.first >= 0 && position.first < grid.size() &&
position.second >= 0 && position.second < grid[0].size() &&
grid[position.first][position.second] == 0)
return true;
return false;
}
bool checkPath(vector<vector<int>> grid,pair<int,int> currentPosition,pair<int,int> endPosition){
if(currentPosition == endPosition)
{
solution[currentPosition.first][currentPosition.second] = 2;
return true;
}
if(isSafe(grid,currentPosition) == true)
{
solution[currentPosition.first][currentPosition.second] = 2;
if (checkPath(grid, make_pair(currentPosition.first+1,currentPosition.second),endPosition) == true)
return true;
if (checkPath(grid, make_pair(currentPosition.first-1,currentPosition.second),endPosition) == true)
return true;
if (checkPath(grid, make_pair(currentPosition.first,currentPosition.second+1),endPosition) == true)
return true;
if (checkPath(grid, make_pair(currentPosition.first,currentPosition.second-1),endPosition) == true)
return true;
solution[currentPosition.first][currentPosition.second] = 0;
return false;
}
return false;
}
bool solver(vector<vector<int>> grid,pair<int,int> startPosition,pair<int,int> endPosition,int R,int C){
solution.resize(R,vector<int>(C));
bool isPath = checkPath(grid,startPosition,endPosition);
printSolution(solution);
if(isPath==false){
cout<<"No Path Found"<<endl;
return false;
}
else{
cout<<"Path Found"<<endl;
return true;
}
}
This code is giving segmentation error. Please help to find it. I am stuck for almost a whole day to find where it is present.
So help me correct the logic of code. Assume I have following fields :
int R,C;
int grid[R][C];
pair<int,int> startPosition;
pair<int,int> endPosition;
This recursion is running infinite. i Checked for a simple case with startPosition as (1,0) and endPosition as (2,2) , R=3 and C=3 and grid as :
1 1 1
0 0 1
1 0 0
First, I tried with BFS, then start to make recursive solution.
When implementing a BFS path finding algorithm, it helps to have a shadow array that keeps track of the distance from the origin for all visited nodes.
int distance[R][C];
Initially, set all of the distances to -1 to indicate that the location has not been visited. Then set the distance at the origin to 0, and push the origin onto the stack.
While the stack is not empty, pop an item from the stack. For each adjacent location (that hasn't been visited) set the distance for that location, and push that location.
When you reach the end position, you can find the path by working backwards. Knowing the distance at the end position, the previous point on the path is an adjacent position that has been visited, and has a lower distance.

Implementing Alpha Beta into Minimax

I'm trying to add Alpha Beta pruning into my minimax, but I can't understand where I'm going wrong.
At the moment I'm going through 5,000 iterations, where I should be going through approximately 16,000 according to a friend. When choosing the first position, it is returning -1 (a loss) whereas it should be able to definitely return a 0 at this point (a draw) as it should be able to draw from an empty board, however I can't see where I'm going wrong as I follow my code it seems to be fine
Strangely if I switch returning Alpha and Beta inside my checks (to achieve returning 0) the computer will attempt to draw but never initiate any winning moves, only blocks
My logical flow
If we are looking for alpha:
If the score > alpha, change alpha. if alpha and beta are overlapping, return alpha
If we are looking for beta:
If the score < beta, change beta. if alpha and beta are overlapping, return beta
Here is my
Recursive call
int MinimaxAB(TGameBoard* GameBoard, int iPlayer, bool _bFindAlpha, int _iAlpha, int _iBeta)
{
//How is the position like for player (their turn) on iGameBoard?
int iWinner = CheckForWin(GameBoard);
bool bFull = CheckForFullBoard(GameBoard);
//If the board is full or there is a winner on this board, return the winner
if(iWinner != NONE || bFull == true)
{
//Will return 1 or -1 depending on winner
return iWinner*iPlayer;
}
//Initial invalid move (just follows i in for loop)
int iMove = -1;
//Set the score to be instantly beaten
int iScore = INVALID_SCORE;
for(int i = 0; i < 9; ++i)
{
//Check if the move is possible
if(GameBoard->iBoard[i] == 0)
{
//Put the move in
GameBoard->iBoard[i] = iPlayer;
//Recall function
int iBestPositionSoFar = -MinimaxAB(GameBoard, Switch(iPlayer), !_bFindAlpha, _iAlpha, _iBeta);
//Replace Alpha and Beta variables if they fit the conditions - stops checking for situations that will never happen
if (_bFindAlpha == false)
{
if (iBestPositionSoFar < _iBeta)
{
//If the beta is larger, make the beta smaller
_iBeta = iBestPositionSoFar;
iMove = i;
if (_iAlpha >= _iBeta)
{
GameBoard->iBoard[i] = EMPTY;
//If alpha and beta are overlapping, exit the loop
++g_iIterations;
return _iBeta;
}
}
}
else
{
if (iBestPositionSoFar > _iAlpha)
{
//If the alpha is smaller, make the alpha bigger
_iAlpha = iBestPositionSoFar;
iMove = i;
if (_iAlpha >= _iBeta)
{
GameBoard->iBoard[i] = EMPTY;
//If alpha and beta are overlapping, exit the loop
++g_iIterations;
return _iAlpha;
}
}
}
//Remove the move you just placed
GameBoard->iBoard[i] = EMPTY;
}
}
++g_iIterations;
if (_bFindAlpha == true)
{
return _iAlpha;
}
else
{
return _iBeta;
}
}
Initial call (when computer should choose a position)
int iMove = -1; //Invalid
int iScore = INVALID_SCORE;
for(int i = 0; i < 9; ++i)
{
if(GameBoard->iBoard[i] == EMPTY)
{
GameBoard->iBoard[i] = CROSS;
int tempScore = -MinimaxAB(GameBoard, NAUGHT, true, -1000000, 1000000);
GameBoard->iBoard[i] = EMPTY;
//Choosing best value here
if (tempScore > iScore)
{
iScore = tempScore;
iMove = i;
}
}
}
//returns a score based on Minimax tree at a given node.
GameBoard->iBoard[iMove] = CROSS;
Any help regarding my logical flow that would make the computer return the correct results and make intelligent moves would be appreciated
Does your algorithm work perfectly without alpha-beta pruning? Your initial call should be given with false for _bFindAlpha as the root node behaves like an alpha node, but it doesn't look like this will make a difference:
int tempScore = -MinimaxAB(GameBoard, NAUGHT, false, -1000000, 1000000);
Thus I will recommend for you to abandon this _bFindAlpha nonsense and convert your algorithm to negamax. It behaves identically to minimax but makes your code shorter and clearer. Instead of checking whether to maximize alpha or minimize beta, you can just swap and negate when recursively invoking (this is the same reason you can return the negated value of the function right now). Here's a slightly edited version of the Wikipedia pseudocode:
function negamax(node, α, β, player)
if node is a terminal node
return color * the heuristic value of node
else
foreach child of node
val := -negamax(child, -β, -α, -player)
if val ≥ β
return val
if val > α
α := val
return α
Unless you love stepping through search trees, I think that you will find it easier to just write a clean, correct version of negamax than debug your current implementation.