JS/제로초 JS

피셔 예이츠 셔플

배워도끝이없네 2021. 4. 29. 17:17
    const hor = parseInt(document.querySelector('#hor').value);
    const ver = parseInt(document.querySelector('#ver').value);
    const mine = parseInt(document.querySelector('#mine').value);
    console.log(hor,ver,mine);

    let minelist = Array(hor*ver) //가로 +세로의 크기만큼 배열생성
        .fill() //배열을 undefine으로 채움
        .map((element,idx)=>{ //map을 이용해 모든 배열에 각각의 idx +1만큼을 대치시켜줌)
            return idx+1;
        });
        
    let suffle = [];
    while(minelist.length > 0){ 
        let moveValue = minelist.splice(Math.floor(Math.random()*minelist.length), 1)[0];
        suffle.push(moveValue);
    }

 

피셔 예이츠가 만든 셔플방식이라 피셔 예이츠 셔플이라고 불린다함

 

1부터 100까지의 숫자를 매번 랜덤하게 섞어서 배열에 넣을 수 있음.

'JS > 제로초 JS' 카테고리의 다른 글

3-2. document 객체와 DOM  (2) 2021.04.29
3-1. window 객체  (0) 2021.04.29