Beispiel #1
0
    public FastMove GetMove(FastBoardNode root)
    {
        moveGenTimer.Reset();
        moveSortTimer.Reset();
        moveValidateTimer.Reset();
        evalTimer.Reset();
        evalThreatsTimer.Reset();
        applyTimer.Reset();
        getMoveTimer.Restart();
        boardEvaluations = 0;
        invalidMoves     = 0;

        int color = (root.currentMove == Team.White) ? 1 : -1;

        int      bestMoveValue = -CheckmateValue * 3;
        FastMove bestMove      = FastMove.Invalid;

        for (int searchDepth = 1; searchDepth <= maxSearchDepth; searchDepth++)
        {
            int alpha = -CheckmateValue * 2; // Best move for current player
            int beta  = CheckmateValue * 2;  // Best move our opponent will let us have

            (int moveValue, FastMove move) = Search(root, searchDepth, 0, alpha, beta, color);

            bestMoveValue = moveValue;
            bestMove      = move;

            if (moveValue >= (CheckmateValue - searchDepth))
            {
                return(bestMove);
            }
        }

        return(bestMove);
    }
Beispiel #2
0
    public FastMove GetMove(FastBoardNode root)
    {
        diagnostics = new DiagnosticInfo();
        using (diagnostics.getMove.Measure())
        {
            previousScores.Clear();

            int color          = (root.currentMove == Team.White) ? 1 : -1;
            int minSearchDepth = iterativeDeepeningEnabled ? 1 : maxSearchDepth;
            bestMove              = FastMove.Invalid;
            currentDepth          = minSearchDepth;
            cancellationRequested = false;

            for (; currentDepth <= maxSearchDepth; currentDepth++)
            {
                bestMoveThisIteration = FastMove.Invalid;
                int alpha     = -CheckmateValue * 2; // Best move for current player
                int beta      = CheckmateValue * 2;  // Best move our opponent will let us have
                int moveValue = Search(root, currentDepth, 0, alpha, beta, color);

                bestMove = bestMoveThisIteration;

                if (moveValue >= (CheckmateValue - currentDepth))
                {
                    return(bestMove);
                }
            }

            return(bestMove);
        }
    }
Beispiel #3
0
    private int MoveValuer(FastBoardNode node, FastMove move)
    {
        int value = 1000; // Start high to always exceed invalid move scores of 0

        var mover         = node[move.start];
        int attackerValue = GetPieceValue(mover.piece);

        EvaluationData.BoardCollection us;
        EvaluationData.BoardCollection them;
        if (node.currentMove == Team.White)
        {
            us   = evaluationData.White;
            them = evaluationData.Black;
        }
        else
        {
            us   = evaluationData.Black;
            them = evaluationData.White;
        }

        // Devalue moving into threatened hexes
        if (them.Threats[move.target])
        {
            value -= attackerValue / 2;
        }

        // Value moving threatened pieces
        if (them.Threats[move.start])
        {
            value += attackerValue / 2;
        }

        if (move.moveType == MoveType.Move)
        {
            // TODO: what else?
        }
        else if (move.moveType == MoveType.Attack)
        {
            bool isFree = !them.Threats[move.target];
            var  victim = node[move.target];
            if (isFree)
            {
                value += GetPieceValue(victim.piece) / 2;
            }
            else
            {
                int attackValue = GetPieceValue(victim.piece) - attackerValue;
                value += attackValue / 10;
            }
        }
        else if (move.moveType == MoveType.EnPassant)
        {
            value += 5;
        }

        return(value);
    }
