Exemple #1
0
        private static string ProcessGame(ThorGame game, int year, IDictionary <int, string> tournaments, IDictionary <int, string> players)
        {
            //Console.WriteLine();
            //Console.WriteLine("{0} ({1})", tournaments[game.TournamentId], year);
            //Console.WriteLine("{0} vs {1}", players[game.BlackId], players[game.WhiteId]);
            //Console.WriteLine("Black Score: {0}", game.BlackScore);

            var plays = GameManager.DeserialsePlays(game.SerialisedPlays);

            var gameManager = new GameManager
            {
                BlackName = players[game.BlackId],
                WhiteName = players[game.WhiteId],
            };

            plays.ForEach(play =>
            {
                if (!gameManager.CanPlay((short)play))
                {
                    gameManager.PlacePiece(null);
                    gameManager.NextTurn();
                }

                gameManager.PlacePiece(play);
                gameManager.NextTurn();
            });

            Console.WriteLine(GameManager.SerialsePlays(plays));

            if (gameManager.IsGameOver)
            {
                if (game.BlackScore != gameManager.BlackScore)
                {
                    throw new Exception(string.Format("At gameover, calculated score ({0}) does not match recorded score ({1}).", gameManager.BlackScore, game.BlackScore));
                }

                var stringBuilder = new StringBuilder(gameManager.Plays.ToChars());
                stringBuilder.Append(",");
                stringBuilder.Append(gameManager.Winner);

                return(stringBuilder.ToString());
            }

            return(null);



            //else if (game.BlackScore != 0 && game.BlackScore != gameManager.BlackPieces.CountBits())
            //    Console.WriteLine(string.Format("At resignation, number of black pieces ({0}) does not match recorded score ({1}).", gameManager.BlackPieces.CountBits(), game.BlackScore));
        }
Exemple #2
0
        public static KeyValuePair <int, List <ThorGame> > ReadThorDb(string f)
        {
            const int fileHeaderLength = 16;
            int       gameHeaderLength = 8;
            int       numberOfPlays    = 60;
            int       gameBlockLength  = gameHeaderLength + numberOfPlays;

            var thorArray = File.ReadAllBytes(f);

            var year = int.Parse(f.Substring(9, 4));

            if (thorArray[12] != 8)
            {
                throw new Exception("Thor processor only supports 8x8 boards");
            }

            var gameDatabase = new List <ThorGame>();

            for (var i = fileHeaderLength; i < thorArray.Length; i += gameBlockLength)
            {
                var game = new ThorGame
                {
                    TournamentId     = BitConverter.ToInt16(new [] { thorArray[i], thorArray[i + 1] }, 0),
                    BlackId          = BitConverter.ToInt16(new[] { thorArray[i + 2], thorArray[i + 3] }, 0),
                    WhiteId          = BitConverter.ToInt16(new[] { thorArray[i + 4], thorArray[i + 5] }, 0),
                    BlackScore       = thorArray[i + 6],
                    TheoreticalScore = thorArray[i + 7],
                };

                for (var playIndex = 0; playIndex < numberOfPlays; playIndex++)
                {
                    var play = thorArray[i + gameHeaderLength + playIndex];

                    if (play > 0)
                    {
                        game.Plays.Add(play.ToAlgebraicNotation());
                    }
                }
                gameDatabase.Add(game);
            }
            return(new KeyValuePair <int, List <ThorGame> >(year, gameDatabase));
        }