본문 바로가기

C#/개발

[C#] 로또

프로그램 자체는 별 거 없지만 중복되지 않는 난수를 만드는 방법을 아는건 중요하다. 공을 뽑고 0.5초 이후에 출력하거나(쓰레드 사용) 6개를 다 뽑으면 버튼이 비활성화 되고 초기화 시 다시 활성화 되는 등 나름 디테일을 넣었다...

 

 

코드

 

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

namespace Lotto
{
    public partial class Form1 : Form
    {
        private List<int> lot;
        int result;
        Lotto lotto;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Ball.Visible=false;
            lbBall.Visible=false;
            lotto = new Lotto();
            lot = new List<int>();
            btnChoose.Enabled = true;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Ball.Visible=false;
            lbBall.Visible=false;
            lbNum.Text="";
            lot.Clear();
            btnChoose.Enabled = true;
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            Ball.Visible = false;
            lbBall.Visible = false;
            result = lotto.choose(lot);
            lot.Add(result);
            lbBall.Text = result.ToString();
            lbNum.Text = lbNum.Text + result.ToString()+" ";
            Thread.Sleep(500);
            Ball.Visible = true;
            lbBall.Visible = true;
            if (lot.Count >= 6)
                btnChoose.Enabled = false;
        }
    }
    public class Lotto
    {
        static int SEED=17;
        Random r;
        public Lotto()
        {
            r = new Random(SEED + System.DateTime.Now.Millisecond);
        }
        public int rand(){
            return r.Next(45)+1;
        }
        public int choose(List<int> list)
        {
            int result;
            while (true)
            {
                result = rand();
                if (list.Contains(result))
                    continue;
                else
                    return result;
            }
        }
    }
}

'C# > 개발' 카테고리의 다른 글

[C#] 하노이의 탑  (0) 2022.08.05
[C#] RSA 암호화 & 복호화  (0) 2022.08.05
[C#] 웹 이미지 다운로더  (0) 2022.08.05
[C#] 시리얼 통신과 장비조작  (0) 2022.08.05
[C#] 그래픽  (0) 2022.08.05