IEnumerator gameProcess() { while (true) { switch (GameState.StateOfGame) { case StateOfGame.Betting: { break; } case StateOfGame.DealFirstCardToPlayer: { yield return(StartCoroutine(PlayerController.AddCard(deck.DealCard()))); GameState.StateOfGame = StateOfGame.DealSecondCardToPlayer; break; } case StateOfGame.DealSecondCardToPlayer: { yield return(StartCoroutine(PlayerController.AddCard(deck.DealCard()))); GameState.StateOfGame = StateOfGame.DealTwoCardsToDealer; break; } case StateOfGame.DealTwoCardsToDealer: { yield return(StartCoroutine(DealerController.AddCard(deck.DealCard()))); yield return(StartCoroutine(DealerController.AddCard(deck.DealCard()))); if (IsBlackjack(PlayerController.Player.Hand)) { //blackjack, player wins! GameState = new GameState(StateOfGame.ProcessResult, Result.DealerLoseToBlackjack); } else { GameState.StateOfGame = StateOfGame.WaitForPlayerToAct; } break; } case StateOfGame.WaitForPlayerToAct: { break; } case StateOfGame.WaitForDealerToAct: { Action action = DealerController.DecideAction(PlayerController.Player.Hand); if (action == Action.Hit) { yield return(StartCoroutine(DealerController.AddCard(deck.DealCard()))); if (isBreaking(DealerController.Dealer.Hand)) { GameState = new GameState(StateOfGame.ProcessResult, Result.DealerLose); } } else if (action == Action.Stand) { GameState = new GameState( StateOfGame.ProcessResult, compareHands(DealerController.Dealer.Hand, PlayerController.Player.Hand)); } break; } case StateOfGame.ProcessResult: { Result gameResult = (Result)GameState.Data; if (gameResult == Result.DealerLoseToBlackjack) { Debug.Log("Blackjack"); ResultText.GetComponent <TextMeshProUGUI>().text = "Player Win - Blackjack"; int addCash = 2 * PlayerController.Player.Bet; PlayerController.AddCash(addCash); //PlayerController.Player.SaveCash(); PlayerController.ResetBet(); } else { DealerController.showSecondCard(); //PlayerController.ResetBet(); } if (gameResult == Result.DealerLose) { ResultText.GetComponent <TextMeshProUGUI>().text = "Dealer Lose"; //Debug.Log("Dealer Lose"); int addCash = 2 * PlayerController.Player.Bet; PlayerController.AddCash(addCash); PlayerController.ResetBet(); } else if (gameResult == Result.PlayerLose) { Debug.Log("Player Lose"); ResultText.GetComponent <TextMeshProUGUI>().text = "Player Lose"; PlayerController.AddCash(0); PlayerController.ResetBet(); } else if (gameResult == Result.Tie) { Debug.Log("Tie"); ResultText.GetComponent <TextMeshProUGUI>().text = "Tie"; int betCash = PlayerController.Player.Bet; PlayerController.AddCash(0); PlayerController.BetCash(betCash); } PlayerController.Player.Hand.Clear(); DealerController.Dealer.Hand.Clear(); deck.ShuffleCard(); if (PlayerController.Player.Cash == 0) { GoToMenu(); } else { GameState.StateOfGame = StateOfGame.WaitForRestart; } break; } case StateOfGame.WaitForRestart: { buttonPlayAgain.gameObject.SetActive(true); break; } } yield return(null); } }