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 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179