Beispiel #4
0
        private async Task ReadMovesetExcelData(SLDocument document)
        {
            document.SelectWorksheet("Movesets");
            int row = 2, col = 2;

            while (!string.IsNullOrWhiteSpace(document.GetCellValueAsString(row, col)))
            {
                col = 2;
                string       name    = document.GetCellValueAsString(row, col);
                PokedexEntry species = this.Pokedex.SingleOrDefault(x => x.Species.Equals(name));
                if (species == null)
                {
                    Debug.WriteLine($"Could not find species {name}");
                    row++;
                    continue;
                }
                species.FastMoves.Clear();
                species.ChargeMoves.Clear();
                for (int i = 0; i < 2; i++)
                {
                    string moveName = document.GetCellValueAsString(row, 3 + i);
                    if (!string.IsNullOrWhiteSpace(moveName))
                    {
                        FastMove fastMove = await Task.Run(() => this.FastMoveList.SingleOrDefault(x => x.Name.Equals(moveName)));

                        if (fastMove != null)
                        {
                            species.FastMoves.Add(new PokedexFastMoveWrapper(fastMove));
                        }
                    }
                }
                int index = 0;
                while (!string.IsNullOrWhiteSpace(document.GetCellValueAsString(row, 5 + index)))
                {
                    string     moveName   = document.GetCellValueAsString(row, 5 + index);
                    ChargeMove chargeMove = await Task.Run(() => this.ChargeMoveList.SingleOrDefault(x => x.Name.Equals(moveName)));

                    if (chargeMove != null)
                    {
                        species.ChargeMoves.Add(new PokedexChargeMoveWrapper(chargeMove));
                    }
                    index++;
                }
                row++;
            }
        }
Beispiel #5
0
        public void Migrate(string dest)
        {
            this.Scan();

            Console.WriteLine("Migrating game: " + this.Name);

            if (!Directory.Exists(dest))
            {
                Directory.CreateDirectory(dest);
            }

            foreach (string s in Folders)
            {
                string path = dest + s;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }

            for (int i = 0; i < GameFiles.Length; i++)
            {
                MigrationFile item = new MigrationFile()
                {
                    source      = Location + GameFiles[i].Location,
                    destination = dest + GameFiles[i].Location,
                    size        = GameFiles[i].Size,
                    Status      = MigrationStatus.Pending
                };
                FastMove.MoveGameItem(ref item);

                //GameFiles[i].MovingItem = item;
            }

            /*
             * Serializer<GameFile[]>
             *  .WriteToJSONFile(GameFiles
             *  .Where(x => x.MovingItem.Status == MigrationStatus.Failed)
             *  .ToArray(), Path.Combine(dest, "failed.json"));
             */
            Location = dest;
            Scan();
        }
Beispiel #6
0
    int Search(FastBoardNode node, int searchDepth, int plyFromRoot, int alpha, int beta, int color)
    {
        if (searchDepth == 0)
        {
            using (diagnostics.quiescence.Measure())
                return(QuiescenceSearch(node, plyFromRoot, alpha, beta, color));
        }

        List <FastMove> moves;

        using (diagnostics.moveGen.Measure())
        {
            moves = moveCache[searchDepth];
            moves.Clear();
            node.AddAllPossibleMoves(moves, node.currentMove);
        }

        evaluationData.Prepare(node);

        using (diagnostics.moveSort.Measure())
        {
            OrderMoves(node, moves, plyFromRoot);
        }

        bool isTerminal = true;
        int  value      = int.MinValue;

        foreach (var move in moves)
        {
            if (cancellationRequested)
            {
                return(0);
            }

            using (diagnostics.apply.Measure())
                node.DoMove(move);

            bool isKingVulnerable;
            using (diagnostics.moveValidate.Measure())
                isKingVulnerable = node.IsChecking(node.currentMove);

            if (isKingVulnerable)
            {
                diagnostics.invalidMoves++;
                using (diagnostics.apply.Measure())
                    node.UndoMove(move);
                continue;
            }

            isTerminal = false;
            int currentValue = -Search(node, searchDepth - 1, plyFromRoot + 1, -beta, -alpha, -color);

            if (previousOrderingEnabled && plyFromRoot == 0)
            {
                previousScores[move] = currentValue;
            }

            using (diagnostics.apply.Measure())
                node.UndoMove(move);

            if (currentValue > value)
            {
                if (plyFromRoot == 0)
                {
                    bestMoveThisIteration = move;
                }
                value = currentValue;
            }
            alpha = Math.Max(alpha, value);
            if (alpha >= beta)
            {
                diagnostics.searchCutoff++;
                break;
            }
        }

        if (isTerminal)
        {
            value = color * EvaluateTerminalBoard(node, plyFromRoot);
        }

        return(value);
    }
Beispiel #7
0
 public ScoredMove(FastMove move, int score)
 {
     this.move  = move;
     this.score = score;
 }
Beispiel #8
0
        private void button1_Click(object sender, EventArgs e)
        {
            Game game = new Game(source.Text);

            FastMove.MigrateGame(game, dest.Text);
        }