Example #1
0
        public Game TryMove(Player p, int index, out bool error)
        {
            Game result = new Game();

            result.LoadState(this);

            error = false;
            List <List <int> > holes = new List <List <int> >();

            holes.Add(result.playerAHoles);
            if (p == Player.A)
            {
                holes.Add(result.playerBHoles);
            }
            else
            {
                holes.Insert(0, result.playerBHoles);
            }

            if (holes[0][index] == 0 || p != CurrentPlayer)
            {
                error = true;
                return(null);
            }

            int stones = holes[0][index];

            holes[0][index] = 0;
            Player nextWellOwner = p;
            bool   keepTurnFlag  = true;

            while (stones > 0)
            {
                index++;
                keepTurnFlag = false;
                if (index == GameSize)
                {
                    holes.Add(holes[0]);
                    holes.RemoveAt(0);
                    index = 0;
                    if (nextWellOwner == p)
                    {
                        stones--;
                        if (p == Player.A)
                        {
                            result.playerAWell++;
                        }
                        else
                        {
                            result.playerBWell++;
                        }

                        keepTurnFlag = true;
                    }
                    nextWellOwner = PlayerUtils.Other(nextWellOwner);
                }

                if (stones > 0)
                {
                    holes[0][index]++;
                    stones--;

                    if (holes[0][index] == 1 && nextWellOwner == p && stones == 0)
                    {
                        // Strike
                        keepTurnFlag = false;
                        if (p == Player.A)
                        {
                            result.playerAWell += holes[1][GameSize - index - 1];
                            result.playerAWell += holes[0][index];
                        }
                        else
                        {
                            result.playerBWell += holes[1][GameSize - index - 1];
                            result.playerBWell += holes[0][index];
                        }

                        holes[1][GameSize - index - 1] = 0;
                        holes[0][index] = 0;
                    }
                }
            }

            result.CurrentPlayer = PlayerUtils.Other(result.CurrentPlayer);
            if (keepTurnFlag)
            {
                result.Pass(result.CurrentPlayer);
            }

            return(result);
        }