Created
August 6, 2020 14:30
-
-
Save kurtkaiser/7ccb35cfdcd9f631fb014cbd4754cf27 to your computer and use it in GitHub Desktop.
Unity-Blackjack-Game-Tutorial: https://github.com/kurtkaiser/Unity-Blackjack-Game-Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.SocialPlatforms.Impl; | |
using UnityEngine.UI; | |
public class GameManager : MonoBehaviour | |
{ | |
// Game Buttons | |
public Button dealBtn; | |
public Button hitBtn; | |
public Button standBtn; | |
public Button betBtn; | |
private int standClicks = 0; | |
// Access the player and dealer's script | |
public PlayerScript playerScript; | |
public PlayerScript dealerScript; | |
// public Text to access and update - hud | |
public Text scoreText; | |
public Text dealerScoreText; | |
public Text betsText; | |
public Text cashText; | |
public Text mainText; | |
public Text standBtnText; | |
// Card hiding dealer's 2nd card | |
public GameObject hideCard; | |
// How much is bet | |
int pot = 0; | |
void Start() | |
{ | |
// Add on click listeners to the buttons | |
dealBtn.onClick.AddListener(() => DealClicked()); | |
hitBtn.onClick.AddListener(() => HitClicked()); | |
standBtn.onClick.AddListener(() => StandClicked()); | |
betBtn.onClick.AddListener(() => BetClicked()); | |
} | |
private void DealClicked() | |
{ | |
// Reset round, hide text, prep for new hand | |
playerScript.ResetHand(); | |
dealerScript.ResetHand(); | |
// Hide deal hand score at start of deal | |
dealerScoreText.gameObject.SetActive(false); | |
mainText.gameObject.SetActive(false); | |
dealerScoreText.gameObject.SetActive(false); | |
GameObject.Find("Deck").GetComponent<DeckScript>().Shuffle(); | |
playerScript.StartHand(); | |
dealerScript.StartHand(); | |
// Update the scores displayed | |
scoreText.text = "Hand: " + playerScript.handValue.ToString(); | |
dealerScoreText.text = "Hand: " + dealerScript.handValue.ToString(); | |
// Place card back on dealer card, hide card | |
hideCard.GetComponent<Renderer>().enabled = true; | |
// Adjust buttons visibility | |
dealBtn.gameObject.SetActive(false); | |
hitBtn.gameObject.SetActive(true); | |
standBtn.gameObject.SetActive(true); | |
standBtnText.text = "Stand"; | |
// Set standard pot size | |
pot = 40; | |
betsText.text = "Bets: $" + pot.ToString(); | |
playerScript.AdjustMoney(-20); | |
cashText.text = "$" + playerScript.GetMoney().ToString(); | |
} | |
private void HitClicked() | |
{ | |
// Check that there is still room on the table | |
if (playerScript.cardIndex <= 10) | |
{ | |
playerScript.GetCard(); | |
scoreText.text = "Hand: " + playerScript.handValue.ToString(); | |
if (playerScript.handValue > 20) RoundOver(); | |
} | |
} | |
private void StandClicked() | |
{ | |
standClicks++; | |
if (standClicks > 1) RoundOver(); | |
HitDealer(); | |
standBtnText.text = "Call"; | |
} | |
private void HitDealer() | |
{ | |
while (dealerScript.handValue < 16 && dealerScript.cardIndex < 10) | |
{ | |
dealerScript.GetCard(); | |
dealerScoreText.text = "Hand: " + dealerScript.handValue.ToString(); | |
if (dealerScript.handValue > 20) RoundOver(); | |
} | |
} | |
// Check for winnner and loser, hand is over | |
void RoundOver() | |
{ | |
// Booleans (true/false) for bust and blackjack/21 | |
bool playerBust = playerScript.handValue > 21; | |
bool dealerBust = dealerScript.handValue > 21; | |
bool player21 = playerScript.handValue == 21; | |
bool dealer21 = dealerScript.handValue == 21; | |
// If stand has been clicked less than twice, no 21s or busts, quit function | |
if (standClicks < 2 && !playerBust && !dealerBust && !player21 && !dealer21) return; | |
bool roundOver = true; | |
// All bust, bets returned | |
if (playerBust && dealerBust) | |
{ | |
mainText.text = "All Bust: Bets returned"; | |
playerScript.AdjustMoney(pot / 2); | |
} | |
// if player busts, dealer didnt, or if dealer has more points, dealer wins | |
else if (playerBust || (!dealerBust && dealerScript.handValue > playerScript.handValue)) | |
{ | |
mainText.text = "Dealer wins!"; | |
} | |
// if dealer busts, player didnt, or player has more points, player wins | |
else if (dealerBust || playerScript.handValue > dealerScript.handValue) | |
{ | |
mainText.text = "You win!"; | |
playerScript.AdjustMoney(pot); | |
} | |
//Check for tie, return bets | |
else if (playerScript.handValue == dealerScript.handValue) | |
{ | |
mainText.text = "Push: Bets returned"; | |
playerScript.AdjustMoney(pot / 2); | |
} | |
else | |
{ | |
roundOver = false; | |
} | |
// Set ui up for next move / hand / turn | |
if (roundOver) | |
{ | |
hitBtn.gameObject.SetActive(false); | |
standBtn.gameObject.SetActive(false); | |
dealBtn.gameObject.SetActive(true); | |
mainText.gameObject.SetActive(true); | |
dealerScoreText.gameObject.SetActive(true); | |
hideCard.GetComponent<Renderer>().enabled = false; | |
cashText.text = "$" + playerScript.GetMoney().ToString(); | |
standClicks = 0; | |
} | |
} | |
// Add money to pot if bet clicked | |
void BetClicked() | |
{ | |
Text newBet = betBtn.GetComponentInChildren(typeof(Text)) as Text; | |
int intBet = int.Parse(newBet.text.ToString().Remove(0, 1)); | |
playerScript.AdjustMoney(-intBet); | |
cashText.text = "$" + playerScript.GetMoney().ToString(); | |
pot += (intBet * 2); | |
betsText.text = "Bets: $" + pot.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment