You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

网球锦标赛模拟程序meci函数返回异常值求助

Troubleshooting Your Tennis Match Simulation Function

Hey George, nice first post! Let's break down the issues in your meci function that's causing unexpected return values. I spotted several key problems in the logic and implementation that are likely throwing off your match results:

1. Critical Typo in Score Assignment

Looking at the second return block, you have:

jucator2.scor1 = o;
jucator2.scor1 = u;

You're overwriting scor1 twice instead of setting scor2 to the opponent's score. This means the losing player's score is never stored correctly, leading to incorrect output values. It should be:

jucator2.scor1 = o;
jucator2.scor2 = u;

2. Flawed Serve Turn Logic

Your current loop structure doesn't follow a consistent serve pattern (even a simplified one):

  • When Player 2 wins a point on Player 1's serve, you jump into a loop where Player 2 serves repeatedly until Player 1 wins a point. This creates unpredictable serve turns that skew point distribution.
  • The break in the inner loop triggers when l == p, but tied random values should probably re-run the point instead of switching servers immediately.

3. Risk of Infinite Recursion

Your final else return meci(jucator1,jucator2); recursively calls the function if the score isn't a decisive win. If the score stays close for a long time, this will cause a stack overflow and crash your program. Replace recursion with a single loop instead.

4. Unhandled Tied Points

When p == l in the outer loop, you do nothing—this point is never counted for either player. You should re-run the point generation until there's a clear winner.

5. Missing Random Seed Initialization

If you're not calling srand(time(0)); at the start of your program, rand() will generate the same sequence of numbers every run. This can make results seem "abnormal" because they're not truly random.

Fixed Version of the meci Function

Here's a revised version with these issues addressed, plus a logical alternate serve pattern (switching servers every point—adjust to match real tennis rules if needed):

#include <stdlib.h>
#include <time.h>

jucatori meci(jucatori jucator1, jucatori jucator2) {
    int u = 0; // scor jucator 1
    int o = 0; // scor jucator 2
    int p, l;
    int servingPlayer = 1; // Start with player 1 serving

    // Note: Call srand(time(0)) ONCE in your main() function, not here!

    while (1) {
        // Generate point based on current server
        if (servingPlayer == 1) {
            p = rand() % jucator1.sansaServire;
            l = rand() % jucator2.sansaPrimire;
        } else {
            p = rand() % jucator1.sansaPrimire;
            l = rand() % jucator2.sansaServire;
        }

        // Determine point winner; re-run if tied
        if (l > p) {
            o++;
        } else if (p > l) {
            u++;
        } else {
            continue; // Re-generate point for tie
        }

        // Switch server for next point
        servingPlayer = servingPlayer == 1 ? 2 : 1;

        // Check for decisive win (difference >10)
        if ((u > o && u - o > 10) || (o > u && o - u > 10)) {
            if (u > o) {
                jucator1.meciurijucate++;
                jucator2.descalificat = 1;
                jucator1.scor1 = u;
                jucator1.scor2 = o;
                return jucator1;
            } else {
                jucator2.meciurijucate++;
                jucator1.descalificat = 1;
                jucator2.scor1 = o;
                jucator2.scor2 = u;
                return jucator2;
            }
        }

        // Safety limit to prevent infinite loop in rare stalemates
        if (u + o > 200) {
            // Fallback: declare player with higher total points as winner
            if (u > o) {
                jucator1.meciurijucate++;
                jucator2.descalificat = 1;
                jucator1.scor1 = u;
                jucator1.scor2 = o;
                return jucator1;
            } else {
                jucator2.meciurijucate++;
                jucator1.descalificat = 1;
                jucator2.scor1 = o;
                jucator2.scor2 = u;
                return jucator2;
            }
        }
    }
}

Key Improvements:

  • Fixed the score assignment typo
  • Implemented a consistent alternate serve pattern
  • Replaced recursive re-runs with a single loop to avoid stack overflow
  • Added handling for tied points (re-run instead of ignoring)
  • Added a safety limit to prevent infinite loops
  • Clarified where to initialize the random seed

If you're still seeing issues after these fixes, feel free to share the full program code—we can dig deeper into the jucatori struct or other parts of the simulation!

内容的提问来源于stack exchange,提问作者George Grigoras

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:43:24