Guest_1013 2020-08-11 16:20 采纳率: 71.4%
浏览 93
已采纳

以下C#程序运行时,二维数组用户初始化之后输入start,清屏后输出46乘47个“O ",而不是开始正常生命游戏运行,如何解决?

Program.cs

using System;
using System.Threading;

namespace LifeGame
{
    partial class LifeGameMethod
    {
        public string[,] board = new string[48, 48];//主棋盘
        public string[,] board_copy = new string[48, 48];//复制棋盘,用于判断生死状态时不影响原状态
        public void boardOrigined()//原棋盘初始化
        {
            for (int a = 0; a <= 47; a += 1)
            {
                for (int b = 0; b <= 47; b += 1)
                {
                    switch (b)
                    {
                        case 47:
                            board[a, 47] = "\n";
                            break;
                        default:
                            board[a, b] = "O ";//“死”
                            break;
                    }
                }
            }
            for (int a = 0; a <= 47; a += 1)
            {
                for (int b = 0; b <= 47; b += 1)
                {
                    board_copy[a, b] = board[a, b];//复制棋盘初始化
                }
            }
        }

        public void firstConsole()
        {
            boardOrigined();
            for (int c = 0; c <= 47; c += 1)
            {
                for (int d = 0; d <= 47; d += 1)
                {
                    Console.Write("{0}", board[c, d]);//原棋盘输出
                }
                Thread.Sleep(100);
            }
        }

        public int userInput()
        {
            Thread.Sleep(500);
            while (true)
            {
                Console.Write(">>>");
                string input = Console.ReadLine();
                switch (input)
                {
                    case "start":
                        for (int f = 0; f <= 47; f += 1)
                        {
                            for (int g = 0; g <= 47; g += 1)
                            {
                                board_copy[f, g] = board[f, g];//复制棋盘与原棋盘状态同步
                            }
                        }
                        Console.Clear();
                        return 0;//退出方法
                    case "die":
                        Console.Write("Row....");
                        int input_row_w = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Line...");
                        int input_line_w = Convert.ToInt32(Console.ReadLine());
                        board[input_row_w, input_line_w] = "O ";
                        Console.Clear();
                        boardConsoleOut();
                        break;
                    case "live":
                        Console.Write("Row....");
                        int input_row_b = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Line...");
                        int input_line_b = Convert.ToInt32(Console.ReadLine());
                        board[input_row_b, input_line_b] =  "* ";
                        Console.Clear();
                        boardConsoleOut();
                        break;
                    case "help":
                        Console.WriteLine("die[enter]<value of row>[enter]<value of line>:  make the cell \"die\".");
                        Console.WriteLine("live[enter]<value of row>[enter]<value of line>: make the cell \"alive\".");
                        Console.WriteLine("start                                          : begin.");
                        Console.WriteLine("If you have finished reading,press any button to quit the help page.");
                        Console.ReadKey();
                        Console.Clear();
                        boardConsoleOut();
                        break;
                    case "about":
                        Console.WriteLine("Author  :。。。");
                        Console.WriteLine("If you have finished reading,press any button to quit the about page.");
                        Console.ReadKey();
                        Console.Clear();
                        boardConsoleOut();
                        break;
                    default:
                        Console.Write("Error.Try again.");
                        Thread.Sleep(1000);
                        Console.Clear();
                        boardConsoleOut();
                        break;
                }
            }
        }

        public void boardConsoleOut()
        {
            for (int a = 0; a <= 47; a += 1)
            {
                for (int b = 0; b <= 47; b += 1)
                {
                    Console.Write("{0}", board[a, b]);
                }
            }
        }

