/// <summary> /// Sets the flag for the occupied squares. /// <param name="square"></param> /// <returns> </returns> public Boolean ProcessAttack(BoardSquare targetSquare) { try { if (targetSquare != null) { Console.WriteLine("\nYou received an attack at " + targetSquare.Rowheader + targetSquare.Index); if (targetSquare.IsOccupied) { if (HitList.Find(item => item.Rowheader == targetSquare.Rowheader && item.Index == targetSquare.Index) == null) { HitList.Add(targetSquare); } else { Console.WriteLine("\nThis position was already Hit "+targetSquare.Rowheader+ targetSquare.Index+"); return false; } } return targetSquare.IsOccupied; } return false; } catch { throw; } }
/// <summary> /// /// Returns Hit or Miss when receives the attacks. /// </summary> /// <param name="rowHeader"></param> /// <param name="indexStart"></param> public void ReceiveAttack(String rowHeader, Int32 indexStart) { // Update the Player board with occupied ship spaces BoardSquare targetSquare = this.GameBoard.SquareList.Find(item => item.Rowheader == rowHeader && item.Index == indexStart); if (ProcessAttack(targetSquare)) { targetSquare.Value = Globals.GetShipTypeSymbol(Mode.Hit); Console.WriteLine("\nIt was a Hit!"); } else { targetSquare.Value = Globals.GetShipTypeSymbol(Mode.Miss); Console.WriteLine("\nIt was a Miss!"); } }
/// <summary> /// Addsthe ship with specified type, position, orientation and size parameters. /// Sets the flag for board squares that are occupied by ship. /// Prints the value "##" for board squares that are occupied by ship based on the ship type symbol. /// </summary> /// <param name="gameShip"></param> public void AddShip(Ship gameShip) { try { BoardSquare StartBoard = gameShip.Square; shipCount++; // When ship is placed horizontally. if (gameShip.ShipOrientation == Orientation.Horizontal) { List <BoardSquare> squares = SquareList.FindAll(item => item.Value.Contains(StartBoard.Rowheader)).ToList(); Int32 startIndex = squares.FindIndex(item => item.Value == StartBoard.Value); for (int index = 0; index < gameShip.Size; index++) { squares[index + startIndex].IsOccupied = true; squares[index + startIndex].ShipType = gameShip.ShipType; squares[index + startIndex].Value = Globals.GetShipTypeSymbol(gameShip.ShipType); } Console.WriteLine(); } else { // when the ship is placed vertically List <BoardSquare> squares = SquareList.FindAll(item => item.Index == (StartBoard.Index)); Int32 startIndex = squares.FindIndex(item => item.Value == StartBoard.Value); for (int index = 0; index < gameShip.Size; index++) { squares[index + startIndex].IsOccupied = true; squares[index + startIndex].ShipType = gameShip.ShipType; squares[index + startIndex].Value = Globals.GetShipTypeSymbol(gameShip.ShipType); } Console.WriteLine(); } Console.WriteLine("\nSuccesfully added " + gameShip.ShipType + "(" + Globals.GetShipTypeSymbol(gameShip.ShipType) + ") at " + StartBoard.Value + " " + gameShip.ShipOrientation + "ly."); } catch { Console.WriteLine("\n" + gameShip.ShipType + " doesn't fit in the given position " + gameShip.Square.Value + " as it was already occupied." + " Please adjust the position and try again."); } }
/// <summary> /// Creates a new Board with the given size. /// Assumption : Board is 10x10 dimension . /// </summary> /// <param name="intSize"></param> public void CreateBoard(Int32 intSize) { try { for (int row = 0; row < intSize; row++) { string strName = Alpha[row].ToString(); for (int col = 0; col < intSize; col++) { BoardSquare square = new BoardSquare(strName, col); SquareList.Add(square); } } // Print(); } catch { throw; } }