Need Help with AI Movement Mechanics (Urgent)

Also, Can you check if the code I implemented is correct or not also i have a question while using for each loop loop body i am getting the max index space in 10 thousands while if i use completed i get the value correct. Why is that so i cant find the reason.