Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>가위바위보</title>
    <style>
        #computer{
            width:150px;
            height:243px;
            background: url('http://en.pimg.jp/023/182/267/1/23182267.jpg') 0 0;
        }
    </style>
</head>
<body>
    <div id="computer">

    </div>
    <div class="buttons">
        <button id="scissor" class="btn">가위</button>
        <button id="rock" class="btn">바위</button>
        <button id="paper" class="btn">보</button>
    </div>
</body>
</html>
              
            
!

CSS

              
                div{margin:auto;}
.buttons{display:block;text-align:center;}

h2{
  text-align:center;
  color:skyblue;
}
              
            
!

JS

              
                var 이미지좌표=0;
var 가위바위보={ //딕셔너리 자료구조
    가위:'-142px',
    바위:'0',
    보:'-284px'
};

// var 가위바위보2={
//     '-142px':'가위',
//     '0':'바위',
//     '-284px':'보'
// };


function 컴퓨터의선택(이미지좌표){
    return Object.entries(가위바위보).find(function(v){
        return v[1]===이미지좌표; //v[1]의 위치:좌표값
    })[0];
}

var 인터벌;
function 인터벌메이커(){
    인터벌=setInterval(function(){
        if(이미지좌표===가위바위보.바위){
            이미지좌표=가위바위보.가위;
        } else if(이미지좌표===가위바위보.가위){
            이미지좌표=가위바위보.보;
        } else{
            이미지좌표=가위바위보.바위;
        }
    
        document.querySelector('#computer').style.background='url("http://en.pimg.jp/023/182/267/1/23182267.jpg") '+이미지좌표+' 0';
    
    },100);
}

인터벌메이커();

var 점수표={
    가위:1,
    바위:0,
    보:-1,
}

document.querySelectorAll('.btn').forEach(function(btn){
    btn.addEventListener('click',function(){
        clearInterval(인터벌);
        setTimeout(function(){
            인터벌메이커();
        },1000);
        var 나의선택=this.textContent;
        var 나의점수=점수표[나의선택];
        var 컴퓨터점수=점수표[컴퓨터의선택(이미지좌표)];
        var 점수차=나의점수-컴퓨터점수;
        console.log(나의선택,컴퓨터의선택(이미지좌표));

        if(점수차===0){
            console.log('비김');
            
            var 결과=document.createElement('h2');
            document.body.append(결과);
            결과.textContent="비김";
        }else if([-1,2].includes(점수차)){
            console.log('이김');
           var 결과=document.createElement('h2');
            document.body.append(결과);
            결과.textContent="이김";
        }else{
            console.log('짐');
           var 결과=document.createElement('h2');
            document.body.append(결과);
            결과.textContent="짐";
        }

        //점수표[나의선택]-점수표[컴퓨터의선택(이미지좌표)]===-1 || 점수표[나의선택]-점수표[컴퓨터의선택(이미지좌표)]===2
        //->배열.includes로 줄일 수 있다.

        // if(나의선택==='가위'){
        //     if(컴퓨터의선택(이미지좌표)==='가위'){
        //         console.log('비김');
        //     }else if(컴퓨터의선택(이미지좌표)==='바위'){
        //         console.log('짐');
        //     }else{
        //         console.log('이김');
        //     }
        // }else if(나의선택==='바위'){
        //     if(컴퓨터의선택(이미지좌표)==='바위'){
        //         console.log('비김');
        //     }else if(컴퓨터의선택(이미지좌표)==='보'){
        //         console.log('짐');
        //     }else{
        //         console.log('이김');
        //     }
        // }else if(나의선택==='보'){
        //     if(컴퓨터의선택(이미지좌표)==='보'){
        //         console.log('비김');
        //     }else if(컴퓨터의선택(이미지좌표)==='가위'){
        //         console.log('짐');
        //     }else{
        //         console.log('이김');
        //     }
        // }

    });
});

//가위바위보 규칙 찾기
//가위:1 / 바위:0 / 보:-1
//  나 | 컴퓨터     가위   바위    보
//            가위 1 1   1 0   1 -1
//            바위 0 1   0 0   0 -1
//            보  -1 1  -1 0  -1 -1
              
            
!
999px

Console