private MyShipPosition GetNewShip(List <MyShipPosition> positionsSoFar, int length) { while (true) { Square startSquare = Square.randomSquare(); bool horizontal = r.Next(2) == 1; MyShipPosition shipAttempt; if (horizontal) { shipAttempt = new MyShipPosition( startSquare.x, startSquare.y, startSquare.x, startSquare.y + length - 1); } else { shipAttempt = new MyShipPosition( startSquare.x, startSquare.y, startSquare.x + length - 1, startSquare.y); } if (isLegal(shipAttempt, positionsSoFar)) { return(shipAttempt); } } }
private bool borders(MyShipPosition other) { return (other.end.x >= this.start.x - 1 && this.end.x >= other.start.x - 1 && other.end.y >= this.start.y - 1 && this.end.y >= other.start.y - 1); }
private bool isLegal(MyShipPosition shipAttempt, List <MyShipPosition> shipsSoFar) { if (shipAttempt.outOfBounds()) { return(false); } if (shipAttempt.bordersAny(shipsSoFar)) { return(false); } return(true); }