示例#1
0
        public GameState(Phase _phase, PColor _turn, AbstractField[] _fields, DiceState _dicestate, PreGame _preGame)
        {
            this._dicestate = _dicestate;
            this._turn      = _turn;
            this._fields    = _fields;
            this._phase     = _phase;
            this.CurPreGame = _preGame;

            CalculatePossibleMoves();
        }
        public MoveResult RegisterMove(Move m)
        {
            if (_gameState.CurTurnType != GameState.TurnType.Move)
            {
                return(new MoveResult(MoveResult.ResultType.Negative, "Musisz najpierw rzucić kośćmi."));
            }

            if (m == null)
            {
                return(new MoveResult(MoveResult.ResultType.Negative, "Próba wgrania pustego ruchu."));
            }

            if (_gameState.CurTurn != m.Color)
            {
                return(new MoveResult(MoveResult.ResultType.Negative, "Nie Twój ruch!"));
            }

            if (m.IsEmpty)
            {
                if (_gameState.PossibleMoves.Length == 0)
                {
                    GameState = new GameState(GameState.CurPhase, GetOpposite(GameState.CurTurn), (AbstractField[])GameState.CurFields.Clone(), null, GameState.CurPreGame);
                    return(new MoveResult(MoveResult.ResultType.Positive, null));
                }
                else
                {
                    return(new MoveResult(MoveResult.ResultType.Negative, "Próba wgrania pustego ruchu, gdy są możliwe prawdziwe."));
                }
            }


            if (!_gameState.PossibleMoves.Contains <Move>(m))
            {
                return(new MoveResult(MoveResult.ResultType.Negative, "Nie ma takiego ruchu."));
            }

            AbstractField[] newFields    = (AbstractField[])_gameState.CurFields.Clone();
            PColor          newTurn      = _gameState.CurTurn;
            DiceState       newDiceState = _gameState.CurDiceState.ReducedByOne(m.Length);

            newFields[m.SourceField].RemoveStone(m.Color);
            newFields[m.TargetField].AddStone(m.Color);

            // zbijanie.
            if (newFields[m.TargetField].StonesOfColor(GetOpposite(m.Color)) == 1)
            {
                newFields[m.TargetField].RemoveStone(GetOpposite(m.Color));
                if (GetOpposite(m.Color) == PColor.White)
                {
                    newFields[C.WhiteBand].AddStone(PColor.White);
                }
                else
                {
                    newFields[C.BlackBand].AddStone(PColor.Black);
                }
            }

            if (newDiceState.Pips.Length == 0)       // ruch się wyczerpał
            {
                newDiceState = null;                 //trzeba rzucić
                newTurn      = GetOpposite(newTurn); //ale ruch kogo innego
            }

            GameState newGameState = new GameState(_gameState.CurPhase, newTurn, newFields, newDiceState, GameState.CurPreGame);

            /*
             * O ile to możliwe, gracz musi wykonać przesunięcia o liczbę oczek z każdej kostki.
             * Jeśli może wykonać przesunięcie o liczbę oczek tylko z jeden kostki,
             * wykonuje to jedno przesunięcie. Jeśli może wykonać tylko jedno
             * przesunięcie, ale o liczbę oczek z dowolnej z kostek, wykonuje
             * przesunięcie o większą liczbę oczek. Jeśli nie może wykonać żadnego
             * przesunięcia, traci kolejkę.
             */


            if (_gameState.CurDiceState != null)
            {
                if (_gameState.CurDiceState.PrimevalLength == 2 &&
                    _gameState.CurDiceState.Pips.Length == 2)        //pierwszy z dwóch
                {
                    if (m.Length == _gameState.CurDiceState.Pips[0]) // i to krótszy
                    {
                        if (newGameState.PossibleMoves.Length == 0)  // i nie ma potem ruchów
                        {
                            return(new MoveResult(MoveResult.ResultType.Negative, "Ten ruch sprawiłby, że kostka o większej ilości oczek nie zostałaby wykorzystana. Zasady gry nie pozwalają na jego wykonanie."));
                        }
                    }
                }
            }

            GameState = newGameState;

            return(new MoveResult(MoveResult.ResultType.Positive, null));
        }
