Unity学习之C#基础13——数组的常用发方法及属性、2048简单游戏算法

 

 大家可以用上面的方法来做一个双色球的应用,用于熟悉数组的运用。下面的代码仅供参考:

public static int[] getRedAndBlueGoalArray()
        {
            int[] goalNumLine = new int[7];
            int randNum = -1;
            Random random = new Random();
            for (int i = 0; i < 6;)
            {
                randNum = random.Next(1, 34);
                if (Array.IndexOf(goalNumLine, randNum) != -1 && i > 0)
                {
                    continue;
                }
                else
                {
                    goalNumLine[i] = randNum;
                    i++;
                }
            }
            goalNumLine[goalNumLine.Length - 1] = random.Next(1, 16);
            return goalNumLine;
        }
        public static int[] getRedAndBlueRandGoalArray()
        {
            int[] goalNumLine = new int[7];
            int randNum = -1;
            Random random = new Random();
            for (int i = 0; i < 6;)
            {
                randNum = random.Next(1, 34);
                if (Array.IndexOf(goalNumLine, randNum) != -1 && i > 0)
                {
                    continue;
                }
                else
                {
                    goalNumLine[i] = randNum;
                    i++;
                }
            }
            goalNumLine[goalNumLine.Length - 1] = random.Next(1, 16);
            return goalNumLine;
        }
        public static int[] getuserWriteNum()
        {
            int writeNum = -1;
            int[] userWriteNum = new int[7];
            for (int i1 = 0; i1 < 6;)
            {
                Console.WriteLine("Please write the {0} red ball's num:", i1 + 1);
                writeNum = int.Parse(Console.ReadLine());
                if (writeNum > 33 || writeNum < 1)
                {
                    Console.WriteLine("You need to write in 1~33");
                    continue;
                }
                if (Array.IndexOf(userWriteNum, writeNum) != -1 && i1 > 0)
                {
                    Console.WriteLine("You cant write the same num(you write[{0}])!", writeNum);

                }
                else
                {
                    userWriteNum[i1] = writeNum;
                    i1++;
                }
            }
            int userWriteBlueBoalNum = -1;
            do
            {

                Console.WriteLine("please write the blue boal Num:");

                userWriteBlueBoalNum = int.Parse(Console.ReadLine());
                if (userWriteBlueBoalNum > 16 || userWriteBlueBoalNum < 1)
                {
                    Console.WriteLine("please write in 1~16!");
                }
            } while (userWriteBlueBoalNum > 16 || userWriteBlueBoalNum < 1);


            userWriteNum[userWriteNum.Length - 1] = userWriteBlueBoalNum;
            readAllitemsInArrayInALine(userWriteNum);
            return userWriteNum;
        }
        public static int[] getGoalState(int[] goalNumLine,int[] userWriteNum)
        {
            int redGoalNum = 0, blueGoalNum = 0;
            int[] goalRedNumLine=new int[goalNumLine.Length - 1];
            Array.Copy(goalNumLine, goalRedNumLine, goalNumLine.Length - 1);
            int[] userRedWriteNum = new int[userWriteNum.Length - 1];
            Array.Copy(userWriteNum, userRedWriteNum, userWriteNum.Length - 1);
            for (int i = 0; i < goalRedNumLine.Length; i++)
            {
                for(int i1=0;i1< userRedWriteNum.Length; i1++)
                {
                    if(goalRedNumLine[i]== userRedWriteNum[i1])
                    {
                        redGoalNum++;
                        break;
                    }
                }

            }
            if (userWriteNum[6] == goalNumLine[6])
            {
                blueGoalNum++;
            }
            return new int[] { redGoalNum, blueGoalNum };
            //Console.WriteLine("The Goal NumLine is:");
            //readAllitemsInArrayInALine(goalNumLine);
            //Console.WriteLine("What you are write is:");
            //readAllitemsInArrayInALine(userWriteNum);
            //Console.WriteLine("You get red Goal: [{0}] and blue Goal[{1}]!", redGoalNum, blueGoalNum);
        }
        public class BeepOn
        {
            [DllImport("Kernel32.dll")]
            public static extern bool Beep(int a, int b);
        }
        static void Main()//做一个双色球的玩法
        {
            //红球1~33不能重复,6个
            //蓝球1~16,1个       
            int guessStep = 0;
            while (true)
            {
                int[] goalNumLine = getRedAndBlueGoalArray();
                //readAllitemsInArray(goalNumLine);
                int[] userWriteNum = getRedAndBlueRandGoalArray();
                //readAllitemsInArray(userWriteNum);
                int[] goalState = getGoalState(goalNumLine, userWriteNum);
                if (goalState[0] == 6 && goalState[1] == 1)
                {
                    guessStep++;
                    Console.WriteLine("This is the {0} times!", guessStep);
                    Console.WriteLine("The Goal NumLine is:");
                    readAllitemsInArrayInALine(goalNumLine);
                    Console.WriteLine("\nWhat you are write is:");
                    readAllitemsInArrayInALine(userWriteNum);
                    Console.WriteLine("You got the first price!!!!");
                    BeepOn.Beep(1000, 1000);
                    break;
                }
                
                guessStep++;
                Console.WriteLine("This is the {0} times!",guessStep);
                Console.WriteLine("Red: {0} ,Blue: {1}", goalState[0], goalState[1]);
            }
        }

2.数组的排序

我们第一个会想到数组Array.Sort()方法。

但是这里我们来自己创建一个排序方法。

下面是一种排序方法:

public static void printArrayItemsInALine(Array arr)
        {
            int i = 0;
            foreach(var item in arr)
            {
                if (i % 8 == 0&&i>0)
                {
                    Console.Write("\n");
                }
                Console.Write(item + "\t");
                i++;
            }
            Console.WriteLine();
        }
        public static int[] getRandomInrArray(int num)
        {
            int[] arr = new int[num];
            Random random = new Random();
            for(int i = 0; i < num; i++)
            {
                arr[i] = random.Next(1, 101);
            }
            return arr;
        }
        static void Main(string[] args)
        {
            int[] a = getRandomInrArray(10000);
            sortArray(a);
            printArrayItemsInALine(a);
        }
        public static void sortArray(int[] arr)
        {
            int[] arrCopyType = arr;
            int ArrayLength = arr.Length;
            for(int step=0;step< ArrayLength; step++)
            {
                for (int i = 0; i < ArrayLength - 1; i++)
                {
                    if (arrCopyType[i] <= arrCopyType[i + 1])
                    {
                        continue;
                    }
                    else
                    {
                        int temp = arrCopyType[i];
                        arrCopyType[i] = arrCopyType[i + 1];
                        arrCopyType[i + 1] = temp;
                    }
                }
            }
            
            arr = arrCopyType;
        }

3.多维数组的创建

            int[,] a = new int[5,3];
            int temp = 0;
            for(int i = 0; i < 5; i++)
            {
                for(int i1 = 0; i1 < 3; i1++)
                {
                    a[i, i1] = i * i1;
                }
            }

上面是一个简单的二维数组创建和赋值的过程。

当我们想获取某一维度的个数,比如多少行或者多少列?

我们可以用Array.GetLength()方法。

二维数组的赋值,有时是可以用下面的方法赋值的:

 实例2048(仅供参考):

int[,] gamePlane = new int[4, 4];
            for (int i = 0; i < gamePlane.GetLength(0); i++)
            {
                for (int i1 = 0; i1 < gamePlane.GetLength(1); i1++)
                {
                    gamePlane[i, i1] = 0;
                }
            }

            Random random = new Random();
            int raw = -1;
            int column = -1;
            for (int i = 0; i < 2; i++)
            {
                do
                {
                    raw = random.Next(0, 4);
                    column = random.Next(0, 4);
                } while (gamePlane[raw, column] != 0);
                gamePlane[raw, column] = 2;
            }
            //初始化
            printArrayItemsInALine(gamePlane, gamePlane.GetLength(1));
            bool certai
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

言行物恒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值