Ejemplo n.º 1
0
        private void addToGroup(Space space)
        {
            SpaceCollection newGroup = new SpaceCollection();

            newGroup.AddAt(space.X, space.Y);
            this._groups.Add(newGroup);
            space.SetGroup(newGroup);

            // TODO: Modify: get each connected space and add the groups for all connected spaces together
            List <Space> connected = GetConnectedSpaces(space.X, space.Y);

            foreach (Space cSpace in connected)
            {
                if (cSpace.GetGroup() != space.GetGroup())
                {
                    SpaceCollection cGroup = cSpace.GetGroup();
                    for (int i = cGroup.Count - 1; i >= 0; i--)
                    {
                        Space groupSpace      = cGroup.GetByIndex(i);
                        Space boardGroupSpace = Spaces.GetSpace(groupSpace.X, groupSpace.Y);
                        space.GetGroup().AddAt(boardGroupSpace.X, boardGroupSpace.Y);
                        boardGroupSpace.SetGroup(space.GetGroup());
                    }
                    this._groups.Remove(cGroup);
                }
            }
        }
Ejemplo n.º 2
0
        private bool loadHistory(int index)
        {
            if (index >= 0 && index < this.History.Count && !this._scoringMode)
            {
                BasicGame historyGame = this.History[index];

                this._lastPassed = historyGame._lastPassed;

                this._spaces = new SpaceCollection(this, this.BoardSize);
                this._groups.Clear();
                foreach (Space space in historyGame.Spaces)
                {
                    Space gameSpace = this.Spaces.GetSpace(space.X, space.Y);
                    if (space.SpaceColor != Space.Color.None)
                    {
                        gameSpace.SpaceColor = space.SpaceColor;
                        this.addToGroup(gameSpace);
                    }
                }
                this._spaces.SpaceClick += _spaces_SpaceClick;

                this._capturesB = historyGame._capturesB;
                this._capturesW = historyGame._capturesW;

                this._historyIndex = index;

                this.NextMove = historyGame._nextMove;

                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        protected void setupGame(Board board, Board.BoardSize size, int handicaps, IRuleSet rules)
        {
            this._board   = board;
            this._rules   = rules;
            this._spaces  = new SpaceCollection(this, size);
            int[,] hcList = Rules.GetHandicaps(board.Game, handicaps);
            for (int x = 1; x <= this._spaces.Size; x++)
            {
                for (int y = 1; y <= this._spaces.Size; y++)
                {
                    if (hcList[x - 1, y - 1] == 1)
                    {
                        this.AddStone(x, y, Space.Color.Black);
                    }
                }
            }
            this._spaces.SpaceClick += _spaces_SpaceClick;

            if (handicaps > 1)
            {
                this.NextMove = Space.Color.White;
            }
            else
            {
                this.NextMove = Space.Color.Black;
            }

            this.History.Add((BasicGame)this.Copy());
            this._historyIndex = 0;
        }
Ejemplo n.º 4
0
        public int RemoveDeadStones(int exceptionX = 0, int exceptionY = 0)
        {
            bool exception  = false;
            int  deadStones = 0;

            for (int i = this._groups.Count - 1; i >= 0; i--)
            {
                SpaceCollection grp = this._groups[i];

                if (this.GetGroupLiberties(grp) == 0)
                {
                    foreach (Space space in grp)
                    {
                        if (space.X == exceptionX && space.Y == exceptionY)
                        {
                            exception = true;
                        }
                    }

                    if (!exception)
                    {
                        deadStones += grp.Count;
                        while (grp.Count > 0)
                        {
                            this.RemoveStone(grp.GetByIndex(0).X, grp.GetByIndex(0).Y);
                        }
                    }
                    exception = false;
                }
            }

            return(deadStones);
        }
Ejemplo n.º 5
0
 private void copySpaces(SpaceCollection src, SpaceCollection dest, SpaceCollection groupSpaces = null)
 {
     foreach (Space space in src)
     {
         dest.AddAt(space.X, space.Y);
         Space copySpace = dest.GetSpace(space.X, space.Y);
         copySpace.SpaceColor = space.SpaceColor;
         if (groupSpaces != null)
         {
             groupSpaces.GetSpace(space.X, space.Y).SetGroup(dest);
         }
     }
 }
Ejemplo n.º 6
0
        private void removeFromGroup(Space space)
        {
            SpaceCollection group = space.GetGroup();

            if (group.RemoveAt(space.X, space.Y))
            {
                if (group.Count == 0)
                {
                    if (this._groups.Contains(group))
                    {
                        this._groups.Remove(group);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public int GetGroupLiberties(SpaceCollection group)
        {
            SpaceCollection liberties = new SpaceCollection();

            foreach (Space space in group)
            {
                SpaceCollection spaceLiberties = GetSpaceLibertyList(space.X, space.Y);

                foreach (Space liberty in spaceLiberties)
                {
                    liberties.AddAt(liberty.X, liberty.Y);
                }
            }

            return(liberties.Count);
        }
Ejemplo n.º 8
0
        public SpaceCollection GetSpaceLibertyList(int x, int y)
        {
            if (Spaces.GetSpace(x, y).SpaceColor == Space.Color.None)
            {
                return(null);
            }

            SpaceCollection liberties = new SpaceCollection();

            int libertyX = x - 1;
            int libertyY = y;

            if (libertyX > 0 && Spaces.GetSpace(libertyX, libertyY).SpaceColor == Space.Color.None)
            {
                liberties.AddAt(libertyX, libertyY);
            }

            libertyX = x + 1;
            if (libertyX <= this.BoardSize && Spaces.GetSpace(libertyX, libertyY).SpaceColor == Space.Color.None)
            {
                liberties.AddAt(libertyX, libertyY);
            }

            libertyX = x;
            libertyY = y - 1;
            if (libertyY > 0 && Spaces.GetSpace(libertyX, libertyY).SpaceColor == Space.Color.None)
            {
                liberties.AddAt(libertyX, libertyY);
            }

            libertyY = y + 1;
            if (libertyY <= this.BoardSize && Spaces.GetSpace(libertyX, libertyY).SpaceColor == Space.Color.None)
            {
                liberties.AddAt(libertyX, libertyY);
            }

            return(liberties);
        }
Ejemplo n.º 9
0
        public Game Copy()
        {
            Game copy = new BasicGame();

            copy._nextMove   = this._nextMove;
            copy._lastPassed = this._lastPassed;

            copy._spaces = new SpaceCollection();
            copySpaces(this._spaces, copy._spaces);
            copy._spaces.Size = (int)Math.Sqrt(this._spaces.Count);

            copy._groups = new List <SpaceCollection>();
            foreach (SpaceCollection grp in this._groups)
            {
                SpaceCollection copyGrp = new SpaceCollection();
                copySpaces(grp, copyGrp, copy._spaces);
                copy._groups.Add(copyGrp);
            }

            copy._capturesB = this._capturesB;
            copy._capturesW = this._capturesW;

            return(copy);
        }
Ejemplo n.º 10
0
 public void SetGroup(SpaceCollection group)
 {
     this._group = group;
 }