示例#3
0
        void CalculatePossibleMoves()
        {
            DiceState state = _dicestate;

            if (state == null || _phase == Phase.PreGame)
            {
                _possibleMoves   = new Move[0];
                _possibleSources = new int[0];
                _possibleTargets = new Dictionary <int, int[]>();
                return;
            }

            Dictionary <Move, object> moves = new Dictionary <Move, object>();

            // istotnie rozróżniający IF!
            if (_turn == PColor.White)
            {
                if (_fields[C.WhiteBand].WhiteStones == 0)
                {
                    for (int j = 0; j < state.Pips.Length; ++j)
                    {
                        int p = state.Pips[j];
                        for (int i = 0; i < 24; ++i)
                        {
                            if (_fields[i].IsAccessibleFor(PColor.White))
                            {
                                if (i - p >= 0)
                                {
                                    if (_fields[i - p].WhiteStones > 0)
                                    {
                                        Move m = new Move(i - p, i, _turn);
                                        if (!moves.ContainsKey(m))
                                        {
                                            moves.Add(m, null);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else //wyprowadzanie
                {
                    int magic = 6;
                    for (int j = 0; j < state.Pips.Length; ++j)
                    {
                        int p      = state.Pips[j];
                        int target = magic - p;
                        if (_fields[target].IsAccessibleFor(PColor.White))
                        {
                            Move m = new Move(C.WhiteBand, target, _turn);
                            if (!moves.ContainsKey(m))
                            {
                                moves.Add(m, null);
                            }
                        }
                    }
                }

                if (IsPlayerWhiteHome()) // wywalanie
                {
                    for (int j = 0; j < state.Pips.Length; ++j)
                    {
                        int p      = state.Pips[j];
                        int source = 24 - p;
                        if (source > 0 && source < 23)
                        {
                            if (_fields[source].WhiteStones > 0)
                            {
                                Move m = new Move(source, C.Nowhere, _turn);
                                if (!moves.ContainsKey(m))
                                {
                                    moves.Add(m, null);
                                }
                            }
                        }
                    }
                }
            }
            else // black
            {
                if (_fields[C.BlackBand].BlackStones == 0)
                {
                    for (int j = 0; j < state.Pips.Length; ++j)
                    {
                        int p = state.Pips[j];
                        for (int i = 0; i < 24; ++i)
                        {
                            if (_fields[i].IsAccessibleFor(PColor.Black))
                            {
                                if (i + p < _fields.Length) //24
                                {
                                    if (_fields[i + p].BlackStones > 0)
                                    {
                                        Move m = new Move(i + p, i, _turn);
                                        if (!moves.ContainsKey(m))
                                        {
                                            moves.Add(m, null);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else // wyprowadzanie
                {
                    for (int j = 0; j < state.Pips.Length; ++j)
                    {
                        int p      = state.Pips[j];
                        int target = C.BlackBand + p - 8;
                        if (_fields[target].IsAccessibleFor(PColor.Black))
                        {
                            Move m = new Move(C.BlackBand, target, _turn);
                            if (!moves.ContainsKey(m))
                            {
                                moves.Add(m, null);
                            }
                        }
                    }
                }

                if (IsPlayerBlackHome()) // wywalanie
                {
                    for (int j = 0; j < state.Pips.Length; ++j)
                    {
                        int p      = state.Pips[j];
                        int source = p - 1;
                        if (source > 0 && source < 23)
                        {
                            if (_fields[source].BlackStones > 0)
                            {
                                Move m = new Move(source, C.Nowhere, _turn);
                                if (!moves.ContainsKey(m))
                                {
                                    moves.Add(m, null);
                                }
                            }
                        }
                    }
                }
            }

            Move[] result = moves.Keys.ToArray <Move>();

            Dictionary <int, List <int> > pr = new Dictionary <int, List <int> >();

            for (int i = 0; i < result.Length; ++i)
            {
                int source = result[i].SourceField;
                int target = result[i].TargetField;

                if (!pr.ContainsKey(source))
                {
                    pr.Add(source, new List <int>());
                }

                pr[source].Add(target);
            }

            _possibleSources = pr.Keys.ToArray <int>();

            _possibleTargets = new Dictionary <int, int[]>();
            foreach (KeyValuePair <int, List <int> > p in pr)
            {
                _possibleTargets.Add(p.Key, p.Value.ToArray <int>());
            }


            _possibleMoves = result;
        }