2026-06-19 UI, UI로직
This commit is contained in:
199
Assets/My project/Blackjeck Scripts/UI/BlackjackUITestDriver.cs
Normal file
199
Assets/My project/Blackjeck Scripts/UI/BlackjackUITestDriver.cs
Normal file
@@ -0,0 +1,199 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class BlackjackGameManager : MonoBehaviour
|
||||
{
|
||||
[Header("UI")]
|
||||
[SerializeField] private UIController ui;
|
||||
[SerializeField] private GameObject blackjackCanvas;
|
||||
|
||||
[Header("Game Settings")]
|
||||
[SerializeField] private int maxCardCount = 4;
|
||||
[SerializeField] private int startCardCount = 2;
|
||||
|
||||
[Header("Score Settings")]
|
||||
[SerializeField] private int blackjackScore = 21;
|
||||
[SerializeField] private int successScore = 18;
|
||||
[SerializeField] private int normalScore = 15;
|
||||
|
||||
[Header("Events")]
|
||||
[SerializeField] private UnityEvent onSuccess;
|
||||
[SerializeField] private UnityEvent onFail;
|
||||
[SerializeField] private UnityEvent onExit;
|
||||
|
||||
private readonly List<int> playerCards = new List<int>();
|
||||
private int currentScore;
|
||||
private bool isGameOver;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
StartGame();
|
||||
}
|
||||
|
||||
public void StartGame()
|
||||
{
|
||||
playerCards.Clear();
|
||||
currentScore = 0;
|
||||
isGameOver = false;
|
||||
|
||||
if (ui != null)
|
||||
{
|
||||
ui.ClearUI();
|
||||
ui.SetTurn("Player Turn");
|
||||
}
|
||||
|
||||
for (int i = 0; i < startCardCount; i++)
|
||||
{
|
||||
DrawCard();
|
||||
}
|
||||
|
||||
CheckAutoResult();
|
||||
}
|
||||
|
||||
public void OnHitClicked()
|
||||
{
|
||||
if (isGameOver)
|
||||
return;
|
||||
|
||||
if (playerCards.Count >= maxCardCount)
|
||||
{
|
||||
FinishByStand();
|
||||
return;
|
||||
}
|
||||
|
||||
DrawCard();
|
||||
CheckAutoResult();
|
||||
}
|
||||
|
||||
public void OnStandClicked()
|
||||
{
|
||||
if (isGameOver)
|
||||
return;
|
||||
|
||||
FinishByStand();
|
||||
}
|
||||
|
||||
public void OnRestartClicked()
|
||||
{
|
||||
StartGame();
|
||||
}
|
||||
|
||||
public void OnExitClicked()
|
||||
{
|
||||
if (blackjackCanvas != null)
|
||||
blackjackCanvas.SetActive(false);
|
||||
|
||||
onExit?.Invoke();
|
||||
}
|
||||
|
||||
private void DrawCard()
|
||||
{
|
||||
int cardValue = Random.Range(1, 11);
|
||||
|
||||
playerCards.Add(cardValue);
|
||||
currentScore += cardValue;
|
||||
|
||||
if (ui != null)
|
||||
{
|
||||
ui.SetCardsByValues(playerCards);
|
||||
ui.SetScore(currentScore);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckAutoResult()
|
||||
{
|
||||
if (currentScore > blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BUST!",
|
||||
$"현재 점수 : {currentScore}\n21을 초과했습니다.",
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore == blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BLACKJACK!",
|
||||
$"현재 점수 : {currentScore}\n정확히 21입니다!\n보상 획득!",
|
||||
true
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (playerCards.Count >= maxCardCount)
|
||||
{
|
||||
FinishByStand();
|
||||
}
|
||||
}
|
||||
|
||||
private void FinishByStand()
|
||||
{
|
||||
if (currentScore > blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BUST!",
|
||||
$"최종 점수 : {currentScore}\n21을 초과했습니다.",
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore == blackjackScore)
|
||||
{
|
||||
FinishGame(
|
||||
"BLACKJACK!",
|
||||
$"최종 점수 : {currentScore}\n완벽합니다!\n보상 획득!",
|
||||
true
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore >= successScore)
|
||||
{
|
||||
FinishGame(
|
||||
"성공!",
|
||||
$"최종 점수 : {currentScore}\n21에 아주 가깝습니다.\n보상 획득!",
|
||||
true
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentScore >= normalScore)
|
||||
{
|
||||
FinishGame(
|
||||
"보통",
|
||||
$"최종 점수 : {currentScore}\n나쁘지 않은 선택입니다.",
|
||||
false
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
FinishGame(
|
||||
"실패",
|
||||
$"최종 점수 : {currentScore}\n점수가 너무 낮습니다.",
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private void FinishGame(string title, string description, bool isSuccess)
|
||||
{
|
||||
isGameOver = true;
|
||||
|
||||
if (ui != null)
|
||||
{
|
||||
ui.ShowResult(title, description);
|
||||
}
|
||||
|
||||
if (isSuccess)
|
||||
{
|
||||
onSuccess?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
onFail?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user