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

求完善3x3井字棋游戏赢者判断代码及二维数组填充方法

3x3井字棋赢者判断程序完善

需求说明

实现3x3井字棋赢者判断逻辑:

  • 先输入落子总步数N
  • 依次输入N个落子位置(格式示例:1 0,第一个数字为行索引,第二个为列索引)
  • X先手落子,O后手交替落子
  • 程序输出结果:胜者为"X"或"O",无胜者则输出"Draw"

现有代码问题

当前代码中ReadValues方法逻辑不完整,FillMatrix方法未实现,需要完善输入读取与二维数组填充逻辑,同时修正原逻辑中的冗余与错误。

现有代码片段

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;
            int n = Convert.ToInt32(Console.ReadLine());
            bool isFirstPlayer = true;
            for (int i = 0; i < n; 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()
        {
            ReadValues(Tictactoe);
        }

        static int[][] ReadValues()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = n; n > 0; n--)
            {
                string[] moves = Console.ReadLine().Split();
                int a = int.Parse(moves[0]);
                int b = int.Parse(moves[1]);

             // This method needs some adjustments

     static int[][] FillMatrix

            // This is the method that I need help at.

完善后的完整代码

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 winCondition = 3;
            bool isFirstPlayer = true;

            // 直接使用传入的moves长度,无需重复读取N
            for (int i = 0; i < moves.Length; i++)
            {
                int rowIndex = moves[i][0];
                int columnIndex = moves[i][1];
                int value = isFirstPlayer ? 1 : -1;

                // 计算对角线和
                if (rowIndex == columnIndex)
                {
                    diagonalSum += value;
                }

                // 计算反对角线和
                if (columnIndex == winCondition - rowIndex - 1)
                {
                    antiDiagonalSum += value;
                }

                // 计算行、列和
                rowsValues[rowIndex] += value;
                columnsValues[columnIndex] += value;

                // 判断是否获胜
                if (Math.Abs(diagonalSum) == winCondition ||
                    Math.Abs(antiDiagonalSum) == winCondition ||
                    Math.Abs(rowsValues[rowIndex]) == winCondition ||
                    Math.Abs(columnsValues[columnIndex]) == winCondition)
                {
                    return isFirstPlayer ? "X" : "O"; // 修正原代码中返回"0"的错误,应为"O"
                }

                isFirstPlayer = !isFirstPlayer;
            }

            return "Draw";
        }

        public static void Main()
        {
            int[][] moves = ReadValues();
            Console.WriteLine(Tictactoe(moves));
        }

        // 完善ReadValues方法:读取输入并生成落子位置的二维数组
        static int[][] ReadValues()
        {
            int n = Convert.ToInt32(Console.ReadLine());
            int[][] moves = new int[n][];

            for (int i = 0; i < n; i++)
            {
                // 读取一行输入并分割为行、列索引
                string[] input = Console.ReadLine().Trim().Split();
                int row = int.Parse(input[0]);
                int col = int.Parse(input[1]);
                moves[i] = new int[] { row, col };
            }

            return moves;
        }

        // 单独实现FillMatrix方法:接收步数N,返回填充好的落子位置数组
        static int[][] FillMatrix(int n)
        {
            int[][] matrix = new int[n][];
            for (int i = 0; i < n; i++)
            {
                string[] input = Console.ReadLine().Trim().Split();
                matrix[i] = new int[] { int.Parse(input[0]), int.Parse(input[1]) };
            }
            return matrix;
        }
    }
}

关键修改说明

  1. 修正Tictactoe方法:

    • 移除冗余的重复读取N操作,直接使用moves.Length作为落子步数
    • 修正获胜返回值错误,将"0"改为"O"
    • 重命名常量c1为winCondition,提升代码可读性
  2. 完善ReadValues方法:

    • 创建对应长度的二维数组,循环读取输入并解析为行、列索引存入数组
    • 修复原循环的遍历逻辑错误,确保正确读取N次输入
  3. 实现FillMatrix方法:

    • 单独封装输入填充逻辑,接收步数参数N,返回填充完成的落子位置数组,可按需替换ReadValues中的逻辑使用

内容的提问来源于stack exchange,提问作者Bogdan Dragoş

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 23:41:29