コード例 #1
0
ファイル: Pgn.cs プロジェクト: rayokota/carballo
        public virtual string GetPgn(Board b, string whiteName, string blackName, string 
			@event, string site, string result)
        {
            // logger.debug("PGN start");
            StringBuilder sb = new StringBuilder();
            if (whiteName == null || string.Empty.Equals(whiteName))
            {
                whiteName = "?";
            }
            if (blackName == null || string.Empty.Equals(blackName))
            {
                blackName = "?";
            }
            if (@event == null)
            {
                @event = "Chess Game";
            }
            if (site == null)
            {
                site = "-";
            }
            sb.Append("[Event \"" + @event + "\"]\n");
            sb.Append("[Site \"" + site + "\"]\n");
            //DateTime d = new DateTime();
            // For GWT we use deprecated methods
            //sb.Append("[Date \"" + d.GetYear() + "." + d.GetMonth() + "." + d.GetDay() + "\"]\n"
            //	);
            sb.Append("[Round \"?\"]\n");
            sb.Append("[White \"" + whiteName + "\"]\n");
            sb.Append("[Black \"" + blackName + "\"]\n");
            if (result == null)
            {
                result = "*";
                switch (b.IsEndGame())
                {
                    case 1:
                    {
                        result = "1-0";
                        break;
                    }

                    case -1:
                    {
                        result = "0-1";
                        break;
                    }

                    case 99:
                    {
                        result = "1/2-1/2";
                        break;
                    }
                }
            }
            sb.Append("[Result \"" + result + "\"]\n");
            if (!Board.FEN_START_POSITION.Equals(b.initialFen))
            {
                sb.Append("[FEN \"" + b.initialFen + "\"]\n");
            }
            sb.Append("[PlyCount \"" + (b.moveNumber - b.initialMoveNumber) + "\"]\n");
            sb.Append("\n");
            StringBuilder line = new StringBuilder();
            for (int i = b.initialMoveNumber; i < b.moveNumber; i++)
            {
                line.Append(" ");
                if ((i & 1) == 0)
                {
                    line.Append(((int)(((uint)i) >> 1)) + 1);
                    line.Append(".");
                }
                line.Append(b.GetSanMove(i));
            }
            if (!"*".Equals(result))
            {
                line.Append(" ");
                line.Append(result);
            }
            // Cut line in a limit of 80 characters
            string[] tokens = line.ToString().Split("[ \\t\\n\\x0B\\f\\r]+");
            int length = 0;
            for (int i_1 = 0; i_1 < tokens.Length; i_1++)
            {
                string next = tokens[i_1];
                if (length + next.Length + 1 > 80)
                {
                    sb.Append("\n");
                    length = 0;
                }
                else
                {
                    if (length > 0)
                    {
                        sb.Append(" ");
                        length++;
                    }
                }
                length += next.Length;
                sb.Append(next);
            }
            // logger.debug("PGN end");
            // logger.debug("PGN:\n" + sb.toString());
            return sb.ToString();
        }