Exemplo n.º 1
0
        public List <Board> GetParsed()
        {
            bool completeTable = false;

            Board.Tables myTable = Board.Tables.Inner;
            Board.Tables yoTable = Board.Tables.Inner;
            uint[,] stones = null;

            foreach (KeyValuePair <string, string> pair in matchInfo)
            {
                string tag     = pair.Key;
                string content = pair.Value;
                if (tag == "판")
                {
                    stones = new uint[Board.Height, Board.Width];
                    string[] lines = content.Split('/');

                    int x = 0;
                    int y = 0;

                    HashSet <uint> existingStones = new HashSet <uint>();

                    foreach (string line in lines)
                    {
                        foreach (char letter in line)
                        {
                            bool parsed = int.TryParse(letter.ToString(), out int result);
                            //숫자라면
                            if (parsed)
                            {
                                x += result;
                            }
                            //숫자가 아니면
                            else
                            {
                                void insertStone(uint[] inputStones)
                                {
                                    foreach (uint stone in inputStones)
                                    {
                                        if (!existingStones.Contains(stone))
                                        {
                                            stones[y, x] = stone;
                                            existingStones.Add(stone);
                                            break;
                                        }
                                    }
                                }

                                if (letter == '차')
                                {
                                    insertStone(new [] { Stones.MyCha1, Stones.MyCha2 });
                                }

                                else if (letter == '상')
                                {
                                    insertStone(new[] { Stones.MySang1, Stones.MySang2 });
                                }

                                else if (letter == '마')
                                {
                                    insertStone(new[] { Stones.MyMa1, Stones.MyMa2 });
                                }

                                else if (letter == '포')
                                {
                                    insertStone(new[] { Stones.MyPo1, Stones.MyPo2 });
                                }

                                else if (letter == '사')
                                {
                                    insertStone(new[] { Stones.MySa1, Stones.MySa2 });
                                }

                                else if (letter == '장')
                                {
                                    insertStone(new[] { Stones.MyKing });
                                }

                                else if (letter == '졸')
                                {
                                    insertStone(new[] { Stones.MyJol1, Stones.MyJol2,
                                                        Stones.MyJol3, Stones.MyJol4, Stones.MyJol5 });
                                }

                                else if (letter == '車')
                                {
                                    insertStone(new[] { Stones.YoCha1, Stones.YoCha2 });
                                }

                                else if (letter == '象')
                                {
                                    insertStone(new[] { Stones.YoSang1, Stones.YoSang2 });
                                }

                                else if (letter == '馬')
                                {
                                    insertStone(new[] { Stones.YoMa1, Stones.YoMa2 });
                                }

                                else if (letter == '包')
                                {
                                    insertStone(new[] { Stones.YoPo1, Stones.YoPo2 });
                                }

                                else if (letter == '士')
                                {
                                    insertStone(new[] { Stones.YoSa1, Stones.YoSa2 });
                                }

                                else if (letter == '將')
                                {
                                    insertStone(new[] { Stones.YoKing });
                                }

                                else if (letter == '兵')
                                {
                                    insertStone(new[] { Stones.YoJol1, Stones.YoJol2,
                                                        Stones.YoJol3, Stones.YoJol4, Stones.YoJol5 });
                                }

                                else if (letter == ' ')                                 //그리고 아마도 한 접장기
                                {
                                    break;
                                }

                                else
                                {
                                    throw new Exception("unknown letter! : " + letter);
                                }
                                x++;
                            }
                        }
                        y++;
                        x = 0;
                    }
                }
                else if (tag == "대국결과")
                {
                    if (content[0] == '초')
                    {
                        isMyWin = 1;
                    }
                    else if (content[0] == '한')
                    {
                        isMyWin = 0;
                    }
                    else
                    {
                        isMyWin = -1;
                    }
                }
                else if (tag == "초차림" || tag == "초포진")
                {
                    if (content == "마상마상")
                    {
                        myTable = Board.Tables.Right;
                    }
                    else if (content == "상마상마")
                    {
                        myTable = Board.Tables.Left;
                    }
                    else if (content == "마상상마")
                    {
                        myTable = Board.Tables.Inner;
                    }
                    else
                    {
                        myTable = Board.Tables.Outer;
                    }
                    completeTable = true;
                }
                else if (tag == "한차림" || tag == "한포진")
                {
                    if (content == "마상마상")
                    {
                        yoTable = Board.Tables.Left;
                    }
                    else if (content == "상마상마")
                    {
                        yoTable = Board.Tables.Right;
                    }
                    else if (content == "마상상마")
                    {
                        yoTable = Board.Tables.Inner;
                    }
                    else
                    {
                        yoTable = Board.Tables.Outer;
                    }
                    completeTable = true;
                }
            }             //end of foreach



            if (completeTable)
            {
                stones = GetStones(myTable, yoTable);
            }


            //한이 아래쪽에 있으면 일단 바꿈
            if (stones[8, 4] == Stones.YoKing)
            {
                stones = GetFlipVer(stones);
                moves  = Move.GetOpposite(moves);
            }

            //상대방부터 시작하면
            Pos  starting = moves[0].From;
            bool myFirst  = true;

            //일부러 봐주는 이런 판은 그냥 무시한다.
            if (starting.IsEmpty)
            {
                return(new List <Board>());
            }

            if (IsYours(stones[starting.Y, starting.X]))
            {
                myFirst = false;
            }

            List <Board> boards = new List <Board>();
            Board        board  = new Board(stones, myFirst, false);

            boards.Add(board);

            foreach (Move move in moves)
            {
                Board next = board.GetNext(move);
                boards.Add(next);
                board = next;
            }

            return(boards);
        }
