Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:12:12+00:00 2026-05-13T13:12:12+00:00

Okay so basically I’m trying to do a depth-first search for a mini-peg solitaire

  • 0

Okay so basically I’m trying to do a depth-first search for a mini-peg solitaire game. For those unfamiliar with the game it’s pretty simple.

There’s a board with 10 holes and 9 pegs, a peg is represented by a 1 and an empty spot by a 0. You can move a peg backwards or forwards two holes at a time (but you can only move to an empty hole), and if you jump over another peg in the process you take it out and it becomes a hole.

So here’s what a game looks like:

[1, 1, 1, 1, 1, 0, 1, 1, 1, 1]
[1, 1, 1, 0, 0, 1, 1, 1, 1, 1]
[1, 0, 0, 1, 0, 1, 1, 1, 1, 1]
[1, 0, 0, 1, 1, 0, 0, 1, 1, 1]
[1, 0, 0, 0, 0, 1, 0, 1, 1, 1]
[1, 0, 0, 0, 0, 1, 1, 0, 0, 1]
[1, 0, 0, 0, 0, 0, 0, 1, 0, 1] #etc until only 1 peg left

So, I have a generator function here that finds all legal moves for a certain “node”, or a game board:

def succ(self, node):
    size = len(node)

    # find all legal moves going forward
    for pos in range(0, size-1):
        new_node = list(node)
        if ((node[pos] == 1) and (pos < (size - 2)) and (node[pos+2] == 0)):
            new_node[pos] = 0  # we're moving now
            new_node[pos+2] = 1 # this is where we're moving the peg to
            new_node[pos+1] = 0  # take out the peg here if there was one
            yield new_node

    # find all legal moves going backwards
    for pos in range(0, size-1):
        new_node = list(node)
        if ((node[pos] == 1) and (pos > 1) and (node[pos-2] == 0)):
            new_node[pos] = 0  # we're moving now
            new_node[pos-2] = 1 # this is where we're moving the peg
            new_node[pos-1] = 0  # take out the peg here if there was one
            yield new_node

Now if you know depth-first search, this seems like a GREAT generator to use when solving this puzzle. Or is it? (I think it is, maybe you can help come up with a more Pythonic way?)

Well, my recursive puzzle solver function that will use the generator isn’t working, maybe you can help me out?

def goal(self, node):
    pegs = 0

    for pos in node:
        if pos == 1:
            pegs += 1

    return (pegs == 1) # returns True if there is only 1 peg

def solve_board(dfs_obj, node):
    if goal(node):  # only 1 peg!
        print node
        return node

    for new_node in succ(node):
        print new_node
        return solve_board(new_node)

if __name__ == "__main__":
    solve_board([1, 1, 1, 1, 1, 0, 1, 1, 1, 1])

So basically I think my succ() function is doing the right thing (maybe it’s not?), but my solve_board() recursion might be funky, because the board doesn’t solve.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:12:12+00:00Added an answer on May 13, 2026 at 1:12 pm

    Since you are allowed to jump over empty holes, you’ll have to keep track of any nodes you have already visited. Otherwise you will have an infinite loop.

    You also need to not shortcircuit the for loop unless you have found a goal

    tested_nodes=set()
    def solve_board(dfs_obj, node):
        if goal(node):  # only 1 peg!
            print node
            return node
    
        for new_node in succ(node):
            if tuple(new_node) not in tested_nodes:
                tested_nodes.add(tuple(new_node))
                print new_node
                result = solve_board(new_node)
                if result:  # True if it's a goal, None otherwise
                    return result
    

    You have the range wrong in your succ function too, you should not subtrace 1 from the size for the range. You could also rewrite it like this to remove one of the conditions from the if

    def succ(self, node):
        size = len(node)
    
        # find all legal moves going forward
        for pos in range(size-2):
            new_node = list(node)
            if ((node[pos] == 1) and (node[pos+2] == 0)):
                new_node[pos] = 0  # we're moving now
                new_node[pos+2] = 1 # this is where we're moving the peg to
                new_node[pos+1] = 0  # take out the peg here if there was one
                yield new_node
    
        # find all legal moves going backwards
        for pos in range(1,size):
            new_node = list(node)
            if ((node[pos] == 1) and (node[pos-2] == 0)):
                new_node[pos] = 0  # we're moving now
                new_node[pos-2] = 1 # this is where we're moving the peg
                new_node[pos-1] = 0  # take out the peg here if there was one
                yield new_node
    

    Another way to write the succ funtion would be

    def succ(self, node):
        for i in range(len(node)-2):
            j=i+3
            if node[i:j]==[1,1,0]:
                yield node[:i]+[0,0,1]+node[j:]
            if node[i:j]==[0,1,1]:
                yield node[:i]+[1,0,0]+node[j:]
            if node[i:j]==[1,0,0]:
                yield node[:i]+[0,0,1]+node[j:]
            if node[i:j]==[0,0,1]:
                yield node[:i]+[1,0,0]+node[j:]
    

    This tunes the depth first slightly by preferring moves that remove a peg

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 293k
  • Answers 293k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As written, the class/methods are garbage. However, I can see… May 13, 2026 at 6:31 pm
  • Editorial Team
    Editorial Team added an answer Facebook include one in their PHP Library. Hopefully they won't… May 13, 2026 at 6:31 pm
  • Editorial Team
    Editorial Team added an answer If it's not set, fetching it will yield an empty… May 13, 2026 at 6:31 pm

Related Questions

Okay so basically : i have this simple example: main.cpp using namespace VHGO::Resource; std::list<BaseTable*>
Okay, so basically I want to be able to retrieve keyboard text. Like entering
okay, so here is what im doing: class Connection { public int SERVERID; private
Okay so i have a packed a proprietary binary format. That is basically a
Okay, so I am basically making a script to pass post data using cURL.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.