0

I am making a class to read a chess board from the screen. In my function to check if a square is empty I use the GetPixel().GetBrightness() to check if the square contains a piece. GetPixel() seems to hang when I debug. (it never reaches the next line). Yet I reach the printing phase in my main() without getting errors.

private static bool IsSquareEmpty(Bitmap square, bool isSquareWhite)
{
    if (isSquareWhite)
    {
        var returnable = square.GetPixel(43, 25);
        var test = returnable.GetBrightness();
        var returnable2 = test >= minBrightnessOfWhiteSquare;
        return returnable2;
    }
    else
    {
        var returnable = square.GetPixel(43, 25).GetBrightness();
        var returnable2 = returnable >= minBrightnessOfWhiteSquare && returnable <= maxBrightnessOfBlackSquare;
        return returnable2;
    }
}

I am currently trying to learn async programming so the issue might have to do with this, altough it does not seem to go wrong there while debugging. I have included my full class so far below.

The full code:

Board.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;

namespace chessbot
{
    internal class Board
    {
        private static char[][] Bord { get; set; }
        public static float minBrightnessOfWhitePiece = 80;
        public static float maxBrightnessOfBlackPiece = 30;
        public static float minBrightnessOfWhiteSquare = 90;
        public static float maxBrightnessOfBlackSquare = 50;

        public static async Task<char[][]> GetBoardAsync()
        {
            GetEmptyBoard();
            await FillBoard();
            return Bord;
        }

        private static async Task FillBoard()
        {
            var listOfTasks = new List<Task>();

            for (int i = 0; i < Bord.Length; i++)
            {
                for (int j = 0; j < Bord[i].Length; j++)
                {
                    listOfTasks.Add(FindWhatIsInThisSquareAsync(i, j));
                }
            }

            while (listOfTasks.Count > 0)
            {
                listOfTasks.Remove(await Task.WhenAny(listOfTasks));
            }
        }

        private static async Task FindWhatIsInThisSquareAsync(int i, int j)
        {
            Bitmap square = getPrintScreenForSquare(i, j);
            bool isSquareWhite = (i + j) % 2 == 0;

            if (IsSquareEmpty(square, isSquareWhite))
            {
                Bord[i][j] = ' ';
            }
            else
            {
                Bord[i][j] = GetValueForPiece();
            }
        }

        private static char GetValueForPiece()
        {
            return 'p';
        }

        private static bool IsSquareEmpty(Bitmap square, bool isSquareWhite)
        {
            if (isSquareWhite)
            {
                var returnable = square.GetPixel(43, 25);
                var test = returnable.GetBrightness();
                var returnable2 = test >= minBrightnessOfWhiteSquare;
                return returnable2;
            }
            else
            {
                var returnable = square.GetPixel(43, 25).GetBrightness();
                var returnable2 = returnable >= minBrightnessOfWhiteSquare && returnable <= maxBrightnessOfBlackSquare;
                return returnable2;
            }
        }

        private static Bitmap getPrintScreenForSquare(int i, int j)
        {
            Rectangle bounds = new Rectangle(308 + i * 99, 170 + j * 99, 95, 95);
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(new Point(bounds.X, bounds.Y), Point.Empty, bounds.Size);
                }
                return bitmap;
            }
        }

        private static void GetEmptyBoard()
        {
            Bord = new char[8][];
            for (int i = 0; i < Bord.Length; i++)
            {
                Bord[i] = new char[8] { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
            }
        }

    }
}

Program.cs

using Segment.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace chessbot
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var bord = await Board.GetBoardAsync();
            printBord(bord);
        }

        private static void printBord(char[][] arr)
        {
            int rowLength = 8;
            int colLength = 8;

            for (int i = 0; i < rowLength; i++)
            {
                for (int j = 0; j < colLength; j++)
                {
                    Console.Write(string.Format("{0} ", arr[i][j]));
                }
                Console.Write(Environment.NewLine + Environment.NewLine);
            }
        }
    }
}
Stef
  • 11

0 Answers0