Ejemplo n.º 1
0
 public GUIField(FieldValue value, Coords coord)
 {
     Value = value;
     Coords = coord;
     BackgroundColor = new SolidColorBrush(Colors.White);
     BackgroundColor.Opacity = 0.0;
 }
Ejemplo n.º 2
0
        public void Equals_PositiveTest()
        {
            var c1 = new Coords(3, 4);
            var c2 = new Coords(3, 4);

            Assert.IsTrue(c1.Equals(c2));
        }
Ejemplo n.º 3
0
        public void AddOperator_Test()
        {
            var c1 = new Coords(3, 1);
            var c2 = new Coords(-1, 1);

            var c3 = c1 + c2;

            Assert.AreEqual(c3, new Coords(2, 2));
        }
Ejemplo n.º 4
0
        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));
        }
 public static bool IsValidMove(MoveType type, FieldValue color, Coords coords, IBoardReader board)
 {
     if (type.Equals(MoveType.AddPiece))
     {
         return IsValidMove(color, coords, board);
     }
     else if (type.Equals(MoveType.Pass))
     {
         return !GetValidMovesForColor(color, board).Any();
     }
     else
     {
         return false;
     }
 }
        public static bool IsValidMove(FieldValue color, Coords coords, IBoardReader board)
        {
            var value = board.GetFieldValue(coords);
            if (value == FieldValue.Empty)
            {
                foreach (var direction in Directions.All)
                {
                    if (IsDirectionValid(color, coords, direction, board))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Ejemplo n.º 7
0
        public void Equals_NegativeTest()
        {
            var c1 = new Coords(3, 4);
            var c2 = new Coords(3, 1);

            Assert.IsFalse(c1.Equals(c2));

            c1 = new Coords(2, 4);
            c2 = new Coords(3, 4);

            Assert.IsFalse(c1.Equals(c2));

            c1 = new Coords(2, 4);
            c2 = new Coords(3, 2);

            Assert.IsFalse(c1.Equals(c2));
        }
        public static List<Coords> GetValidMovesForColor(FieldValue color, IBoardReader board)
        {
            var validMoves = new List<Coords>();
            for (int y = 0; y < board.Size; ++y)
            {
                for (int x = 0; x < board.Size; ++x)
                {
                    var coords = new Coords(x, y);
                    if (IsValidMove(color, coords, board))
                    {
                        validMoves.Add(coords);
                    }
                }
            }

            return validMoves;
        }
        public static List<Coords> GetPossibleDirections(FieldValue color, Coords coords, IBoardReader board)
        {
            var possibleDirections = new List<Coords>();

            if (board.GetFieldValue(coords) == FieldValue.Empty)
            {
                foreach (var direction in Directions.All)
                {
                    if (IsDirectionValid(color, coords, direction, board))
                    {
                        possibleDirections.Add(direction);
                    }
                }
            }

            return possibleDirections;
        }
Ejemplo n.º 10
0
        public bool MakeMove(MoveType type, Coords coords)
        {
            if (OthelloRules.IsValidMove(type, m_activePlayer.Color, coords, m_board))
            {
                if (type.Equals(MoveType.AddPiece))
                {
                    FieldValue color = m_activePlayer.Color;

                    var flipDirections = OthelloRules.GetPossibleDirections(color, coords, m_board);
                    foreach (var dir in flipDirections)
                    {
                        FlipDirection(color, coords, dir);
                    }
                    m_board.SetFieldValue(color, coords);
                }

                ToggleActivePlayer();
                return true;
            }

            return false;
        }
        /// <summary>
        /// Sets the board that should be shown in the GUI.
        /// 
        /// TODO: Remove listener for old board.
        /// </summary>
        public void SetBoard(Board board)
        {
            Rows = new ObservableCollection<ObservableCollection<GUIField>>();
                m_board = board;

                //Listen for new board
                m_board.BoardChanged += BoardChanged;

                for (int row = 0; row < m_board.Size; row++)
                {
                    var columns = new ObservableCollection<GUIField>();
                    for (int col = 0; col < m_board.Size; col++)
                    {
                        var coords = new Coords(col, row);
                        var field = new GUIField(board.GetFieldValue(coords), coords);
                        columns.Add(field);
                    }

                    Rows.Add(columns);
                }

                this.DataContext = Rows;
        }
Ejemplo n.º 12
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);
        }
 private void BoardChanged(Coords coords, FieldValue value)
 {
     if (Rows != null)
     {
         Rows[coords.Y][coords.X].Value = value;
     }
 }
Ejemplo n.º 14
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);
         }
     }
 }
Ejemplo n.º 15
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;
            }
        }
Ejemplo n.º 16
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];
        }
Ejemplo n.º 17
0
        /// <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;
        }