/// <summary> /// Adds a marker to the battle field. /// </summary> /// <param name="marker">The marker.</param> public void AddMarker(Marker marker) { BFSquareViewModel square = this.Squares.Where(s => s.Position.Equals(marker.Position)).First(); if (marker is HitMarker) { square.State = SquareState.Hit; } else { square.State = SquareState.Missed; } }
/// <summary> /// Highlights the squares to indicate the ship position. /// </summary> /// <param name="shipToPlace">The ship to place.</param> /// <returns>A boolean value indicating whether the position is valid.</returns> public bool HighlightSquares(Ship shipToPlace) { Position controlPos; bool validShipPosition = true; List <BFSquareViewModel> squares = new List <BFSquareViewModel>(); HighlightedState state = HighlightedState.Highlighted; for (int l = 0; l < shipToPlace.Length; l++) { if (shipToPlace.Orientation == Orientation.Horizontal) { controlPos = new Position(shipToPlace.Position.X + l, shipToPlace.Position.Y); } else { controlPos = new Position(shipToPlace.Position.X, shipToPlace.Position.Y + l); } try { BFSquareViewModel square = this.Squares.First(s => s.Position.Equals(controlPos)); if (square.RealShipUnit) { validShipPosition = false; } squares.Add(square); } catch { return(false); } } if (!validShipPosition) { state = HighlightedState.HighlightedAsWrong; } for (int i = 0; i < squares.Count; i++) { squares[i].HighlightedState = state; } if (state == HighlightedState.HighlightedAsWrong) { return(false); } return(true); }
/// <summary> /// Moves the fake ship units. /// </summary> private void Worker() { bool removePrevious = false; for (int i = 0; i < this.ship.Length; i++) { Position squarePosition; if (this.ship.Orientation == Orientation.Horizontal) { squarePosition = new Position(this.ship.Position.X + i, this.ship.Position.Y); } else { squarePosition = new Position(this.ship.Position.X, this.ship.Position.Y + i); } this.squares.Where(square => square.Position.Equals(squarePosition)).First().RealShipUnit = true; for (int y = this.maxHeight - 1; y >= squarePosition.Y; y--) { if (removePrevious && y != this.maxHeight - 1) { this.squares.Where(s => s.Position.Equals(new Position(squarePosition.X, y + 1))).First().FakeShipUnit = false; } BFSquareViewModel square = this.squares.Where(s => s.Position.Equals(new Position(squarePosition.X, y))).First(); if (!square.FakeShipUnit) { removePrevious = true; square.FakeShipUnit = true; } else { removePrevious = false; } Thread.Sleep(Delay); } this.squares.Where(square => square.Position.Equals(squarePosition)).First().FakeShipUnit = true; } }