        public int aliveOrDead(int a, int b)//判断board[a,b]周围生细胞个数并返回,若本身为"\n",返回100
        {
            switch (a)
            {
                case 0:
                    switch (b)
                    {
                        case 0:
                            int sum_1 = 0;
                            if (board[47, 0] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[47, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[0, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[0, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[1, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[1, 0] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[1, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[47, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            return sum_1;
                        case 46:
                            int sum_2 = 0;
                            if (board[47, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[47, 46] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[47, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[0, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[0, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[1, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[1, 46] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[1, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            return sum_2;
                        case 47:
                            return 100;
                        default:
                            int sum_3 = 0;
                            if (board[47, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[47, b] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[47, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[0, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[0, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[1, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[1, b] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[1, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            return sum_3;
                    }
                case 47:
                    switch (b)
                    {
                        case 0:
                            int sum_1 = 0;
                            if (board[46, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[46, 0] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[46, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[47, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[47, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[0, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[0, 0] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[0, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            return sum_1;
                        case 46:
                            int sum_2 = 0;
                            if (board[46, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[46, 46] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[46, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[47, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[47, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[0, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[0, 46] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[0, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            return sum_2;
                        case 47:
                            return 100;
                        default:
                            int sum_3 = 0;
                            if (board[46, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[46, b] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[46, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[47, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[47, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[0, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[0, b] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[0, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            return sum_3;
                    }
                default:
                    switch (b)
                    {
                        case 0:
                            int sum_1 = 0;
                            if (board[a - 1, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[a - 1, 0] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[a - 1, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[a, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[a, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[a + 1, 46] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[a + 1, 0] == "* ")
                            {
                                sum_1 += 1;
                            }
                            else if (board[a + 1, 1] == "* ")
                            {
                                sum_1 += 1;
                            }
                            return sum_1;
                        case 46:
                            int sum_2 = 0;
                            if (board[a - 1, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[a, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[a + 1, 45] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[a - 1, 46] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[a + 1, 46] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[a - 1, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[a, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            else if (board[a + 1, 0] == "* ")
                            {
                                sum_2 += 1;
                            }
                            return sum_2;
                        case 47:
                            return 100;
                        default:
                            int sum_3 = 0;
                            if (board[a - 1, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[a - 1, b] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[a - 1, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[a, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[a, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[a + 1, b - 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[a + 1, b] == "* ")
                            {
                                sum_3 += 1;
                            }
                            else if (board[a + 1, b + 1] == "* ")
                            {
                                sum_3 += 1;
                            }
                            return sum_3;
                    }
            }
        }

        public void boardGoing()//开始演化
        {
            while (true)
            {
                Console.Clear();
                for (int a = 0; a <= 47; a += 1)
                {
                    for (int b = 0; b <= 47; b += 1)
                    {
                        board_copy[a, b] = board[a, b];//copy状态更新
                    }
                }
                for (int f = 0; f <= 47; f += 1)
                {
                    for (int g = 0; g <= 47; g += 1)
                    {
                        switch (aliveOrDead(f, g))
                        {
                            case 0:
                            case 1:
                                board_copy[f, g] = "O ";
                                Console.Write("{0}", board_copy[f, g]);
                                break;
                            case 2:
                                Console.Write("{0}", board_copy[f, g]);
                                break;
                            case 3:
                                board_copy[f, g] = "* ";
                                Console.Write("{0}", board_copy[f, g]);
                                break;
                            case 4:
                            case 5:
                            case 6:
                            case 7:
                            case 8:
                                board_copy[f, g] = "O ";
                                Console.Write("{0}", board_copy[f, g]);
                                break;
                            case 100:
                                Console.Write("\n");
                                break;
                        }
                        if (f == 47 && g == 47)
                        {
                            for(int h = 0;h <= 47;f += 1)
                            {
                                for(int i = 0;i <= 47;i += 1)
                                {
                                    board[h, i] = board_copy[h, i];
                                }
                            }
                            Thread.Sleep(1000);
                        }
                    }
                }
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                LifeGameMethod lifeGame = new LifeGameMethod();
                Console.WriteLine("==================================Life Game==================================");
                Thread.Sleep(5000);
                Console.Clear();
                lifeGame.firstConsole();
                lifeGame.userInput();
                lifeGame.boardGoing();
            }
        }
    }
}
  • 写回答

2条回答 默认 最新

  • threenewbee 2020-08-11 22:12
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。