# How to develop an algorithm for brute-forcing / backtracking?

As a beginner programmer, I don't know how to conceptually think about brute-forcing. My mind can't really fathom how to write code that will try every possibility.

I have a problem that I want to solve. Here is a code snippet (there are other functions, but no reason to include them here, they just do background work):

``````main():
step_segment = []           # contains current segment
STATS = [250,0,0,0,13,0]        # contains 1 step (current step stats)
# STATS = [step_id, danger, danger_limit, rnd, b_loop, enc]

# Temporary Interface
choice = None
while choice != "0":
print \
"\n1 - Benchmark Run"
choice = raw_input("Choice: ")
if choice == "0":
print "\nGoodbye."

elif choice == "1":
while (len(step_segment)) < 128:
step_segment, STATS = take_step(step_segment, STATS)
if STATS[5] == "B":
"Dont know what to do here. Try every possibility!?"
``````

The goal: I want to produce a list of every possible 'route' through a segment, along with how long it takes.

Description:

1. I will take a step in the route (There's only one direction: forward). Every time I take a step, my stats are updated. This takes a step and updates the stats: `step_segment, STATS = take_step(step_segment, STATS)`

2. A list of steps taken, along with the stats, are kept in `step_segment`. This is so I can 'undo' an arbitrary amount of steps, if I want. To undo a step call the function: `step_segment, STATS = undo_step(step_segment, STATS)`

3. I can see how long my current route has taken by doing: `time = frames(step_segment)`.

4. At some point, I will get into a Battle. I get into a Battle when `STATS[5] == "B"`

5. When there is a battle, I simply have two choices: i. Fight the battle, or, ii. Run away.

6. If I want to Fight, I do: `step_segment = do_fight(step_segment, STATS)`. This also records that I chose to fight, along with the stats, in `step_segment`. (So I can undo it, if i want).

7. If I want to Run Away, I do: `step_segment = run_away(step_segment,STATS)`.

Can someone advise me, how I can code a brute forcer for this problem?

Specifically, I would like to know how I can tell the computer to try every possibility; I simply cannot think how to... >.<'

I want to see every possible combination of Run & Fight (the only two choices, when I reach a battle).

There are only around 200 possibilities. I only need to take 128 steps, so there are a finite amount of possibilities, hence: `while (len(step_segment)) < 128`.

I just don't know how to accomplish such a thing in Python. My whole reason for learning how to program is to solve this problem..

I think I can explain the solution in English, as I have done above, but I don't know how to code it. I would be very appreciative if someone could advise me on this!!

Thank you.