Beispiel #1
0
 /// <summary>
 /// Checks to see if a given Ship instance can be placed on a given Playgrid without being out of bounds
 /// </summary>
 /// <param name="s">The ship instance to place</param>
 /// <param name="pg">Playgrid instance</param>
 /// <param name="x">The x coordinate to place the ship on</param>
 /// <param name="y">The y coordinate to place the ship on</param>
 /// <returns>A boolean if the placement spot is valid or not</returns>
 public bool IsValidPlacement(Ship s, Playgrid pg, int x, int y)
 {
     if ((x > pg.Width - s.Length && !s.IsVertical) || (y > pg.Height - s.Length && s.IsVertical) || x < 0 || y < 0) //out of bounds check
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
Beispiel #2
0
 /// <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);
     }
 }
Beispiel #3
0
 /// <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);
 }
Beispiel #4
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);
        }
Beispiel #5
0
 /// <summary>
 /// Checks to see if a given ship is intersecting
 /// </summary>
 /// <param name="s">The ship to check if its intersecting anything</param>
 /// <param name="pg">The playgrid to check</param>
 /// <param name="x">The x coordinate of the ships top/left most point</param>
 /// <param name="y">The y coordinate of the ships top/left most point</param>
 /// <returns>True if the ship is intersecting another and false if it is not</returns>
 public bool IsShipIntersecting(Ship s, Playgrid pg, int x, int y)
 {
     for (int i = 0; i < s.Length; i++)
     {
         if (s.IsVertical && IsShipHere(pg, x, y + i))
         {
             return(true);
         }
         else if (!s.IsVertical && IsShipHere(pg, x + i, y))
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #6
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);
             }
         }
     }
 }
Beispiel #7
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];
             }
         }
     }
 }
Beispiel #8
0
 public CheatingAI(Playgrid playergrid)
 {
     Playergrid = playergrid;
 }
Beispiel #9
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);
                            }
                        }
                    }
                }
            }
        }
Beispiel #10
0
 public MediumAI(Playgrid playerGrid)
 {
     PlayerGrid = playerGrid;
 }
Beispiel #11
0
 public HardAI(Playgrid playerGrid)
 {
     this.PlayerGrid = playerGrid;
 }
Beispiel #12
0
 public EasyAI(Playgrid playerGrid)
 {
     this.PlayerGrid = playerGrid;
 }