---
# System prepended metadata

title: Js 練習｜剪刀石頭布規則（流程圖 / if）
tags: [JavaScript]

---

# Js 練習｜剪刀石頭布規則（流程圖 / if）  

###### tags: `JavaScript`

## 程式目的：判斷A玩家勝負狀態

* ### 流程圖：
    https://whimsical.com/10-08-paper-scissor-stone-QWPx4sqXJGVW6xr5s2dVwi

    >流程概念：
    >1. 判斷 playerAB 是否出相同的拳，是則平手，否則進入 2&3 判斷
    >2. 確認 playerA 出什麼拳
    >3. 確認 playerB 出什麼拳
    >4. 判斷 playerA 勝負


    ![](https://i.imgur.com/OjgOxWp.png)
    

---

* ### 程式碼

```javascript=
// 程式目的：make sure playerA status

// declare variable
let playerA = 'paper';
let playerB = 'paper';

// 測試區
playerA = 'paper';
playerB = 'stone';

// 程式開始

// 判斷是否平手
if(playerA==playerB){
    console.log('平手');
}
// 不是平手，check playerA 出什麼拳
// check playerA 是否出剪刀
else if(playerA=='scissor'){
    console.log('playerA 出剪刀');
    
    // playerA 出布，check playerB 是否出布
    if(playerB=='papre'){
        console.log('playerB 出布');
        console.log('PlayerA win')
    }else{
        console.log('playerB 出石頭');
        console.log('PlayerA lose')
    }
    
// check playerA 是否出石頭
}else if(playerA=='stone'){
    console.log('playerA 出石頭');
    
    // playerA 出布，check playerB 是否出剪刀
    if(playerB=='sicccor'){
        console.log('playerB 出剪刀');
        console.log('PlayerA win')
    }else{
        console.log('playerB 出布');
        console.log('PlayerA lose')
    }

// check playerA 是否出布
}else if(playerA=='paper'){
    console.log('playerA 出布');
        // playerA 出石頭，check playerB 是否出布
        if(playerB=='stone'){
            console.log('playerB 出石頭');
            console.log('PlayerA win')
        }else{
            console.log('playerB 出剪刀');
            console.log('PlayerA lose')
        }
}else{
    console.log('有人亂出，無法判斷');
}

// 程式結束
```