/// <summary> /// Checks to see if a ship is present at the given tile /// </summary> /// <param name="pg">The Playgrid instance to check on</param> /// <param name="x">The x coordinate to check</param> /// <param name="y">The y coordinate to check</param> /// <returns></returns> public bool IsShipHere(Playgrid pg, int x, int y) { if (pg.ValueAt(x, y) == TileState.ShipHere || pg.ValueAt(x, y) == TileState.PreviewCollision) { return(true); } else { return(false); } }
/// <summary> /// Fires at a given x,y coordinate /// </summary> /// <param name="x">The x coordinate of where to 'shoot'</param> /// <param name="y">The y coordinate of where to 'shoot'</param> /// <param name="target">The target to base off of</param> /// <returns>True if the shot was a hit, false if it missed</returns> public bool Shoot(int x, int y, Playgrid target) { bool hit = false; if (target.ValueAt(x, y) == TileState.ShipHere && target.ValueAt(x, y) != TileState.Hit) { target.ChangeTile(x, y, TileState.Hit); hit = true; } else if (target.ValueAt(x, y) != TileState.Hit) { target.ChangeTile(x, y, TileState.Missed); } return(hit); }
/// <summary> /// Reverts the changed tiles states that previewing made /// </summary> /// <param name="pg">The affected Playgrid instance to revert on</param> public void RevertPreviewChanges(Playgrid pg) { for (int i = 0; i < pg.Width; i++) { for (int j = 0; j < pg.Height; j++) { if (pg.ValueAt(i, j) == TileState.PreviewOK) { pg.ChangeTile(i, j, TileState.Normal); } else if (pg.ValueAt(i, j) == TileState.PreviewCollision) { pg.ChangeTile(i, j, TileState.ShipHere); } } } }
/// <summary> /// Checks to see if any placement collisions exist when previewing /// </summary> /// <param name="pg">The Playgrid instance to check</param> /// <returns>True if any collisions are found, false if non are present</returns> public bool IsCollisionPresent(Playgrid pg) { for (int i = 0; i < pg.Width; i++) { for (int j = 0; j < pg.Height; j++) { if (pg.ValueAt(i, j) == TileState.PreviewCollision) { return(true); } } } return(false); }
/// <summary> /// Changes tile states to preview states when view visually /// </summary> /// <param name="s">The ship to place</param> /// <param name="pg">The Playgrid instance to place</param> /// <param name="x">The x coordinate to place on</param> /// <param name="y">The y coordinate to place on</param> public void PreviewShipPlace(Ship s, Playgrid pg, int x, int y) { if (s == null) { return; } bool inBounds = (x >= 0 && y >= 0) && (x < pg.Width && y < pg.Height); //isValid limits the bounds further because of ship length, need a bool for 'normal' out of bounds if (inBounds) { if (s.IsVertical) { if (IsValidPlacement(s, pg, x, y)) { for (int i = 0; i < s.Length; i++) { if (pg.ValueAt(x, y + i) == TileState.ShipHere) { pg.ChangeTile(x, y + i, TileState.PreviewCollision); } else { pg.ChangeTile(x, y + i, TileState.PreviewOK); } } } else { for (int i = 0; i < s.Length; i++) { if (pg.ValueAt(x, pg.Height - s.Length + i) == TileState.ShipHere) { pg.ChangeTile(x, pg.Height - s.Length + i, TileState.PreviewCollision); } else { pg.ChangeTile(x, pg.Height - s.Length + i, TileState.PreviewOK); } } } } else { if (IsValidPlacement(s, pg, x, y)) { for (int i = 0; i < s.Length; i++) { if (pg.ValueAt(x + i, y) == TileState.ShipHere) { pg.ChangeTile(x + i, y, TileState.PreviewCollision); } else { pg.ChangeTile(x + i, y, TileState.PreviewOK); } } } else { for (int i = 0; i < s.Length; i++) { if (pg.ValueAt(pg.Width - s.Length + i, y) == TileState.ShipHere) { pg.ChangeTile(pg.Width - s.Length + i, y, TileState.PreviewCollision); } else { pg.ChangeTile(pg.Width - s.Length + i, y, TileState.PreviewOK); } } } } } }