Exemplo n.º 1
0
        public static IEnumerable<Piece> GetPiecesBetweenY(this Board board, Piece p1, Piece p2)
        {
            // Make sure they share axis
            if (p1.X != p2.X)
                return Enumerable.Empty<Piece>();

            // Get first and last piece on line
            var first = p1.Y < p2.Y ? p1 : p2;
            var last = p1.Y < p2.Y ? p2 : p1;

            return board.Pieces.Where(p =>
                p.Y > first.Y &&
                p.Y < last.Y &&
                p.X == p1.X &&
                p.State != PieceState.Open);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a collection of the pieces that are to be turned if the supplied piece is placed
        /// </summary>
        public IEnumerable<Piece> GetValidConnectingPieces(Piece piece)
        {
            var connectingPieces = new List<Piece>();
            // Create a new traverser and move it one step in current direction
            var traverser = new BoardTraverser(this);

            foreach (Direction direction in Enum.GetValues(typeof(Direction)))
            {
                // List to store connecting pieces in current direction
                var connectingPiecesInDirection = new List<Piece>();

                // Reset traverser
                traverser.SetCoords(piece);
                traverser.MovePointer(direction);

                // Loop while traverser is within bounds of the board
                while (!traverser.OutOfBounds)
                {
                    var current = traverser.GetCurrentPiece();

                    if (current.State == PieceState.Open)
                    {
                        break;
                    }
                    else if (Piece.HasOppositeStates(current, piece))
                    {
                        connectingPiecesInDirection.Add(current);
                    }
                    else if (connectingPiecesInDirection.Count > 0 && current.State != PieceState.Open)
                    {
                        // If the last connecting piece is of the same color
                        // and there are opposite colors in between, add to the list
                        connectingPieces.AddRange(connectingPiecesInDirection);
                        break;
                    }

                    // Move pointer to the next place
                    traverser.MovePointer(direction);
                }
            }

            return connectingPieces.AsEnumerable();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns all filled pieces in specified direction in relation to the speicified piece
        /// </summary>
        public static IEnumerable<Piece> GetPiecesInDirection(this Board board, Piece piece, Direction dir)
        {
            IEnumerable<Piece> pieces;
            switch (dir)
            {
                case Direction.North:
                    pieces = board.Pieces.Where(p =>
                        p.X == piece.X &&
                        p.Y > piece.Y
                    );
                    break;
                case Direction.East:
                    pieces = board.Pieces.Where(p =>
                        p.X > piece.X &&
                        p.Y == piece.Y
                    );
                    break;
                case Direction.South:
                    pieces = board.Pieces.Where(p =>
                        p.X == piece.X &&
                        p.Y < piece.Y
                    );
                    break;
                case Direction.West:
                    pieces = board.Pieces.Where(p =>
                        p.X < piece.X &&
                        p.Y == piece.Y
                    );
                    break;
                default:
                    return Enumerable.Empty<Piece>();
            }

            // Return only the fields which are not blank
            return pieces.Where(p => p.State != PieceState.Open);
        }
Exemplo n.º 4
0
 public void SetCoords(Piece piece)
 {
     SetCoords(piece.X, piece.Y);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Determine if a piece is equal to Piece.Empty
 /// </summary>
 public static bool IsEmpty(Piece piece)
 {
     return piece.Equals(Empty);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Determine if two pieces are of the opposite color (state). Throws an exception if one of the pieces are open.
 /// </summary>
 public static bool HasOppositeStates(Piece p1, Piece p2)
 {
     if (p1.State == PieceState.Open || p2.State == PieceState.Open)
         throw new ArgumentException("One of the states are open and therefore has no opposite state");
     return p1.State != p2.State;
 }
Exemplo n.º 7
0
 public static bool IsConnectedY(this Board board, Piece p1, Piece p2)
 {
     return GetPiecesBetweenY(board, p1, p2).Count() == p1.Y - p2.Y;
 }
Exemplo n.º 8
0
 public static bool IsConnectedX(this Board board, Piece p1, Piece p2)
 {
     return GetPiecesBetweenX(board, p1, p2).Count() == p1.X - p2.X;
 }
Exemplo n.º 9
0
        public void SetPiece(int x, int y, PieceState state)
        {
            var piece = GetPiece(x, y);
            var dummy = new Piece(piece.X, piece.Y);
            dummy.SetState(state);

            // Throw an exception if the location has already been set
            if (piece.State != PieceState.Open)
                throw new ArgumentException("Location already set");

            // Throw an exception if placement is invalid
            if (!IsPlacingAllowed(dummy))
                throw new ArgumentException("The piece does not have any valid adjacent pieces");

            // Place the piece
            piece.SetState(state);

            // Turn connecting pieces
            var connectingPieces = GetValidConnectingPieces(piece);
            foreach (var p in connectingPieces)
            {
                p.SetState(state);
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// Determines if placing of the piece is a valid placement
 /// </summary>
 public bool IsPlacingAllowed(Piece piece)
 {
     return GetValidConnectingPieces(piece).Count() > 0;
 }