private void Panel_Click(object sender, MouseEventArgs me) { if (game == false) { if (me.Button == MouseButtons.Left) { if (Battleships.CheckPlace(x, y, horizontal, vertical, fieldArray1, ref place) == true) { Down.Play(); Battleships.SetShip(ref fieldArray1, x, y, true, horizontal, vertical); Draw(fieldArray1, ref Field1); if (Battleships.NextShip(ref horizontal, ref vertical, ref shipAmmount) == false) { Battleships.ShootingMode(ref fieldArray1); Battleships.ShootingMode(ref fieldArray2); DrawGame(fieldArray2, ref Field2); Draw(fieldArray1, ref Field1); game = true; } } } else { Clear(ref Field1); Draw(fieldArray1, ref Field1); Battleships.Rotate(ref horizontal, ref vertical); if (x + horizontal > 9) { x = 6; } if (y + vertical > 9) { y = 6; } DrawShip(horizontal, vertical, fieldArray1, ref Field1, x, y); } } else { if (turn == true) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (Field2[i, j] == sender) { Shoot(i, j, ref fieldArray2, ref Field2); } } } } } }
private void DrawShip(int horizontal, int vertical, int[,] array, ref Panel[,] panel, int x, int y) { for (int i = x; i < x + horizontal; i++) { for (int j = y; j < y + vertical; j++) { if (Battleships.CheckPlace(x, y, horizontal, vertical, fieldArray1, ref place) == true) { panel[i, j].BackgroundImage = Properties.Resources.NewGreen; } else { panel[i, j].BackgroundImage = Properties.Resources.NewRed; } } } }
private void Shoot(int i, int j, ref int[,] fieldArray, ref Panel[,] Field) { switch (fieldArray[i + 1, j + 1]) { case (int)Battleships.State.Alive: { if (Battleships.CheckShipState(i, j, fieldArray) == true) { Hit.Play(); fieldArray[i + 1, j + 1] = (int)Battleships.State.Damaged; } else { Kill.Play(); Battleships.KillShip(i, j, fieldArray); if (fieldArray == fieldArray1) { AddScore(true); } else { AddScore(false); } } } break; case (int)Battleships.State.Empty: { Miss.Play(); fieldArray[i + 1, j + 1] = (int)Battleships.State.Miss; turn = !turn; } break; } Draw(fieldArray1, ref Field1); DrawGame(fieldArray2, ref Field2); if (turn == false) { timer1.Enabled = true; } }
private void FormBattleships_Load(object sender, EventArgs e) { brokenChalk = new Font(fonts.Families[0], 18.0F); label1.Font = brokenChalk; CreatePanel(18, Field1); CreatePanel(592, Field2); NewGame(); Battleships.SetPCShips(ref fieldArray2); Battleships.CreateShootArrays(ref shoots1, ref shoots2, ref shoots3); /*string s=""; * for(int i =0;i<50;i++) * { * for(int j=0;j<2;j++) * { * s += Convert.ToString(shoots3[j, i]); * s += " "; * } * s += "\r\n"; * } * MessageBox.Show(s);*/ }
private void NewGame() { Battleships.NextShip(ref horizontal, ref vertical, ref shipAmmount); Draw(fieldArray1, ref Field1); DrawShip(horizontal, vertical, fieldArray1, ref Field1, x, y); }
private void PCShoot(ref int[,] fieldArray, ref int[,] shoots1, ref int[,] shoots2, ref int[,] shoots3) { int x, y; int [,] count; int lines; int rand; Battleships.Direction direction1 = new Battleships.Direction(); Battleships.Direction direction2 = new Battleships.Direction(); if (Battleships.SearchDamaged(fieldArray1, out count) == true) { lines = Battleships.CountArrayLines(count); if (lines > 1) { rand = r.Next(lines); Battleships.FindDamagedDirection(count[0, rand], count[1, rand], fieldArray1, ref direction1, ref direction2); while (ShootRandomDirection(count[0, rand], count[1, rand], direction1, direction2) == false) { rand = r.Next(lines); } ; } else { while (ShootRandomDirection(count[0, 0], count[1, 0], Battleships.Direction.Null, Battleships.Direction.Null) == false) { ; } } } else { if (Battleships.CountArrayLines(shoots1) == 0) { if (Battleships.CountArrayLines(shoots2) == 0) { if (Battleships.CountArrayLines(shoots3) == 0) { } else { Battleships.ChooseRandomCoordinates(ref shoots3, out x, out y); while (fieldArray1[x + 1, y + 1] != (int)Battleships.State.Alive && fieldArray1[x + 1, y + 1] != (int)Battleships.State.Empty) { if (Battleships.CountArrayLines(shoots3) == 0) { break; } Battleships.ChooseRandomCoordinates(ref shoots3, out x, out y); } Shoot(x, y, ref fieldArray1, ref Field1); } } else { Battleships.ChooseRandomCoordinates(ref shoots2, out x, out y); while (fieldArray1[x + 1, y + 1] != (int)Battleships.State.Alive && fieldArray1[x + 1, y + 1] != (int)Battleships.State.Empty) { if (Battleships.CountArrayLines(shoots2) == 0) { break; } Battleships.ChooseRandomCoordinates(ref shoots2, out x, out y); } Shoot(x, y, ref fieldArray1, ref Field1); } } else { Battleships.ChooseRandomCoordinates(ref shoots1, out x, out y); while (fieldArray1[x + 1, y + 1] != (int)Battleships.State.Alive && fieldArray1[x + 1, y + 1] != (int)Battleships.State.Empty) { if (Battleships.CountArrayLines(shoots1) == 0) { break; } Battleships.ChooseRandomCoordinates(ref shoots1, out x, out y); } Shoot(x, y, ref fieldArray1, ref Field1); } } }