Exemplo n.º 2
0
        public void Read(string path)
        {
            Encoding     euckr  = Encoding.GetEncoding(51949);
            FileStream   stream = new FileStream(path, FileMode.Open);
            StreamReader reader = new StreamReader(stream, euckr);


            historyList = new List <List <Board> >();
            isMyWinList = new List <int>();

            //컴플릿 셋팅
            bool completeTable = false;
            bool isMyFirst     = true;

            Board.Tables myTable = Board.Tables.Inner;
            Board.Tables yoTable = Board.Tables.Inner;

            //접장기의 경우
            uint[,] stones = new uint[Board.Height, Board.Width];

            //기타 기본
            Board        board      = null;
            int          totalCount = 0;
            List <Board> history    = null;
            int          isMyWin    = -1;

            while (true)
            {
                string line = reader.ReadLine();
                //Console.WriteLine(line);

                if (line.Length == 0)
                {
                    //초기 셋팅
                    if (stones != null)
                    {
                        if (completeTable)
                        {
                            board = new Board(myTable, yoTable, isMyFirst);
                        }
                        else
                        {
                            board = new Board();
                            board.Set(stones, isMyFirst, isMyFirst);
                        }

                        history = new List <Board>
                        {
                            board
                        };

                        stones = null;
                    }
                    //마무리
                    else
                    {
                        if (completeTable != false)
                        {
                            historyList.Add(history);
                            isMyWinList.Add(isMyWin);
                        }

                        stones        = new uint[Board.Height, Board.Width];
                        completeTable = false;
                    }
                }
                else if (line[0] == '{')
                {
                    while (true)
                    {
                        if (line.IndexOf('}') != -1)
                        {
                            break;
                        }
                        line = reader.ReadLine();
                    }
                    continue;
                }
                else if (line[0] == '[')
                {
                    int    tagEnd  = line.IndexOf(' ');
                    string tag     = line.Substring(1, tagEnd - 1);
                    int    quBegin = line.IndexOf('\"');
                    int    quEnd   = line.LastIndexOf(']');
                    string content = line.Substring(quBegin + 1, quEnd - quBegin - 2);
                    if (tag == "판")
                    {
                        //귀찮.
                    }
                    else if (tag == "총수")
                    {
                        totalCount = int.Parse(content);
                    }
                    else if (tag == "대국결과")
                    {
                        if (content[0] == '초')
                        {
                            isMyWin = 1;
                        }
                        else if (content[0] == '한')
                        {
                            isMyWin = 0;
                        }
                        else
                        {
                            isMyWin = -1;
                        }
                    }
                    else if (tag == "초차림")
                    {
                        if (content == "마상마상")
                        {
                            myTable = Board.Tables.Right;
                        }
                        else if (content == "상마상마")
                        {
                            myTable = Board.Tables.Left;
                        }
                        else if (content == "마상상마")
                        {
                            myTable = Board.Tables.Inner;
                        }
                        else
                        {
                            myTable = Board.Tables.Outer;
                        }
                        completeTable = true;
                    }
                    else if (tag == "한차림")
                    {
                        if (content == "마상마상")
                        {
                            yoTable = Board.Tables.Left;
                        }
                        else if (content == "상마상마")
                        {
                            yoTable = Board.Tables.Right;
                        }
                        else if (content == "마상상마")
                        {
                            yoTable = Board.Tables.Inner;
                        }
                        else
                        {
                            yoTable = Board.Tables.Outer;
                        }
                        completeTable = true;
                    }
                }


                //나머지 수순
                else
                {
                    string[] words = line.Split('.');

                    for (int k = 1; k < words.Length; k++)
                    {
                        string word = words[k];

                        Move move;
                        if (word[1] == '한')                        //수쉼
                        {
                            move = Move.Empty;
                        }
                        else
                        {
                            sbyte[] posList  = new sbyte[4];
                            int     posIndex = 0;
                            for (int i = 1; i < word.Length && posIndex < 4; i++)
                            {
                                bool succeed = int.TryParse(word[i].ToString(), out int result);
                                if (succeed)
                                {
                                    result--;
                                    if (result == -1)
                                    {
                                        result = 9;
                                    }
                                    posList[posIndex++] = (sbyte)result;
                                }
                            }

                            move = new Move(posList[1], posList[0], posList[3], posList[2]);
                        }
                        board = board.GetNext(move);
                        history.Add(board);
                    }
                }

                if (reader.EndOfStream)
                {
                    break;
                }
            }

            //foreach (var h in historyList)
            //{
            //	foreach (var b in h)
            //	{
            //		b.PrintStones();
            //		Console.WriteLine();
            //		Console.ReadKey();
            //	}
            //}
        }