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 7196529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:47:41+00:00 2026-05-28T20:47:41+00:00

I have this project that I’m working on for a class, and I’m not

  • 0

I have this project that I’m working on for a class, and I’m not looking for the answer, but maybe just a few tips since I don’t feel like I’m using true recursion. The problem involves solving the game of Hi Q or “peg solitaire” where you want the end result to be one peg remaining (it’s played with a triangular board and one space is open at the start.)

I’ve represented the board with a simple array, each index being a spot and having a value of 1 if it has a peg, and 0 if it doesn’t and also the set of valid moves with a 2 dimensional array that is 36, 3 (each move set contains 3 numbers; the peg you’re moving, the peg it hops over, and the destination index.)

So my problem is that in my recursive function, I’m using a lot of iteration to determine things like which space is open (or has a value of 0) and which move to use based on which space is open by looping through the arrays. Secondly, I don’t understand how you would then backtrack with recursion, in the event that an incorrect move was made and you wanted to take that move back in order to choose a different one.

This is what I have so far; the “a” array represents the board, the “b” array represents the moves, and the “c” array was my idea of a reminder as to which moves I used already. The functions used are helper functions that basically just loop through the arrays to find an empty space and corresponding move. :

void solveGame(int a[15], int b[36][3], int c[15][3]){

    int empSpace;
    int moveIndex;
    int count = 0;

    if(pegCount(a) < 2){

        return;

    }
    else{

        empSpace = findEmpty(a);
        moveIndex = chooseMove( a, b, empSpace);

        a[b[moveIndex][0]] = 0;
        a[b[moveIndex][1]] = 0;
        a[b[moveIndex][2]] = 1;

        c[count][0] = b[moveIndex][0];
        c[count][1] = b[moveIndex][1];
        c[count][2] = b[moveIndex][2];

            solveGame(a,b,c);

    }

}
  • 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-28T20:47:42+00:00Added an answer on May 28, 2026 at 8:47 pm

    Here’s the key concept to keep in your mind: you’re not actually playing the game. What you’re doing is searching for a solution – this is called a space search, because you have a space of possible boards through which you want to chart a path. To guarantee that you’ll find the right answer, you have to write code that can search all possibilities – since, as you know from finding your house keys, the right answer is always in the last place you look.

    So, you can’t just make one recursive call and phone it in for the day. You’re going to have to write code which will loop over every possible move from a given board state, and make a recursive call for each one.

    From the perspective of each of those calls, their parents don’t even need to exist – the current call is just examining a board which happens to be a little further into the game than the ones which came before it.

    Now, when you actually run a space search, it’s likely you won’t really bother checking all those moves out – if you’re at all lucky, you’ll find the “right” move before then. So you return something from the function saying “I’ve got it, everybody above me bail out” and all your parent callers break their loops.

    This pattern is what’s called depth-first search. I’m going to illustrate by creating a hypothetical simpler game. Imagine you had to choose “A”, “B”, or “C” at each move, and the game lasts three moves. First you’d explore A, then AA, then AAA, then if that’s no good you look at AAB, then AAC, then AB, then ABA, then ABB, then ABC, then AC, then ACA, ACB, ACC, B, BAA, and so forth.

    That’s why it’s depth-first: you go all the way to the bottom, then backtrack as little as possible. It’s not easy to recursively implement breadth-first search, which would explore A, then B, then C, then AA, then AB, then BA, then BB, and so on until it finally got to the “complete-game” states with three moves. This requires using a queue instead of the implicit stack you get from recursion.

    Now, because you’re using recursion, you’re limited in how deep you can go: you’re not generally allowed to have too many frames. So in the real world you usually make an explicit stack instead, but the heart of the algorithm doesn’t much change for a depth-first search.

    There are entire textbook chapters on searching algorithms (“hill climbing”, “heuristics”, “A-star search”, and the like). Some of the algorithms are very interesting.

    As for your first question, on “using a lot of iteration” – it’s not how much iteration you’ve got but how smart it is :-). That is to say, if you don’t know you have a performance issue, you probably don’t need to optimize, and even then, intuition about what takes time can be bad. Get something where you can say “it works, but…” before you worry about getting something you’re proud of to put on your metaphorical trophy wall. Come back here if you need help with topics like pruning, or building a better heuristic for an A-star search.

    Good luck with your game program!

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

Sidebar

Related Questions

I have this project that it's due in a few hours and I still
Im sure this will be a simple one but have a project that started
So, I have this project that creates multiple instances of a class, and list
This might be a bit tricky setup but... I have this Silverlight project that
I have this large C++ project that I need to build on a platform
I have a project that is stored in a Subversion repository. In this repository,
I have a project that I am trying to convert to OSGi. However, this
I have data in an MYSQL database that looks like this: Project Date Time
So for this one project, we have a bunch of queries that are executed
This is for a web project so i have several classes that inherit from

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.