示例#1
0
        private void MenuOpen_Click(object sender, EventArgs e)
        {
            PgnFileStruct PGN;

            if (openPGNDialog.ShowDialog() == DialogResult.OK)
            {
                PGN         = pos.ReadPgnFile(openPGNDialog.FileName);
                MoveList    = PGN.MoveList;
                CommentList = PGN.CommentList;
            }
            else
            {
                return;
            }
            labelEvent.Text = PGN.Event;
            string result;

            switch (PGN.Result)
            {
            case "1-0":
                result = "胜";
                break;

            case "0-1":
                result = "负";
                break;

            case "1/2-1/2":
                result = "和";
                break;

            default:
                result = "*";
                break;
            }
            StringBuilder sb = new StringBuilder();

            sb.Append(PGN.RedTeam);
            sb.Append(' ');
            sb.Append(PGN.Red);
            sb.Append(" (先");
            sb.Append(result);
            sb.Append(") ");
            sb.Append(PGN.BlackTeam);
            sb.Append(' ');
            sb.Append(PGN.Black);
            labelPlayer.Text   = sb.ToString();
            labelDateSite.Text = PGN.Date + " 弈于 " + PGN.Site;


            ListboxMove.Items.Clear();

            if (string.IsNullOrEmpty(CommentList[0]))
            {
                ListboxMove.Items.Add("==开始==");
            }
            else
            {
                ListboxMove.Items.Add("==开始==*");
            }

            for (FENStep = 0; FENStep < MoveList.Count; FENStep++)
            {
                MOVE   step  = MoveList[FENStep];
                string label = step.ToString();
                if (FENStep % 2 == 0)
                {
                    label = ((FENStep / 2 + 1).ToString() + "." + label);
                }
                label = label.PadLeft(8);
                if (!string.IsNullOrEmpty(CommentList[FENStep + 1]))
                {
                    label += "*";
                }
                ListboxMove.Items.Add(label);
            }
            pos.FromFEN(PGN.StartFEN);
            engine.FromFEN(PGN.StartFEN);
            FENStep = 0;
            ListboxMove.SelectedIndex = 0;
            PanelBoard.Refresh();
            App_inGame = false;
        }
示例#2
0
        void MakeMove(int sqFrom, int sqTo)
        {
            Graphics g = PanelBoard.CreateGraphics();

            if (pos.IsLegalMove(sqFrom, sqTo))
            {
                int  pcCaptured = pos.pcSquares[sqTo];
                MOVE step       = new MOVE(sqFrom, sqTo, pos.pcSquares[sqFrom], pcCaptured);
                pos.MakeMove(step);
                if (pos.CheckedBy(1 - pos.sdPlayer) > 0)
                {
                    PlaySound("ILLEGAL");
                    pos.UnmakeMove();
                    return;
                }
                engine.MakeMove(step);
                MoveList.Add(step);
                CommentList.Add(textBoxComment.Text);

                if (FENStep > 0)
                {
                    //擦除上一步的起始和结束位置选择框
                    DrawBoard(ptLastFrom, g);
                    DrawBoard(ptLastTo, g);
                    DrawPiece(ptLastTo, pcLast, g);
                }

                ptLastFrom = POSITION.UI_Coord2XY(sqFrom, bFlipped);
                ptLastTo   = POSITION.UI_Coord2XY(sqTo, bFlipped);
                pcLast     = cnPieceImages[step.pcSrc];

                //擦除原来的位置
                DrawBoard(ptLastFrom, g);
                DrawSelection(ptLastFrom, g);
                //移动到新位置
                DrawSelection(ptLastTo, g);
                DrawPiece(ptLastTo, pcLast, g);
                bSelected = false;

                FENStep++;
                string label = step.ToString();
                if (FENStep % 2 == 1)
                {
                    label = ((FENStep / 2 + 1).ToString() + "." + label);
                }
                label = label.PadLeft(8);
                ListboxMove.Items.Add(label);
                if (pos.pcSquares[sqTo] > 0)
                {
                    PlaySound("CAPTURE");
                }
                else
                {
                    PlaySound("MOVE");
                }

                if (pos.IsMate())
                {//直接吃王或者绝杀
                    if (pos.sdPlayer == 1 && MenuAIBlack.Checked && !MenuAIRed.Checked ||
                        pos.sdPlayer == 0 && MenuAIRed.Checked && !MenuAIBlack.Checked)
                    {
                        PlaySound("WIN");
                    }
                    else if (pos.sdPlayer == 1 && !MenuAIBlack.Checked && MenuAIRed.Checked ||
                             pos.sdPlayer == 0 && !MenuAIRed.Checked && MenuAIBlack.Checked)
                    {
                        PlaySound("LOSS");
                    }
                    if (pos.sdPlayer == 0)
                    {
                        MessageBox.Show("黑方胜!");
                    }
                    else
                    {
                        MessageBox.Show("红方胜!");
                    }
                    App_inGame = false;
                }
            }
        }
