ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JavaScript] 간단한 로또시뮬레이션 중복제거 2등판별 등
    JavaScript 2023. 1. 21. 01:38
    반응형

    무언가 만들어볼까 하다가 간단한 로또 시뮬레이션을 만들어 보았다.

    늘 그렇듯이 디자인적인 부분은 스킵..

    자동추첨 1번당 5게임을 한다고 가정하였다. (1게임 1천원)

    아무래도 난 로또하면 안될 것 같다.

    ----------------------------------------------------------------------------------------

     >> 만들고 보니 미흡한 부분이 있어 추가했음.

     - input에 숫자만 입력가능하도록 수정.

     - 연속게임 1회 최대횟수는 10만회로 설정. (횟수가 너무 커지면 브라우저가 뻗음)

    ----------------------------------------------------------------------------------------

    아래는 내가만든 코드 샘플이다.

     

    로또게임을 구현할 때 중요한 점은, 자동으로 번호 생성을 할 때,

    중복번호 제거와 2등의 판별 이라고 생각한다.

    중복 제거부분 :

    먼저 배열에 넣었던 값들 중 새로 생성한 번호의 판별 :

    Array.includes(num)

    2등 번호 판별 :

    로또 2등은 1등과 5개의 번호가 같으며, 보너스번호 1개를 맞춘 경우이다.

    즉, 1등번호와 번호 5개가 매칭되며 추가로 보너스번호를 포함하고 있느냐를 보면 된다.

    document.addEventListener('DOMContentLoaded', function(){
        script.init();
    })
    
    let jackpots = {};
    let myNums = {};
    let records = {
        'gameCnt' : 0,
        'rank1' : 0,
        'rank2' : 0,
        'rank3' : 0,
        'rank4' : 0,
        'rank5' : 0,
        'lose' : 0
    };
    let directClick = false;
    
    let script = {
        init : function(){
            script.makeJackpots();
            script.addBtnEvent();
        },
    
        makeJackpots : function(){
            script.make6Nums(1, jackpots); //당첨번호 선정
        },
    
        make6Nums : function(trycnt, target){
            let makeCnt = 0;
            while(makeCnt < trycnt){
                let column = 0;
                target[makeCnt] = [];
                while(column < 6){
                    let num = Number.parseInt((Math.random()) * 45) +1; // 1~ 45번
                    //중복 체크
                    if(!target[makeCnt].includes(num)){
                        target[makeCnt].push(num);
                        column ++;
                    }
    
                    if(column === 6 && target === jackpots){
                        //마지막번호 뽑았고, 당첨번호 만들 경우 보너스번호 생성
                        let stop = false;
                        let bonus;
                        while(stop === false){
                            bonus = Number.parseInt((Math.random()) * 45) +1;
                            if(!target[makeCnt].includes(bonus)) stop = true;
                        }
                        target['bonus'] = bonus;
                        column ++;
                    }
                }
                target[makeCnt] = target[makeCnt].sort(function(a, b){
                    //오름차순 정렬
                    if(a > b) return 1;
                    if(a < b) return -1;
                    else return 0;
                });
                if(target === jackpots){
                    console.log(`1등 당첨번호 : ${jackpots[makeCnt]}`);
                }
                
                makeCnt ++;
            }
        },
    
        //당첨여부 확인
        checkWin : function(){
            let lotteries = Object.keys(myNums);
            let winNums = jackpots[0];
            let hasBought = lotteries.length > 0 ? true : false; //내 번호가 있는가
            if(hasBought){
                myNums['results'] = new Object();
                let currectCnt = 0;
                lotteries.forEach((e, idx) => {
                    const results = {
                        6 : 'rank1',
                        5 : 'rank3',
                        4 : 'rank4',
                        3 : 'rank5',
                        'second' : 'rank2',
                    };
    
                    let result;
                    // 당첨번호 배열과 내 번호배열 비교
                    if(Array.isArray(myNums[e])){
                        let chk = myNums[e].filter(x => winNums.includes(x));
                        currectCnt = chk.length; //당첨번호 갯수
                        result = results[currectCnt] ?? 'lose';
                        //2등여부 판별
                        if(currectCnt === 5){
                            let isSecond = myNums[e].includes(jackpots['bonus']) ? true : false;
                            if(isSecond) result = results['second'];
                        }
                        myNums['results'][idx] = {'result' : result, 'cnt' : currectCnt};
                    }
                })
            } else console.log('아직 복권이 없다.');
    
            //결과기록
            Object.keys(myNums['results']).forEach(e => {
                function takeMsgAndRecord(key){
                    const msgs = {
                        'rank1' : '1등당첨!!!!',
                        'rank2' : '2등!!!!',
                        'rank3' : '오예 100만원 3등!',
                        'rank4' : '그래도 10만원 개꿀 4등',
                        'rank5' : '5천원..ㅎㅎ 본전 ㄴㅇㅅ',
                        'lose' : null
                    }
                    records[key['result']] ++;
                    return msgs[key['result']] ?? '낙첨....'
                }
                if(directClick){
                    console.log(takeMsgAndRecord(myNums['results'][e]));
                } else {
                    takeMsgAndRecord(myNums['results'][e]);
                }
                
                records['gameCnt'] ++;
            })
        },
    
        addBtnEvent : function(){
            let eventBtns = Array.from(document.getElementsByClassName('sampleBtn'));
            myNums = {};
    
            let gameCntInput = document.getElementById('gameCnt');
    
            eventBtns.forEach((e, idx) => {
                if(idx === 0){
                    e.addEventListener('click', function(){
                        directClick = true;
                        script.doGame();
                    }, true);
                } else if(idx === 1){
                    e.addEventListener('click', function(){
                        let gamePrice = (records['gameCnt'] * 1000).toLocaleString('ko-KR');
                        let totalEarn = 0;
                        let priceInfo = {
                            'rank1' : 1000000000,
                            'rank2' : 50000000,
                            'rank3' : 1000000,
                            'rank4' : 100000,
                            'rank5' : 5000
                        }
                        Object.keys(priceInfo).forEach(e => {
                            totalEarn += priceInfo[e] * records[e];
                        })
                        totalEarn = totalEarn.toLocaleString('ko-KR');
                        let gameCnt = records['gameCnt'].toLocaleString('ko-KR');
                        let result = `============================================================================
                                        게임 결과
                            총 게임 횟수 :  ${gameCnt}
                            1등 횟수 :  ${records['rank1']}
                            2등 횟수 :  ${records['rank2']}
                            3등 횟수 :  ${records['rank3']}
                            4등 횟수 :  ${records['rank4']}
                            5등 횟수 :  ${records['rank5']}
                            꽝 횟수  :  ${records['lose']}
                            
                            총 소모비용 : ${gamePrice} 원
                            총 회수비용 : ${totalEarn} 원
    ============================================================================`
                        console.log(result);
                    }, true);
                } else if(idx === 2){
                    e.addEventListener('click', function(){
                        let inputCnt = Number(document.getElementById('gameCnt').value);
                        script.doManyGames(inputCnt);
                    }, true);
                }
                
            })
    
            //숫자입력이벤트
            gameCntInput.addEventListener('input',function(){
                let words = /[^0-9]/; //문자 거르기용
                this.value = this.value.replaceAll( new RegExp(words, 'ig'), '');
    
                let gameCnt = document.getElementById('gameCnt');
                if(Number(gameCnt.value) > 100000){
                    gameCnt.value = 100000;
                    alert('연속게임 1회 최대 횟수는 10만회 입니다.');
                }
            }, true);
        },
    
        doGame : function(){
            script.make6Nums(5, myNums);
            script.checkWin();
        },
        doManyGames : function(gameCnt){
            gameCnt = Number(gameCnt);
            if(Number.isNaN(gameCnt)){
                alert('숫자를 입력해주세요');
                document.getElementById('gameCnt').value = '';
                return false;
            }
            directClick = false;
            let tryCnt = 0;
    
            //연속게임중 버튼들 잠그기
            let btns = Array.from(document.getElementsByTagName('button'));
            btns.forEach(e => {
                e.disabled = true;
            })
            
            while(tryCnt < gameCnt){
                script.doGame();
                tryCnt ++;
            }
    
            //버튼들 잠금해제
            btns.forEach(e => {
                e.disabled = false;
            })
        }
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    <script src="./practice.js"></script>
    <body>
        <div class="container">
            <button class="sampleBtn">로또 자동추첨</button>
            <button class="sampleBtn">결과 확인</button>
            <input id="gameCnt"/>
            <button class="sampleBtn">연속게임</button>
        </div>
    </body>
    </html>
    반응형

    댓글

Designed by Tistory.