private bool ShootRandomDirection(int x, int y, Battleships.Direction direction1, Battleships.Direction direction2) { if (direction1 == Battleships.Direction.Null) { switch (r.Next(4)) { case (int)Battleships.Direction.Left: if (ShootDirection(x, y, Battleships.Direction.Left) == true) { return(true); } else { return(false); } case (int)Battleships.Direction.Up: if (ShootDirection(x, y, Battleships.Direction.Up) == true) { return(true); } else { return(false); } case (int)Battleships.Direction.Right: if (ShootDirection(x, y, Battleships.Direction.Right) == true) { return(true); } else { return(false); } case (int)Battleships.Direction.Down: if (ShootDirection(x, y, Battleships.Direction.Down) == true) { return(true); } else { return(false); } default: return(false); } } else { if (r.Next(2) == 0) { if (ShootDirection(x, y, direction1) == true) { return(true); } else { return(false); } } else { if (ShootDirection(x, y, direction2) == true) { return(true); } else { return(false); } } } }
private bool ShootDirection(int x, int y, Battleships.Direction direction) { switch (direction) { case Battleships.Direction.Left: if (fieldArray1[x, y + 1] == (int)Battleships.State.Empty || fieldArray1[x, y + 1] == (int)Battleships.State.Alive) { if (x > 0) { Shoot(x - 1, y, ref fieldArray1, ref Field1); return(true); } else { return(false); } } else { return(false); } case Battleships.Direction.Up: if (fieldArray1[x + 1, y] == (int)Battleships.State.Empty || fieldArray1[x + 1, y] == (int)Battleships.State.Alive) { if (y > 0) { Shoot(x, y - 1, ref fieldArray1, ref Field1); return(true); } else { return(false); } } else { return(false); } case Battleships.Direction.Right: if (fieldArray1[x + 2, y + 1] == (int)Battleships.State.Empty || fieldArray1[x + 2, y + 1] == (int)Battleships.State.Alive) { if (x < 10) { Shoot(x + 1, y, ref fieldArray1, ref Field1); return(true); } else { return(false); } } else { return(false); } case Battleships.Direction.Down: if (fieldArray1[x + 1, y + 2] == (int)Battleships.State.Empty || fieldArray1[x + 1, y + 2] == (int)Battleships.State.Alive) { if (y < 10) { Shoot(x, y + 1, ref fieldArray1, ref Field1); return(true); } else { return(false); } } else { return(false); } default: return(false); } }
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); } } }