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

Unity C#中限制平局(T)不沿X轴递增的实现问题

Got it, let's tackle this problem. Your core goal is to make sure tie results (T) don't increment the X-axis index—instead, they should be placed in the same X row as the last non-tie result, only moving down the Y-axis. Let's break down what's off with your current code and fix it step by step.

What's Wrong With Your Current Logic

Your existing code doesn't explicitly handle tie results as a separate case. The check you added (previousValue.Contains(newResult)) targets the wrong scenario—you need to detect when the current result is a tie, not if the previous result includes one. Also, you aren't tracking the last valid non-tie result to know which X index to stick with for ties.

Fixed Code With Explanation

Here's a revised version of your method that implements the tie behavior you want:

string[] scoreboardWinner = new string[] { "P ","B ","T ", "BP ","B P","B B","BB ", "PB ","P B","P P","PP ", "TP ","TB ","T P","T B", "TTT", "BBP","BPB","BPP","BBB", "PPB","PBP","PBB","PPP" };

private void XandYaxis() {
    string[,] table = new string[104, 6];
    string lastNonTieResult = null; // Tracks the last non-tie result to anchor ties to its X index
    int xIndex = -1;
    int yIndex = 0;

    if (gametable_no == 1) {
        // Build the result array from your list (cleaned up with StringBuilder for efficiency)
        StringBuilder resultBuilder = new StringBuilder();
        foreach (var item in list) {
            resultBuilder.Append(item.r);
        }
        string[] newChars = resultBuilder.ToString().Split(',');

        foreach (string rawResult in newChars) {
            string result = rawResult.Trim(); // Remove whitespace to avoid mismatches
            bool isTie = result.Equals(scoreboardWinner[2].Trim(), StringComparison.OrdinalIgnoreCase);

            if (isTie) {
                // For ties: reuse the last valid X index, increment Y if within bounds
                if (xIndex >= 0 && yIndex < table.GetLength(1) - 1) {
                    table[xIndex, yIndex] = rawResult;
                    yIndex++;
                }
            } else {
                // For non-tie results: check if it's a new group or same as last non-tie
                bool isNewResultGroup = !result.Equals(lastNonTieResult?.Trim(), StringComparison.OrdinalIgnoreCase);
                
                if (isNewResultGroup) {
                    // Move to next X row, reset Y
                    xIndex++;
                    yIndex = 0;
                    // Stop if we hit the table's X limit
                    if (xIndex >= table.GetLength(0)) break;
                } else {
                    // Same result as last non-tie: increment Y, handle overflow if needed
                    yIndex++;
                    if (yIndex >= table.GetLength(1)) {
                        // Optional: if Y is full, start a new X row for duplicates
                        xIndex++;
                        yIndex = 0;
                        if (xIndex >= table.GetLength(0)) break;
                    }
                }

                // Update the last non-tie reference and assign to table
                lastNonTieResult = rawResult;
                table[xIndex, yIndex] = rawResult;
            }
        }
    }
}

Key Logic Breakdown

  • lastNonTieResult: This variable keeps track of the most recent non-tie result, so we know which X row to use when a tie comes in.
  • Tie Handling: When we detect a tie (T), we skip incrementing the X index and just add the result to the current X row's next available Y position.
  • Non-Tie Handling: For regular results, we check if it's a new value (different from lastNonTieResult). If yes, we move to a new X row and reset Y. If it's the same as the last non-tie, we increment Y (with overflow handling if Y hits the table's column limit).

Test With Your Sample Data

Let's walk through your sample sequence P, B, B, T, P:

  1. P (non-tie): xIndex becomes 0, yIndex 0. lastNonTieResult = "P". Table[0,0] = "P".
  2. B (non-tie): different from P, so xIndex becomes 1, yIndex 0. lastNonTieResult = "B". Table[1,0] = "B".
  3. B (non-tie): same as lastNonTieResult, so yIndex becomes 1. Table[1,1] = "B".
  4. T (tie): uses xIndex 1, yIndex becomes 2. Table[1,2] = "T".
  5. P (non-tie): different from B, so xIndex becomes 2, yIndex 0. lastNonTieResult = "P". Table[2,0] = "P".

This exactly matches your desired behavior—ties don't trigger X index increments, they stay anchored to the last non-tie's row.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:57:29