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

  • Home
  • SEARCH
  • 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 8607493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:22:12+00:00 2026-06-12T03:22:12+00:00

I cant figure out why a completely unrelated variable gets changed after every time

  • 0

I cant figure out why a completely unrelated variable gets changed after every time I run a recursive algorithm. There is absolutely no place where I in any way assign new values to this variable, but still, the recursive method runs and the variable gets changed.

The program I wrote is playing connect four. It has a ‘board’ object that stores where the players chips are, and a method that evaluate how many possible connect fours the person can get and assigns a score to that configuration.

The recursive algorithm is designd to find the optimal sequence of chips. It makes a copy of the board. Adds a chip to one of several possible locations (Hence the recursion). And Recurses. Once it reaches the base case it finds the ‘score’ that the hypothetical configuration of chips would recieve. As it backs out of the algorithm it selects the path that gives the greates number of points.

The problem is that the original copy of the board keeps getting overwritten? The program goes something like this. (generalized)

int[] FindBestPath(Board OrginalBoard, int Depth)
{
  Board TempBoard = OriginalBoard;
  if(Depth > 0)
    for(x of many possible modifications)
    {
      Tempboard.MakeModification(x);
      Depth--;
      Score = FindBestPath(TempBoard, Depth);
      if(Score > highestScoreYet)
      {
           highestScoreYet = Score;
           BestModification = x;
      }
    }
  else if(Depth == 0)
    Return new int[2] {ConfigurationScore(TempBoard), -1};

  return new int[2] {HighestScoreYet, BestModification};
}

Why does the ‘Orginal Board’ that I pass when I actually call the recursive algorithm get modification ‘x’ in all possible ways? I didn’t add a ‘ref’ statement or anything so shouldn’t the orginal be preserved?

Is there some known oddity about object behavior and recursive algorithms? Can I, in Microsoft C# Express 2010, somehow breakpoint at the currently unknown moment when the orginal board object gets modified?

Thank you.

  • 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-06-12T03:22:13+00:00Added an answer on June 12, 2026 at 3:22 am

    The culprit here is that Board is a class, a reference type. When you do:

    Board TempBoard = OriginalBoard;
    

    you do NOT get a copy of the board. You get a reference to the same board.

    The solution is either to make Board a struct, which is a value type, or to clone your board. Since using struct for your situation is most likely a bad idea, you should consider implenting a Clone method, and instead of:

    Board TempBoard = OriginalBoard;
    

    do:

    Board TempBoard = OriginalBoard.Clone();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely new to ActiveAdmin and RoR and i cant figure out how
I can't seem to figure out why SQL Server is taking a completely different
I cant figure out how to get lua to do any common timing tricks,
I want to do something but cant figure out how to do this (i
Im having a odd Problem with the TTS engine and I cant figure out
Hi I keep getting a syntax error but I cant figure out why. My
I am getting segmentation fault in this code but i cant figure out why.
This seems in my head like it should work but I cant figure out
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't
Total newbie question, but I cant figure out what im doing wrong. I want

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.