Exemplo n.º 1
0
 public void TestAllPossibleMoves(bool whitePlayer)
 {
     Console.WriteLine("All possible moves:");
     foreach (int[] coords in Logic.AllPossibleMoves(board, whitePlayer))
     {
         foreach (int i in coords)
         {
             Console.Write($"{i} ");
         }
         Console.WriteLine();
     }
     Console.WriteLine("End of coordinates.");
 }
Exemplo n.º 2
0
        private int[] BestMove(Board board, int left)
        {
            int[]  bestMove = new int[4];
            double max      = 0;
            double min      = 0;

            int[][] moves   = Logic.AllPossibleMoves(board, white);
            int[][] threats = Logic.Threats(board, white, true);
            foreach (int[] t in threats)
            {
                int newMax = board.GetPieceAt(t[0], t[1]).Value() - board.GetPieceAt(t[2], t[3]).Value();
                if (max < newMax)
                {
                    max      = newMax;
                    bestMove = t;
                }
                if (max != 0)
                {
                    return(bestMove);
                }
            }
            foreach (int[] m in moves)
            {
                Board newBoard = new Board(board.AfterMove(m));
                if (Logic.IsCheckMate(newBoard, white))
                {
                    return(m);
                }
                if (ValueBoard(board) > ValueBoard(newBoard))
                {
                    continue;
                }
                int[][] enemyMoves = Logic.AllPossibleMoves(newBoard, !white);

                foreach (int[] e in enemyMoves)
                {
                    Board newNewBoard = new Board(newBoard.AfterMove(e));
                    if (ValueBoard(board) > ValueBoard(newNewBoard))
                    {
                        continue;
                    }
                    if (left == 0)
                    {
                        double newMin = ValueBoard(newNewBoard);
                        if (newMin <= min)
                        {
                            min = newMin;
                        }
                    }
                    else
                    {
                        bestMove = BestMove(newNewBoard, left - 1);
                    }
                }
                if (max < min)
                {
                    max      = min;
                    bestMove = m;
                }
            }
            if (bestMove == null)
            {
                bestMove = moves[rng.Next(moves.Length)];
            }
            return(bestMove);
        }