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.

There are more “possibilities” than you might think, I will explain to you why:

As I understand your explanation of your scenario, you have effectively a tree structure of steps.
You start at the “root” (your first step) and then go up.

At every point where you encounter a battle and thus get to choose, you create a branch of possibilites:
Your path splits into a “do fight” path and a “don’t fight” path, on which you will continue your journey.

These branches will branch again at every battle point. When you reach your “end”, you will backtrack to the previous decision and take the opposite action.

From a programmers perspective, this would be an ideal situation for a recursive algorithm, as you know you have a finite amount of steps.

The keywords to look for are “recursive tree traversal”. I can not help you with specific code, as I have no experience with python.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật