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;
- using System.Threading;
- namespace SeaBattle
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- int[,] field1 = new int[10, 10];
- int[,] field2 = new int[10, 10];
- bool start = false;
- int n1, n2;
- /*
- 0: пусто
- 1: корабль
- 2: промах
- 3: попадание
- 4: убит
- */
- private bool Check(int[,] field1)
- {
- bool check = true;
- int n = 0;
- int[] mass_boat = new int[5];
- for (int i = 0; i < 10; i++)
- {
- int k = 0;
- for (int j = 0; j < 10; j++)
- {
- if (field1[i, j] == 0 || j == 9)
- {
- if (field1[i, j] == 1)
- {
- k++;
- n++;
- }
- if (k > 4)
- {
- check = false;
- goto TheEnd;
- }
- else
- {
- if (k != 0)
- {
- mass_boat[k]++;
- k = 0;
- }
- }
- }
- else if (field1[i, j] == 1)
- {
- k++;
- n++;
- }
- }
- }
- //MessageBox.Show(mass_boat[1].ToString());
- for (int j = 0; j < 10; j++)
- {
- int k = 0;
- for (int i = 0; i < 10; i++)
- {
- if (field1[i, j] == 0 || i == 9)
- {
- if (field1[i, j] == 1)
- {
- k++;
- n++;
- }
- if (k > 4)
- {
- check = false;
- goto TheEnd;
- }
- else
- {
- if (k != 0)
- {
- mass_boat[k]++;
- k = 0;
- }
- }
- }
- else if (field1[i, j] == 1)
- {
- k++;
- n++;
- }
- }
- }
- if (n != 40 || mass_boat[1] != 24 || mass_boat[2] != 3 || mass_boat[3] != 2 || mass_boat[4] != 1)
- {
- check = false;
- }
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- if (field1[i, j] == 1)
- {
- for (int x = -1; x <= 1; x += 2)
- {
- for (int y = -1; y <= 1; y += 2)
- {
- if (i + x >= 0 && i + x < 10 && j + y >= 0 && j + y < 10)
- {
- if (field1[i + x, j + y] == 1)
- {
- check = false;
- goto TheEnd;
- }
- }
- }
- }
- }
- }
- }
- TheEnd:
- if (check) return true;
- else return false;
- }
- private void Placement(int f)
- {
- for (int length = 4; length > 0; length--)
- {
- for (int k = 0; k <= 4 - length; k++)
- {
- bool place = false;
- while (!place)
- {
- Random rnd = new Random();
- int value = rnd.Next(0, 2);
- int l = length + 2;
- int w = 3;
- if (value == 1)
- {
- int temp = l;
- l = w;
- w = temp;
- }
- int x = rnd.Next(0, 10);
- int y = rnd.Next(0, 10);
- bool clear = true;
- if (x + l - 3 >= 10 || y + w - 3 >= 10)
- {
- clear = false;
- }
- else
- {
- for (int i = x - 1; i < x + l - 1; i++)
- {
- for (int j = y - 1; j < y + w - 1; j++)
- {
- if (i >= 0 && i < 10 && j >= 0 && j < 10)
- {
- if (Field(f)[i, j] == 1)
- {
- clear = false;
- goto Flag;
- }
- }
- }
- }
- }
- Flag:
- if (clear)
- {
- place = true;
- for (int i = x; i < x + l - 2; i++)
- {
- for (int j = y; j < y + w - 2; j++)
- {
- Field(f)[i, j] = 1;
- }
- }
- }
- }
- }
- }
- }
- private Button Colorize(int x, int y, int f)
- {
- if (f == 1) return Button(x * 10 + y);
- else return Button1(x * 10 + y + 100);
- }
- private Button Button(int position)
- {
- switch (position)
- {
- case 0: return button1;
- case 1: return button2;
- case 2: return button3;
- case 3: return button4;
- case 4: return button5;
- case 5: return button6;
- case 6: return button7;
- case 7: return button8;
- case 8: return button9;
- case 9: return button10;
- case 10: return button11;
- case 11: return button12;
- case 12: return button13;
- case 13: return button14;
- case 14: return button15;
- case 15: return button16;
- case 16: return button17;
- case 17: return button18;
- case 18: return button19;
- case 19: return button20;
- case 20: return button21;
- case 21: return button22;
- case 22: return button23;
- case 23: return button24;
- case 24: return button25;
- case 25: return button26;
- case 26: return button27;
- case 27: return button28;
- case 28: return button29;
- case 29: return button30;
- case 30: return button31;
- case 31: return button32;
- case 32: return button33;
- case 33: return button34;
- case 34: return button35;
- case 35: return button36;
- case 36: return button37;
- case 37: return button38;
- case 38: return button39;
- case 39: return button40;
- case 40: return button41;
- case 41: return button42;
- case 42: return button43;
- case 43: return button44;
- case 44: return button45;
- case 45: return button46;
- case 46: return button47;
- case 47: return button48;
- case 48: return button49;
- case 49: return button50;
- case 50: return button51;
- case 51: return button52;
- case 52: return button53;
- case 53: return button54;
- case 54: return button55;
- case 55: return button56;
- case 56: return button57;
- case 57: return button58;
- case 58: return button59;
- case 59: return button60;
- case 60: return button61;
- case 61: return button62;
- case 62: return button63;
- case 63: return button64;
- case 64: return button65;
- case 65: return button66;
- case 66: return button67;
- case 67: return button68;
- case 68: return button69;
- case 69: return button70;
- case 70: return button71;
- case 71: return button72;
- case 72: return button73;
- case 73: return button74;
- case 74: return button75;
- case 75: return button76;
- case 76: return button77;
- case 77: return button78;
- case 78: return button79;
- case 79: return button80;
- case 80: return button81;
- case 81: return button82;
- case 82: return button83;
- case 83: return button84;
- case 84: return button85;
- case 85: return button86;
- case 86: return button87;
- case 87: return button88;
- case 88: return button89;
- case 89: return button90;
- case 90: return button91;
- case 91: return button92;
- case 92: return button93;
- case 93: return button94;
- case 94: return button95;
- case 95: return button96;
- case 96: return button97;
- case 97: return button98;
- case 98: return button99;
- case 99: return button100;
- default: return button1;
- }
- } //return button
- private Button Button1(int position)
- {
- switch (position)
- {
- case 100: return button101;
- case 101: return button102;
- case 102: return button103;
- case 103: return button104;
- case 104: return button105;
- case 105: return button106;
- case 106: return button107;
- case 107: return button108;
- case 108: return button109;
- case 109: return button110;
- case 110: return button111;
- case 111: return button112;
- case 112: return button113;
- case 113: return button114;
- case 114: return button115;
- case 115: return button116;
- case 116: return button117;
- case 117: return button118;
- case 118: return button119;
- case 119: return button120;
- case 120: return button121;
- case 121: return button122;
- case 122: return button123;
- case 123: return button124;
- case 124: return button125;
- case 125: return button126;
- case 126: return button127;
- case 127: return button128;
- case 128: return button129;
- case 129: return button130;
- case 130: return button131;
- case 131: return button132;
- case 132: return button133;
- case 133: return button134;
- case 134: return button135;
- case 135: return button136;
- case 136: return button137;
- case 137: return button138;
- case 138: return button139;
- case 139: return button140;
- case 140: return button141;
- case 141: return button142;
- case 142: return button143;
- case 143: return button144;
- case 144: return button145;
- case 145: return button146;
- case 146: return button147;
- case 147: return button148;
- case 148: return button149;
- case 149: return button150;
- case 150: return button151;
- case 151: return button152;
- case 152: return button153;
- case 153: return button154;
- case 154: return button155;
- case 155: return button156;
- case 156: return button157;
- case 157: return button158;
- case 158: return button159;
- case 159: return button160;
- case 160: return button161;
- case 161: return button162;
- case 162: return button163;
- case 163: return button164;
- case 164: return button165;
- case 165: return button166;
- case 166: return button167;
- case 167: return button168;
- case 168: return button169;
- case 169: return button170;
- case 170: return button171;
- case 171: return button172;
- case 172: return button173;
- case 173: return button174;
- case 174: return button175;
- case 175: return button176;
- case 176: return button177;
- case 177: return button178;
- case 178: return button179;
- case 179: return button180;
- case 180: return button181;
- case 181: return button182;
- case 182: return button183;
- case 183: return button184;
- case 184: return button185;
- case 185: return button186;
- case 186: return button187;
- case 187: return button188;
- case 188: return button189;
- case 189: return button190;
- case 190: return button191;
- case 191: return button192;
- case 192: return button193;
- case 193: return button194;
- case 194: return button195;
- case 195: return button196;
- case 196: return button197;
- case 197: return button198;
- case 198: return button199;
- case 199: return button200;
- default: return button101;
- }
- } //return button
- private void Restart()
- {
- button203.Visible = true;
- button201.Visible = true;
- n = 0;
- listBox1.Items.Clear();
- textBox1.Clear();
- start = false;
- step = true;
- for (int i = 0; i < 100; i++)
- {
- Button(i).FlatAppearance.BorderSize = 0;
- Button(i).FlatStyle = FlatStyle.Flat;
- Button(i).BackColor = Color.LightSkyBlue;
- Button1(i + 100).FlatAppearance.BorderSize = 0;
- Button1(i + 100).FlatStyle = FlatStyle.Flat;
- Button1(i + 100).BackColor = Color.LightSkyBlue;
- }
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- field1[i, j] = 0;
- field2[i, j] = 0;
- }
- }
- } //Restart
- private void Button1(object sender, EventArgs e) //placement field1
- {
- if (!start)
- {
- int position = Convert.ToInt16(((Button)sender).Tag);
- int x = position / 10;
- int y = position % 10;
- field1[x, y] = Math.Abs(field1[x, y] - 1);
- if (field1[x, y] == 1)
- {
- Button(position).BackColor = Color.SlateGray;
- }
- else Button(position).BackColor = Color.LightSkyBlue;
- }
- }
- private void button202_Click(object sender, EventArgs e) // restart
- {
- Restart();
- }
- private void Button101(object sender, EventArgs e)
- {
- } //-
- private void button203_Click(object sender, EventArgs e)
- {
- for(int i = 0; i < 10; i++)
- {
- for(int j = 0; j < 10; j++)
- {
- if(Field(1)[i, j] == 1)
- {
- Field(1)[i, j] = 0;
- Colorize(i, j, 1).BackColor = Color.LightSkyBlue;
- }
- }
- }
- Placement(1);
- for(int i = 0; i < 10; i++)
- {
- for(int j = 0; j < 10; j++)
- {
- if (Field(1)[i, j] == 1)
- {
- Colorize(i, j, 1).BackColor = Color.SlateGray;
- }
- }
- }
- }
- private void output(string str)
- {
- listBox1.Items.Insert(n++, str);
- listBox1.SelectedIndex = listBox1.Items.Count - 1;
- listBox1.SelectedIndex = -1;
- }
- private void button201_Click(object sender, EventArgs e) //start
- {
- if (!Check(field1) && start == false)
- {
- MessageBox.Show(" Корабли раставлены неверно");
- }
- else if (start == false)
- {
- button201.Visible = false;
- button203.Visible = false;
- listBox1.Items.Clear();
- n1 = 10; n2 = 10;
- start = true;
- Placement(2);
- str = " ***[ Игра началась ]***";
- output(str);
- str = "Ваш ход первый";
- output(str);
- textBox1.Clear();
- str = (10 - n1) + ":" + (10 - n2);
- textBox1.Text = str;
- }
- }
- bool step = false;
- bool trigger = false;
- string str;
- int n = 0;
- int R = 0;
- private void Pause()
- {
- Thread.Sleep(250);
- Task.Factory.StartNew(() => {
- });
- }
- private int[,] Field (int i)
- {
- if (i == 1) return field1;
- else return field2;
- }
- private void Buttons_field2(object sender, EventArgs e)// GAME
- {
- void Game_cheak()
- {
- textBox1.Clear();
- str = (10 - n1) + ":" + (10 - n2);
- textBox1.Text = str;
- if (n1 < 1)
- {
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- if (Field(2)[i, j] == 1)
- {
- Colorize(i, j, 2).BackColor = Color.SlateGray;
- }
- }
- }
- MessageBox.Show("Вы проиграли");
- Restart();
- }
- if (n2 < 1)
- {
- MessageBox.Show("Вы победили");
- Restart();
- }
- }
- bool Dfs(int x, int y, int x1, int y1, int f)
- {
- if (Field(f)[x, y] == 1 && (x != x1 || y != y1)) return true;
- bool t1 = false, t2 = false, t3 = false, t4 = false;
- if ((x - 1 != x1 || y != y1) && x - 1 >= 0) if (Field(f)[x - 1, y] != 2 && Field(f)[x - 1, y] != 0) t1 = Dfs(x - 1, y, x, y, f);
- if ((x + 1 != x1 || y != y1) && x + 1 < 10) if (Field(f)[x + 1, y] != 2 && Field(f)[x + 1, y] != 0) t2 = Dfs(x + 1, y, x, y, f);
- if ((x != x1 || y + 1 != y1) && y + 1 < 10) if (Field(f)[x, y + 1] != 2 && Field(f)[x, y + 1] != 0) t3 = Dfs(x, y + 1, x, y, f);
- if ((x != x1 || y - 1 != y1) && y - 1 >= 0) if (Field(f)[x, y - 1] != 2 && Field(f)[x, y - 1] != 0) t4 = Dfs(x, y - 1, x, y, f);
- return t1 || t2 || t3 || t4;
- }
- void Kill(int x, int y, int f)
- {
- for (int i = -1; i < 2; i++)
- {
- for (int j = -1; j < 2; j++)
- {
- if (x + i >= 0 && x + i < 10 && y + j >= 0 && y + j < 10)
- {
- if (Field(f)[x + i, y + j] == 0)
- {
- Field(f)[x + i, y + j] = 2;
- Colorize((x + i),(y + j), f).BackColor = Color.DeepSkyBlue;
- }
- else if (Field(f)[x + i, y + j] == 1 || Field(f)[x + i, y + j] == 3)
- {
- Field(f)[x + i, y + j] = 4;
- Colorize((x + i), (y + j), f).BackColor = Color.Red;
- Kill(x + i, y + j, f);
- }
- }
- }
- }
- }
- void Shoot(int f, int x, int y, int t)
- {
- if(t != 4) Field(f)[x, y] = t;
- if (f == 1) Pause();
- int x1 = x;
- if (x1 == 9) x1 = 10;
- if (f == 1) str = "Он: ";
- else str = "Вы: ";
- str = str + Convert.ToChar(x1 + 'А') + (y + 1);
- if (t == 2)
- {
- str += " - мимо";
- Colorize(x, y, f).BackColor = Color.DeepSkyBlue;
- }
- else if (t == 3)
- {
- str += " - ранил";
- Colorize(x, y, f).BackColor = Color.Orange;
- }
- else
- {
- str += " - убил";
- Colorize(x, y, f).BackColor = Color.Red;
- }
- output(str);
- Colorize(x, y, f).Refresh();
- }
- if (start)
- {
- Game_cheak();
- int position = Convert.ToInt16(((Button)sender).Tag);
- int x = (position - 100) / 10;
- int y = position % 10;
- if (field2[x, y] != 0 && field2[x, y] != 1)
- {
- return;
- }
- if (field2[x, y] == 0)
- {
- Shoot(2, x, y, 2);
- }
- else if (field2[x, y] == 1)
- {
- if (Dfs(x, y, x, y, 2))
- {
- Shoot(2, x, y, 3);
- return;
- }
- else
- {
- Shoot(2, x, y, 4);
- Kill(x, y, 2);
- n2--;
- Game_cheak();
- return;
- }
- }
- Game_cheak();
- //==============================================================================================================================
- Random rnd = new Random();
- step = false;
- while (!step)
- {
- if (trigger)
- {
- if (R == 0)
- {
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- if (field1[i, j] == 3)
- {
- int t = rnd.Next(0, 4);
- int r = rnd.Next(0, 2);
- if (r == 0) r = -1;
- for (int k = 0; k < 4; k++)
- {
- if (t == 0) { x = i + 1; y = j; };
- if (t == 1) { x = i - 1; y = j; };
- if (t == 2) { x = i; y = j + 1; };
- if (t == 3) { x = i; y = j - 1; };
- if (x >= 0 && x < 10 && y >= 0 && y < 10)
- {
- if (field1[x, y] == 1)
- {
- if (t == 0 || t == 1) R = 1;
- else R = 2;
- if (Dfs(x, y, x, y, 1))
- {
- Shoot(1, x, y, 3);
- goto Break1;
- }
- else
- {
- Shoot(1, x, y, 4);
- Kill(x, y, 1);
- trigger = false;
- R = 0;
- n1--;
- goto Break1;
- }
- }
- else if (field1[x, y] == 0)
- {
- Shoot(1, x, y, 2);
- step = true;
- goto Break1;
- }
- else { }
- }
- t = (t + r + 4) % 4;
- }
- }
- }
- }
- }
- else if (R == 1) //// R 1
- {
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- if (field1[i, j] == 3)
- {
- x = i;
- y = j;
- int r = rnd.Next(0, 2);
- if (r == 0) r = -1;
- int x2 = x;
- while (start)
- {
- x2 += r;
- if (x2 < 0 || x2 > 9) break;
- if (field1[x2, y] == 3) continue;
- if (field1[x2, y] == 2) break;
- else
- {
- if (field1[x2, y] == 0)
- {
- Shoot(1, x2, y, 2);
- step = true;
- goto Break1;
- }
- else if (field1[x2, y] == 1)
- {
- if (Dfs(x2, y, x2, y, 1))
- {
- Shoot(1, x2, y, 3);
- goto Break1;
- }
- else
- {
- Shoot(1, x2, y, 4);
- Kill(x2, y, 1);
- trigger = false;
- R = 0;
- n1--;
- goto Break1;
- }
- }
- }
- }
- x2 = x;
- while (start)
- {
- x2 -= r;
- if (x2 < 0 || x2 > 9) break;
- if (field1[x2, y] == 3) continue;
- if (field1[x2, y] == 2) break;
- else
- {
- if (field1[x2, y] == 0)
- {
- Shoot(1, x2, y, 2);
- step = true;
- goto Break1;
- }
- else if (field1[x2, y] == 1)
- {
- if (Dfs(x2, y, x2, y, 1))
- {
- Shoot(1, x2, y, 3);
- goto Break1;
- }
- else
- {
- Shoot(1, x2, y, 4);
- Kill(x2, y, 1);
- trigger = false;
- R = 0;
- n1--;
- goto Break1;
- }
- }
- }
- }
- }
- }
- }
- }
- else if (R == 2) //// R 2
- {
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- if (field1[i, j] == 3)
- {
- x = i;
- y = j;
- int r = rnd.Next(0, 2);
- if (r == 0) r = -1;
- int y2 = y;
- while (start)
- {
- y2 += r;
- if (y2 < 0 || y2 > 9) break;
- if (field1[x, y2] == 3) continue;
- if (field1[x, y2] == 2) break;
- else
- {
- if (field1[x, y2] == 0)
- {
- Shoot(1, x, y2, 2);
- step = true;
- goto Break1;
- }
- else if (field1[x, y2] == 1)
- {
- if (Dfs(x, y2, x, y2, 1))
- {
- Shoot(1, x, y2, 3);
- goto Break1;
- }
- else
- {
- Shoot(1, x, y2, 4);
- Kill(x, y2, 1);
- trigger = false;
- R = 0;
- n1--;
- goto Break1;
- }
- }
- }
- }
- y2 = y;
- while (start)
- {
- y2 -= r;
- if (y2 < 0 || y2 > 9) break;
- if (field1[x, y2] == 3) continue;
- if (field1[x, y2] == 2) break;
- else
- {
- if (field1[x, y2] == 0)
- {
- Shoot(1, x, y2, 2);
- step = true;
- goto Break1;
- }
- else if (field1[x, y2] == 1)
- {
- if (Dfs(x, y2, x, y2, 1))
- {
- Shoot(1, x, y2, 3);
- goto Break1;
- }
- else
- {
- Shoot(1, x, y2, 4);
- Kill(x, y2, 1);
- trigger = false;
- R = 0;
- n1--;
- goto Break1;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- ////////////////////
- x = rnd.Next(0, 10);
- y = rnd.Next(0, 10);
- if(field1[x, y] != 0 && field1[x, y] != 1)
- {
- continue;
- }
- else if(field1[x, y] == 0)
- {
- Shoot(1, x, y, 2);
- step = true;
- }
- else if(field1[x, y] == 1)
- {
- if(Dfs(x, y, x, y, 1))
- {
- Shoot(1, x, y, 3);
- trigger = true;
- R = 0;
- }
- else
- {
- Shoot(1, x, y, 4);
- Kill(x, y, 1);
- n1--;
- }
- }
- Break1:
- Game_cheak();
- }
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- button203.Visible = true;
- Restart();
- button202.FlatAppearance.BorderSize = 0;
- button202.FlatStyle = FlatStyle.Flat;
- button202.BackColor = Color.LightGray;
- button203.FlatAppearance.BorderSize = 0;
- button203.FlatStyle = FlatStyle.Flat;
- button203.BackColor = Color.LightGray;
- button201.BackColor = Color.LightGray;
- button201.FlatAppearance.BorderSize = 0;
- button201.FlatStyle = FlatStyle.Flat;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement