public Ship(int xStartCoordinate, int yStartCoordinate, int length, ShipAlignment alignment) { XStartCoordinate = xStartCoordinate; YStartCoordinate = yStartCoordinate; Length = length; Alignment = alignment; Hits = 0; AddShipRange(); }
/// <summary> /// Tries to place the ship in the playfield /// </summary> /// <param name="ship">the ship which is should be placed</param> private void PlaceShip(Ship ship) { bool placed = false; do { ShipAlignment shipAlignment = (ShipAlignment) new Random() .Next(0, 2); int startCoordinateX = new Random().Next(_playfield.Fields.Min(f => f.XCoordinate), _playfield.Fields.Max(f => f.XCoordinate) + 1); int startCoordinateY = new Random().Next(_playfield.Fields.Min(f => f.YCoordinate), _playfield.Fields.Max(f => f.YCoordinate) + 1); placed = true; ship.ShipPieces = new List <ShipPiece>(); for (int piece = 0; piece < (int)ship.ShipType && placed; piece++) { if (!_playfield.Fields.Any(f => f.XCoordinate == startCoordinateX && f.YCoordinate == startCoordinateY)) { placed = false; } else { placed = PlaceShipPiece(ship, startCoordinateX, startCoordinateY); if (ShipAlignment.Vertical == shipAlignment) { startCoordinateY++; } else { startCoordinateX++; } } } } while (!placed); _playfield.Ships.Add(ship); }
/// <summary> /// Checks the ships placement and "predicted placement" (Based on the new rotation) /// against the outter boundaries and the other ships placement /// </summary> /// <param name="newAlignment">the new alignment of the ship</param> /// <param name="ship">the ship being moved</param> /// <param name="newPositions">the new positions of the ships parts</param> /// <returns></returns> private bool CheckIfClearToRotate( ShipAlignment newAlignment, ShipType ship, out List<string> newPositions ) { newPositions = null; List<string> tempNewPositions = new List<string>(); tempNewPositions.Add(myShips[ship][0]); if ( newAlignment == ShipAlignment.Vertical ) { StringBuilder firstBlock = new StringBuilder(myShips[ship][0]); for (int i = 1; i < myShips[ship].Count; i++) { StringBuilder currentBlock = new StringBuilder(myShips[ship][i]); string newTile = ""; int currentNum = int.Parse(currentBlock[1].ToString()); int diff = currentNum - int.Parse( firstBlock[1].ToString() ); if ( firstBlock[0] + diff > 'J' ) return false; newTile = ((char)(firstBlock[0] + diff)).ToString() + firstBlock[1].ToString(); tempNewPositions.Add( newTile ); } } else if ( newAlignment == ShipAlignment.Horizontal) { StringBuilder firstBlock = new StringBuilder( myShips[ship][0] ); for ( int i = 1; i < myShips[ship].Count; i++ ) { StringBuilder currentBlock = new StringBuilder( myShips[ship][i] ); string newTile = ""; char currentLetter = currentBlock[0]; int diff = currentLetter - firstBlock[0]; if ( int.Parse(firstBlock[1].ToString()) + diff > 9 ) return false; newTile = firstBlock[0].ToString() + (int.Parse(firstBlock[1].ToString()) + diff); tempNewPositions.Add( newTile ); } } foreach ( string tile in tempNewPositions ) { if ( CheckIfTileIsOccupied( tile, ship ) ) return false; } newPositions = tempNewPositions; return true; }
/// <summary> /// Attemps to change the alingment of a ship when changing the corresponding radio buttons /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ChangeShipAlignment(object sender, EventArgs e) { RadioButton button = (RadioButton)sender; if ( button.Checked == false ) return; List<string> newPositions; bool success = false; switch ( button.Name ) { case "rb2ByHor": case "rb2ByVert": if ( GetAlignment( ShipType.TwoBy ) == by2Align ) return; break; case "rb3ByHor": case "rb3ByVert": if ( GetAlignment( ShipType.ThreeBy ) == by3Align ) return; break; case "rb4ByHor": case "rb4ByVert": if ( GetAlignment( ShipType.FourBy ) == by4Align ) return; break; case "rb5ByHor": case "rb5ByVert": if ( GetAlignment( ShipType.FiveBy ) == by5Align ) return; break; } switch(button.Name) { case "rb2ByHor": success = CheckIfClearToRotate(ShipAlignment.Horizontal, ShipType.TwoBy, out newPositions); if ( success ) { myShips[ShipType.TwoBy] = newPositions; by2Align = ShipAlignment.Horizontal; } else rb2ByVert.Checked = true; break; case "rb2ByVert": success = CheckIfClearToRotate(ShipAlignment.Vertical, ShipType.TwoBy, out newPositions); if (success) { myShips[ShipType.TwoBy] = newPositions; by2Align = ShipAlignment.Vertical; } else rb2ByHor.Checked = true; break; case "rb3ByHor": success = CheckIfClearToRotate(ShipAlignment.Horizontal, ShipType.ThreeBy, out newPositions); if (success) { myShips[ShipType.ThreeBy] = newPositions; by3Align = ShipAlignment.Horizontal; } else rb3ByVert.Checked = true; break; case "rb3ByVert": success = CheckIfClearToRotate(ShipAlignment.Vertical, ShipType.ThreeBy, out newPositions); if (success) { myShips[ShipType.ThreeBy] = newPositions; by3Align = ShipAlignment.Vertical; } else rb3ByHor.Checked = true; break; case "rb4ByHor": success = CheckIfClearToRotate(ShipAlignment.Horizontal, ShipType.FourBy, out newPositions); if (success) { myShips[ShipType.FourBy] = newPositions; by4Align = ShipAlignment.Horizontal; } else rb4ByVert.Checked = true; break; case "rb4ByVert": success = CheckIfClearToRotate( ShipAlignment.Vertical, ShipType.FourBy, out newPositions ); if ( success ) { myShips[ShipType.FourBy] = newPositions; by4Align = ShipAlignment.Vertical; } else rb4ByHor.Checked = true; break; case "rb5ByHor": success = CheckIfClearToRotate(ShipAlignment.Horizontal, ShipType.FiveBy, out newPositions); if (success) { myShips[ShipType.FiveBy] = newPositions; by5Align = ShipAlignment.Horizontal; } else rb5ByVert.Checked = true; break; case "rb5ByVert": success = CheckIfClearToRotate(ShipAlignment.Vertical, ShipType.FiveBy, out newPositions); if (success) { myShips[ShipType.FiveBy] = newPositions; by5Align = ShipAlignment.Vertical; } else rb5ByHor.Checked = true; break; } RefreshMyShips(); }
public void AddShipToBoard(int xStartCoordinate, int yStartCoordinate, int length, ShipAlignment alignment) { try { xStartCoordinate.ValidateXStartCoordinate(); yStartCoordinate.ValidateYStartCoordinate(); length.ValidateLength(); var ship = new Ship(xStartCoordinate, yStartCoordinate, length, alignment); Ships.Add(ship); Board.AddShipToBoard(ship); } catch (XCoordOutOfBoundsException exception) { Console.WriteLine(exception.Message); } catch (YCoordOutOfBoundsException exception) { Console.WriteLine(exception.Message); } catch (LengthOutOfBoundsException exception) { Console.WriteLine(exception.Message); } }
private static bool ShipCheck(int xCoor, int yCoor, int shipLength, ShipAlignment alignment, PlayerType type) // Checks if Enemy or Friendly, how long and if vertical or horizontal. { // Here is checked wheter the placed ship sticks out beyond the grid or if there is already a ship. // This method also gives the "allowed" coordinates the seaState "Battleship". if (alignment == ShipAlignment.Horizontal && xCoor + shipLength > Program.seaWidth) { return(false); } if (alignment == ShipAlignment.Vertical && yCoor + shipLength > Program.seaHeight) { return(false); } if (type == PlayerType.Player) { if (alignment == ShipAlignment.Horizontal) { for (int i = 0; i < shipLength; i++) { if (Program.seaFriendlyShips[xCoor + i, yCoor] != Program.SeaState.EmptySea) { return(false); } } for (int i = 0; i < shipLength; i++) { Program.seaFriendlyShips[xCoor + i, yCoor] = Program.SeaState.Battleship; } return(true); } for (int i = 0; i < shipLength; i++) { if (Program.seaFriendlyShips[xCoor, yCoor + i] != Program.SeaState.EmptySea) { return(false); } } for (int i = 0; i < shipLength; i++) { Program.seaFriendlyShips[xCoor, yCoor + i] = Program.SeaState.Battleship; } return(true); } if (alignment == ShipAlignment.Horizontal) { for (int i = 0; i < shipLength; i++) { if (Program.seaEnemyShips[xCoor + i, yCoor] != Program.SeaState.EmptySea) { return(false); } } for (int i = 0; i < shipLength; i++) { Program.seaEnemyShips[xCoor + i, yCoor] = Program.SeaState.Battleship; } return(true); } for (int i = 0; i < shipLength; i++) { if (Program.seaEnemyShips[xCoor, yCoor + i] != Program.SeaState.EmptySea) { return(false); } } for (int i = 0; i < shipLength; i++) { Program.seaEnemyShips[xCoor, yCoor + i] = Program.SeaState.Battleship; } return(true); }