Hello,
I’m still trying to make a Checkers game and this time I’m blocked with a problem of infinite loop. The part that creates the infinite loop is the ia for the computer turn, it’s called once the player has moved on of his pieces.
Here is all my code (I commented it the best way i can to help you) :
public void computerTurn () {
bool continueSearching = true;
int randomZ = Random.Range (6, 9);
int randomX = Random.Range (0, 9);
Tile[] tiles = FindObjectsOfType(typeof(Tile)) as Tile[];
while(continueSearching == true) {
// Browse all the tiles
foreach (Tile a_tile in tiles) {
// If the tile is equals to the new coordinates
if(a_tile.coordinates == new Vector2(randomX, randomZ)){
// If its piece isn't null and its piece color is black
if(a_tile.piece != default(Piece) && a_tile.piece.color == new Color(0,0,0)) {
int diagonal1ID = a_tile.id - 11;
int diagonal2ID = a_tile.id + 9;
// If the diagonals are in the board
if(diagonal1ID > 0 && diagonal1ID <= 100 || diagonal2ID > 0 && diagonal2ID <= 100) {
// Browse a second time all the tiles
foreach(Tile a_second_tile in tiles) {
// If its id is equals to the first diagonal
if(a_second_tile.id == diagonal1ID){
// If it hasn't any piece and it hasn't any children
if(a_second_tile.piece == default(Piece) && a_second_tile.transform.childCount == 0){
// Then move the piece to the first diagonal
Piece randomPiece = a_tile.piece;
randomPiece.transform.position = new Vector3(a_tile.transform.position.x - 1, 0.6f, a_tile.transform.position.z - 1);
foreach(Tile a_fourth_tile in tiles) {
if(a_fourth_tile.transform.position == new Vector3(randomPiece.transform.position.x, 0, randomPiece.transform.position.z)) {
a_fourth_tile.piece = randomPiece;
randomPiece.transform.parent = a_fourth_tile.transform;
}
}
a_tile.piece = default(Piece);
continueSearching = false;
// If the first diagonal isn't empty
}else{
// Then browse the tiles a third time
foreach(Tile a_third_tile in tiles) {
// If the tile id is equals to the second diagonal
if(a_third_tile.id == diagonal2ID){
// If the second diagonal hasn't any piece or children
if(a_third_tile.piece == default(Piece) && a_third_tile.transform.childCount == 0) {
// Then move the piece to the second diagonal
Piece randomPiece = a_tile.piece;
randomPiece.transform.position = new Vector3(a_tile.transform.position.x + 1, 0.6f, a_tile.transform.position.z - 1);
foreach(Tile a_fourth_tile in tiles) {
if(a_fourth_tile.transform.position == new Vector3(randomPiece.transform.position.x, 0, randomPiece.transform.position.z)) {
a_fourth_tile.piece = randomPiece;
randomPiece.transform.parent = a_fourth_tile.transform;
}
}
a_tile.piece = default(Piece);
continueSearching = false;
}else{
// If the two diagonals have got a piece, find a new tile
randomZ = Random.Range (6, 10);
randomX = Random.Range (0, 10);
continueSearching = true;
continue;
}
}
}
}
}
}
}
}else{
// If the tile hasn't any piece, find a new tile
randomZ = Random.Range (6, 9);
randomX = Random.Range (0, 9);
continueSearching = true;
continue;
}
}
}
}
}
Sometimes it works but sometimes, unity crashes and I need to restart it.
If you find where’s the problem, please tell me
Thank you.