2266 發表於 2021-1-23 10:43:47

用亂數選出不重複的數字陣列

class Program
    {
      static int max =10;
      static Random r = new Random();
      static int[] a = new int;
      
      static bool checkOK(int n)
      {
            for(int i=0;i<max;i++)
            {
                if(n==a)
                {
                  return false;
                  
                }
            
            }
            return true;
      }
      static void Main(string[] args)
      {
            int x = 0;
            int i = 0;
            for (int k = 0; k < max; k++)
                a = 999;
            while(i<max)
            {
                x = r.Next(1, max+1);
                if(checkOK(x))
                {
                  a = x;
                  i++;
                }
            }

            for (int j = 0; j < max; j++) Console.WriteLine(a);
      }

    }

2266 發表於 2021-1-26 18:15:49

本帖最後由 2266 於 2021-6-27 18:12 編輯

好像這是更簡單的寫法

static void randomPickNoRepeat(int[] A, int n)
      {
            Random rnd = new Random();
            int x = 0;
            bool OK = true; //OK 做為檢查該數字是否可以放入陣列的旗標
            for (int i = 0; i < n; i++)
            {
                OK = true; //預設是true
                x = rnd.Next(n);//亂數給值從0到n-1
                for(int k=0;k<i;k++)//再從頭檢查新出的是否和已有的數字一樣
                  if(A==x)//如果檢查到已有該數字,則i退回一個,OK變成false,並中斷此迴圈
                  {
                        i--;
                        OK = false;
                        break;
                  }
                if (OK) A = x;//如果以上檢查完成並未被中斷,且OK依然為true,表示x的數字從未出現過,所以可以加到陣列中
               
            }

      }

2266 發表於 2021-1-30 12:15:20

本帖最後由 2266 於 2021-6-27 18:13 編輯

最簡單!

void randomPickNoRepeat(int[] a)
      {
          Random rnd = new Random();
            for(int i=0;i<a.Length;i++)
            {
                a = rnd.Next(a.Length);
                //Console.WriteLine("a[" + i + "] is " + a);
                for(int j=0;j<i;j++)
                  if(a==a) //如果陣列中已有數字和目前選出來的數字一樣的話
                  {
                        //Console.WriteLine("Because a[" + i + "] is as the same as a[" + j + "] so i is going back to " + i + " and will ++ again.");
                        i--; // i 減去一個
                        break; // 立即中斷回到上面再次迴圈
                  }
            }
      }

2266 發表於 2021-6-27 17:31:20

用亂數打亂原有陣列之元素排序:


using System.Linq;

Random rnd = new Random;
int[] n = new int{0,1,2,3,4,5,6,7,8,9};//要先有元素
n = n.OrderBy(x=>rnd.Next()).ToArray();//x可以隨意取,例如k,z什麼都行,不必先宣告。
此時n的陣列排序已亂排了。

csharp 發表於 2021-6-30 09:56:07

本帖最後由 csharp 於 2021-6-30 13:35 編輯

