English 中文(简体)
无法将变数设定在 for形体内的价值(变数已申报)。 C#
原标题:Unable to have a variable be set a value inside of a foreach loop (the variable had been declared before it). C#
         int PositionEvalueation = 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 > PositionEvalueation)
                {
                    //Record this to then play + for seeing its thinking procces in Console
                    FirstDepth = move;
                    SecondDepth = move1;


                    PositionEvalueation = 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);

在上述法典中,我宣布每 lo前的变数,而每 lo的变数则按预期价值计算。 但在那里,我试图写这封信,以便赶上这段话,并与它一道行动,却告诉我,这是没有签名的。

我曾试图为其面前的变数确定价值,但仅仅保留这一价值而没有改变:

Move FirstDepth = moveToPlay; Move SecondDepth = moveToPlay;

我也试图设定一种临时价值,以获得这一价值,但也做了一些工作。

For more context, this is part of my attempt at a tiny chess bot contest which was created by SebLeague. Here is a link to it: https://github.com/SebLague/Chess-Challenge Here is full file of 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 PositionEvalueation = 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 > PositionEvalueation)
                {
                    //Record this to then play + for seeing its thinking procces in Console
                    FirstDepth = move;
                    SecondDepth = move1;


                    PositionEvalueation = 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;
    }
}
问题回答

为了解决你的问题,你需要提供<>一级和<>二级>和初步数值”。

//  Give these two a value
Move FirstDepth = null;
Move SecondDepth = null;

然后是:

if (CurrentEvaluation1 > PositionEvalueation)

两个变量在封顶休息室的条件中被分配到价值之内,如果条件永远不会真实,那么这两个变数仍未转让。

Hope this helps!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签