I am building an Armadillo game using C # .NET. He should use a fairly simple scoring mechanism. No ship crashes, if either the player or the computer scores 17 hits, they win. If you strike, you can take another turn. AI attacks randomly until it strikes, after which it attacks the tile in each direction until it finds a trend, and then continues to attack in a straight line until it finds a dead end (either unallocated space or edge If there were spaces that didn't were deleted in the opposite direction that the computer attacked, it then attacks these spaces, it doesn’t target the spaces that it has already reached, or following the patterns that have already been followed.
Here is my AI so far.
int shipCounter = 0, trend = 0; static Random rnd = new Random(); bool gameOver = false, playerTurn = false; int[] score = { 0, 0 }; struct gameData { public bool occupied, hit, marked; } gameData[,,] data; public void computerMove() { Point target = seekTarget(); try { if (data[1, target.X, target.Y].hit) computerMove(); else { data[1, target.X, target.Y].hit = true; if (data[1, target.X, target.Y].occupied) { attacking = true; score[0]++; computerMove(); } } playerTurn = true; } catch (IndexOutOfRangeException) { computerMove(); } } public Point seekTarget() { Point origin = new Point(-1, -1); //find a point that been hit. int x = 0, y = 0; while (x < gridSize && y < gridSize) { if (data[1, x, y].hit && data[1, x, y].occupied && !data[1, x, y].marked) { origin = new Point(x, y); break; } x++; if (x == gridSize && y != gridSize) { x = 0; y++; } } return findTargets(origin); } public Point findTargets(Point origin) { Point[] lim = { origin, origin, origin, origin }; Point[] possibleTargets = { origin, origin, origin, origin }; //Find the edges. while (lim[0].X >= -1 && ((!data[1, lim[0].X, lim[0].Y].hit && !data[1, lim[0].X, lim[0].Y].occupied) || (data[1, lim[0].X, lim[0].Y].hit && data[1, lim[0].X, lim[0].Y].occupied))) { lim[0].X--; if (lim[0].X == -1) break; } while (lim[1].Y >= -1 && ((!data[1, lim[0].X, lim[0].Y].hit && !data[1, lim[0].X, lim[0].Y].occupied) || (data[1, lim[0].X, lim[0].Y].hit && data[1, lim[0].X, lim[0].Y].occupied))) { lim[1].Y--; if (lim[1].Y == -1) break; } while (lim[2].X <= gridSize && ((!data[1, lim[0].X, lim[0].Y].hit && !data[1, lim[0].X, lim[0].Y].occupied) || (data[1, lim[0].X, lim[0].Y].hit && data[1, lim[0].X, lim[0].Y].occupied))) { lim[2].X++; if (lim[2].X == gridSize) break; } while (lim[3].Y <= gridSize && ((!data[1, lim[0].X, lim[0].Y].hit && !data[1, lim[0].X, lim[0].Y].occupied) || (data[1, lim[0].X, lim[0].Y].hit && data[1, lim[0].X, lim[0].Y].occupied))) { lim[3].Y++; if (lim[3].Y == gridSize) break; } //Cell targeting AI } return new Point(rnd.Next(10), rnd.Next(10)); }
This became extremely messy due to my inability to figure out what was going wrong. If I quote the findTargets function and just accidentally attack the computer, it works fine. Computer and player trade is rotated, and the computer enters the register.
When findTargets turned findTargets player can make one attack, and the computer never takes its turn. Then it does not return to the player’s turn, even if the player’s interceptor is still visible. If anyone could help, he would greatly appreciate it. Sorry for not including the Paint or mouseDown methods, they have exceeded character limits.
User interface without findTargets (rotation of players and computers). 
UI with findTargets (the computer cannot rotate, the player takes only one revolution). 
Thanks in advance for your help.
EDIT: I highlighted the problem, it seems that it cannot break out of while loops in findTargets . Even if I solve the problem by stopping it from looping when origin is (-1, -1) , it gets into the loop on the first hit.
EDIT 2: It enters the first cycle and is infinitely cyclical. For some reason, it does not increase lim[0].X at all. When I insert a message in a loop to display some data, it is displayed twice and then does not appear again, although it is still cyclical. Does anyone know why this is?