public async Task NextMove() { Directory.GetFiles(@".\", @"*.png").ToList().ForEach((f) => { File.Delete(f); }); var check = new List <(int, int)>(); var movedBoard = MoveOneBoard ?? CurrentBoard; for (int y = 0; y < 6; y += 1) { for (int x = 0; x < 6; x += 1) { var num = movedBoard[x + (y * 6)]; if (num.IsEmpty || num.IsMatched || num.IsTrail) { check.Add((x, y)); } } } MoveOneBoard = null; MoveTwoBoard = null; var nums = CurrentBoard.Select(n => n.Number).ToArray(); var nextNums = await CaptureScreen.CaptureNextNumbers(iphoneScreen); await CaptureScreen.Capture(iphoneScreen, nums, check); var squares = new Square[6, 6]; var curBoard = new Square[36]; var futureSpots = new List <(int, int)>(); for (int y = 0; y < 6; y += 1) { for (int x = 0; x < 6; x += 1) { var num = nums[x + (y * 6)]; if (num > 0 && num < 10) { squares[x, y] = new Square(num) { IsEmpty = false }; } else { squares[x, y] = new Square(0) { IsEmpty = true }; } curBoard[x + (y * 6)] = squares[x, y]; if (num == 10) { futureSpots.Add((x, y)); } } } CurrentBoard = curBoard; await Task.Yield(); var board = new Board(squares); var blockSquares = new Square[] { Square.BlockSquare(), Square.BlockSquare(), Square.BlockSquare() }; Square[] newSquares = new Square[] { new Square(nextNums[0]), new Square(nextNums[1]), new Square(nextNums[2]) }; NextNumbers = nextNums; var newDup = newSquares.Select(x => x.Number).Distinct().Count(); if (newDup == 1 && nextNums[0] != 0) { blockSquares = newSquares; } var nextMoves = Turn.Moves(board, blockSquares, futureSpots); // BEST!!! var bestMoves = nextMoves .OrderBy(m => m.Board.Count) .ThenBy(m => m.ParentMove.Board.Count) .ThenByDescending(m => m.ParentMove.Board.Points) .ThenByDescending(m => m.Board.Points) .ThenBy(m => m.Board.CenterCount) .ThenBy(m => m.Board.Sum) .FirstOrDefault(); if (bestMoves != null) { var bestMove = bestMoves.ParentMove; var newNumbers = new Square[36]; var secondMove = new Square[36]; for (int y = 0; y < 6; y += 1) { for (int x = 0; x < 6; x += 1) { var s = bestMove.Board.Squares[x, y]; newNumbers[x + (y * 6)] = s; secondMove[x + (y * 6)] = bestMoves.Board[x, y]; } } MoveOneBoard = newNumbers; MoveTwoBoard = secondMove; } else { MoveOneBoard = null; MoveTwoBoard = null; } }