Anonymous avatar Anonymous committed 94e1e2a

ship rectangles...

Comments (0)

Files changed (5)

BattleShipConsole/Program.cs

 
 		}
 
-        /*
-        public static bool IsShipHere(L.Ship ship, L.Point p)
-        {
-            if (ship.orientation == L.Ship.Orientation.Horizontal)
-            {
-                if (ship.point.Y != p.Y) 
-                {
-                    return false;
-                }
-                // ship.point.Y == p.Y
-                int shipXLeft = ship.point.X;
-                int shipXRight = ship.point.X + ship.size - 1;
-
-                return shipXLeft <= p.X && p.X <= shipXRight;
-            }
-
-            if (ship.orientation == L.Ship.Orientation.Vertical)
-            {
-                if (ship.point.X != p.X)
-                {
-                    return false;
-                }
-                // ship.point.X == p.X
-                int shipYTop = ship.point.Y;
-                int shipYBottom = ship.point.Y + ship.size - 1;
-
-                return shipYTop <= p.Y && p.Y <= shipYBottom;
-            }
-
-            throw new Exception("unknown orientation.");
-        }
-         * */
-
         public static void Test(L.Ship ship, L.Point p, bool expected)
         {
             bool result = ship.Contains(p);
 			Draw(test);
 			
             Test(
-                new L.Ship(new L.Point(0, 0), 4, L.Ship.Orientation.Horizontal), 
+                new L.Ship(new L.Point(0, 0), 4, L.Ship.OrientationT.Horizontal), 
                 new L.Point(0, 1),
                 false);
             Test(
-                new L.Ship(new L.Point(0, 0), 4, L.Ship.Orientation.Horizontal),
+                new L.Ship(new L.Point(0, 0), 4, L.Ship.OrientationT.Horizontal),
                 new L.Point(1, 0),
                 true);
 
             Test(
-                new L.Ship(new L.Point(0, 0), 4, L.Ship.Orientation.Vertical),
+                new L.Ship(new L.Point(0, 0), 4, L.Ship.OrientationT.Vertical),
                 new L.Point(1, 0),
                 false);
             Test(
-                new L.Ship(new L.Point(0, 0), 4, L.Ship.Orientation.Vertical),
+                new L.Ship(new L.Point(0, 0), 4, L.Ship.OrientationT.Vertical),
                 new L.Point(0, 1),
                 true);
 
             Test(
-                new L.Ship(new L.Point(5, 5), 4, L.Ship.Orientation.Vertical),
+                new L.Ship(new L.Point(5, 5), 4, L.Ship.OrientationT.Vertical),
                 new L.Point(5, 0),
                 false);
 

BattleShipLogic/Field.cs

 {
     public class Field: IFieldInfo
     {
-        public Ship[] ShipArray;
+        // # | size
+        // 1 | 5
+        // 1 | 4
+        // 1 | 3
+        // 2 | 2
+        // 2 | 1
+
+        //  0123456789
+        // 0___++__+__
+        // 1__________
+        // 2__+_______
+        // 3__+___+___
+        // 4__+___+___
+        // 5__+___+___
+        // 6__+_______
+        // 7____++++_+
+        // 8__________
+        // 9___++_____
+        //  0123456789
+        public List<Ship> ShipList = new List<Ship>()
+        {
+            new Ship(new Point(3, 0), 2, Ship.OrientationT.Horizontal),
+            new Ship(new Point(7, 0), 1, Ship.OrientationT.Horizontal),
+            new Ship(new Point(2, 2), 5, Ship.OrientationT.Vertical),
+            new Ship(new Point(6, 3), 3, Ship.OrientationT.Vertical),
+            new Ship(new Point(4, 7), 4, Ship.OrientationT.Horizontal),
+            new Ship(new Point(9, 7), 1, Ship.OrientationT.Horizontal),
+            new Ship(new Point(3, 9), 2, Ship.OrientationT.Horizontal),
+        };
 
         public Cell GetCell(Point p)
         {
-            for (int i = 0; i < this.ShipArray.Length; ++i) 
+            for (int i = 0; i < this.ShipList.Count; ++i)
             {
-                Ship ship = this.ShipArray[i];
-                // if(ship is on p) { return Cell.Ship; }
+                if(this.ShipList[i].Contains(p))
+                {
+                    return Cell.Ship; 
+                }
             }
             return Cell.Empty;
         }

BattleShipLogic/Rectangle.cs

 {
     public struct Rectangle
     {
-        public Point leftTop;
+        public Point LeftTop;
         public Point RightBottom;
 
         public Point RightTop
         {
             get
             {
-                return new Point(RightBottom.X, leftTop.Y);
+                return new Point(RightBottom.X, LeftTop.Y);
             }
         }
 
-        public Point leftBottom
+        public Point LeftBottom
         {
             get
             {
-                return new Point(leftTop.X, RightBottom.Y);
+                return new Point(LeftTop.X, RightBottom.Y);
             }
         }
 
-        public Rectangle(Point leftTop, Point RightBottom)
+        public Rectangle(Point leftTop, Point rightBottom)
         {
-            this.leftTop = leftTop;
-            this.RightBottom = RightBottom;
+            this.LeftTop = leftTop;
+            this.RightBottom = rightBottom;
         }
 
         public Point Size
         {
             get
             {
-                return RightBottom - leftTop;
+                return RightBottom - LeftTop;
             }
         }
-        public bool Contains(Point F)
+
+        public bool Contains(Point point)
         {
             return
-                Contains(leftTop.Y, RightBottom.Y, F.Y) &&
-                Contains(leftTop.X, RightBottom.X, F.X);
+                Contains(LeftTop.Y, RightBottom.Y, point.Y) &&
+                Contains(LeftTop.X, RightBottom.X, point.X);
+        }
+
+        public bool Contains(Rectangle rectangle)
+        {
+            return 
+                this.Contains(rectangle.LeftTop) && 
+                this.Contains(rectangle.RightBottom);
         }
 
         private static bool Contains(int min, int max, int D)
         {
             return min <= D && D <= max;
         }
+
+        public bool Overlap(Rectangle r)
+        {
+            for(int x = this.LeftTop.X; x <= this.RightBottom.X; ++x)
+            {
+                for (int y = this.LeftTop.Y; y <= this.RightBottom.Y; ++y)
+                {
+                    if (r.Contains(new Point(x, y)))
+                    {
+                        return true;
+                    }
+                }
+            }
+            return false;
+        }
     }
 }

BattleShipLogic/Ship.cs

 {
     public class Ship
     {
-        public enum Orientation
+        public enum OrientationT
         {
             Horizontal,
             Vertical,
         }
 
-        public Orientation orientation;
+        public OrientationT Orientation;
 
-        public int size;
+        public int Size;
         
-        public Point leftTop;
+        public Point LeftTop;
 
-        public Point RightBottom()
+        public Ship(Point leftTop, int size, OrientationT orientation)
         {
-            if(orientation == Orientation.Horizontal)
+            this.LeftTop = leftTop;
+            this.Size = size;
+            this.Orientation = orientation;
+        }
+
+        public Point RightBottom
+        {
+            get
             {
-                return new Point(leftTop.X + size - 1, leftTop.Y);
-            }
-            else
-            {
-                return new Point(leftTop.X, leftTop.Y + size - 1);
+                if(Orientation == OrientationT.Horizontal)
+                {
+                    return new Point(LeftTop.X + Size - 1, LeftTop.Y);
+                }
+                else
+                {
+                    return new Point(LeftTop.X, LeftTop.Y + Size - 1);
+                }
             }
         }
 
-        public Ship(Point leftTop_, int size_, Orientation orientation_)
+        public Rectangle Rectangle
         {
-            this.leftTop = leftTop_;
-            this.size = size_;
-            this.orientation = orientation_;
+            get
+            {
+                return new Rectangle(LeftTop, RightBottom);
+            }
         }
 
         public bool Contains(Point p)
         {
-            if (orientation == Ship.Orientation.Horizontal)
-            {
-                if (leftTop.Y != p.Y)
-                {
-                    return false;
-                }
-
-                int shipXLeft = leftTop.X;
-                int shipXRight = leftTop.X + size - 1;
-
-                return shipXLeft <= p.X && p.X <= shipXRight;
-            }
-
-            if (orientation == Ship.Orientation.Vertical)
-            {
-                if (leftTop.X != p.X)
-                {
-                    return false;
-                }
-
-                int shipYTop = leftTop.Y;
-                int shipYBottom = leftTop.Y + size - 1;
-
-                return shipYTop <= p.Y && p.Y <= shipYBottom;
-            }
-
-            throw new Exception("unknown orientation.");
+            return Rectangle.Contains(p);
         }
-
     }
 }

BattleShipLogic/TestPlayer.cs

 {
     public class TestPlayer : IPlayer
     {
-        private class CMyField : IMyField
+        private class CMyField : Field, IMyField
         {
             public bool SetShip(Ship ship)
             {
-                throw new System.NotImplementedException();
-            }
+                // 1
+                
+                // 2
+                Ship oldShip = this.ShipList[0];
 
-            public Cell GetCell(Point p)
-            {
-                if (p.X == 3)
+
+
+                // 3
+                Rectangle fieldRectangle = new Rectangle(new Point(0, 0), new Point(9, 9));
+                Rectangle shipRectangle = ship.Rectangle;
+
+                if (!fieldRectangle.Contains(shipRectangle))
                 {
-                    return Cell.EmptyShot;
+                    return false;
                 }
-                else if (p == new Point(1, 5) || p == new Point(7, 4))
-                {
-                    return Cell.Ship;
-                }
-                else if (p == new Point(8, 1))
-                {
-                    return Cell.DestroyedShip;
-                }
-                else if (p == new Point(9, 2))
-                {
-                    return Cell.DamagedShip;
-                }
-                else 
-                {
-                    return Cell.Empty;
-                }
+                
+                // Ура!
+                this.ShipList.Add(ship);
+                return true;
             }
         }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.