/// <summary> /// Cans the ship go down. /// </summary> /// <returns><c>true</c>, if ship go down was caned, <c>false</c> otherwise.</returns> /// <param name="startPosition">Start position.</param> /// <param name="ship">Ship.</param> private bool canShipGoDown(int startPosition, Ship ship) { bool shipCanGoDown = true; //Because its vertical we check every position in column by adding row size for (int i = startPosition; i <= (startPosition + ((ship.getSize() - 1) * this.xSize)); i = i + xSize) { if (!values[i].Equals('.')) { shipCanGoDown = false; Debug.WriteLine("Ship {0} had collision while trying to place", ship.getName()); break; } } return(shipCanGoDown); }
/// <summary> /// Cans the ship go up. /// </summary> /// <returns><c>true</c>, if ship go up was caned, <c>false</c> otherwise.</returns> /// <param name="startPosition">Start position.</param> /// <param name="ship">Ship.</param> private bool canShipGoUp(int startPosition, Ship ship) { bool shipCanGoUp = true; //Because its horizontal we just go incrementally through grid array for (int i = startPosition; i >= (startPosition - ((ship.getSize() - 1) * this.xSize)); i = i - xSize) { if (!values[i].Equals('.')) { shipCanGoUp = false; Debug.WriteLine("Ship {0} had collision while trying to place", ship.getName()); break; } } return(shipCanGoUp); }
public void checkSpaceAvailable(Ship ship) { bool locationFound = false; bool cordInUse = true; Tuple <int, int>[] tupleArray; while (!locationFound && cordInUse) { int xcord = rancord.Next(0, 4); int ycord = rancord.Next(0, 4); int shipLength = ship.getTiles(); int maxCord = ycord + shipLength - 1; if (maxCord <= 4) { if (ship.getName() == "Frigate") { tupleArray = new Tuple <int, int>[3] { Tuple.Create(xcord, ycord), Tuple.Create(xcord, ycord + 1), Tuple.Create(xcord, ycord + 2), }; cordInUse = isNewCordsValidlocation(tupleArray); if (!cordInUse) { for (int j = 0; j < tupleArray.Length; j++) { Tuple <int, int> currentTuple = tupleArray[j]; Ship.tilesInUse[currentTuple.Item1, currentTuple.Item2] = 1; map[currentTuple.Item1, currentTuple.Item2] = "F"; } } } else if (ship.getName() == "Attack_Boat") { tupleArray = new Tuple <int, int>[2] { Tuple.Create(xcord, ycord), Tuple.Create(xcord, ycord + 1), }; cordInUse = isNewCordsValidlocation(tupleArray); if (!cordInUse) { for (int j = 0; j < tupleArray.Length; j++) { Tuple <int, int> currentTuple = tupleArray[j]; Ship.tilesInUse[currentTuple.Item1, currentTuple.Item2] = 1; map[currentTuple.Item1, currentTuple.Item2] = "A"; } } } else if (ship.getName() == "Dingy") { tupleArray = new Tuple <int, int>[1] { Tuple.Create(xcord, ycord), }; cordInUse = isNewCordsValidlocation(tupleArray); if (!cordInUse) { for (int j = 0; j < tupleArray.Length; j++) { Tuple <int, int> currentTuple = tupleArray[j]; Ship.tilesInUse[currentTuple.Item1, currentTuple.Item2] = 1; map[currentTuple.Item1, currentTuple.Item2] = "D"; } } } else { // } } else { } } }