int PositionEvaluation = 0;
Move FirstDepth;
Move SecondDepth;
foreach (Move move in FirstDepthMoves)
{
// Try a move
board.MakeMove(move);
Move[] SecondDepthMoves = board.GetLegalMoves();
foreach (Move move1 in SecondDepthMoves)
{
// Try a second move
board.MakeMove(move1);
int CurrentEvaluation1 = EvaluatePosition(board, !IsBotWhite);
// If move be good, then we memorize it
if (CurrentEvaluation1 > PositionEvaluation)
{
// Record this to then play + for seeing its thinking process in Console
FirstDepth = move;
SecondDepth = move1;
PositionEvaluation = CurrentEvaluation1;
}
board.UndoMove(move1);
}
board.UndoMove(move);
}
// Here it tells me that it didn't get the value assigned to FirstDepth and SecondDepth from foreach loop
// Idk what to do else about it
Console.WriteLine(FirstDepth);
board.MakeMove(FirstDepth);
Console.WriteLine(EvaluatePosition(board, !IsBotWhite));
Console.WriteLine(SecondDepth);
board.MakeMove(SecondDepth);
Console.WriteLine(EvaluatePosition(board, !IsBotWhite));
board.UndoMove(SecondDepth);
board.UndoMove(FirstDepth);
在上面的代码中,我在 foreach 循环之前声明一个变量,然后在每个循环中为它分配所需的值。但是当我尝试将其写入控制台并对其进行移动时,它告诉我它未分配。
我曾尝试为之前的变量设置一个值,但这只是保留了该值而不更改它:
Move FirstDepth = moveToPlay;
Move SecondDepth = moveToPlay;
我也尝试过设置一个临时值来获取它,但也没有成功。
为了了解更多背景信息,这是我尝试由 SebLeague 创建的小型国际象棋机器人竞赛的一部分。这是它的链接:https://github.com/SebLague/Chess-Challenge
这是 MyBot 的完整文件:
using ChessChallenge.API;
using System;
public class MyBot : IChessBot
{
const PieceType Pawn = (PieceType)1;
const PieceType Knight = (PieceType)2;
const PieceType Bishop = (PieceType)3;
const PieceType Rook = (PieceType)4;
const PieceType Queen = (PieceType)5;
public Move Think(Board board, Timer timer)
{
bool IsBotWhite = board.IsWhiteToMove;
Move[] FirstDepthMoves = board.GetLegalMoves();
Random rng = new();
Move moveToPlay = FirstDepthMoves[rng.Next(FirstDepthMoves.Length)];
int PositionEvaluation = 0;
Move FirstDepth = moveToPlay;
Move SecondDepth = moveToPlay;
foreach (Move move in FirstDepthMoves)
{
//Try a move
board.MakeMove(move);
FirstDepth = moveToPlay;
SecondDepth = moveToPlay;
Move[] SecondDepthMoves = board.GetLegalMoves();
foreach (Move move1 in SecondDepthMoves)
{
// Try a second move
board.MakeMove(move1);
int CurrentEvaluation1 = EvaluatePosition(board, !IsBotWhite);
// If move be good, then we memorize it
if (CurrentEvaluation1 > PositionEvaluation)
{
//Record this to then play + for seeing its thinking process in Console
FirstDepth = move;
SecondDepth = move1;
PositionEvaluation = CurrentEvaluation1;
}
board.UndoMove(move1);
}
board.UndoMove(move);
}
// Here it tells me that It didnt get the value assigned to FirstDepth and SecondDepth from foreach loop
//Idk what to do else about it
Console.WriteLine(FirstDepth);
board.MakeMove(FirstDepth);
Console.WriteLine(EvaluatePosition(board, !IsBotWhite));
Console.WriteLine(SecondDepth);
board.MakeMove(SecondDepth);
Console.WriteLine(EvaluatePosition(board, !IsBotWhite));
board.UndoMove(SecondDepth);
board.UndoMove(FirstDepth);
return FirstDepth;
}
int EvaluatePosition(Board board, bool IsWhite)
{
//the E stands for Enemy
PieceList Pawns = board.GetPieceList(pieceType: Pawn, white: IsWhite);
PieceList Knights = board.GetPieceList(pieceType: Knight, white: IsWhite);
PieceList Bishops = board.GetPieceList(pieceType: Bishop, white: IsWhite);
PieceList Rooks = board.GetPieceList(pieceType: Rook, white: IsWhite);
PieceList Queens = board.GetPieceList(pieceType: Queen, white: IsWhite);
bool IsWhiteE = !IsWhite;
PieceList PawnsE = board.GetPieceList(pieceType: Pawn, white: IsWhiteE);
PieceList KnightsE = board.GetPieceList(pieceType: Knight, white: IsWhiteE);
PieceList BishopsE = board.GetPieceList(pieceType: Bishop, white: IsWhiteE);
PieceList RooksE = board.GetPieceList(pieceType: Rook, white: IsWhiteE);
PieceList QueensE = board.GetPieceList(pieceType: Queen, white: IsWhiteE);
int TotalEval = ((Pawns.Count * 1) + (Knights.Count * 3) + (Bishops.Count * 3) + (Rooks.Count * 5) + (Queens.Count * 9)) - ((PawnsE.Count * 1) + (KnightsE.Count * 3) + (BishopsE.Count * 3) + (RooksE.Count * 5) + (QueensE.Count * 9));
if (board.IsDraw())
{
TotalEval = -100;
}
else if (board.IsInCheckmate())
{
TotalEval = 100;
}
if (board.IsInCheck())
{
TotalEval = TotalEval + 1;
}
return -TotalEval;
}
}