Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 24.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace OceanWar
  12. {
  13.     public partial class main : Form
  14.     {
  15.         public main()
  16.         {
  17.             InitializeComponent();
  18.         }
  19.         static Pole pol = new Pole();
  20.         private void Form1_Load(object sender, EventArgs e)
  21.         {
  22.            
  23.         }
  24.         private void button1_Click(object sender, EventArgs e)
  25.         {
  26.             pol.generatepole(textBox1);
  27.  
  28.         }
  29.  
  30.         private void button2_Click(object sender, EventArgs e)
  31.         {
  32.             pol.deleteship(1);
  33.         }
  34.  
  35.         private void RENDER(object sender, EventArgs e)
  36.         {
  37.             pol.renderer.renderAll(pictureBox1);
  38.         }
  39.  
  40.         class Pole
  41.         {
  42.             enum Napr { left = 1, right, top, bottom };
  43.             enum Item_state { nothing = 0, nothing_die = 1, ship = 2, ship_die = 3, ship_located = 99 };
  44.             //
  45.             public Render renderer;
  46.             static (Item_state state, int[] ship_ids)[,] Items = new (Item_state state, int[] ship_ids)[10, 10];
  47.             Ship[] ships = new Ship[11];
  48.  
  49.             //static Item[,] Items = new Item[10, 10];
  50.  
  51.  
  52.             public Pole()
  53.             {
  54.                 for (int i = 0; i < 10; i++)
  55.                 {
  56.                     for (int j = 0; j < 10; j++)
  57.                     {
  58.                         Items[i, j].ship_ids = new int[4];
  59.                     }
  60.                 }
  61.  
  62.                 ships[1] = new Ship(4,1);
  63.                 ships[2] = new Ship(3,2); ships[3] = new Ship(3,3);
  64.                 ships[4] = new Ship(2,4); ships[5] = new Ship(2,5); ships[6] = new Ship(2,6);
  65.                 ships[7] = new Ship(1,6); ships[8] = new Ship(1,8); ships[9] = new Ship(1,9); ships[10] = new Ship(1,10);
  66.  
  67.             }
  68.  
  69.             public Boolean generatepole(TextBox tx)
  70.             {
  71.                 Boolean kek;
  72.                 Random rand = new Random();
  73.                 for (int i = 1; i < 11; i++)
  74.                 {
  75.                     do
  76.                     {
  77.                         kek = generateship(i, (i, i));
  78.                         tx.Text += kek ? $"\r\n {ships[i].length}хпалубный корабль {i} успех" : $"\r\n {ships[i].length}хпалубный корабль {i} ошибка";
  79.                     } while (!kek);
  80.                 }
  81.                 return true;
  82.             }
  83.  
  84.             public Boolean generateship(int id, (int x, int y) coor)
  85.             {
  86.                 if ((Items[coor.x-1, coor.y-1].state == Item_state.ship_located) || (Items[coor.x-1, coor.y-1].state == Item_state.ship)) return false;
  87.                 Random rand = new Random();
  88.                 for (Napr kek = ((Napr)rand.Next(1, 5)); kek < (Napr)5; kek++)
  89.                 {
  90.                     if (ships[id].buildship(kek, coor)) break;
  91.                 }
  92.                 return true;
  93.             }
  94.  
  95.             public Boolean deleteship(int id)
  96.             {
  97.                 if (!ships[id].deleteship()) return false;
  98.                 return true;
  99.             }
  100.  
  101.             class Ship
  102.             {
  103.                 Boolean die { get; set; } = false;
  104.  
  105.                 Boolean has_built = false;
  106.                 public int length{ get; set; }
  107.                 int id { get; set; }
  108.                 Napr nap { get; set; }
  109.                 (int x, int y) start_coord { get; set; }
  110.                 public Ship(int lengthp,int idd)
  111.                 {
  112.                     this.length = lengthp;
  113.                     this.id = idd;
  114.                 }
  115.                
  116.                 void delete_location((int x, int y) coor)
  117.                 {
  118.                     for (int i = 0; i < Items[coor.x, coor.y].ship_ids.Length; i++)
  119.                     {
  120.                         if ((Items[coor.x, coor.y].ship_ids[i] != 0) && (Items[coor.x, coor.y].ship_ids[i] == this.id))
  121.                         {
  122.                             Items[coor.x, coor.y].ship_ids[i] = 0;
  123.                             break;
  124.                         }
  125.                     }
  126.                     if (Items[coor.x, coor.y].ship_ids[1] == 0) Items[coor.x, coor.y].state = Item_state.nothing;
  127.                 }
  128.                 void add_location((int x, int y) coor)
  129.                 {
  130.                     for (int i=0;i< Items[coor.x, coor.y].ship_ids.Length; i++)
  131.                     {
  132.                         if (Items[coor.x, coor.y].ship_ids[i]==0)
  133.                         {
  134.                             Items[coor.x, coor.y].ship_ids[i] = this.id;
  135.                             break;
  136.                         }
  137.                     }
  138.                     Items[coor.x, coor.y].state = Item_state.ship_located;
  139.                 }
  140.  
  141.                 public Boolean deleteship()
  142.                 {
  143.                     if (!this.has_built) return false;
  144.                     switch (nap)
  145.                     {
  146.                         case Napr.bottom:
  147.                             if (start_coord.y > 1)
  148.                             {
  149.                                 if (start_coord.x + 1 < 10)
  150.                                     delete_location((start_coord.x + 1, start_coord.y - 1));
  151.                                 delete_location((start_coord.x, start_coord.y - 1));
  152.                                 if (start_coord.x - 1 > 0)
  153.                                     delete_location((start_coord.x - 1, start_coord.y - 1));
  154.                             }
  155.                             for (int i = start_coord.y; i < length + start_coord.y; i++)
  156.                             {
  157.                                 if (start_coord.x + 1 < 10)
  158.                                     delete_location((start_coord.x + 1, i));
  159.                                 if (start_coord.x - 1 > 0)
  160.                                     delete_location((start_coord.x - 1, i));
  161.                                 Items[start_coord.x, i].state = Item_state.nothing;
  162.                             }
  163.                             if (length + start_coord.y < 10)
  164.                             {
  165.                                 if (start_coord.x + 1 < 10)
  166.                                     delete_location((start_coord.x + 1, start_coord.y + length));
  167.                                 delete_location((start_coord.x, start_coord.y + length));
  168.                                 if (start_coord.x - 1 > 0)
  169.                                     delete_location((start_coord.x - 1, start_coord.y + length));
  170.                             }
  171.                             break;
  172.                         case Napr.top:
  173.                             if (start_coord.y + 1 < 10)
  174.                             {
  175.                                 if (start_coord.x + 1 < 10)
  176.                                     delete_location((start_coord.x + 1, start_coord.y + 1));
  177.                                 delete_location((start_coord.x, start_coord.y + 1));
  178.                                 if (start_coord.x - 1 > 0)
  179.                                     delete_location((start_coord.x - 1, start_coord.y + 1));
  180.                             }
  181.                             for (int i = start_coord.y; i > start_coord.y - length; i--)
  182.                             {
  183.                                 if (start_coord.x + 1 < 10)
  184.                                     delete_location((start_coord.x + 1, i));
  185.                                 if (start_coord.x - 1 > 0)
  186.                                     delete_location((start_coord.x - 1, i));
  187.                                 Items[start_coord.x, i].state = Item_state.nothing;
  188.                             }
  189.                             if ( start_coord.y - length >= 0)
  190.                             {
  191.                                 if (start_coord.x + 1 < 10)
  192.                                     delete_location((start_coord.x + 1, start_coord.y - length));
  193.                                 delete_location((start_coord.x, start_coord.y - length));
  194.                                 if (start_coord.x - 1 > 0)
  195.                                     delete_location((start_coord.x - 1, start_coord.y - length));
  196.                             }
  197.                             break;
  198.                         case Napr.right:
  199.                             if (start_coord.x > 1)
  200.                             {
  201.                                 if (start_coord.y - 1 > 0)
  202.                                     delete_location((start_coord.x - 1, start_coord.y - 1));
  203.                                 delete_location((start_coord.x - 1, start_coord.y));
  204.                                 if (start_coord.y + 1 < 10)
  205.                                     delete_location((start_coord.x - 1, start_coord.y + 1));
  206.                             }
  207.                             for (int i = start_coord.x; i < length + start_coord.x; i++)
  208.                             {
  209.                                 if (start_coord.y + 1 < 10)
  210.                                     delete_location((i, start_coord.y + 1));
  211.                                 if (start_coord.y - 1 > 0)
  212.                                     delete_location((i, start_coord.y - 1));
  213.                                 Items[i, start_coord.y].state = Item_state.nothing;
  214.  
  215.                             }
  216.                             if (length + start_coord.x < 10)
  217.                             {
  218.                                 if (start_coord.y - 1 > 0)
  219.                                     delete_location((length + start_coord.x, start_coord.y - 1));
  220.                                 delete_location((length + start_coord.x, start_coord.y));
  221.                                 if (start_coord.y + 1 < 10)
  222.                                     delete_location((length + start_coord.x, start_coord.y + 1));
  223.                             }
  224.                             break;
  225.                         case Napr.left:
  226.                             if (start_coord.x + 1 < 10)
  227.                             {
  228.                                 if (start_coord.y - 1 > 0)
  229.                                     delete_location((start_coord.x + 1, start_coord.y - 1));
  230.                                 delete_location((start_coord.x + 1, start_coord.y));
  231.                                 if (start_coord.y + 1 < 10)
  232.                                     delete_location((start_coord.x + 1, start_coord.y + 1));
  233.                             }
  234.                             for (int i = start_coord.x; i > start_coord.x - length; i--)
  235.                             {
  236.                                 if (start_coord.y + 1 < 10)
  237.                                     delete_location((i, start_coord.y + 1));
  238.                                 if (start_coord.y - 1 > 0)
  239.                                     delete_location((i, start_coord.y - 1));
  240.                                 Items[i, start_coord.y].state = Item_state.nothing;
  241.                             }
  242.                             if (start_coord.x - length >= 0)
  243.                             {
  244.                                 if (start_coord.y - 1 > 0)
  245.                                     delete_location((start_coord.x - length, start_coord.y - 1));
  246.                                 delete_location((start_coord.x - length, start_coord.y));
  247.                                 if (start_coord.y + 1 < 10)
  248.                                     delete_location((start_coord.x - length, start_coord.y + 1));
  249.                             }
  250.                             break;
  251.                     }
  252.                     this.has_built = false;
  253.                     return true;
  254.                 }
  255.                 public Boolean buildship(Napr napp, (int x, int y) coor)
  256.                 {
  257.                     if (this.has_built) deleteship();
  258.                     coor.x--;coor.y--;
  259.                     this.nap = napp;
  260.                     this.start_coord = coor;
  261.                     if (!checkship(length, coor)) return false;
  262.                     switch (nap)
  263.                     {
  264.                         case Napr.bottom:
  265.                             if (coor.y > 1)
  266.                             {
  267.                                 if (coor.x + 1 < 10)
  268.                                     add_location((coor.x + 1, coor.y - 1));
  269.                                 add_location((coor.x, coor.y - 1));
  270.                                 if (coor.x - 1 > 0)
  271.                                     add_location((coor.x - 1, coor.y - 1));
  272.                             }
  273.                             for (int i = coor.y; i < length + coor.y; i++)
  274.                             {
  275.                                 if (coor.x + 1 < 10)
  276.                                     add_location((coor.x + 1, i));
  277.                                 if (coor.x - 1 > 0)
  278.                                     add_location((coor.x - 1, i));
  279.                                 Items[coor.x, i].state = Item_state.ship;
  280.                             }
  281.                             if (length + coor.y < 10)
  282.                             {
  283.                                 if (coor.x + 1 < 10)
  284.                                     add_location((coor.x + 1, coor.y + length));
  285.                                 add_location((coor.x, coor.y + length));
  286.                                 if (coor.x - 1 > 0)
  287.                                     add_location((coor.x - 1, coor.y + length));
  288.                             }
  289.                             break;
  290.                         case Napr.top:
  291.                             if (coor.y + 1 < 10)
  292.                             {
  293.                                 if (coor.x + 1 < 10)
  294.                                     add_location((coor.x + 1, coor.y + 1));
  295.                                 add_location((coor.x, coor.y + 1));
  296.                                 if (coor.x - 1 > 0)
  297.                                     add_location((coor.x - 1, coor.y + 1));
  298.                             }
  299.                             for (int i = coor.y; i > coor.y - length; i--)
  300.                             {
  301.                                 if (coor.x + 1 < 10)
  302.                                     add_location((coor.x + 1, i));
  303.                                 if (coor.x - 1 > 0)
  304.                                     add_location((coor.x - 1, i));
  305.                                 Items[coor.x, i].state = Item_state.ship;
  306.                             }
  307.                             if ( coor.y - length >= 0)
  308.                             {
  309.                                 if (coor.x + 1 < 10)
  310.                                     add_location((coor.x + 1, coor.y - length));
  311.                                 add_location((coor.x, coor.y - length));
  312.                                 if (coor.x - 1 > 0)
  313.                                     add_location((coor.x - 1, coor.y - length));
  314.                             }
  315.                             break;
  316.                         case Napr.right:
  317.                             if (coor.x > 1)
  318.                             {
  319.                                 if (coor.y - 1 > 0)
  320.                                     add_location((coor.x - 1, coor.y - 1));
  321.                                 add_location((coor.x - 1, coor.y));
  322.                                 if (coor.y + 1 < 10)
  323.                                     add_location((coor.x - 1, coor.y + 1));
  324.                             }
  325.                             for (int i = coor.x; i < length + coor.x; i++)
  326.                             {
  327.                                 if (coor.y + 1 < 10)
  328.                                     add_location((i, coor.y + 1));
  329.                                 if (coor.y - 1 > 0)
  330.                                     add_location((i, coor.y - 1));
  331.                                 Items[i, coor.y].state = Item_state.ship;
  332.  
  333.                             }
  334.                             if (length + coor.x < 10)
  335.                             {
  336.                                 if (coor.y - 1 > 0)
  337.                                     add_location((length + coor.x, coor.y - 1));
  338.                                 add_location((length + coor.x, coor.y));
  339.                                 if (coor.y + 1 < 10)
  340.                                     add_location((length + coor.x, coor.y + 1));
  341.                             }
  342.                             break;
  343.                         case Napr.left:
  344.                             if (coor.x + 1 < 10)
  345.                             {
  346.                                 if (coor.y - 1 > 0)
  347.                                     add_location((coor.x + 1, coor.y - 1));
  348.                                 add_location((coor.x + 1, coor.y));
  349.                                 if (coor.y + 1 < 10)
  350.                                     add_location((coor.x + 1, coor.y + 1));
  351.                             }
  352.                             for (int i = coor.x; i > coor.x - length; i--)
  353.                             {
  354.                                 if (coor.y + 1 < 10)
  355.                                     add_location((i, coor.y + 1));
  356.                                 if (coor.y - 1 > 0)
  357.                                     add_location((i, coor.y - 1));
  358.                                 Items[i, coor.y].state = Item_state.ship;
  359.                             }
  360.                             if (coor.x - length >= 0)
  361.                             {
  362.                                 if (coor.y - 1 > 0)
  363.                                     add_location((coor.x - length, coor.y - 1));
  364.                                 add_location((coor.x - length, coor.y));
  365.                                 if (coor.y + 1 < 10)
  366.                                     add_location((coor.x - length, coor.y + 1));
  367.                             }
  368.                             break;
  369.                     }
  370.                     this.has_built = true;
  371.                     return true;
  372.                 }
  373.                 Boolean checkship(int length, (int x, int y) coor)
  374.                 {
  375.                     //
  376.                     switch (nap)
  377.                     {
  378.                         case Napr.bottom:
  379.                             if (coor.y + length > 10) return false;
  380.                             for (int i = coor.y; i < length + coor.y; i++)
  381.                             {
  382.                                 if ((0 > i) || (i > 10)) return false;
  383.                                 if ((Items[coor.x, i].state == Item_state.ship_located) || (Items[coor.x, i].state == Item_state.ship)) return false;
  384.                                 /*if (coor.x + 1 < 10)
  385.                                     if ((Items[coor.x+1, i].state == Item_state.ship_located) || (Items[coor.x+1, i].state == Item_state.ship)) return false;
  386.                                 if (coor.x - 1 < 10)
  387.                                     if ((Items[coor.x-1, i].state == Item_state.ship_located) || (Items[coor.x-1, i].state == Item_state.ship)) return false;*/
  388.                             }
  389.                             break;
  390.                         case Napr.top:
  391.                             if (coor.y - length + 1 < 0) return false;
  392.                             for (int i = coor.y; i > coor.y - length; i--)
  393.                             {
  394.                                 if ((0 > i) || (i > 10)) return false;
  395.                                 if ((Items[coor.x, i].state == Item_state.ship_located) || (Items[coor.x, i].state == Item_state.ship)) return false;
  396.                                 /*if (coor.x + 1 < 10)
  397.                                     if ((Items[coor.x + 1, i].state == Item_state.ship_located) || (Items[coor.x + 1, i].state == Item_state.ship)) return false;
  398.                                 if (coor.x - 1 < 10)
  399.                                     if ((Items[coor.x - 1, i].state == Item_state.ship_located) || (Items[coor.x - 1, i].state == Item_state.ship)) return false;*/
  400.                             }
  401.                             break;
  402.                         case Napr.right:
  403.                             if (coor.x + length >10) return false;
  404.                             for (int i = coor.x; i < length + coor.x; i++)
  405.                             {
  406.                                 if ((0 > i) || (i > 10)) return false;
  407.                                 if ((Items[i, coor.y].state == Item_state.ship_located) || (Items[i, coor.y].state == Item_state.ship)) return false;
  408.                                 /*if (coor.y - 1 < 10)
  409.                                     if ((Items[i, coor.y-1].state == Item_state.ship_located) || (Items[i, coor.y-1].state == Item_state.ship)) return false;
  410.                                 if (coor.y + 1 < 10)
  411.                                     if ((Items[i, coor.y+1].state == Item_state.ship_located) || (Items[i, coor.y+1].state == Item_state.ship)) return false;*/
  412.                             }
  413.                             break;
  414.                         case Napr.left:
  415.                             if (coor.x - length + 1< 0) return false;
  416.                             for (int i = coor.x; i > coor.x - length; i--)
  417.                             {
  418.                                 if ((0 > i) || (i > 10)) return false;
  419.                                 if ((Items[i, coor.y].state == Item_state.ship_located) || (Items[i, coor.y].state == Item_state.ship)) return false;
  420.                                 /*if (coor.y - 1 < 10)
  421.                                     if ((Items[i, coor.y - 1].state == Item_state.ship_located) || (Items[i, coor.y - 1].state == Item_state.ship)) return false;
  422.                                 if (coor.y + 1 < 10)
  423.                                     if ((Items[i, coor.y + 1].state == Item_state.ship_located) || (Items[i, coor.y + 1].state == Item_state.ship)) return false;*/
  424.                             }
  425.                             break;
  426.                     }
  427.                     //
  428.                     return true;
  429.                 }
  430.  
  431.             }
  432.  
  433.             public struct Render
  434.             {
  435.                 int thickness;
  436.                 public void renderAll(PictureBox picbox, int thickn=2)
  437.                 {
  438.                     renderOcean(picbox, thickn);
  439.                     renderItems(picbox);
  440.                 }
  441.                 public void renderItems(PictureBox picbox)
  442.                 {
  443.                     Graphics g = picbox.CreateGraphics();
  444.                     Pen n = new Pen(Color.Black, thickness);
  445.                     int rrr = picbox.Width / 11;
  446.                     for (int i = 1; i < 11; i++)
  447.                     {
  448.                         for (int j = 1; j < 11; j++)
  449.                         {
  450.                             switch (Items[i-1, j-1].state)
  451.                             {
  452.                                 case Item_state.nothing:
  453.                                     {
  454.                                         g.FillRectangle(Brushes.White, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
  455.                                         break;
  456.                                     }
  457.                                 case Item_state.nothing_die:
  458.                                     {
  459.                                         g.FillRectangle(Brushes.Gray, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
  460.                                         break;
  461.                                     }
  462.                                 case Item_state.ship:
  463.                                     {
  464.                                         g.FillRectangle(Brushes.Aqua, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
  465.                                         break;
  466.                                     }
  467.                                 case Item_state.ship_die:
  468.                                     {
  469.                                         g.FillRectangle(Brushes.Red, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
  470.                                         break;
  471.                                     }
  472.                                 case Item_state.ship_located:
  473.                                     {
  474.                                         g.FillRectangle(Brushes.Gray, (i * rrr) + thickness , (j * rrr) + thickness, rrr - thickness, rrr - thickness);
  475.                                         break;
  476.                                     }
  477.  
  478.                             }
  479.                         }
  480.                     }
  481.                 }
  482.                 public void renderOcean(PictureBox picbox, int thickn=2)
  483.                 {
  484.                     thickness = thickn;
  485.                     Graphics g = picbox.CreateGraphics();
  486.                     Pen n = new Pen(Color.Black, thickness);
  487.                     //int a = picbox.Width;
  488.                     //int b = picbox.Height;
  489.                     int rrr = picbox.Width / 11;
  490.                     //g.DrawLine(n, 0, 0,a,0);
  491.                     //g.DrawLine(n,a, 0,a,b);
  492.                     //g.DrawLine(n, a, b,0,b);
  493.                     //g.DrawLine(n, 0, b,0,0);
  494.                     for (int i = 1; i < 12; i++)
  495.                     {
  496.                         g.DrawLine(n, 0, i * rrr + 1, 11 * rrr, i * rrr + 1);
  497.                         g.DrawLine(n, i * rrr + 1, 0, i * rrr + 1, 11 * rrr);
  498.                     }
  499.                 }
  500.             }
  501.         }
  502.     }
  503.    
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement