12月28日 二维数组的应用:第一个小游戏(推箱子)

二维数组的应用:

案例1:成绩表:

static void Main(string[] args)
        {
            int i, j;
            int[,] a = new int[3, 4];

            for (i = 0; i < 3; i++)
            {
                a[i, 0] = i + 1;
                Console.Write(""+(i+1)+"位同学的语文成绩为");
                a[i, 1] = Convert.ToInt32(Console.ReadLine());

                Console.Write("" + (i + 1) + "位同学的数学成绩为");
                a[i, 2] = Convert.ToInt32(Console.ReadLine());

                a[i, 3] = a[i, 1] + a[i, 2];

            }
            Console.Clear();
            Console.Write("学号\t语文\t数学\t总分");
            Console.WriteLine();
            for (i = 0; i < 3;i++ )
            {
                for (j = 0; j < 4; j++)
                {
                    Console.Write(a[i, j] + "\t");
                }
                Console.WriteLine();
            }


        }

效果图:                                                       回车后:

12月28日 二维数组的应用:第一个小游戏(推箱子)_第1张图片           12月28日 二维数组的应用:第一个小游戏(推箱子)_第2张图片

案例2:推箱子

 static void Main(string[] args)
        {
            int i, j;
            int[,] a = new int[10, 10]
            {
                {1,1,1,1,1,1,1,1,1,1},
                {1,0,0,0,0,0,0,0,0,1},
                {1,0,2,0,0,8,0,0,0,1},
                {1,0,0,0,0,1,1,0,0,1},
                {1,0,0,0,0,1,0,0,0,1},
                {1,0,0,1,1,1,0,0,0,1},
                {1,0,1,1,0,0,0,0,0,1},
                {1,0,0,0,0,0,9,0,0,1},
                {1,0,0,0,0,0,0,0,0,1},
                {1,1,1,1,1,1,1,1,1,1},
            };

            int hang = 2, lie = 2, temp = 0;
                      
            while (true)
            {
                //做地图
                for (i = 0; i < 10; i++)
                {
                    for (j = 0; j < 10; j++)
                    {
                        if (a[i, j] == 1)
                        {
                            Console.Write("");
                        }
                        if (a[i, j] == 0)
                        {
                            Console.Write("  ");
                        }
                        if (a[i, j] == 2)
                        {
                            Console.Write("");
                        } if (a[i, j] == 9)
                        {
                            Console.Write("");
                        } if (a[i, j] == 8)
                        {
                            Console.Write("");
                        }

                    }

                    Console.WriteLine();
                }
               
                Console.Write("请按键盘上的前后左右键行走:");
                Console.WriteLine();

                ConsoleKey s = Console.ReadKey().Key;
                Console.Clear();

                // 向右
                if (s.ToString() == "RightArrow")
                {
                    //判断前边是不是空地
                    if (a[hang, lie+1] == 0)
                    {
                        temp = a[hang, lie];
                        a[hang, lie] = a[hang, (lie + 1)];
                        a[hang, (lie + 1)] = temp;
                        lie++;
                    }
                    //判断前边是不是墙
                    else if (a[hang, lie + 1] == 1)
                    {
                        Console.Write("撞墙了");
                    }
                    //判断前边是不是箱子
                    else if (a[hang, lie + 1] == 8)
                    {
                        if (a[hang, lie + 2] != 1 && a[hang, lie - 2] != 9)
                        {
                            temp = a[hang, (lie + 2)];
                            a[hang, lie + 2] = a[hang, lie + 1];
                            a[hang, lie + 1] = a[hang, lie];
                            a[hang, lie] = temp;
                            lie++;
                        }
                        //判断前边是不是★
                             else if (a[hang, lie + 2] == 9)
                         {                             
                             a[hang, lie + 2] = a[hang, lie + 1];
                             a[hang, lie + 1] = a[hang, lie];
                             a[hang, lie] = 0;
                             Console.Write("你赢了,请进入下一关");
                             break;
                         }
                        else { Console.Write("撞墙了");}
                    }
                }

                    //向左
                else if (s.ToString() == "LeftArrow")
                {
                    if (a[hang, lie-1] == 0)
                    {
                        temp = a[hang, lie];
                        a[hang, lie] = a[hang, (lie - 1)];
                        a[hang, (lie - 1)] = temp;
                        lie--;
                    }
                    else if (a[hang, lie - 1] == 1)
                    {
                        Console.Write("撞墙了");
                    }
                    else if (a[hang, lie - 1] == 8 )
                     {
                         if (a[hang, lie - 2] != 1 && a[hang, lie - 2] != 9)
                         {
                             temp = a[hang, (lie - 2)];
                             a[hang, lie - 2] = a[hang, lie - 1];
                             a[hang, lie - 1] = a[hang, lie];
                             a[hang, lie] = temp;
                             lie--;
                         }
                         else if (a[hang, lie - 2] == 9)
                         {                             
                             a[hang, lie - 2] = a[hang, lie - 1];
                             a[hang, lie - 1] = a[hang, lie];
                             a[hang, lie] = 0;
                             Console.Write("你赢了,请进入下一关");
                             break;
                         }
                         else { Console.Write("撞墙了"); }
                     }
                   
                }

                    //向上
                else if (s.ToString() == "UpArrow")
                {
                    if (a[hang-1, lie] == 0)
                    {
                        temp = a[hang, lie];
                        a[hang, lie] = a[(hang - 1), lie];
                        a[(hang - 1), lie] = temp;
                        hang--;
                    }
                    else if (a[hang-1, lie] == 1)
                    {
                        Console.Write("撞墙了");
                    }
                    else if (a[hang-1, lie] == 8)
                    {
                        if (a[hang - 2, lie] != 1 && a[hang - 2, lie] != 9)
                        {
                            temp = a[hang - 2, lie];
                            a[hang - 2, lie] = a[hang - 1, lie];
                            a[hang - 1, lie] = a[hang , lie];
                            a[hang, lie] = temp;
                            hang--;
                        }
                        else if (a[hang - 2, lie] == 9)
                        {                           
                            a[hang - 2, lie] = a[hang - 1, lie];
                            a[hang - 1, lie] = a[hang, lie];
                            a[hang, lie] = 0;
                            Console.Write("你赢了,请进入下一关");
                            break;
                        }
                        else { Console.Write("撞墙了"); }
                    }
                }

                    //向下
                else if (s.ToString() == "DownArrow")
                {
                    if (a[hang+1, lie] == 0)
                    {
                        temp = a[hang, lie];
                        a[hang, lie] = a[(hang + 1), lie];
                        a[(hang + 1), lie] = temp;
                        hang++;
                    }
                    else if (a[hang + 1, lie] == 1)
                    {
                        Console.Write("撞墙了");
                    }
                    else if (a[hang + 1, lie] == 8)
                    {
                        if (a[hang + 2, lie] != 1 && a[hang + 2, lie] != 9)
                        {
                            temp = a[hang + 2, lie];
                            a[hang + 2, lie] = a[hang + 1, lie];
                            a[hang + 1, lie] = a[hang , lie];
                            a[hang, lie] = temp;
                            hang++;
                        }
                        else if (a[hang + 2, lie] == 9)
                        {
                            a[hang + 2, lie] = a[hang + 1, lie];
                            a[hang + 1, lie] = a[hang, lie];
                            a[hang, lie] = 0;
                            Console.Write("你赢了,请进入下一关");
                            break;
                        }
                        else { Console.Write("撞墙了"); }
                    }
                }

                Console.WriteLine();
            }

        }

效果图:

12月28日 二维数组的应用:第一个小游戏(推箱子)_第3张图片

转载于:https://www.cnblogs.com/tzq9308/p/4190763.html

你可能感兴趣的:(12月28日 二维数组的应用:第一个小游戏(推箱子))