const BJ_Path = "pictures/BJ/";
const CN_BJ反応処理 = 809;
let Bj_Deck = []; 
class Trnp {
    constructor(_Mark , _No){
        this.Mark = _Mark;
        this.No = _No;
    }
}
let _TrnpImgGet = function(_Card){
    let _RtnStr = "Card_";
    switch(_Card.Mark){
        case 0:
            _RtnStr += "C_" + _Card.No;
            break;
        case 1:
            _RtnStr += "D_" + _Card.No;
            break;
        case 2:
            _RtnStr += "H_" + _Card.No;
            break;
        case 3:
            _RtnStr += "S_" + _Card.No;
            break;
    }
    return _RtnStr;
}
let Bj_flame = 0;
Game_Interpreter.prototype.BJ_View = function(){
    Bj_Deck = [];
    this.BJ_デッキ補充();
    _BJ_Mode = BJ_Mode.Start;
    _Bj_Statflame = 0;
    this.BJ_Base_View();
}
let BJ_Mode = {
    Start: 0 ,  
    Bet: 1 ,    
    Play: 2 ,   
    Rzlt: 3 ,   
    Rrse : 4,   
    Btl : 5     
};
let _BJ_Mode = BJ_Mode.Start;
let _PlGWinX = 1121; let _PlGWinY = 723;
let _Bj_EmFace = "PN_BjEmFace"; 
let _Bj_EmGold = 0; 
let _Bj_PLGold = 0; 
let _Bj_BetGold = 0; 
let _Bj_Nanido = 0; 
/*
0 → 敵はカードを２枚しかドローしない
1 → カードが17以下の場合かつ、ドローしても21を超えない場合ドロー
2 → 21をオーバーしない範囲で限界までドローをする。
3 → 次に引くカードを操作を行い 必ず19以上になるように引く
（21をオーバーする場合はスキップして、次のカードを引く） 
*/
let VN_BJ敵_所持金 = 382;
let VN_BJ敵_難易度 = 383;
let VN_BJEndPlGold = 384;
let VN_BJ敵_画像 = 385;
Game_Interpreter.prototype.BJ_Base_View = function(){
    _DrawStac = []; 
    Bj_Em難易度処理実行FLg = false;
    PL_Hand = [];
    Em_Hand = [];
    D_Text_Cng_font = "Makinas";
    _Bj_PLGold = $gameParty._gold;
    _Bj_EmGold = $gameVariables.value(VN_BJ敵_所持金); 
    _Bj_Nanido = $gameVariables.value(VN_BJ敵_難易度); 
    _Bj_BetGold = 0;
    this.SetSpriteFIn( PN_BjBase , BJ_Path + "PN_BjBase" , 0 , 0 , 0 , 0 , 15);
    this.SetSpriteFIn( PN_BjTopUi , BJ_Path + "PN_BjTopUi" , 0 , 0 , 0 , 0 , 15);
    _Bj_EmFace = $gameVariables.value(VN_BJ敵_画像);
    if(_Bj_EmFace == 0) _Bj_EmFace = "PN_BjEmFace";
    this.SetSpriteFIn( PN_BjEmFace , BJ_Path + _Bj_EmFace , 34 , 27 , 0 , 0 , 15);
    this.SpriteStrR(PN_BjEmGold , _Bj_EmGold , 25 , 255 , 195 , 0);
    this.MoveSprite(PN_BjEmGold , true , true , 15);
    this.SetSpriteFIn( PN_BjYamaCard , BJ_Path + "PN_BjYamaCard" , -156 , 286 , 0 , 0 , 15);
    this.SetSpriteFInC( PN_BjEmWin , BJ_Path + "PN_BjEmWin" , 672 , 278 - 247 , 0 , 0 , 15);
    this.SetSpriteFIn( PN_BjBetCoinWin , BJ_Path + "PN_BjBetCoinWin" , 378 , 368 , 0 , 0 , 15);
    this.SpriteStrR(PN_BjBetCoinTxt , _Bj_BetGold , 35 , 378 + 318 , 368 + 57 , 0);
    this.MoveSprite(PN_BjBetCoinTxt , true , true , 15);
    _PlGWinX = 1121; _PlGWinY = 723;
    this.SetSpriteFIn( PL_BjPLGoldWin , BJ_Path + "PL_BjPLGoldWin" , _PlGWinX , _PlGWinY , 0 , 0 , 15);
    this.SpriteStrR(PL_BjPLGoldTxt , _Bj_PLGold , 25 , _PlGWinX + 228 , _PlGWinY + 26 , 0);
    this.MoveSprite(PL_BjPLGoldTxt , true , true , 15);
    D_Text_Cng_font = "";
}
let Bj_反応待機Flg = false;
let Bj_反応Str = "";
Game_Interpreter.prototype.BJ_Update = function(){
    if(Bj_反応待機Flg){
        return true; 
    }
    switch(_BJ_Mode){
        case BJ_Mode.Start:
            this.BJ_Start_Update();
            break;
        case BJ_Mode.Bet:
            this.BJ_Bet_Update();
            break;
        case BJ_Mode.Play:
            this.BJ_Play_Update();
            break;
        case BJ_Mode.Rzlt:
            this.BJ_Rzlt_Update();
            break;
        case BJ_Mode.Rrse: 
            this.BJ_Rrse_Update();
            break;
        case BJ_Mode.Btl: 
            this.BJ_Btl_Update();
            break;
        case BJ_Mode.Exit: 
            return false;
    }
    return true; 
}
let _Bj_Statflame = 0;
Game_Interpreter.prototype.BJ_Start_Update = function(){
    if(_Bj_Statflame == 29){
        this.BJ_Cmn処理("開始");
    }
    if(_Bj_Statflame == 30){
        this.BJ_Bet_Cng();
    }
    _Bj_Statflame++;
}
Game_Interpreter.prototype.BJ_Cmn処理 = function(_処理Str){
    Bj_反応Str = _処理Str;
    Bj_反応待機Flg = true;
    this.SetCmnEvent(CN_BJ反応処理);
}
let BjBetBtnX = 326; let BjBetBtnY = 667;
let bj_bet_flame = 0;
Game_Interpreter.prototype.BJ_Bet_Cng = function(){
    bj_bet_flame = 0;
    _BJ_Mode = BJ_Mode.Bet;
    this.BJ_Bet_View();
}
Game_Interpreter.prototype.BJ_Bet_View = function(){
    _DrawStac = []; 
    Bj_Em難易度処理実行FLg = false;
    this.SetSpriteFIn( PN_BjBetWin , BJ_Path + "PN_BjBetWin" , 0 , 0 , 0 , 0 , 15);
    this.SetSpriteFInC(PN_BjBetBtn + 0 , BJ_Path + "PN_BjBetBtn_1" , BjBetBtnX + (352 * 0 * 0.8) , BjBetBtnY , 0 , 0 , 15);
    this.SetSpriteFInC(PN_BjBetBtn + 1 , BJ_Path + "PN_BjBetBtn_2" , BjBetBtnX + (352 * 1 * 0.8) , BjBetBtnY , 0 , 0 , 15);
    this.SetSpriteFInC(PN_BjBetBtn + 2 , BJ_Path + "PN_BjBetBtn_3" , BjBetBtnX + (352 * 2 * 0.8) , BjBetBtnY , 0 , 0 , 15);
    this.SetSpriteFIn(PN_BjAllBetBtn , BJ_Path + "PN_BjAllBetBtn" , 1113 , 544 , 0 , 0 , 15);
    this.SetSpriteFInC(PN_BjExitBtn , BJ_Path + "PN_BjExitBtn" , 1281 , 743 , 0 , 0 , 15);
    this.SetSpriteFInC(PN_BjBetRstBtn , BJ_Path + "PN_BjBetRstBtn" , BjBetBtnX + (352 * 1 * 0.8) , BjBetBtnY + 92 , 0 , 0 , 15);
    this.SetSpriteFIn(PN_BjStartBtn , BJ_Path + "PN_BjStartBtn" , 780 , 395 , 0 , 0 , 15);
    _PlGWinX = 826; _PlGWinY = 529;
    this.MoveSprite(PL_BjPLGoldWin , _PlGWinX , _PlGWinY , 10);
    this.MoveSprite(PL_BjPLGoldTxt , _PlGWinX + 228 , _PlGWinY + 26 , 10 , 255);
}
let Bj_Bet_wait_flame = 0;
Game_Interpreter.prototype.BJ_Bet_Update = function() {
    let _BetBtnArr = [PN_BjBetBtn + 0 , PN_BjBetBtn + 1 , PN_BjBetBtn + 2 , PN_BjAllBetBtn , PN_BjBetRstBtn , PN_BjStartBtn , PN_BjExitBtn];
    this.SpriteCngColorArr(_BetBtnArr);
    if(_Bj_BetGold == 0)  this.SpriteCngColor(PN_BjStartBtn , [-50 , -50 , -50 , 0]);
    let _CNo = overPointerSpCkArr(_BetBtnArr);
    if(_CNo != -1){
        if(_Bj_BetGold == 0 && _CNo == PN_BjStartBtn) return; 
        let _XPsNo = _CNo - PN_BjBetBtn;
        this.SpriteCngColor(_CNo , [100 , 100 , 100,0]);
        if(_CNo == PN_BjBetBtn + 0 || _CNo == PN_BjBetBtn + 1 || _CNo == PN_BjBetBtn + 2){
            this.SetSpriteC(PN_BjBetBtnH , BJ_Path + "PN_BjBetBtnH"  , BjBetBtnX + (352 * _XPsNo * 0.8) , BjBetBtnY);
        } else if(_CNo == PN_BjBetRstBtn) {
            this.SetSpriteC(PN_BjBetBtnH , BJ_Path + "PN_BjBetBtnH_R"  , BjBetBtnX + (352 * 1 * 0.8) , BjBetBtnY + 92);
        } else if(_CNo == PN_BjExitBtn) {
            let _EBtnData = GetSpriteData(PN_BjExitBtn);
            this.SetSpriteC(PN_BjBetBtnH , BJ_Path + "PN_BjBetBtnH"  , _EBtnData._x , _EBtnData._y);
        } else if(_CNo == PN_BjAllBetBtn) {
            this.SetSprite(PN_BjBetBtnH , BJ_Path + "PN_BjAllBetBtnH"  , 1099 , 528);
        }
        if(TouchInput.isTriggered() && Bj_Bet_wait_flame <= 0){
            switch(_CNo) {
                case PN_BjBetBtn + 0:
                    let cost1 = 500;
                    if(_Bj_PLGold >= cost1) {
                        _Bj_BetGold += cost1;
                        _Bj_PLGold -= cost1;
                        this.PlayPtcPic("Gold_Set",675,454);
                        this.PlaySe("Coin");
                    }
                    Bj_Bet_wait_flame = 5;
                    break;
                case PN_BjBetBtn + 1:
                    let cost2 = 5000;
                    if(_Bj_PLGold >= cost2) {
                        _Bj_BetGold += cost2;
                        _Bj_PLGold -= cost2;
                        this.PlayPtcPic("Gold_Set",675,454);
                        this.PlaySe("Coin");
                    }
                    Bj_Bet_wait_flame = 5;
                    break;
                case PN_BjBetBtn + 2:
                    let cost3 = 30000;
                    if(_Bj_PLGold >= cost3) {
                        _Bj_BetGold += cost3;
                        _Bj_PLGold -= cost3;
                        this.PlayPtcPic("Gold_Set",675,454);
                        this.PlaySe("Coin");
                    }
                    Bj_Bet_wait_flame = 5;
                    break;
                case PN_BjAllBetBtn: 
                    _Bj_BetGold += _Bj_PLGold;
                    _Bj_PLGold = 0;
                    this.PlayPtcPic("Gold_Set",1214,568);
                    this.PlaySe("Coin");
                    Bj_Bet_wait_flame = 5;
                    break;
                case PN_BjBetRstBtn: 
                    _Bj_PLGold += _Bj_BetGold;
                    _Bj_BetGold = 0;
                    this.PlaySe("Key");
                    break;
                case PN_BjExitBtn: 
                    this.Bj_End_Cng();
                    this.PlaySe("Key");
                    break;
                case PN_BjStartBtn: 
                    if(_Bj_BetGold == 0) return; 
                    for (let i = PN_BjBetBack; i <= PN_BjStartBtn; i++) {
                        this.MoveSprite(i , true , true , 15 , 0);
                    }
                    _PlGWinX = 1121; _PlGWinY = 723;
                    this.MoveSprite(PL_BjPLGoldWin , _PlGWinX , _PlGWinY , 15);
                    this.MoveSprite(PL_BjPLGoldTxt , _PlGWinX + 228 , _PlGWinY + 26, 15 , 255);
                    this.BJ_Play_Cng();
                    break;
            }
            this.CngSpriteStr(PN_BjBetCoinTxt , _Bj_BetGold);
            if (_Bj_PLGold < 0) _Bj_PLGold = 0;
            this.CngSpriteStr(PL_BjPLGoldTxt , _Bj_PLGold); 
        }
    } else {
        this.DelSprite(PN_BjBetBtnH);
    }
    Bj_Bet_wait_flame--;
}
Game_Interpreter.prototype.Bj_End_Cng = function() {
    _BJ_Mode = BJ_Mode.Exit;
    $gameVariables.setValue(VN_BJEndPlGold ,_Bj_PLGold);
    for (let i = PN_BjBase; i <= PN_BjEmCard + 10; i++) {
        this.MoveSprite(i , true , true , 10 , 0 , true , true);
    }
    for (let i = PN_BjBtlBtn; i <= PN_BjSetuTxt + 1; i++) {
        this.MoveSprite(i , true , true , 10 , 0, true , true);
    }
    this.MoveSprite(PN_BjBetCoinWin, true , true , 10 , 0, true , true);
    this.MoveSprite(PN_BjBetCoinTxt, true , true , 10 , 0, true , true);
    this.DelSpriteSpan(PN_BjBetCoinWin , PN_BjKkLogo);
}
let bj_play_flame = 0;
let PL_Hand = [];
let Em_Hand = [];
Game_Interpreter.prototype.BJ_Play_Cng = function(){
    this.DelSprite(PN_BjEmWinTxt);
    _BJ_Mode = BJ_Mode.Play;
    this.BJ_Play_PLDrawSetting(10);
    this.BJ_Play_PLDrawSetting(10);
    this.BJ_Play_EmDrawSetting(10);
    this.BJ_Play_EmDrawSetting(10);
    bj_play_flame = 0;
}
let _PlayBtn_Arr = [];
Game_Interpreter.prototype.BJ_Play_View = function(){
    this.SetSpriteFIn( PN_BjEndBtn , BJ_Path + "PN_BjEndBtn" , 778 , 351 + (70 * 0) , 0 , 0 , 10 , 255);
    this.SetSpriteFIn( PN_BjDblBtn , BJ_Path + "PN_BjDblBtn" , 778 , 351 + (70 * 1) , 0 , 0 , 10 , 255);
    this.SetSpriteFIn( PN_BjDrawBtn , BJ_Path + "PN_BjDrawBtn" , 778 , 351 + (70 * 2) , 0 , 0 , 10 , 255);
    this.SetSpriteFIn( PN_BjBtlBtn , BJ_Path + "PN_BjBtlBtn" , 481 , 492 , 0 , 0 , 10 , 255);
    _PlayBtn_Arr = [PN_BjDblBtn , PN_BjDrawBtn , PN_BjEndBtn , PN_BjBtlBtn];
}
let Bj_BtlDblFlg = false; 
let Bj_Em難易度処理実行FLg = false;
Game_Interpreter.prototype.BJ_Play_Update = function(){
    this.BJ_Play_UIUpdate(); 
    if(_Drawflame > 0) {
        this.BJ_Play_DrawUpdate();
        return; 
    }
    if(_DrawStac.length > 0){
        _Drawflame = _DrawStac[0].flame;
        this.BJ_Play_DrawMove(_DrawStac[0].PlFlg , _Drawflame);
        _DrawStac.shift();
        return;
    }
    if( this.Enemy_難易度Draw()) return; 
    if(this.Enemy_BtlhandOpn()) return; 
    if(HandScore_Get(PL_Hand) == 21) { 
        this.BJ_Rzlt_Cng(BjKekka.Win);
        Bj_BtlDblFlg = false;
        return;
    }
    if(HandScore_Get(PL_Hand) > 21) { 
        this.BJ_Rzlt_Cng(BjKekka.Over);
        Bj_BtlDblFlg = false;
        return;
    }
    if(Bj_BtlDblFlg) {
        this.BJ_Btl_Cng(); 
        Bj_BtlDblFlg = false;
        return;
    }
    if(bj_play_flame == 5) {
        this.BJ_Play_View();
    }
    this.SpriteCngColorArr(_PlayBtn_Arr);
    let _CNo = overPointerSpCkArr(_PlayBtn_Arr);
    if(_CNo != -1) {
        this.SpriteCngColor( _CNo , [100,100,100,0]);
        let _SpData = GetSpriteData(_CNo);
        let _SetuArr = [];
        switch(_CNo){
            case PN_BjDblBtn: 
                _SetuArr = ["掛け金を倍にしてカードを１枚引く" , "その後、即勝負へ移行する"];
                this.BJ_Play_SetuWin(_SpData._x + 116 , _SpData._y , _SetuArr);
                if(TouchInput.isTriggered()) {
                    _Bj_PLGold -= _Bj_BetGold;
                    if (_Bj_PLGold < 0) _Bj_PLGold = 0;
                    _Bj_BetGold *= 2;
                    this.CngSpriteStr(PN_BjBetCoinTxt , _Bj_BetGold);
                    this.CngSpriteStr(PL_BjPLGoldTxt , _Bj_PLGold); 
                    this.BJ_Play_PLDrawSetting(10);
                    Bj_BtlDblFlg = true;
                    this.BJ_Cmn処理("ダブル");
                }
                break;
            case PN_BjDrawBtn: 
                _SetuArr = ["カードを１枚引く", ""];
                this.BJ_Play_SetuWin(_SpData._x + 116 , _SpData._y , _SetuArr);
                if(TouchInput.isTriggered()) {
                    this.BJ_Play_PLDrawSetting(10);
                    this.BJ_Cmn処理("ドロー");
                }
                break;
            case PN_BjEndBtn: 
                _SetuArr = ["この勝負を降ります", "※掛け金の半分が戻ってきます"];
                this.BJ_Play_SetuWin(_SpData._x + 116 , _SpData._y , _SetuArr);
                if(TouchInput.isTriggered()) {
                    this.BJ_Rzlt_Cng(BjKekka.Surrender);
                }
                break;
            case PN_BjBtlBtn: 
                _SetuArr = ["相手と勝負を行う。", "※21に近い方が勝利"];
                this.BJ_Play_SetuWin(_SpData._x + 116 , _SpData._y , _SetuArr);
                if(TouchInput.isTriggered()) {
                    this.BJ_Btl_Cng();
                    this.BJ_Cmn処理("勝負");
                }
                break;
        }
    } else {
        this.DelSpriteSpan(PN_BjSetuWin , PN_BjSetuTxt + 1);
    }
    bj_play_flame++;
}
Game_Interpreter.prototype.Enemy_難易度Draw = function() {
    if(Bj_Em難易度処理実行FLg) return false;
    let _Ck_Hand = null;
    let _Loopi = 0;
    switch(_Bj_Nanido){
        case 1: 
            if(HandScore_Get(Em_Hand) < 17){
                _Ck_Hand = Em_Hand.slice();
                _Ck_Hand.push(Bj_Deck[0]);
                if(HandScore_Get(_Ck_Hand) <= 21){
                    this.BJ_Play_EmDrawSetting(10);
                }
            }
            break;
        case 2: 
            _Ck_Hand = Em_Hand.slice();
            _Loopi = 0;
            while(true){
                _Ck_Hand.push(Bj_Deck[_Loopi]);
                if(HandScore_Get(_Ck_Hand) <= 21){
                    this.BJ_Play_EmDrawSetting(10);
                    _Loopi++;
                } else {
                    break; 
                }
            }
            break;
        case 3: 
            _Ck_Hand = Em_Hand.slice();
            _Loopi = 0;
            if(HandScore_Get(_Ck_Hand) >= 19) break;
            while(true){
                _Ck_Hand.push(Bj_Deck[_Loopi]);
                if(HandScore_Get(_Ck_Hand) <= 21) {
                    this.BJ_Play_EmDrawSetting(10);
                    _Loopi++;
                } else {
                    break; 
                }
            }
            _Ck_Hand.pop(); 
            if(HandScore_Get(_Ck_Hand) < 19){ 
                while(true){
                    _Ck_Hand.push(Bj_Deck[_Loopi]);
                    if(HandScore_Get(_Ck_Hand) <= 21 && HandScore_Get(_Ck_Hand) >= 19){
                        this.BJ_Play_EmDrawSetting(10);
                        break;
                    } else {
                        _Ck_Hand.pop(); 
                        Bj_Deck.splice(_Loopi , 1);
                        if(Bj_Deck.length == 0) this.BJ_デッキ補充();
                    }
                }
            }
            break;
    }
    Bj_Em難易度処理実行FLg = true;
    Bj_EmHandOpenFlg = false;
    _bj_Btl_OpnCnt = 0;
    return true;
}
let Bj_EmHandOpenFlg = false;
Game_Interpreter.prototype.Enemy_BtlhandOpn = function() {
    if(_bj_Btl_OpnCnt >= (Em_Hand.length / 2)) return false;
    if(bj_Btl_flame % 20 == 0) { 
        EasingStr = "easeInOutQuad";
        this.MoveSprite(PN_BjEmCard + _bj_Btl_OpnCnt , true , true , 15 , 255 , 0 , 65);
        EasingStr = "";
        this.PlaySe("card");
    }
    if(bj_Btl_flame % 20 == 10) { 
        EasingStr = "easeInOutQuad";
        let _Img = _TrnpImgGet(Em_Hand[_bj_Btl_OpnCnt]);
        this.CngSprite(PN_BjEmCard + _bj_Btl_OpnCnt , BJ_Path + _Img);
        this.MoveSprite(PN_BjEmCard + _bj_Btl_OpnCnt , true , true , 15 , 255 , 65 , 65);
        EasingStr = "";
        _bj_Btl_OpnCnt++;
        if(_bj_Btl_OpnCnt >= (Em_Hand.length / 2)){
            let Opn_Hand = [];
            for (let i = 0; i <= _bj_Btl_OpnCnt - 1; i++) {
                Opn_Hand.push(Em_Hand[i]);
            }
            this.SpriteStrC(PN_BjEmWinTxt , HandScore_Get(Opn_Hand) + " ＋？" , 30 , 672 , 278 - 247 );
        }
    }
    bj_Btl_flame++;
    return true;
}
Game_Interpreter.prototype.BJ_Play_SetuWin = function(_PsX , _PsY , _SetuArr) {
    this.SetSprite( PN_BjSetuWin , BJ_Path + "PN_BjSetuWin" , _PsX - 46 , _PsY - 112);
    for (let i = 0; i <= _SetuArr.length - 1; i++) {
        this.SpriteStr(PN_BjSetuTxt + i , _SetuArr[i] , 20 , _PsX - 46 + 18 , _PsY - 112 + 22 + (i * 30));
    }
}
Game_Interpreter.prototype.BJ_Play_UIUpdate = function() {
    let _Score = HandScore_Get(PL_Hand);
    this.SpriteStrC(PN_BjCntTxt , _Score , 68 , 128 , 708 , 255);
}
let HandScore_Get = function(_hndArr){
    let _Cnt = 0;
    let _ACnt = 0;
    for (let i = 0; i <= _hndArr.length - 1; i++) {
        if(_hndArr[i].No != 1) {
            let _AddCnt = _hndArr[i].No;
            if(_AddCnt > 10) _AddCnt = 10;
            _Cnt += _AddCnt;
        } else {
            _ACnt++; 
        }
    }
    for (let i = 0; i <= _ACnt - 1; i++) {
        if(_Cnt + 11 + _ACnt - i - 1 <= 21){
            _Cnt += 11;
        } else {
            _Cnt += 1;
        }
    }
    return _Cnt;
}
let _Drawflame = 0; 
let _DrawPlFlg = false;
let _DrawStac = [];
class BJ_DrawStac {
    constructor(_PlFlg , _flame){
        this.PlFlg = _PlFlg;
        this.flame = _flame;
    }
}
Game_Interpreter.prototype.BJ_Play_DrawUpdate = function(){
    _Drawflame--;
}
Game_Interpreter.prototype.BJ_Play_DrawMove = function(_PlFlg , _flame){
    let _Card = Bj_Deck[0];
    Bj_Deck.shift(); 
    this.PlaySe("card");
    if(_PlFlg) {
        PL_Hand.push(_Card);
        let _PicNo = PL_Hand.length - 1;
        let _CardImg = _TrnpImgGet(_Card);
        let _CardZure = 750 / PL_Hand.length;
        if(_CardZure > 160) _CardZure = 160;
        this.SetSpriteC(PN_BjPLCard + _PicNo , BJ_Path + _CardImg , 167 , 426);
        for (let i = 0; i <= PL_Hand.length - 1; i++) {
            this.MoveSprite(PN_BjPLCard + i ,370 + (_CardZure * i) , 715 , _flame , 255 , 75 , 75);
        }
        this.SetSprite( PN_BjYamaCard , BJ_Path + "PN_BjYamaCard" , -156 - 17 , 286);
        this.MoveSpriteAdd( PN_BjYamaCard , 17 , 0 , 10);
    } else {
        Em_Hand.push(_Card);
        let _PicNo = Em_Hand.length - 1;
        this.SetSpriteC(PN_BjEmCard  + _PicNo , BJ_Path + "Card_Back_1" , 167 , 426);
        let _CardZure = 560 / Em_Hand.length;
        if(_CardZure > 140) _CardZure = 140;
        for (let i = 0; i <= Em_Hand.length - 1; i++) {
            this.MoveSprite(PN_BjEmCard  + i , 444 + (_CardZure * i) , 155 , _flame , 255 , 65 , 65);
        }
        this.SetSprite( PN_BjYamaCard , BJ_Path + "PN_BjYamaCard" , -156 - 17 , 286);
        this.MoveSpriteAdd( PN_BjYamaCard , 17 , 0 , 10);
    }
    if(Bj_Deck.length == 0) {
        this.BJ_デッキ補充();
    }
}
Game_Interpreter.prototype.BJ_デッキ補充 = function(){
    let _Counter = 10; 
    Bj_Deck = [];
    while(true) {
        if(_Counter == 0) break;
        _Counter--;
        let _SettingArr = []
        for (let i = 1; i <= 13; i++) {
            _SettingArr.push(new Trnp(0 , i));
            _SettingArr.push(new Trnp(1 , i));
            _SettingArr.push(new Trnp(2 , i));
            _SettingArr.push(new Trnp(3 , i));
        }
        ShuffleArr(_SettingArr); 
        for (let i = 0; i <= _SettingArr.length - 1; i++) {
            Bj_Deck.push(_SettingArr[i]);
        }
    }
}
Game_Interpreter.prototype.BJ_Play_PLDrawSetting = function(_flame) {
    _DrawStac.push(new BJ_DrawStac(true,_flame));
}
Game_Interpreter.prototype.BJ_Play_EmDrawSetting = function(_flame) {
    _DrawStac.push(new BJ_DrawStac(false,_flame));
}
let bj_Btl_flame = 0;
let _bj_Btl_OpnCnt = 0; 
Game_Interpreter.prototype.BJ_Btl_Cng = function(){
    _BJ_Mode = BJ_Mode.Btl;
    bj_Btl_flame = 0;
}
Game_Interpreter.prototype.BJ_Btl_View = function() {
}
Game_Interpreter.prototype.BJ_Btl_Update = function() {
    if(Em_Hand.length == _bj_Btl_OpnCnt){ 
        this.SpriteStrC(PN_BjEmWinTxt , HandScore_Get(Em_Hand) , 30 , 672 , 278 - 247 );
        if(HandScore_Get(PL_Hand) >= HandScore_Get(Em_Hand)){
            this.BJ_Rzlt_Cng(BjKekka.Win);
        } else {
            this.BJ_Rzlt_Cng(BjKekka.Lose);
        }
        return;
    }
    if(bj_Btl_flame % 20 == 0) { 
        EasingStr = "easeInOutQuad";
        this.MoveSprite(PN_BjEmCard + _bj_Btl_OpnCnt , true , true , 15 , 255 , 0 , 65);
        EasingStr = "";
        this.PlaySe("card");
    }
    if(bj_Btl_flame % 20 == 10) { 
        EasingStr = "easeInOutQuad";
        let _Img = _TrnpImgGet(Em_Hand[_bj_Btl_OpnCnt]);
        this.CngSprite(PN_BjEmCard + _bj_Btl_OpnCnt , BJ_Path + _Img);
        this.MoveSprite(PN_BjEmCard + _bj_Btl_OpnCnt , true , true , 15 , 255 , 65 , 65);
        EasingStr = "";
        _bj_Btl_OpnCnt++;
    }
    bj_Btl_flame++;
}
let BjKekka = {
    Win:1,
    Lose:2,
    Over:3,
    Surrender:4
};
let _BjKekka;
let bj_Rzlt_flame = 0;
let bj_Rzlt_Endflame = 0;
let bj_WinFlg = false;
Game_Interpreter.prototype.BJ_Rzlt_Cng = function(_Kekka){
    this.DelSpriteSpan(PN_BjSetuWin , PN_BjSetuTxt + 1); 
    this.DelSpriteSpan(PN_BjBtlBtn , PN_BjEndBtn); 
    this.SetSpriteFIn( PN_BjKkBlack , "Black" , 0 , 0 , 0 , 0 , 15 , 100);
    EasingStr = "easeOutBack";
    this.SetSpriteC(PN_BjKkLine ,  BJ_Path + "PN_BjKkLine", 720 , 405 , 255 , 100 , 0);
    this.MoveSprite(PN_BjKkLine , true , true , 15);
    EasingStr = "";
    _BJ_Mode = BJ_Mode.Rzlt;
    _BjKekka = _Kekka;
    bj_Rzlt_flame = bj_Rzlt_Endflame = 0;
}
Game_Interpreter.prototype.BJ_Rzlt_View = function() {
}
let Bj_BlackJuckFlg = false;
let Bj_OverFlg = false;
let _bj_Em0Endflame = 0;
Game_Interpreter.prototype.BJ_Rzlt_Update = function() {
    if(bj_Rzlt_Endflame > 0){
        _Bj_BetGold = 0;
        this.SpriteStrR(PN_BjBetCoinTxt , _Bj_BetGold , 35 , 378 + 318 , 368 + 57);
        if (_Bj_PLGold < 0) _Bj_PLGold = 0;
        this.SpriteStrR(PL_BjPLGoldTxt , _Bj_PLGold , 25 , _PlGWinX + 228 , _PlGWinY + 26);
        bj_Rzlt_Endflame--;
        if(bj_Rzlt_Endflame == 0){
            if(_Bj_PLGold <= 0){
                if(!ClickCkFlg){
                    this.ClickCk_WinView(["プレイヤーの所持金が無くなりました…" , "ブラックジャックを終了します。"] , 1);
                    _bj_Em0Endflame = 0;
                }
            } else {
                if(_Bj_EmGold > 0) {
                    this.BJ_Rrse_Cng(); 
                } else {
                    if(!ClickCkFlg){
                        this.ClickCk_WinView(["敵の残金が0になりました。" , "ブラックジャックを終了します。"] , 1);
                        _bj_Em0Endflame = 0;
                    }
                }
            }
        }
        return;
    }
    if(ClickCkFlg){ 
        _bj_Em0Endflame++;
        if(_bj_Em0Endflame < 30) return;
        if (TouchInput.isTriggered()){ 
            this.ClickCk_WinDel(); 
            this.Bj_End_Cng();
            this.PlaySe("Key");
        }
        return;
    }
    if(bj_Rzlt_flame == 20) {
        switch(_BjKekka) {
            case BjKekka.Win:
                Bj_BlackJuckFlg = false;
                if(HandScore_Get(PL_Hand) == 21){
                    this.PlayPtcPic("BJWin",690,300);
                    this.SetSpriteC(PN_BjKkLogo ,  BJ_Path + "PN_BjKk_Bj", 690 , 300 , 255 , 100 , 0);
                    Bj_BlackJuckFlg = true;
                } else {
                    this.PlaySe("itemgetbgm");
                    this.SetSpriteC(PN_BjKkLogo ,  BJ_Path + "PN_BjKk_Win", 690 , 300 , 255 , 100 , 0);
                }
                break;
            case BjKekka.Lose:
                Bj_OverFlg = false;
                this.PlaySe("Down2");
                this.SetSpriteC(PN_BjKkLogo ,  BJ_Path + "PN_BjKkLose", 690 , 300 , 255 , 100 , 0);
                break;
            case BjKekka.Over:
                Bj_OverFlg = true;
                this.PlaySe("Down2");
                this.SetSpriteC(PN_BjKkLogo ,  BJ_Path + "PN_BjKkOver", 690 , 300 , 255 , 100 , 0);
                break;
            case BjKekka.Surrender:
                this.PlaySe("Down2");
                this.SetSpriteC(PN_BjKkLogo ,  BJ_Path + "PN_BjKkSurrender", 690 , 300 , 255 , 100 , 0);
                break;
        }
        EasingStr = "easeOutBack";
        this.MoveSprite(PN_BjKkLogo , true , true , 15);
        EasingStr = "";
    }
    if(bj_Rzlt_flame == 35){
        switch(_BjKekka){
            case BjKekka.Win:
                this.SetSpriteFInC( PN_BjKkGoldIcon , BJ_Path + "PN_BjKkGoldIcon" , 505 , 430 , 0 , 0 , 10);
                let _getCoin = _Bj_BetGold; 
                if(_Bj_EmGold < _Bj_BetGold) {
                    _Bj_BetGold = _Bj_EmGold; 
                }
                _Bj_EmGold -= _Bj_BetGold;
                _getCoin += _Bj_BetGold;
                _Bj_PLGold += _getCoin;
                this.SpriteStrR(PN_BjKkGoldTxt , SCol.Yel +  _getCoin + "G 取得" , 47 , 892 , 403 , 255 , 100 , 0);
                break;
            case BjKekka.Lose:
                _Bj_EmGold += _Bj_BetGold;
                this.SpriteStrC(PN_BjKkGoldTxt , "勝負に負けました…" , 42 , 684 , 440 , 255 , 100 , 0);
                break;
            case BjKekka.Over:
                _Bj_EmGold += _Bj_BetGold;
                this.SpriteStrC(PN_BjKkGoldTxt , "オーバーしました…" , 42 , 684 , 440 , 255 , 100 , 0);
                break;
            case BjKekka.Surrender:
                let _lostGold = Math.floor(_Bj_BetGold / 2);
                _Bj_EmGold += _lostGold;
                _Bj_PLGold += _lostGold;
                this.SpriteStrC(PN_BjKkGoldTxt , "勝負を降りました※掛け金の半分が戻ってきました" , 42 , 684 , 440 , 255 , 100 , 0);
                break;
        }
        $gameVariables.setValue(VN_BJ敵_所持金 ,_Bj_EmGold);
        $gameParty._gold = _Bj_PLGold;
        this.CngSpriteStr(PN_BjEmGold , _Bj_EmGold);
        if (_Bj_PLGold < 0) _Bj_PLGold = 0;
        this.CngSpriteStr(PL_BjPLGoldTxt , _Bj_PLGold); 
        EasingStr = "easeOutBack";
        this.MoveSprite(PN_BjKkGoldTxt , true , true , 15);
        EasingStr = "";
    }
    if( bj_Rzlt_flame == 55) {
        switch(_BjKekka){
            case BjKekka.Win:
                this.BJ_Cmn処理("勝利");
                break;
            case BjKekka.Lose:
            case BjKekka.Over:
                this.BJ_Cmn処理("敗北");
                break;
            case BjKekka.Surrender:
                this.BJ_Cmn処理("諦める");
                break;
        }
    }
    if( bj_Rzlt_flame > 60) {
        if(TouchInput.isTriggered()){
            for (let i = PN_BjKkBlack; i <= PN_BjKkLogo; i++) {
                this.MoveSprite(i , true, true , 30 , 0);
                bj_Rzlt_Endflame = 30;
            }
        }
    }
    bj_Rzlt_flame++;
}
let bj_Rrse_flame = 0;
Game_Interpreter.prototype.BJ_Rrse_Cng = function(){
    _BJ_Mode = BJ_Mode.Rrse;
    bj_Rrse_flame = 0;
}
Game_Interpreter.prototype.BJ_Rrse_View = function(){
}
Game_Interpreter.prototype.BJ_Rrse_Update = function() {
    bj_Rrse_flame++;
    if(bj_Rrse_flame % 10 == 9) {
        if(PL_Hand.length > 0) {
            let _PicNo = PL_Hand.length - 1;
            this.MoveSprite(PN_BjPLCard + _PicNo , 1785 , 384 , 10 , 255 , 65 , 65);
            PL_Hand.pop();
        }
        if(Em_Hand.length > 0) {
            let _PicNo = Em_Hand.length - 1;
            this.MoveSprite(PN_BjEmCard + _PicNo , 1785 , 384 , 10 , 255 , 65 , 65);
            Em_Hand.pop();
        }
        this.PlaySe("card");
    }
    if(PL_Hand.length == 0 && Em_Hand.length == 0) {
        if(Bj_Deck.length == 0) this.BJ_デッキ補充();
        this.BJ_Bet_Cng();
    }
}
