Пример #1
0
    public void ResetStones(ResponseBase response)
    {
        for (int i = 0; i < response.board.n; i++)
        {
            for (int j = 0; j < response.board.n; j++)
            {
                Move   move         = new Move(i, j);
                Stone  initialStone = Stone.EMPTY;
                double stoneSize    = 1.0;
                for (int a = 0; a < response.board.blacks.Count; a++)
                {
                    if (response.board.blacks[a].x == i && response.board.blacks[a].y == j)
                    {
                        initialStone = Stone.BLACK;
                    }
                }


                for (int a = 0; a < response.board.whites.Count; a++)
                {
                    if (response.board.whites[a].x == i && response.board.whites[a].y == j)
                    {
                        initialStone = Stone.WHITE;
                    }
                }

                if (response is CountingResponse)
                {
                    CountingResponse counting = (CountingResponse)response;
                    for (int a = 0; a < counting.dead.Count; a++)
                    {
                        if (counting.dead[a].x == i && counting.dead[a].y == j)
                        {
                            if (initialStone == Stone.WHITE)
                            {
                                initialStone = Stone.WHITE_DEAD;
                            }
                            if (initialStone == Stone.BLACK)
                            {
                                initialStone = Stone.BLACK_DEAD;
                            }
                        }
                    }

                    for (int a = 0; a < counting.blackTerritory.Count; a++)
                    {
                        if (counting.blackTerritory[a].x == i && counting.blackTerritory[a].y == j)
                        {
                            if (initialStone != Stone.WHITE_DEAD)
                            {
                                initialStone = Stone.BLACK_TERRITORY;
                                stoneSize    = counting.blackTerritorySize[a];
                            }
                        }
                    }

                    for (int a = 0; a < counting.whiteTerritory.Count; a++)
                    {
                        if (counting.whiteTerritory[a].x == i && counting.whiteTerritory[a].y == j)
                        {
                            if (initialStone != Stone.BLACK_DEAD)
                            {
                                initialStone = Stone.WHITE_TERRITORY;
                                stoneSize    = counting.whiteTerritorySize[a];
                            }
                        }
                    }
                }
                if (response is HiddenMoveGoGameResponse || response is HiddenMoveGoSetupResponse)
                {
                    List <Move> hiddens = null;
                    if (response is HiddenMoveGoGameResponse)
                    {
                        hiddens = ((HiddenMoveGoGameResponse)response).hiddens;
                    }
                    if (response is HiddenMoveGoSetupResponse)
                    {
                        hiddens = ((HiddenMoveGoSetupResponse)response).hiddens;
                    }


                    for (int a = 0; a < hiddens.Count; a++)
                    {
                        if (hiddens[a].x == i && hiddens[a].y == j)
                        {
                            if (response is HiddenMoveGoGameResponse)
                            {
                                if (initialStone == Stone.WHITE)
                                {
                                    if (me == MeEnum.WHITE || me == MeEnum.OBSERVER)
                                    {
                                        initialStone = Stone.WHITE_HIDDEN;
                                    }
                                    else
                                    {
                                        initialStone = Stone.EMPTY;
                                    }
                                }
                                if (initialStone == Stone.BLACK)
                                {
                                    if (me == MeEnum.BLACK || me == MeEnum.OBSERVER)
                                    {
                                        initialStone = Stone.BLACK_HIDDEN;
                                    }
                                    else
                                    {
                                        initialStone = Stone.EMPTY;
                                    }
                                }
                            }
                            else
                            {
                                HiddenMoveGoSetupResponse setup = ((HiddenMoveGoSetupResponse)response);
                                bool w = true;
                                if (response.black == setup.colors[a])
                                {
                                    w = false;
                                }
                                if (initialStone == Stone.EMPTY)
                                {
                                    if ((me == MeEnum.WHITE || me == MeEnum.OBSERVER) && w)
                                    {
                                        initialStone = Stone.WHITE_HIDDEN;
                                    }
                                    if ((me == MeEnum.BLACK || me == MeEnum.OBSERVER) && !w)
                                    {
                                        initialStone = Stone.BLACK_HIDDEN;
                                    }
                                }
                                else
                                {
                                    initialStone = Stone.EMPTY;
                                }
                            }
                        }
                    }
                }

                if (initialStone == Stone.WHITE && response is OneColorGoGameResponse && !((OneColorGoGameResponse)response).isRevealed)
                {
                    initialStone = Stone.BLACK;
                }

                if (response is BlindGoGameResponse && !((BlindGoGameResponse)response).isRevealed)
                {
                    BlindGoGameResponse rr = (BlindGoGameResponse)response;
                    if (rr.lastMove != null && rr.lastMove.x == i && rr.lastMove.y == j)
                    {
                    }
                    else
                    {
                        initialStone = Stone.EMPTY;
                    }
                }

                if (stonesMatrix[i, j] != initialStone)
                {
                    boardScript.ChangeStone(i, j, initialStone);
                    stonesMatrix[i, j] = initialStone;
                }
            }
        }

        boardScript.DeleteLastMove();
        if (response is GameResponse)
        {
            GameResponse gr = (GameResponse)response;
            if (gr.lastMove != null)
            {
                boardScript.SetLastMove(gr.lastMove.x, gr.lastMove.y);
            }
        }
    }
