public void StrikingFuction(Button[,] but, Color[,] col, int[,] ships, int x, int y) { but[x, y].BackColor = Color.LightSalmon; int a = ships[x, y] / 1000; int b = ships[x, y] / 100 % 10; int c = ships[x, y] / 10 % 10; int d = ships[x, y] % 10; if (d == 1) { for (int i = a; i < a + c; i++) { if (but[i, y].BackColor == Color.Lavender) { sub[1].Text = "Human striked"; break; } if (i == a + c - 1) { IfKilledToRight(but, col, a, y, c); sub[1].Text = "Human killed"; humankilled++; if (humankilled == 10) { sub[1].Text = "Human wins"; sub[0].Text = "CPU lose"; sub[0].BackColor = Color.LightSalmon; sub[1].BackColor = Color.LightGreen; playmode = PLayMode.gameover; } } } } else { for (int i = b; i < b + c; i++) { if (but[x, i].BackColor == Color.Lavender) { sub[1].Text = "Human striked"; break; } if (i == b + c - 1) { IfKilledToDown(but, col, b, x, c); sub[1].Text = "Human killed"; humankilled++; if (humankilled == 10) { sub[1].Text = "Human wins"; sub[0].Text = "CPU lose"; sub[0].BackColor = Color.LightSalmon; sub[1].BackColor = Color.LightGreen; CPUCanMove = false; } } } } }
public void ShipType_Click(object sender, EventArgs e) { Button b = sender as Button; for (int i = 0; i < 6; i++) { if (b == shipchoice[i]) { switch (i) { case 0: shiptype = ShipType.x4; break; case 1: shiptype = ShipType.x3; break; case 2: shiptype = ShipType.x2; break; case 3: shiptype = ShipType.x1; break; case 4: if (direction == Direction.downward) { direction = Direction.rightward; b.Text = "rightward"; } else { direction = Direction.downward; b.Text = "downward"; } break; case 5: playmode = PLayMode.game; HideShipTypes(); for (int a = 0; a < 10; a++) { for (int c = 0; c < 10; c++) { humanfield[a, c].BackColor = Color.Lavender; } } CreateSubtitres(); break; } } } }
public void NewGame() { humanfield = new Button[10, 10]; cpufield = new Button[10, 10]; humanfieldcolor = new Color[10, 10]; cpufieldcolor = new Color[10, 10]; humanfieldships = new int[10, 10]; cpufieldships = new int[10, 10]; humanfieldcanset = new int[10, 10]; cpufieldcanset = new bool[10, 10]; shipchoice = new Button[6]; shipnumber = new Label[4]; sub = new Label[2]; p1 = new Point(); p2 = new Point(); listofpoints = new List <Point>(); allpoints = new List <Button>(); HumanCanSet = true; CPUCanMove = false; HumanMove = true; cpukilled = 0; humankilled = 0; HowMuchStriked = 0; playmode = PLayMode.position; shiptype = new ShipType(); direction = Direction.rightward; CreateHumanfield(); CreateCPUfield(); ShowShipTypes(); ComputerShipSetting(); }
public void CPUStrikes() { timer1.Enabled = false; listofpoints.Clear(); int q = 0; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (humanfield[i, j].BackColor == Color.LightSalmon) { q++; if (q == 1) { p1 = new Point(i, j); } else if (q > 1) { p2 = new Point(i, j); } } } } if (HowMuchStriked == 0) { Random k = new Random(); int r = k.Next(0, allpoints.Count) % allpoints.Count; int x = (allpoints[r].Location.X - 30) / 30; int y = (allpoints[r].Location.Y - 30) / 30; allpoints.Remove(humanfield[x, y]); if (humanfieldcolor[x, y] == Color.Peru) { if (humanfieldships[x, y] / 10 % 10 == 1) { humanfield[x, y].BackColor = humanfieldcolor[x, y]; IfKilledToRight(humanfield, humanfieldcolor, x, y, 1); cpukilled++; sub[0].Text = "CPU killed"; } else { humanfield[x, y].BackColor = Color.LightSalmon; HowMuchStriked++; sub[0].Text = "CPU striked"; } CPUCanMove = true; } else { humanfield[x, y].BackColor = Color.LightSeaGreen; sub[0].Text = "CPU missed"; sub[0].BackColor = Color.White; sub[1].BackColor = Color.LightGreen; CPUCanMove = false; HumanMove = true; } } else if (HowMuchStriked == 1) { if (p1.X - 1 >= 0) { if (humanfield[p1.X - 1, p1.Y].BackColor == Color.Lavender) { listofpoints.Add(new Point(p1.X - 1, p1.Y)); } } if (p1.X + 1 <= 9) { if (humanfield[p1.X + 1, p1.Y].BackColor == Color.Lavender) { listofpoints.Add(new Point(p1.X + 1, p1.Y)); } } if (p1.Y - 1 >= 0) { if (humanfield[p1.X, p1.Y - 1].BackColor == Color.Lavender) { listofpoints.Add(new Point(p1.X, p1.Y - 1)); } } if (p1.Y + 1 <= 9) { if (humanfield[p1.X, p1.Y + 1].BackColor == Color.Lavender) { listofpoints.Add(new Point(p1.X, p1.Y + 1)); } } Random k = new Random(); int ran = k.Next(0, listofpoints.Count) % listofpoints.Count; allpoints.Remove(humanfield[listofpoints[ran].X, listofpoints[ran].Y]); if (humanfieldcolor[listofpoints[ran].X, listofpoints[ran].Y] == Color.Peru) { if (humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 10 % 10 == 2) { int a = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 1000; int b = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 100 % 10; int c = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 10 % 10; int d = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] % 10; int x = listofpoints[ran].X; int y = listofpoints[ran].Y; if (d == 1) { IfKilledToRight(humanfield, humanfieldcolor, a, y, c); } if (d == 2) { IfKilledToDown(humanfield, humanfieldcolor, b, x, c); } HowMuchStriked = 0; cpukilled++; sub[0].Text = "CPU killed"; } else { humanfield[listofpoints[ran].X, listofpoints[ran].Y].BackColor = Color.LightSalmon; HowMuchStriked++; sub[0].Text = "CPU striked"; } CPUCanMove = true; } else { humanfield[listofpoints[ran].X, listofpoints[ran].Y].BackColor = Color.LightSeaGreen; sub[0].Text = "CPU missed"; sub[0].BackColor = Color.White; sub[1].BackColor = Color.LightGreen; CPUCanMove = false; HumanMove = true; } } else if (HowMuchStriked > 1) { if (p1.Y == p2.Y) { if (p1.X - 1 >= 0) { if (humanfield[p1.X - 1, p1.Y].BackColor == Color.Lavender) { listofpoints.Add(new Point(p1.X - 1, p1.Y)); } } if (p2.X + 1 <= 9) { if (humanfield[p2.X + 1, p1.Y].BackColor == Color.Lavender) { listofpoints.Add(new Point(p2.X + 1, p1.Y)); } } Random k = new Random(); int ran = k.Next(0, listofpoints.Count) % listofpoints.Count; allpoints.Remove(humanfield[listofpoints[ran].X, listofpoints[ran].Y]); if (humanfieldcolor[listofpoints[ran].X, listofpoints[ran].Y] == Color.Peru) { if (humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 10 % 10 == HowMuchStriked + 1) { int a = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 1000; int b = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 100 % 10; int c = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 10 % 10; int d = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] % 10; int x = listofpoints[ran].X; int y = listofpoints[ran].Y; IfKilledToRight(humanfield, humanfieldcolor, a, y, c); HowMuchStriked = 0; cpukilled++; sub[0].Text = "CPU killed"; } else { humanfield[listofpoints[ran].X, listofpoints[ran].Y].BackColor = Color.LightSalmon; HowMuchStriked++; sub[0].Text = "CPU striked"; } CPUCanMove = true; } else { humanfield[listofpoints[ran].X, listofpoints[ran].Y].BackColor = Color.LightSeaGreen; sub[0].Text = "CPU missed"; sub[0].BackColor = Color.White; sub[1].BackColor = Color.LightGreen; CPUCanMove = false; HumanMove = true; } } else { if (p1.Y - 1 >= 0) { if (humanfield[p1.X, p1.Y - 1].BackColor == Color.Lavender) { listofpoints.Add(new Point(p1.X, p1.Y - 1)); } } if (p2.Y + 1 <= 9) { if (humanfield[p1.X, p2.Y + 1].BackColor == Color.Lavender) { listofpoints.Add(new Point(p1.X, p2.Y + 1)); } } Random k = new Random(); int ran = k.Next(0, listofpoints.Count) % listofpoints.Count; allpoints.Remove(humanfield[listofpoints[ran].X, listofpoints[ran].Y]); if (humanfieldcolor[listofpoints[ran].X, listofpoints[ran].Y] == Color.Peru) { if (humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 10 % 10 == HowMuchStriked + 1) { int a = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 1000; int b = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 100 % 10; int c = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] / 10 % 10; int d = humanfieldships[listofpoints[ran].X, listofpoints[ran].Y] % 10; int x = listofpoints[ran].X; int y = listofpoints[ran].Y; IfKilledToDown(humanfield, humanfieldcolor, b, x, c); HowMuchStriked = 0; cpukilled++; sub[0].Text = "CPU killed"; } else { humanfield[listofpoints[ran].X, listofpoints[ran].Y].BackColor = Color.LightSalmon; HowMuchStriked++; sub[0].Text = "CPU striked"; } CPUCanMove = true; } else { humanfield[listofpoints[ran].X, listofpoints[ran].Y].BackColor = Color.LightSeaGreen; sub[0].Text = "CPU missed"; sub[0].BackColor = Color.White; sub[1].BackColor = Color.LightGreen; CPUCanMove = false; HumanMove = true; } } } if (cpukilled == 10) { sub[0].Text = "CPU wins"; sub[1].Text = Form2.passingText + " lose"; sub[0].BackColor = Color.LightGreen; sub[1].BackColor = Color.LightSalmon; playmode = PLayMode.gameover; } if (CPUCanMove == true) { timer1.Enabled = true; } }