Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
 /// <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);
             }
         }
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Places a ship on the respective grid at the respective x,y location.
 /// Ships are placed by setting their leftmost or top most point, then add on their length
 /// </summary>
 /// <param name="s">The Ship instance to place</param>
 /// <param name="pg">The Playgrid instance to place on</param>
 /// <param name="x">The x coordinate to place on</param>
 /// <param name="y">The y coordinate to place on</param>
 public void PlaceShip(Ship s, Playgrid pg, int x, int y)
 {
     if (IsValidPlacement(s, pg, x, y) && !IsShipHere(pg, x, y) && !IsCollisionPresent(pg))
     {
         s.Tiles = new Tile[s.Length];
         if (!s.IsVertical)
         {
             for (int i = 0; i < s.Length; i++)
             {
                 pg.ChangeTile(x + i, y, TileState.ShipHere);
                 s.Tiles[i] = pg.Grid[x + i, y];
             }
         }
         else
         {
             for (int i = 0; i < s.Length; i++)
             {
                 pg.ChangeTile(x, y + i, TileState.ShipHere);
                 s.Tiles[i] = pg.Grid[x, y + i];
             }
         }
     }
 }
Exemplo n.º 4
0
        /// <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);
                            }
                        }
                    }
                }
            }
        }