Пример #2
0
    public static ResponseBase Parse(string str, int index)
    {
        ResponseBase res = null;

        ResponseJson rj = JsonConvert.DeserializeObject <ResponseJson>(str);

        string phase     = rj.phase;
        int    boardSize = rj.n;

        int    nPlayers     = 2;
        string gameTitle    = rj.title;
        string boardContent = rj.board;

        Board board = new Board();

        board.n = boardSize;
        int cnt = 0;

        for (int i = 0; i < boardSize; i++)
        {
            for (int j = 0; j < boardSize; j++)
            {
                int val = (int)(boardContent[cnt] - '0');
                if (val != 9)
                {
                    if (val == rj.black_player)
                    {
                        board.blacks.Add(new Move(i, j));
                    }
                    else
                    {
                        board.whites.Add(new Move(i, j));
                    }
                }
                cnt++;
            }
        }
        if (phase == "game")
        {
            bool   ask   = rj.asking == 1;
            int    rev1  = rj.rev1;
            int    rev2  = rj.rev2;
            String time1 = rj.time1;
            String time2 = rj.time2;
            if (gameTitle.Equals("go"))
            {
                res = new RegularGoGameResponse();
            }

            if (gameTitle.Equals("one-color-go"))
            {
                res = new OneColorGoGameResponse();
                ((OneColorGoGameResponse)res).isAsking = ask;
                ((OneColorGoGameResponse)res).reveal1  = rev1;
                ((OneColorGoGameResponse)res).reveal2  = rev2;
            }

            if (gameTitle.Equals("blind-go"))
            {
                res = new BlindGoGameResponse();
                ((BlindGoGameResponse)res).isAsking = ask;
                ((BlindGoGameResponse)res).reveal1  = rev1;
                ((BlindGoGameResponse)res).reveal2  = rev2;
            }

            if (gameTitle.Equals("hidden-move-go"))
            {
                string[] cont = rj.hidden_moves;
                res = new HiddenMoveGoGameResponse();
                ParseHiddenMoves(res, rj);
            }

            if (!rj.last_move.Equals("nada"))
            {
                string   lastMove = rj.last_move;
                string[] ps       = lastMove.Split('-');
                int      a        = int.Parse(ps[0]);
                int      b        = int.Parse(ps[1]);
                Move     lm       = new Move(a, b);
                ((GameResponse)res).lastMove = lm;
            }

            ((GameResponse)res).time1   = rj.time1;
            ((GameResponse)res).time2   = rj.time2;
            ((GameResponse)res).current = rj.current;
        }

        if (phase == "counting")
        {
            string      lifeTableContent = rj.life_table;
            List <Move> dead             = new List <Move>();
            bool[,] lifeTable = new bool[boardSize, boardSize];
            cnt = 0;
            for (int i = 0; i < boardSize; i++)
            {
                for (int j = 0; j < boardSize; j++)
                {
                    lifeTable[i, j] = (lifeTableContent[cnt] == '0');
                    if (lifeTable[i, j])
                    {
                        dead.Add(new Move(i, j));
                    }
                    cnt++;
                }
            }

            string        countingTableContent = rj.territory_table;
            List <Move>   whiteTerritory       = new List <Move>();
            List <Move>   blackTerritory       = new List <Move>();
            List <double> whiteTerritorySize   = new List <double>();
            List <double> blackTerritorySize   = new List <double>();
            cnt = 0;
            for (int i = 0; i < boardSize; i++)
            {
                for (int j = 0; j < boardSize; j++)
                {
                    if (countingTableContent[cnt] != '9')
                    {
                        if (countingTableContent[cnt] == '0' && rj.black_player == 0)
                        {
                            blackTerritory.Add(new Move(i, j));
                            blackTerritorySize.Add(1.0);
                        }

                        if (countingTableContent[cnt] == '1' && rj.black_player == 1)
                        {
                            blackTerritory.Add(new Move(i, j));
                            blackTerritorySize.Add(1.0);
                        }

                        if (countingTableContent[cnt] == '0' && rj.black_player == 1)
                        {
                            whiteTerritory.Add(new Move(i, j));
                            whiteTerritorySize.Add(1.0);
                        }

                        if (countingTableContent[cnt] == '1' && rj.black_player == 0)
                        {
                            whiteTerritory.Add(new Move(i, j));
                            whiteTerritorySize.Add(1.0);
                        }
                    }
                    cnt++;
                }
            }

            if (gameTitle.Equals("go"))
            {
                res = new RegularGoCountingResponse();
                ((RegularGoCountingResponse)res).dead               = dead;
                ((RegularGoCountingResponse)res).whiteTerritory     = whiteTerritory;
                ((RegularGoCountingResponse)res).blackTerritory     = blackTerritory;
                ((RegularGoCountingResponse)res).whiteTerritorySize = whiteTerritorySize;
                ((RegularGoCountingResponse)res).blackTerritorySize = blackTerritorySize;
            }

            if (gameTitle.Equals("one-color-go"))
            {
                res = new OneColorGoCountingResponse();
                ((OneColorGoCountingResponse)res).dead               = dead;
                ((OneColorGoCountingResponse)res).whiteTerritory     = whiteTerritory;
                ((OneColorGoCountingResponse)res).blackTerritory     = blackTerritory;
                ((OneColorGoCountingResponse)res).whiteTerritorySize = whiteTerritorySize;
                ((OneColorGoCountingResponse)res).blackTerritorySize = blackTerritorySize;
            }

            if (gameTitle.Equals("blind-go"))
            {
                res = new BlindGoCountingResponse();
                ((BlindGoCountingResponse)res).dead               = dead;
                ((BlindGoCountingResponse)res).whiteTerritory     = whiteTerritory;
                ((BlindGoCountingResponse)res).blackTerritory     = blackTerritory;
                ((BlindGoCountingResponse)res).whiteTerritorySize = whiteTerritorySize;
                ((BlindGoCountingResponse)res).blackTerritorySize = blackTerritorySize;
            }

            if (gameTitle.Equals("hidden-move-go"))
            {
                res = new HiddenMoveGoCountingResponse();
                ((HiddenMoveGoCountingResponse)res).dead               = dead;
                ((HiddenMoveGoCountingResponse)res).whiteTerritory     = whiteTerritory;
                ((HiddenMoveGoCountingResponse)res).blackTerritory     = blackTerritory;
                ((HiddenMoveGoCountingResponse)res).whiteTerritorySize = whiteTerritorySize;
                ((HiddenMoveGoCountingResponse)res).blackTerritorySize = blackTerritorySize;
            }
        }

        if (phase.Equals("setup"))
        {
            if (gameTitle.Equals("hidden-move-go"))
            {
                res = new HiddenMoveGoSetupResponse();
                HiddenMoveGoSetupResponse tr = ((HiddenMoveGoSetupResponse)res);
                tr.total = rj.hm_count;
                ParseHiddenMoves(tr, rj);

                for (int i = 0; i < tr.colors.Count; i++)
                {
                    if (tr.colors[i] == index)
                    {
                        tr.placed++;
                    }
                }
            }
        }


        if (rj.black_player == index)
        {
            res.me = MeEnum.BLACK;
        }

        if (rj.black_player == 1 - index)
        {
            res.me = MeEnum.WHITE;
        }

        if (index == 9)
        {
            res.me = MeEnum.OBSERVER;
        }

        res.board = board;
        res.black = rj.black_player;
        res.komi1 = rj.komi1;
        res.komi2 = rj.komi2;

        if (res is RevealableGameResponse)
        {
            ((RevealableGameResponse)res).isRevealed = rj.is_revealed;
        }

        res.handi = rj.handi;


        Dump(res);

        return(res);
    }