示例#1
0
        private void InitialiseStartingPieces()
        {
            var ranks = new[] { '1', '2', '7', '8' };

            for (char file = 'a'; file <= 'h'; ++file)
            {
                foreach (char rank in ranks)
                {
                    var si  = new SquareIdentifier(file, rank);
                    var key = KeyFromSquare(si);
                    _currentToOrig[key] = key;
                    _origToCurrent[key] = key;
                }
            }
        }
示例#2
0
        public int MaybeCapture(SquareIdentifier sq, int time)
        {
            var atCurrent = _currentToOrig[KeyFromSquare(sq)];

            if (atCurrent >= 0)
            {
                Debug.Assert(!IsKing(atCurrent));

                _origToCurrent[atCurrent]         = -1;
                _currentToOrig[KeyFromSquare(sq)] = -1;

                _tod.Add(GetStringRep(atCurrent), time);
            }

            return(atCurrent);
        }
示例#3
0
        public int Move(SquareIdentifier from, SquareIdentifier to)
        {
            var atCurrent = _currentToOrig[KeyFromSquare(from)];

            System.Diagnostics.Debug.Assert(atCurrent >= 0);
            System.Diagnostics.Debug.Assert(_origToCurrent[atCurrent] == KeyFromSquare(from));

            var atDest = _currentToOrig[KeyFromSquare(to)];

            System.Diagnostics.Debug.Assert(atDest < 0);

            _currentToOrig[KeyFromSquare(from)] = -1;
            _currentToOrig[KeyFromSquare(to)]   = atCurrent;

            _origToCurrent[atCurrent] = KeyFromSquare(to);

            return(atCurrent);
        }
示例#4
0
 public bool Occupied(SquareIdentifier sq)
 {
     return(_currentToOrig[KeyFromSquare(sq)] >= 0);
 }
示例#5
0
 private static int KeyFromSquare(SquareIdentifier sq)
 {
     return(KeyFromSquare(sq.File, sq.Rank));
 }
示例#6
0
        static void Main(string[] args)
        {
            Dictionary <string, int>         _stats = new Dictionary <string, int>();
            Dictionary <string, List <int> > _tods  = new Dictionary <string, List <int> >();

            var file = new System.IO.StreamReader(string.Join(" ", args));
            var db   = Games(file);

            // Initialise stats
            for (char f = 'a'; f <= 'h'; f++)
            {
                for (char r = '1'; r <= '8'; r++)
                {
                    string s = string.Format("{0}{1}", f, r);
                    if (r == '1' || r == '2' || r == '7' || r == '8')
                    {
                        _stats.Add(s, 0);
                        _tods.Add(s, new List <int>(Enumerable.Repeat(0, 500)));
                    }
                }
            }

            int nGame     = 0;
            int nRejected = 0;

            foreach (var g in db)
            {
                nGame++;

                var moves = new List <string>(g);

                try
                {
                    var plist = new PieceLists();

                    if (moves.Count < 5)
                    {
                        throw new InvalidOperationException("Game too short"); // ignore ridiculously short or empty games
                    }
                    int ply = 0;

                    foreach (var m in moves)
                    {
                        int nMove = (ply / 2) + 1;
                        var mv    = ResolveMove(m);

                        // make the move
                        if (mv.castle)
                        {
                            var rookFrom = GetRookStartSquare(mv);
                            var rookTo   = GetRookEndSquare(mv);

                            plist.Move(rookFrom, rookTo);
                            var movedKing = plist.Move(mv.from, mv.to);
                            Debug.Assert(PieceLists.IsKing(movedKing));
                        }
                        else
                        {
                            if (mv.ep)
                            {
                                var epRank       = mv.to.Rank == '6' ? '5' : '4';
                                var epPawnSquare = new SquareIdentifier(mv.to.File, epRank);

                                var captureResult = plist.MaybeCapture(epPawnSquare, nMove);
                                Debug.Assert(captureResult >= 0);
                            }
                            else
                            {
                                var captureResult = plist.MaybeCapture(mv.to, nMove);
                            }

                            plist.Move(mv.from, mv.to);
                        }

                        ply++;
                    }

                    nMaxMove = Math.Max(nMaxMove, ply / 2);

                    var survivors = plist.GetSurvivors();
                    foreach (var piece in survivors)
                    {
                        _stats[piece]++;
                    }

                    foreach (var deathTime in plist.TimeOfDeath)
                    {
                        _tods[deathTime.Key][deathTime.Value]++;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception thrown in game {0}", nGame);
                    Console.WriteLine(ex.ToString());
                    Console.WriteLine(string.Join(" ", moves));
                    Console.WriteLine();
                    nRejected++;
                }
                if (nGame % 10000 == 0)
                {
                    Console.WriteLine("done {0} games", nGame);
                    PrintStats(_stats, _tods);
                }
            }

            PrintStats(_stats, _tods);

            Console.WriteLine("{0} games could not be read and were ignored", nRejected);
            Console.ReadLine();
        }