Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace OceanWar
- {
- public partial class main : Form
- {
- public main()
- {
- InitializeComponent();
- }
- static Pole pol = new Pole();
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- pol.generatepole(textBox1);
- }
- private void button2_Click(object sender, EventArgs e)
- {
- pol.deleteship(1);
- }
- private void RENDER(object sender, EventArgs e)
- {
- pol.renderer.renderAll(pictureBox1);
- }
- class Pole
- {
- enum Napr { left = 1, right, top, bottom };
- enum Item_state { nothing = 0, nothing_die = 1, ship = 2, ship_die = 3, ship_located = 99 };
- //
- public Render renderer;
- static (Item_state state, int[] ship_ids)[,] Items = new (Item_state state, int[] ship_ids)[10, 10];
- Ship[] ships = new Ship[11];
- //static Item[,] Items = new Item[10, 10];
- public Pole()
- {
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- Items[i, j].ship_ids = new int[4];
- }
- }
- ships[1] = new Ship(4,1);
- ships[2] = new Ship(3,2); ships[3] = new Ship(3,3);
- ships[4] = new Ship(2,4); ships[5] = new Ship(2,5); ships[6] = new Ship(2,6);
- ships[7] = new Ship(1,6); ships[8] = new Ship(1,8); ships[9] = new Ship(1,9); ships[10] = new Ship(1,10);
- }
- public Boolean generatepole(TextBox tx)
- {
- Boolean kek;
- Random rand = new Random();
- for (int i = 1; i < 11; i++)
- {
- do
- {
- kek = generateship(i, (i, i));
- tx.Text += kek ? $"\r\n {ships[i].length}хпалубный корабль {i} успех" : $"\r\n {ships[i].length}хпалубный корабль {i} ошибка";
- } while (!kek);
- }
- return true;
- }
- public Boolean generateship(int id, (int x, int y) coor)
- {
- 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;
- Random rand = new Random();
- for (Napr kek = ((Napr)rand.Next(1, 5)); kek < (Napr)5; kek++)
- {
- if (ships[id].buildship(kek, coor)) break;
- }
- return true;
- }
- public Boolean deleteship(int id)
- {
- if (!ships[id].deleteship()) return false;
- return true;
- }
- class Ship
- {
- Boolean die { get; set; } = false;
- Boolean has_built = false;
- public int length{ get; set; }
- int id { get; set; }
- Napr nap { get; set; }
- (int x, int y) start_coord { get; set; }
- public Ship(int lengthp,int idd)
- {
- this.length = lengthp;
- this.id = idd;
- }
- void delete_location((int x, int y) coor)
- {
- for (int i = 0; i < Items[coor.x, coor.y].ship_ids.Length; i++)
- {
- if ((Items[coor.x, coor.y].ship_ids[i] != 0) && (Items[coor.x, coor.y].ship_ids[i] == this.id))
- {
- Items[coor.x, coor.y].ship_ids[i] = 0;
- break;
- }
- }
- if (Items[coor.x, coor.y].ship_ids[1] == 0) Items[coor.x, coor.y].state = Item_state.nothing;
- }
- void add_location((int x, int y) coor)
- {
- for (int i=0;i< Items[coor.x, coor.y].ship_ids.Length; i++)
- {
- if (Items[coor.x, coor.y].ship_ids[i]==0)
- {
- Items[coor.x, coor.y].ship_ids[i] = this.id;
- break;
- }
- }
- Items[coor.x, coor.y].state = Item_state.ship_located;
- }
- public Boolean deleteship()
- {
- if (!this.has_built) return false;
- switch (nap)
- {
- case Napr.bottom:
- if (start_coord.y > 1)
- {
- if (start_coord.x + 1 < 10)
- delete_location((start_coord.x + 1, start_coord.y - 1));
- delete_location((start_coord.x, start_coord.y - 1));
- if (start_coord.x - 1 > 0)
- delete_location((start_coord.x - 1, start_coord.y - 1));
- }
- for (int i = start_coord.y; i < length + start_coord.y; i++)
- {
- if (start_coord.x + 1 < 10)
- delete_location((start_coord.x + 1, i));
- if (start_coord.x - 1 > 0)
- delete_location((start_coord.x - 1, i));
- Items[start_coord.x, i].state = Item_state.nothing;
- }
- if (length + start_coord.y < 10)
- {
- if (start_coord.x + 1 < 10)
- delete_location((start_coord.x + 1, start_coord.y + length));
- delete_location((start_coord.x, start_coord.y + length));
- if (start_coord.x - 1 > 0)
- delete_location((start_coord.x - 1, start_coord.y + length));
- }
- break;
- case Napr.top:
- if (start_coord.y + 1 < 10)
- {
- if (start_coord.x + 1 < 10)
- delete_location((start_coord.x + 1, start_coord.y + 1));
- delete_location((start_coord.x, start_coord.y + 1));
- if (start_coord.x - 1 > 0)
- delete_location((start_coord.x - 1, start_coord.y + 1));
- }
- for (int i = start_coord.y; i > start_coord.y - length; i--)
- {
- if (start_coord.x + 1 < 10)
- delete_location((start_coord.x + 1, i));
- if (start_coord.x - 1 > 0)
- delete_location((start_coord.x - 1, i));
- Items[start_coord.x, i].state = Item_state.nothing;
- }
- if ( start_coord.y - length >= 0)
- {
- if (start_coord.x + 1 < 10)
- delete_location((start_coord.x + 1, start_coord.y - length));
- delete_location((start_coord.x, start_coord.y - length));
- if (start_coord.x - 1 > 0)
- delete_location((start_coord.x - 1, start_coord.y - length));
- }
- break;
- case Napr.right:
- if (start_coord.x > 1)
- {
- if (start_coord.y - 1 > 0)
- delete_location((start_coord.x - 1, start_coord.y - 1));
- delete_location((start_coord.x - 1, start_coord.y));
- if (start_coord.y + 1 < 10)
- delete_location((start_coord.x - 1, start_coord.y + 1));
- }
- for (int i = start_coord.x; i < length + start_coord.x; i++)
- {
- if (start_coord.y + 1 < 10)
- delete_location((i, start_coord.y + 1));
- if (start_coord.y - 1 > 0)
- delete_location((i, start_coord.y - 1));
- Items[i, start_coord.y].state = Item_state.nothing;
- }
- if (length + start_coord.x < 10)
- {
- if (start_coord.y - 1 > 0)
- delete_location((length + start_coord.x, start_coord.y - 1));
- delete_location((length + start_coord.x, start_coord.y));
- if (start_coord.y + 1 < 10)
- delete_location((length + start_coord.x, start_coord.y + 1));
- }
- break;
- case Napr.left:
- if (start_coord.x + 1 < 10)
- {
- if (start_coord.y - 1 > 0)
- delete_location((start_coord.x + 1, start_coord.y - 1));
- delete_location((start_coord.x + 1, start_coord.y));
- if (start_coord.y + 1 < 10)
- delete_location((start_coord.x + 1, start_coord.y + 1));
- }
- for (int i = start_coord.x; i > start_coord.x - length; i--)
- {
- if (start_coord.y + 1 < 10)
- delete_location((i, start_coord.y + 1));
- if (start_coord.y - 1 > 0)
- delete_location((i, start_coord.y - 1));
- Items[i, start_coord.y].state = Item_state.nothing;
- }
- if (start_coord.x - length >= 0)
- {
- if (start_coord.y - 1 > 0)
- delete_location((start_coord.x - length, start_coord.y - 1));
- delete_location((start_coord.x - length, start_coord.y));
- if (start_coord.y + 1 < 10)
- delete_location((start_coord.x - length, start_coord.y + 1));
- }
- break;
- }
- this.has_built = false;
- return true;
- }
- public Boolean buildship(Napr napp, (int x, int y) coor)
- {
- if (this.has_built) deleteship();
- coor.x--;coor.y--;
- this.nap = napp;
- this.start_coord = coor;
- if (!checkship(length, coor)) return false;
- switch (nap)
- {
- case Napr.bottom:
- if (coor.y > 1)
- {
- if (coor.x + 1 < 10)
- add_location((coor.x + 1, coor.y - 1));
- add_location((coor.x, coor.y - 1));
- if (coor.x - 1 > 0)
- add_location((coor.x - 1, coor.y - 1));
- }
- for (int i = coor.y; i < length + coor.y; i++)
- {
- if (coor.x + 1 < 10)
- add_location((coor.x + 1, i));
- if (coor.x - 1 > 0)
- add_location((coor.x - 1, i));
- Items[coor.x, i].state = Item_state.ship;
- }
- if (length + coor.y < 10)
- {
- if (coor.x + 1 < 10)
- add_location((coor.x + 1, coor.y + length));
- add_location((coor.x, coor.y + length));
- if (coor.x - 1 > 0)
- add_location((coor.x - 1, coor.y + length));
- }
- break;
- case Napr.top:
- if (coor.y + 1 < 10)
- {
- if (coor.x + 1 < 10)
- add_location((coor.x + 1, coor.y + 1));
- add_location((coor.x, coor.y + 1));
- if (coor.x - 1 > 0)
- add_location((coor.x - 1, coor.y + 1));
- }
- for (int i = coor.y; i > coor.y - length; i--)
- {
- if (coor.x + 1 < 10)
- add_location((coor.x + 1, i));
- if (coor.x - 1 > 0)
- add_location((coor.x - 1, i));
- Items[coor.x, i].state = Item_state.ship;
- }
- if ( coor.y - length >= 0)
- {
- if (coor.x + 1 < 10)
- add_location((coor.x + 1, coor.y - length));
- add_location((coor.x, coor.y - length));
- if (coor.x - 1 > 0)
- add_location((coor.x - 1, coor.y - length));
- }
- break;
- case Napr.right:
- if (coor.x > 1)
- {
- if (coor.y - 1 > 0)
- add_location((coor.x - 1, coor.y - 1));
- add_location((coor.x - 1, coor.y));
- if (coor.y + 1 < 10)
- add_location((coor.x - 1, coor.y + 1));
- }
- for (int i = coor.x; i < length + coor.x; i++)
- {
- if (coor.y + 1 < 10)
- add_location((i, coor.y + 1));
- if (coor.y - 1 > 0)
- add_location((i, coor.y - 1));
- Items[i, coor.y].state = Item_state.ship;
- }
- if (length + coor.x < 10)
- {
- if (coor.y - 1 > 0)
- add_location((length + coor.x, coor.y - 1));
- add_location((length + coor.x, coor.y));
- if (coor.y + 1 < 10)
- add_location((length + coor.x, coor.y + 1));
- }
- break;
- case Napr.left:
- if (coor.x + 1 < 10)
- {
- if (coor.y - 1 > 0)
- add_location((coor.x + 1, coor.y - 1));
- add_location((coor.x + 1, coor.y));
- if (coor.y + 1 < 10)
- add_location((coor.x + 1, coor.y + 1));
- }
- for (int i = coor.x; i > coor.x - length; i--)
- {
- if (coor.y + 1 < 10)
- add_location((i, coor.y + 1));
- if (coor.y - 1 > 0)
- add_location((i, coor.y - 1));
- Items[i, coor.y].state = Item_state.ship;
- }
- if (coor.x - length >= 0)
- {
- if (coor.y - 1 > 0)
- add_location((coor.x - length, coor.y - 1));
- add_location((coor.x - length, coor.y));
- if (coor.y + 1 < 10)
- add_location((coor.x - length, coor.y + 1));
- }
- break;
- }
- this.has_built = true;
- return true;
- }
- Boolean checkship(int length, (int x, int y) coor)
- {
- //
- switch (nap)
- {
- case Napr.bottom:
- if (coor.y + length > 10) return false;
- for (int i = coor.y; i < length + coor.y; i++)
- {
- if ((0 > i) || (i > 10)) return false;
- if ((Items[coor.x, i].state == Item_state.ship_located) || (Items[coor.x, i].state == Item_state.ship)) return false;
- /*if (coor.x + 1 < 10)
- if ((Items[coor.x+1, i].state == Item_state.ship_located) || (Items[coor.x+1, i].state == Item_state.ship)) return false;
- if (coor.x - 1 < 10)
- if ((Items[coor.x-1, i].state == Item_state.ship_located) || (Items[coor.x-1, i].state == Item_state.ship)) return false;*/
- }
- break;
- case Napr.top:
- if (coor.y - length + 1 < 0) return false;
- for (int i = coor.y; i > coor.y - length; i--)
- {
- if ((0 > i) || (i > 10)) return false;
- if ((Items[coor.x, i].state == Item_state.ship_located) || (Items[coor.x, i].state == Item_state.ship)) return false;
- /*if (coor.x + 1 < 10)
- if ((Items[coor.x + 1, i].state == Item_state.ship_located) || (Items[coor.x + 1, i].state == Item_state.ship)) return false;
- if (coor.x - 1 < 10)
- if ((Items[coor.x - 1, i].state == Item_state.ship_located) || (Items[coor.x - 1, i].state == Item_state.ship)) return false;*/
- }
- break;
- case Napr.right:
- if (coor.x + length >10) return false;
- for (int i = coor.x; i < length + coor.x; i++)
- {
- if ((0 > i) || (i > 10)) return false;
- if ((Items[i, coor.y].state == Item_state.ship_located) || (Items[i, coor.y].state == Item_state.ship)) return false;
- /*if (coor.y - 1 < 10)
- if ((Items[i, coor.y-1].state == Item_state.ship_located) || (Items[i, coor.y-1].state == Item_state.ship)) return false;
- if (coor.y + 1 < 10)
- if ((Items[i, coor.y+1].state == Item_state.ship_located) || (Items[i, coor.y+1].state == Item_state.ship)) return false;*/
- }
- break;
- case Napr.left:
- if (coor.x - length + 1< 0) return false;
- for (int i = coor.x; i > coor.x - length; i--)
- {
- if ((0 > i) || (i > 10)) return false;
- if ((Items[i, coor.y].state == Item_state.ship_located) || (Items[i, coor.y].state == Item_state.ship)) return false;
- /*if (coor.y - 1 < 10)
- if ((Items[i, coor.y - 1].state == Item_state.ship_located) || (Items[i, coor.y - 1].state == Item_state.ship)) return false;
- if (coor.y + 1 < 10)
- if ((Items[i, coor.y + 1].state == Item_state.ship_located) || (Items[i, coor.y + 1].state == Item_state.ship)) return false;*/
- }
- break;
- }
- //
- return true;
- }
- }
- public struct Render
- {
- int thickness;
- public void renderAll(PictureBox picbox, int thickn=2)
- {
- renderOcean(picbox, thickn);
- renderItems(picbox);
- }
- public void renderItems(PictureBox picbox)
- {
- Graphics g = picbox.CreateGraphics();
- Pen n = new Pen(Color.Black, thickness);
- int rrr = picbox.Width / 11;
- for (int i = 1; i < 11; i++)
- {
- for (int j = 1; j < 11; j++)
- {
- switch (Items[i-1, j-1].state)
- {
- case Item_state.nothing:
- {
- g.FillRectangle(Brushes.White, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
- break;
- }
- case Item_state.nothing_die:
- {
- g.FillRectangle(Brushes.Gray, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
- break;
- }
- case Item_state.ship:
- {
- g.FillRectangle(Brushes.Aqua, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
- break;
- }
- case Item_state.ship_die:
- {
- g.FillRectangle(Brushes.Red, (i * rrr) + thickness, (j * rrr) + thickness, rrr - thickness, rrr - thickness);
- break;
- }
- case Item_state.ship_located:
- {
- g.FillRectangle(Brushes.Gray, (i * rrr) + thickness , (j * rrr) + thickness, rrr - thickness, rrr - thickness);
- break;
- }
- }
- }
- }
- }
- public void renderOcean(PictureBox picbox, int thickn=2)
- {
- thickness = thickn;
- Graphics g = picbox.CreateGraphics();
- Pen n = new Pen(Color.Black, thickness);
- //int a = picbox.Width;
- //int b = picbox.Height;
- int rrr = picbox.Width / 11;
- //g.DrawLine(n, 0, 0,a,0);
- //g.DrawLine(n,a, 0,a,b);
- //g.DrawLine(n, a, b,0,b);
- //g.DrawLine(n, 0, b,0,0);
- for (int i = 1; i < 12; i++)
- {
- g.DrawLine(n, 0, i * rrr + 1, 11 * rrr, i * rrr + 1);
- g.DrawLine(n, i * rrr + 1, 0, i * rrr + 1, 11 * rrr);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement