Ejemplo n.º 1
0
        private static IList <PointInt> getAdjacent(PointInt point, int size)
        {
            validatePnt(point, size);
            List <PointInt> adjacent = new List <PointInt>();

            if (point.Column > 0)
            {
                adjacent.Add(new PointInt(point.Column - 1, point.Row));
            }
            if (point.Row > 0)
            {
                adjacent.Add(new PointInt(point.Column, point.Row - 1));
            }
            if (point.Column < (size - 1))
            {
                adjacent.Add(new PointInt(point.Column + 1, point.Row));
            }
            if (point.Row < (size - 1))
            {
                adjacent.Add(new PointInt(point.Column, point.Row + 1));
            }
            if (point.Column > 0 && point.Row < (size - 1))
            {
                adjacent.Add(new PointInt(point.Column - 1, point.Row + 1));
            }
            if (point.Column < (size - 1) && point.Row > 0)
            {
                adjacent.Add(new PointInt(point.Column + 1, point.Row - 1));
            }

            return(adjacent);
        }
Ejemplo n.º 2
0
        internal bool Play(PointInt point)
        {
            if (!IsFinished)
            {
                validatePnt(point, m_size);
                int index = getIndex(point);

                HexPiece piece = _pieces[index];

                if (piece.State == HexPieceState.Unused)
                {
                    piece.State  = (CurrentPlayer == Player.Black) ? HexPieceState.Black : HexPieceState.White;
                    piece.Number = (++PlayCount);

                    checkFinished();
                    if (!IsFinished)
                    {
                        CurrentPlayer = (CurrentPlayer == Player.Black) ? Player.White : Player.Black;
                    }
                }
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
 internal HexPiece this[PointInt point]
 {
     get
     {
         return(_pieces[getIndex(point)]);
     }
 }
Ejemplo n.º 4
0
 private static void validatePnt(PointInt point, int size)
 {
     if (size < 0)
     {
         throw new ArgumentOutOfRangeException("size");
     }
     if (point.Column >= size || point.Row >= size)
     {
         throw new ArgumentOutOfRangeException("point");
     }
 }
Ejemplo n.º 5
0
        public static Point GetTopLeft(int count, double itemHeight, PointInt location)
        {
            Contract.Requires <ArgumentOutOfRangeException>(count >= 1, "count");
            Contract.Requires <ArgumentOutOfRangeException>(itemHeight > 0 && itemHeight.IsValid(), "itemHeight");

            double itemWidth = itemHeight / HeightOverWidth;

            //determine the location of 0,0
            Point point = new Point(0, itemHeight * .5 * (count - 1));

            //determine the start point for the row
            point.X += (itemWidth * 3 / 4) * location.Row;
            point.Y += (itemHeight / 2) * location.Row;

            //calculate the offset for the column
            point.X += (itemWidth * 3 / 4) * location.Column;
            point.Y -= (itemHeight / 2) * location.Column;

            return(point);
        }
Ejemplo n.º 6
0
 internal HexPiece(PointInt point)
 {
     _point = point;
     _state = HexPieceState.Unused;
 }
Ejemplo n.º 7
0
        private int getIndex(PointInt point)
        {
            validatePnt(point, m_size);

            return(point.Row * m_size + point.Column);
        }
Ejemplo n.º 8
0
        private bool checkPiece(PointInt point)
        {
            //_stackDepth++;

            //should only be checking pieces that match the _currentPlayer
            Debug.Assert(this[point].State == ((CurrentPlayer == Player.White) ? HexPieceState.White : HexPieceState.Black));

            //doing a depth-first search from point
            //use the ptns in _connectionTest to make sure we don't loop back on ourselves

            List <PointInt> adjacentPoints = new List <PointInt>();

            foreach (PointInt p in getAdjacent(point, m_size))
            {
                //figure out which points should stay
                //1) == current kind we're searching for
                if (this[p].State == ((CurrentPlayer == Player.White) ? HexPieceState.White : HexPieceState.Black))
                {
                    //2) not already in the list
                    if (!m_connectionTest.ContainsKey(getIndex(p)))
                    {
                        adjacentPoints.Add(p);
                    }
                }
            }

            //now adjacentPoints contains all of the points we should check for winning
            //if not won, go to the next level

            if (adjacentPoints.Count == 0)
            {
                //_stackDepth--;
                return(false);
            }
            else
            {
                bool won = false;

                //check for winner
                if (CurrentPlayer == Player.White)
                {
                    //see if this makes white win
                    foreach (PointInt pnt in adjacentPoints)
                    {
                        if (pnt.Row == (m_size - 1))
                        {
                            won = true;
                            setWinner(pnt);
                            break;
                        }
                    }
                }
                else
                {
                    //see if this makes black win
                    foreach (PointInt pnt in adjacentPoints)
                    {
                        if (pnt.Column == (m_size - 1))
                        {
                            won = true;
                            setWinner(pnt);
                            break;
                        }
                    }
                }

                if (!won)
                {
                    //add adjacentPoints to global
                    foreach (PointInt pnt in adjacentPoints)
                    {
                        Debug.Assert(!m_connectionTest.ContainsKey(getIndex(pnt)));
                        m_connectionTest.Add(getIndex(pnt));
                    }

                    //no winner, next level
                    foreach (PointInt pnt in adjacentPoints)
                    {
                        won = checkPiece(pnt);
                        if (won)
                        {
                            setWinner(pnt);
                            break;
                        }
                    }

                    //make sure to remove nodes added to _connectionTest during this search
                    foreach (PointInt pnt in adjacentPoints)
                    {
                        Debug.Assert(m_connectionTest.ContainsKey(getIndex(pnt)));
                        m_connectionTest.Remove(getIndex(pnt));
                    }
                }

                //_stackDepth--;
                return(won);
            }
        }
Ejemplo n.º 9
0
 private void setWinner(PointInt pnt)
 {
     _pieces[getIndex(pnt)].IsWinner = true;
 }