Guest_1013
Guest_1013
采纳率66.7%
2020-08-11 16:20

以下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条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 8月前
  • z467057213c z467057213c 8月前

    写的问题模棱两可,你写的逻辑是清屏后先输出一次二维数组的内容,然后等待输入,根据输入执行不同的动作,哪里有问题?或者说你的问题是哪里?

    点赞 评论 复制链接分享