示例#3
0
        public void TestEval()
        {
            //int Compare(KeyValuePair<string, int> a, KeyValuePair<string, int> b)
            //{
            //    return a.Value.CompareTo(b.Value);
            //}
            string sourceDirectory        = @"G:\象棋\全局\1-23届五羊杯";
            IEnumerable <string> pgnFiles = Directory.EnumerateFiles(sourceDirectory, "*.pgn", SearchOption.AllDirectories);
            int           nFile           = 0;
            int           totalMoves      = 0;
            int           totalSteps      = 0;
            List <double> redDelta        = new List <double>();
            List <double> blackDelta      = new List <double>();
            List <double> seq             = new List <double>();

            foreach (string fileName in pgnFiles)
            {
                Console.WriteLine(fileName.Substring(sourceDirectory.Length + 1));
                PgnFileStruct pgn       = ReadPgnFile(fileName);
                List <MOVE>   iMoveList = pgn.MoveList;
                nFile++;
                int nSteps = iMoveList.Count;
                totalSteps += nSteps;
                bool[] captures = new bool[nSteps];
                FromFEN(pgn.StartFEN);
                ivpc = new int[nSteps, 48];
                Complex_Evaluate();
                List <KeyValuePair <string, int> > mv_vals = new List <KeyValuePair <string, int> >();
                for (int i = 1; i < nSteps; i++)
                {
                    MOVE step = iMoveList[i];
                    captures[i] = pcSquares[step.sqDst] > 0;
                    if (pcSquares[step.sqDst] == 0)
                    {
                        mv_vals.Clear();
                        //List<MOVE> moves = GenerateMoves();
                        int    bookmovevalue = 0;
                        string bookmovekey   = step.ToString();
                        foreach (MOVE move in GenerateMoves())
                        {
                            if (move.pcDst == 0)
                            {
                                MovePiece(move);
                                string key = move.ToString();
                                int    val = Complex_Evaluate();
                                mv_vals.Add(new KeyValuePair <string, int>(key, val));
                                UndoMovePiece(move);
                                if (key == bookmovekey)
                                {
                                    bookmovevalue = val;
                                }
                            }
                        }
                        if (i % 2 == 0) //从小到大排序
                        {
                            mv_vals.Sort(delegate(KeyValuePair <string, int> a, KeyValuePair <string, int> b)
                                         { return(a.Value.CompareTo(b.Value)); });
                        }
                        else  //从大到小排序
                        {
                            mv_vals.Sort(delegate(KeyValuePair <string, int> a, KeyValuePair <string, int> b)
                                         { return(b.Value.CompareTo(a.Value)); });
                        }
                        int index = mv_vals.IndexOf(new KeyValuePair <string, int>(bookmovekey, bookmovevalue));
                        seq.Add(index);
                        //totalMoves += moves.Count;
                        Console.WriteLine("{0}. Book move: {1} {2}", i, bookmovekey, index);
                        foreach (var m in mv_vals)
                        {
                            //Console.WriteLine("{0}. {1} {2} / {3}", j++, m.Key, m.Value, moves.Count);
                        }
                    }

                    MakeMove(step, true);
                    Console.WriteLine("-------------------");
                }
                for (int i = 1; i < nSteps; i += 2)
                {
                    if (!captures[i])
                    {
                        redDelta.Add(ivpc[i, 1] - ivpc[i - 1, 1]);
                    }
                }

                for (int i = 2; i < nSteps; i += 2)
                {
                    if (!captures[i])
                    {
                        blackDelta.Add(ivpc[i, 1] - ivpc[i - 1, 1]);
                    }
                }
                break;
            }
            double redMean = Statistics.Mean(redDelta);
            double redVar  = Statistics.Variance(redDelta);

            Console.WriteLine("Red mean:{0}, var:{1}", redMean, redVar);
            double blackMean = Statistics.Mean(blackDelta);
            double blackVar  = Statistics.Variance(blackDelta);

            Console.WriteLine("Black mean:{0}, var:{1}", blackMean, blackVar);
            Console.WriteLine("Score: red{0}, black{1}, average{2}", redVar / redMean, blackVar / blackMean, (redVar + blackVar) / (redMean - blackMean));
            Console.WriteLine("Move average sequence: {0} of {1}", Statistics.Mean(seq), totalMoves / totalSteps);
        }