Home > database >  How can I make recursion switch between loops?
How can I make recursion switch between loops?

Time:01-20

I'm trying to make a Connect 4 AI which involves 7 loops nested inside each other which iterate through every possible counter position collectively. The loops all do the exact same thing apart from the last one. The loops involve breaking so the iteration can go back a step. The only issue with recursion is that I haven't figured out how to make it go back a loop, so for now that is out of the question. (I'm looking for a faster program and less lines of code.) A basic recreation of my problem is below:

for loop1 in range(7):
    breakChance = random.randint(1,5)
    if breakChance > 3:
        break

    for loop2 in range(7):
        breakChance = random.randint(1,5)
        if breakChance > 3:
            break
        
        for loop3 in range(7):...

I have tried putting the main parts of the loop in a function and sending messages to break the loop. This doesn't decrease the speed, but it does reduce the lines. I have tried using recursion, but I can't get it to do what I like: it never breaks when I want it to, and I feel it's a sacrifice of readability, to me at least.

CodePudding user response:

Is this what you want?

def my_loop(i):                                                                                                                                              
    if i==7:                                                                                                                                                 
        return                                                                                                                                               
    else:                                                                                                                                                    
        my_loop(i 1)                                                                                                                                         
                                                                                                                                                             
    for j in range(7):                                                                                                                                       
        print(f"index {j} in loop {i}")                                                                                                                      
                                                                                                                                                             

# Run it!                                                                                                                                                             
my_loop(0)

CodePudding user response:

If you want to simulate walking a 2d board, you can do so via a pair of for loops. You might also check out itertools to do it in one loop. When you find a valid counter move you exit the method with it.

import random

def get_counter_position():
    for x in range(7):
        for y in range(7):
            if random.randint(1,5) > 4:
                return (x, y)
    raise Exception("No Counter Move")

print(f"Found counter move at: { get_counter_position() } ")
print(f"Found counter move at: { get_counter_position() } ")
  • Related