nothing
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace WindowsFormsApp28
{
    public partial class Form1 : Form
    {
      Panel panel = new Panel();
      PictureBox[] pb = new PictureBox;
      PictureBox[] pb1 = new PictureBox;
      PictureBox[] pb2 = new PictureBox;
      PictureBox[] pb3 = new PictureBox;
      PictureBox[] pb4 = new PictureBox;
      Label[] table = new Label;
      PictureBox[] pbIcon = new PictureBox;
      SoundPlayer touch = new SoundPlayer(Properties.Resources.quick000);
      SoundPlayer ending = new SoundPlayer(Properties.Resources.ending001);
      int oldX, oldY;
      int[] ary = new int;
      bool isDown;
      bool[] matched = new bool;
      PictureBox howto = new PictureBox();
      
      public Form1()
      {
            InitializeComponent();
            
            setPanel();
            setIcon();
            setTable();
            setPicturebox();
            howto.Image = Properties.Resources.howtoplay;
            howto.Location = new System.Drawing.Point(Width / 3 * 2, 15);
            howto.Size = new Size(120,20);
            panel.Controls.Add(howto);
            
      }

      


      void setPanel()
      {
            Rectangle screen = Screen.FromControl(this).Bounds;//這三行是求得整個電腦視窗的長寬值
            Width = screen.Width;//這三行是求得整個電腦視窗的長寬值
            Height = screen.Height;//這三行是求得整個電腦視窗的長寬值
            
            panel = new Panel();
            panel.Location = new System.Drawing.Point(35, 5);
            panel.Size = new Size(Width / 10 * 9, Height / 100 * 95);
            //panel.BackgroundImage = Properties.Resources._111;
            //panel.BackgroundImageLayout = ImageLayout.Stretch;
            panel.BackColor = Color.AliceBlue;
            Controls.Add(panel);
      }


      void setPicturebox()
      {
            for(int i = 0; i < 12; i++)
            {
                //先將物件陣列實體化
                pb = new PictureBox();
                pb1 = new PictureBox();
                pb2 = new PictureBox();
                pb3 = new PictureBox();
                pb4 = new PictureBox();

                //將各圖檔尺寸定出來
                pb.Size = new Size(100, 100);
                pb1.Size = new Size(100, 100);
                pb2.Size = new Size(100, 100);
                pb3.Size = new Size(100, 100);
                pb4.Size = new Size(100, 100);

                //除掉主要的陣列之外,將其它陣列給予圖片
                pb1.Image = (Image)Properties.Resources.ResourceManager.GetObject("a"+i);
               
                pb2.Image = (Image)Properties.Resources.ResourceManager.GetObject("b"+i);
               
                pb3.Image = (Image)Properties.Resources.ResourceManager.GetObject("d"+i);
               
                pb4.Image = (Image)Properties.Resources.ResourceManager.GetObject("k"+i);
               
                //將主要陣列賦予位置,並且將其置入panel中
                pb.Location = new System.Drawing.Point(30+i%6*110,325+i/6*103);
                pb.BackColor = Color.Transparent;
                panel.Controls.Add(pb);
               
                pb.MouseDown += new MouseEventHandler(pb_mousedown);
                pb.MouseMove += new MouseEventHandler(pb_mousemove);
                pb.MouseUp += new MouseEventHandler(pb_mouseup);

            }
      }

      
      void pb_mousedown(object sender, MouseEventArgs e)
      {
            isDown = true;
            oldX = e.X;
            oldY = e.Y;
      }

      void pb_mousemove(object sender, MouseEventArgs e)
      {
            PictureBox pb = (PictureBox)sender;
            if (isDown)
            {
                pb.Left += e.X - oldX;
                pb.Top += e.Y - oldY;
                pb.BringToFront();
            }
      }

      void pb_mouseup(object sender, MouseEventArgs e)
      {
            isDown = false;

            closeToBorder(sender, table);
                       
            isThisPictureCorrect(pb, table);
                       
            if (allMatched())
            {

                for (int i = 0; i < 12; i++)
                {
                  pb.Enabled = false;
                     }
                ending.Play();
            }
      }


      void setIcon()
      {
            for(int i = 0; i < 4; i++)
            {
                pbIcon = new PictureBox();
                pbIcon.Image = (Image)Properties.Resources.ResourceManager.GetObject("icon" + (i + 1));
                pbIcon.Size = new Size(120, 90);
                pbIcon.SizeMode = PictureBoxSizeMode.StretchImage;
                pbIcon.Location = new System.Drawing.Point(Width/3*2,20+i*110+30);
                panel.Controls.Add(pbIcon);
               
                pbIcon.Click += new System.EventHandler(pb_click);
            }
      }

      
                void pb_click(object sender, EventArgs e)
                {
                  PictureBox p = (PictureBox)sender;
                  pbRestart();
                  if (p == pbIcon)
                        randomPb(pb, pb1);
                  else if (p == pbIcon)
                        randomPb(pb, pb2);
                  else if (p == pbIcon)
                        randomPb(pb, pb3);
                  else if (p == pbIcon)
                        randomPb(pb, pb4);
                }
      

      void setTable()
      {
            for (int i = 11; i >= 0; i--)
            {
                table = new Label();
                table.Size = new Size(100, 100);
                table.BorderStyle = BorderStyle.FixedSingle;
                table.BackColor = Color.Transparent;
                table.Location = new System.Drawing.Point(150+(11 - i) % 4 * 100,10+ (11 - i) / 4 * 100);
                panel.Controls.Add(table);
               
            }
      }


      void closeToBorder(object sender, Label[] lbl)
      {
            PictureBox p = (PictureBox)sender;
            for (int i = 0; i < 12; i++)
            {
                if (Math.Abs(p.Left - lbl.Left) < 30 && Math.Abs(p.Top - lbl.Top) < 30)
                {
                  p.Left = lbl.Left;
                  p.Top = lbl.Top;
                  touch.Play();
                  
                }
            }
      }
               
               
      void isThisPictureCorrect(PictureBox[] p, Label[] lbl)
      {
            for (int i = 0; i < 12; i++)
                       for(int j=0;j<12;j++)
            {
                if ((p.Top == lbl.Top) && (p.Left == lbl.Left)&&(pbValue==j))
                  matched = true;
                else matched = false;
            }
      }
               
      bool allMatched()
      {        bool ok=true;
            for (int i = 0; i < 12; i++)
            {
                if (matched == false) ok== false;
            }
            return ok;
      }

      
      void randomPickNoRepeat(int[] a)
      {
            Random rnd = new Random();
            for (int i = 0; i < a.Length; i++)
            {
                a = rnd.Next(a.Length);
               
                for (int j = 0; j < i; j++)
                  if (a == a)
                  {
                        
                        i--;
                        break;
                  }
            }

      }

      void pbRestart()
      {
            for (int i = 0; i < 12; i++)
            {
                pb.Enabled = true;
                pb.Location = new System.Drawing.Point(30 + i % 6 * 110, 330 + i / 6 * 103);
            }
      }
                int[] a = new int { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
      int[] pbValue = new int;
                void randomPb(PictureBox[] pb, PictureBox[] p1)
      {
            
            randomPickNoRepeat(a);
                       
            for (int i = 0; i < 12; i++)
            {
                pb.Image = p1].Image;
                                pbValue=a;
                               
               
            }
      }


    }
}
頁: [1]
查看完整版本: 用亂數選出不重複的數字陣列