Calculating Horse Moves in Chinese Chess

  • Thread starter Thread starter tommyxu3
  • Start date Start date
  • Tags Tags
    Chess Recursion
Click For Summary

Discussion Overview

The discussion revolves around a recursive function designed to calculate the number of ways a "horse" can move from one point to another in Chinese Chess. Participants explore debugging techniques, code clarity, and the importance of comments in programming, particularly in recursive functions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Meta-discussion

Main Points Raised

  • One participant shares a recursive function but reports issues with obtaining the correct answer, suggesting a need for feedback on their approach.
  • Another participant questions whether the original poster has stepped through the code to debug it, implying that debugging is essential.
  • A participant expresses skepticism about the structure of the recursive function, suggesting that it should first check for a stopping condition before proceeding with recursion.
  • Concerns are raised about the lack of comments in the code, with multiple participants emphasizing that comments are crucial for understanding and maintaining code.
  • One participant suggests using "printf" statements to trace the execution of the recursive function as a debugging method.
  • Another participant stresses the importance of comments, stating that a lack of them could lead to significant issues in a professional setting.
  • A humorous remark is made about the necessity of comments, reinforcing the previous points about code documentation.
  • The original poster acknowledges the feedback and expresses intent to revise and test the code further.

Areas of Agreement / Disagreement

Participants generally agree on the importance of comments in code and the need for clarity in recursive functions. However, there is no consensus on the specific issues within the provided code or how best to resolve them, as participants offer differing perspectives on debugging techniques and code structure.

Contextual Notes

The discussion highlights the challenges of debugging recursive functions and the potential confusion arising from unclear code structure. Specific assumptions about the correctness of the board state and the intended functionality of the recursive function remain unverified.

tommyxu3
Messages
240
Reaction score
42
I wrote a recursive function to calculate the number of the way for "horse" to move from one point to another in Chinese Chess in the following, but I cannot get the correct answer. My idea is to sum up the other 8(or less than 8) points' way toward the target. Thanks for any opinions in advance!
C:
int ans(int orig_x, int orig_y, int tar_x, int tar_y, int place[][8])
{
    int type[][2]={-1,2,1,2,1,-2,-1,-2,-2,-1,-2,1,2,1,2,-1};
    int next_x,next_y,a[8],b=0,i,j,count_last;
    place[orig_x+1][orig_y+1]=-1;
    place[tar_x+1][tar_y+1]=1;
    for(i=0;i<4;i++)
    {
        next_x=orig_x+type[i][0];
        next_y=orig_y+type[i][1];
        if(place[next_x+1][next_y+1]==1&&place[next_x+1][next_y+type[i][1]/abs(type[i][1])+1]!=2)
        {
            if(next_x==tar_x&&next_y==tar_y)
            {
                count_last=0;
                for(j=0;j<4;j++)
                {
                    if(j=i+(i<2)*2-(i>1)*2)
                        continue;
                    if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+1][next_y+type[i][1]/abs(type[i][1])+1]==2)
                        count_last+=1;
                }
                for(j=4;j<8;j++)
                {
                    if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+type[i][0]/abs(type[i][0])+1][next_y+1]==2)
                        count_last+=1;
                }
                if(count_last==7)
                    return 1;
                else
                    a[i]=ans(next_x, next_y, tar_x, tar_y, place)+1;
            }
            place[orig_x+1][orig_y+1]=-1;
        }
        else
            a[i]=0;
    }
    for(i=4;i<8;i++)
    {
        next_x=orig_x+type[i][0];
        next_y=orig_y+type[i][1];
        if(place[next_x+1][next_y+1]==1&&place[next_x+type[i][0]/abs(type[i][0])+1][next_y+1]!=2)
        {
            if(next_x==tar_x&&next_y==tar_y)
            {
                count_last=0;
                for(j=0;j<4;j++)
                {
                    if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+1][next_y+type[i][1]/abs(type[i][1])+1]==2)
                        count_last+=1;
                }
                for(j=4;j<8;j++)
                {
                    if(j=i+(i<6)*2-(i>5)*2)
                        continue;
                    if(place[next_x+type[i][0]+1][next_y+type[i][1]+1]!=1||place[next_x+type[i][0]/abs(type[i][0])][next_y+1]==2)
                        count_last+=1;
                }
                if(count_last==7)
                    return 1;
                else
                    a[i]=ans(next_x, next_y, tar_x, tar_y, place)+1;
            }
            place[orig_x+1][orig_y+1]=-1;
        }
        else
            a[i]=0;
    }
    for(i=0;i<7;i++)
        b+=a[i];
    return b;
}
 
Technology news on Phys.org
Have you stepped through it to see what it is doing? Do you know how to debug errors by stepping through a program?
 
Yes, but I don't know how to check it in the recursion. I know the numbers in "place[][]"(the board) is correct, but how to check the recursive process?
 
Why do you think stepping through a recursive process would not work? I do it all the time.
 
Although I haven't read this code in great detail, I immediately see something about it that I don't like. The recursive call is near the end of the recursive function. I was taught (quite wisely) that the first thing a recursive function should always do is test and see if it's time to stop recurring and just return. The structure of this code is very confusing and definitely needs to be clarified--besides which, we don't really know what you are trying to accomplish.

Frankly, you need comments in at least a few key places. And maybe some more descriptive variable names. This would help you, and help us help you.

If the code is clarified and simplified, you should be able to dump out local variable contents to your GUI and verify that the recursive computation is occurring as you expected. Or else, use a debugger and painfully step through it. But personally, I advise you to rewrite the whole thing again first, with some comments and clarification about what all the different loops and sections are intending to do.
 
  • Like
Likes   Reactions: FactChecker
Being developer in C / Java for years, I want to ask why there is no single comment in the code. If you wrote it yourself, it would be a lot easier to add comments in some critical points, so any programmer inspecting it, would give help much easier. This would be great for yourself too, in case you revisit the code after some time. Guesswork in these cases, is not of much help. Everyone has his own way of writing code in any language and this is why comments exist.
The simpler way to go through your program and see what it really does, is putting some "printf" statements to the appropriate places. Recursion is no different as phinds pointed out. Of course, there are many more advanced tools to test / debug your code, going through stepwise procedures, but I mention the one most primitive, if you will. Knowing what your recursion must do and maybe with the help of pen and paper if you need to, you can just compare results and see what is going wrong.
 
I would like to add to what has already been said that due to lack of comments in your code, if you worked for me you would be in serious trouble. If you are a relatively inexperience coder, I could forgive the sloppiness of the code itself under the assumption that you are not yet quite there in terms of properly formed algorithms, but the lack of comments is inexcusable. I don't say this to be mean to you but to point out the importance of comments.

I've been programming since 1962 and I can tell you ABSOLUTELY that lack of comments in code is just inexcusable and causes grief for everyone, including you.
 
  • Like
Likes   Reactions: FactChecker
I agree with all the comments about comments. Here is one word of advice to a programmer -- If someone tells you that comments are not needed or that their code is "self-documenting", poke them in the eye. If they persist, poke them in the other eye.
 
  • Like
Likes   Reactions: QuantumQuest and phinds
Yes, above, I realized. It is posted in hurry. I would try more and test. Thanks a lot again!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
3K
Replies
55
Views
7K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K