예제 #1
0
        static public MoveList getMovesToPosition(IEnumerable <Move> moves, Square pos)
        {
            MoveList moveList = new MoveList();

            foreach (Move move in moves)
            {
                if (move.MovePosition == pos)
                {
                    moveList.Add(move);
                }
            }
            return(moveList);
        }
예제 #2
0
        private IEnumerable <Move> getAllMoves()
        {
            MoveList      moves     = new MoveList();
            List <Square> positions = new List <Square>(gamestate.getPiecePositions());

            foreach (Square pos in positions)
            {
                Piece         piece         = gamestate.GetPiece(pos);
                PossibleMoves possibleMoves = new PossibleMoves(gamestate, pos);
                foreach (Move move in possibleMoves)
                {
                    moves.Add(move);
                }
            }
            return(moves);
        }
예제 #3
0
        private Move calculateBestMove(IEnumerable <Move> possibleMoves, uint depth, ref UInt64 candidateCount)
        {
            Console.WriteLine("Please wait. Computer is thinking... ");
            Stack <Move> moveStack = new Stack <Move>();
            MoveList     bestMoves = null;
            Optimize     optimize  = ((depth & 1) == 1) ? Optimize.Lowest : Optimize.Highest;
            int          bestScore = (optimize == Optimize.Highest) ? int.MinValue : int.MaxValue;

            foreach (Move move in possibleMoves)
            {
                Console.Write(".");
                Gamestate   testState   = new Gamestate(gamestate);
                GameControl testControl = new GameControl(testState);
                testControl.makeMove(move);
                Piece piece = gamestate.GetPiece(move.StartPosition);
                //for (int d = 0; d <= 4 - depth; d++) Console.Write("  ");
                //Console.WriteLine(" considering move " + gamestate.turnColor + " " + piece.Type + " from " + move.StartPosition + " to " + move.MovePosition);
                int score = testControl.calculateScore(depth, ref candidateCount, (optimize == Optimize.Highest) ? Optimize.Lowest : Optimize.Highest);
                //for (int d = 0; d <= 4 - depth; d++) Console.Write("  ");
                //Console.WriteLine(" -> score " + score);
                if (optimize == Optimize.Highest && score > bestScore || optimize == Optimize.Lowest && score < bestScore)
                {
                    bestScore = score;
                    bestMoves = new MoveList();
                    bestMoves.Add(move);
                }
                else if (score == bestScore)
                {
                    bestMoves.Add(move);
                }
            }
            Random rand      = new Random();
            int    i         = rand.Next(bestMoves.Count);
            Move   bestMove  = bestMoves[i];
            Piece  bestPiece = gamestate.GetPiece(bestMove.StartPosition);

            Console.WriteLine();
            Console.WriteLine("Best of " + candidateCount + " moves is " + bestPiece.Color + " " + bestPiece.Type + " from " + bestMove.StartPosition + " to " + bestMove.MovePosition + " with score " + bestScore);
            return(bestMove);
        }