public void IsInsideBoard_Test()
        {
            var c1 = new Coords(3, 4);
            Assert.IsTrue(c1.IsInsideBoard(8));
            Assert.IsFalse(c1.IsInsideBoard(4));

            c1 = new Coords(-1, 3);
            Assert.IsFalse(c1.IsInsideBoard(8));

            c1 = new Coords(1, -1);
            Assert.IsFalse(c1.IsInsideBoard(8));
        }
        /// <summary>
        /// </summary>
        private static bool IsDirectionValid(FieldValue color, Coords start, Coords dir, IBoardReader board)
        {
            bool       isValid       = false;
            Coords     current       = start + dir;
            FieldValue oppositeColor = color.OppositeColor();

            //Make sure next is oppositecolor, otherwise move is invalid.
            if (current.IsInsideBoard(board.Size) && board.GetFieldValue(current) == oppositeColor)
            {
                current += dir;
                while (current.IsInsideBoard(board.Size))
                {
                    //Found one of ours, direction is valid, break
                    if (board.GetFieldValue(current) == color)
                    {
                        isValid = true;
                        break;
                    }
                    //Did not find one of ours, direction is invalid, break.
                    else if (board.GetFieldValue(current) == FieldValue.Empty)
                    {
                        break;
                    }
                    //Else, continue looking
                    current += dir;
                }
            }

            return(isValid);
        }
Esempio n. 3
0
        public FieldValue GetFieldValue(Coords coords)
        {
            if (!coords.IsInsideBoard(Size))
            {
                throw new ArgumentOutOfRangeException("Row or column index out of range");
            }

            return(m_fields[coords.X, coords.Y]);
        }
Esempio n. 4
0
 public void SetFieldValue(FieldValue value, Coords coords)
 {
     if (!coords.IsInsideBoard(Size))
     {
         throw new ArgumentOutOfRangeException("Row or column index out of range");
     }
     if (m_fields[coords.X, coords.Y] != value)
     {
         m_fields[coords.X, coords.Y] = value;
         if (BoardChanged != null)
         {
             BoardChanged(coords, value);
         }
     }
 }
Esempio n. 5
0
        public void FlipPiece(Coords coords)
        {
            if (!coords.IsInsideBoard(Size))
            {
                throw new ArgumentOutOfRangeException("Row or column index out of range");
            }

            var value = m_fields[coords.X, coords.Y];

            if (value != FieldValue.Black && value != FieldValue.White)
            {
                throw new ArgumentException("No piece to flip!");
            }

            SetFieldValue(value.OppositeColor(), coords);
        }
Esempio n. 6
0
        /// <summary>
        /// Flips one direction. Make sure the direction is a valid direction
        /// before flipping. Otherwise it will flip all pieces.
        /// </summary>
        private void FlipDirection(FieldValue color, Coords start, Coords dir)
        {
            Coords     current       = start + dir;
            FieldValue oppositeColor = color.OppositeColor();
            FieldValue currentValue;

            while (current.IsInsideBoard(m_board.Size))
            {
                currentValue = m_board.GetFieldValue(current);
                if (currentValue == FieldValue.Empty)
                {
                    throw new InvalidOperationException("Tried to flip invalid direction.");
                }
                if (currentValue == color)
                {
                    break;
                }

                m_board.FlipPiece(current);
                current += dir;
            }
        }
        /// <summary>
        /// </summary>
        private static bool IsDirectionValid(FieldValue color, Coords start, Coords dir, IBoardReader board)
        {
            bool isValid = false;
            Coords current = start + dir;
            FieldValue oppositeColor = color.OppositeColor();

            //Make sure next is oppositecolor, otherwise move is invalid.
            if(current.IsInsideBoard(board.Size) && board.GetFieldValue(current) == oppositeColor)
            {
                current += dir;
                while(current.IsInsideBoard(board.Size))
                {
                    //Found one of ours, direction is valid, break
                    if (board.GetFieldValue(current) == color)
                    {
                        isValid = true;
                        break;
                    }
                    //Did not find one of ours, direction is invalid, break.
                    else if(board.GetFieldValue(current) == FieldValue.Empty)
                    {
                        break;
                    }
                    //Else, continue looking
                    current += dir;
                }
            }

            return isValid;
        }
Esempio n. 8
0
 public void SetFieldValue(FieldValue value, Coords coords)
 {
     if (!coords.IsInsideBoard(Size))
     {
         throw new ArgumentOutOfRangeException("Row or column index out of range");
     }
     if (m_fields[coords.X, coords.Y] != value)
     {
         m_fields[coords.X, coords.Y] = value;
         if (BoardChanged != null)
         {
             BoardChanged(coords, value);
         }
     }
 }
Esempio n. 9
0
        public FieldValue GetFieldValue(Coords coords)
        {
            if (!coords.IsInsideBoard(Size))
            {
                throw new ArgumentOutOfRangeException("Row or column index out of range");
            }

            return m_fields[coords.X, coords.Y];
        }
Esempio n. 10
0
        public void FlipPiece(Coords coords)
        {
            if (!coords.IsInsideBoard(Size))
            {
                throw new ArgumentOutOfRangeException("Row or column index out of range");
            }

            var value = m_fields[coords.X, coords.Y];
            if (value != FieldValue.Black && value != FieldValue.White)
            {
                throw new ArgumentException("No piece to flip!");
            }

            SetFieldValue(value.OppositeColor(), coords);
        }