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

3x3井字棋玩家对战胜者判定代码无输出问题求助

井字棋胜者判定程序调试求助

需求:实现3x3玩家对战井字棋(TicTacToe)的胜者判定程序。流程为:先获取用户输入的移动次数N,再输入N个移动坐标(每行一个,格式如1 2,第一个数为行索引,第二个为列索引)。X玩家先手,O玩家后手,程序需返回胜者X/O,或平局(Draw)。

示例输入:

7
1 1
2 1
2 0
0 2
1 0
1 2
0 0

示例输出:X

问题:本人编写的C#代码运行后控制台无任何输出,恳请提供调试提示。

代码:

using System;

namespace ConsoleApp20
{
    public class Program
    {
        public static string Tictactoe(int[,] moves)
        {
            int[] rowsValues = new int[3];
            int[] columnsValues = new int[3];
            int diagonalSum = 0;
            int antiDiagonalSum = 0;
            const int c1 = 3;
            bool isFirstPlayer = true;
            for (int i = 0; i < c1; i++)
            {
                int rowIndex = moves[i, 0];
                int columnIndex = moves[i, 1];
                int value = isFirstPlayer ? 1 : -1;

                if (rowIndex == columnIndex)
                {
                    diagonalSum += value;
                }

                if (columnIndex == c1 - rowIndex - 1)
                {
                    antiDiagonalSum += value;
                }

                rowsValues[rowIndex] += value;
                columnsValues[columnIndex] += value;
                if (Math.Abs(diagonalSum) == c1 ||
                   Math.Abs(antiDiagonalSum) == c1 ||
                   Math.Abs(rowsValues[rowIndex]) == c1 ||
                   Math.Abs(columnsValues[columnIndex]) == c1)
                {
                    return isFirstPlayer ? "X" : "0";
                }

                isFirstPlayer = !isFirstPlayer;
            }

            return "Draw";
        }

        public static void Main()
        {
           Tictactoe(ReadValues());
        }

        public static int[,] ReadValues()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            int[,] movesArr = new int[n, 2];
            for (int i = 0; i < n; i++)
            {
                string[] moves = Console.ReadLine().Split();
                int a = int.Parse(moves[0]);
                int b = int.Parse(moves[1]);
                movesArr[i, 0] = a;
                movesArr[i, 1] = b;
            }

            return movesArr;
        }
    }
}

调试提示

  • 未输出结果:Main方法中调用Tictactoe后未将返回值输出到控制台,修改为:
    public static void Main()
    {
        Console.WriteLine(Tictactoe(ReadValues()));
    }
    
  • 循环次数错误:Tictactoe方法的循环仅执行3次(i < c1),但实际移动次数N可能大于3,导致后续步骤未被处理。应改为遍历所有移动:
    for (int i = 0; i < moves.GetLength(0); i++)
    
  • 返回值拼写错误:判定O胜利时返回的是数字"0",需求要求返回字母"O",修正为:
    return isFirstPlayer ? "X" : "O";
    
  • 数组越界风险:若用户输入的行/列索引超出0-2范围,会触发数组越界异常。可在读取输入时增加校验:
    if (a < 0 || a >= 3 || b < 0 || b >= 3)
    {
        Console.WriteLine("无效坐标,需输入0-2之间的整数");
        Environment.Exit(1);
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:15:32