Commit d8aa28f4 authored by Michael Schuh's avatar Michael Schuh
Browse files

added blackjack script

parent 4f3ab0f4
......@@ -256,7 +256,7 @@ MeshRenderer:
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 1479287733}
- {fileID: 587659393}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
......@@ -483,7 +483,7 @@ MeshRenderer:
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 1651234971}
- {fileID: 1296341143}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
......@@ -662,35 +662,6 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 339970369}
m_PrefabAsset: {fileID: 0}
--- !u!21 &347408063
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: '2HMaterial
-1796'
m_Shader: {fileID: 10752, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 5
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: 74e0a39f5c3e446caddbf5015e61ef1e, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
m_Colors:
- _Color: {r: 0.82089555, g: 0.82089555, b: 0.82089555, a: 1}
m_BuildTextureStacks: []
--- !u!1 &422218013
GameObject:
m_ObjectHideFlags: 0
......@@ -976,7 +947,7 @@ MeshFilter:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 515669484}
m_Mesh: {fileID: 1672811163}
m_Mesh: {fileID: 1993527992}
--- !u!23 &515669486
MeshRenderer:
m_ObjectHideFlags: 0
......@@ -996,7 +967,7 @@ MeshRenderer:
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 347408063}
- {fileID: 1462234098}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
......@@ -1085,6 +1056,35 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!21 &587659393
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: '2CMaterial
19270'
m_Shader: {fileID: 10752, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 5
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: 8e420b5f1cf94adca921e49bf5ee1974, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
m_Colors:
- _Color: {r: 0.82089555, g: 0.82089555, b: 0.82089555, a: 1}
m_BuildTextureStacks: []
--- !u!1 &891578323
GameObject:
m_ObjectHideFlags: 0
......@@ -1099,6 +1099,7 @@ GameObject:
- component: {fileID: 891578325}
- component: {fileID: 891578324}
- component: {fileID: 891578329}
- component: {fileID: 891578330}
m_Layer: 0
m_Name: ARCamera
m_TagString: MainCamera
......@@ -1209,6 +1210,18 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 7b169c4cbb27ce01e84a33fdf4ecc0f5, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &891578330
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 891578323}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1f15026a1fe99426b89a78d6a4c6062d, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1001 &971602804
PrefabInstance:
m_ObjectHideFlags: 0
......@@ -1396,6 +1409,16 @@ PrefabInstance:
propertyPath: m_RootOrder
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 7740e9249c838b68eb69da975c3e26ce,
type: 3}
propertyPath: m_LocalScale.x
value: 0.8
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 7740e9249c838b68eb69da975c3e26ce,
type: 3}
propertyPath: m_LocalScale.y
value: 0.8
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 7740e9249c838b68eb69da975c3e26ce,
type: 3}
propertyPath: m_LocalPosition.x
......@@ -1623,6 +1646,35 @@ Mesh:
offset: 0
size: 0
path:
--- !u!21 &1296341143
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: '2DMaterial
19290'
m_Shader: {fileID: 10752, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 5
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: a542312610044cbcbcf7fd6b74330e75, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
m_Colors:
- _Color: {r: 0.82089555, g: 0.82089555, b: 0.82089555, a: 1}
m_BuildTextureStacks: []
--- !u!1001 &1366458429
PrefabInstance:
m_ObjectHideFlags: 0
......@@ -1793,16 +1845,16 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 1439585485}
m_PrefabAsset: {fileID: 0}
--- !u!21 &1479287733
--- !u!21 &1462234098
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: '2CMaterial
m_Name: '2HMaterial
-2322'
19316'
m_Shader: {fileID: 10752, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 5
......@@ -1815,7 +1867,7 @@ Material:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: 8e420b5f1cf94adca921e49bf5ee1974, type: 3}
m_Texture: {fileID: 2800000, guid: 74e0a39f5c3e446caddbf5015e61ef1e, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
......@@ -1986,16 +2038,16 @@ Mesh:
offset: 0
size: 0
path:
--- !u!21 &1651234971
--- !u!21 &1715041455
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: '2DMaterial
m_Name: '2SMaterial
-1952'
19374'
m_Shader: {fileID: 10752, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 5
......@@ -2008,20 +2060,20 @@ Material:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: a542312610044cbcbcf7fd6b74330e75, type: 3}
m_Texture: {fileID: 2800000, guid: 3b840f7508034a3396b5efaf083ac8cd, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
m_Colors:
- _Color: {r: 0.82089555, g: 0.82089555, b: 0.82089555, a: 1}
m_BuildTextureStacks: []
--- !u!43 &1672811163
--- !u!43 &1993527992
Mesh:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: ImageTargetMesh-1792
m_Name: ImageTargetMesh19306
serializedVersion: 10
m_SubMeshes:
- serializedVersion: 2
......@@ -2166,8 +2218,8 @@ Mesh:
m_BitSize: 0
m_UVInfo: 0
m_LocalAABB:
m_Center: {x: -0, y: 0, z: -0}
m_Extent: {x: 0, y: 0, z: 0}
m_Center: {x: 0, y: 0, z: 0}
m_Extent: {x: 0.03, y: 0, z: 0.0458465}
m_MeshUsageFlags: 0
m_BakedConvexCollisionMesh:
m_BakedTriangleCollisionMesh:
......@@ -2179,35 +2231,6 @@ Mesh:
offset: 0
size: 0
path:
--- !u!21 &1846456803
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: '2SMaterial
-2544'
m_Shader: {fileID: 10752, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords:
m_LightmapFlags: 5
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: 3b840f7508034a3396b5efaf083ac8cd, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats: []
m_Colors:
- _Color: {r: 0.82089555, g: 0.82089555, b: 0.82089555, a: 1}
m_BuildTextureStacks: []
--- !u!1 &2050270653
GameObject:
m_ObjectHideFlags: 0
......@@ -2256,7 +2279,7 @@ MeshRenderer:
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 1846456803}
- {fileID: 1715041455}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
......
fileFormatVersion: 2
guid: 14322d9f5d877db9fa5dc961f66f6daf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlackJack : MonoBehaviour
{
List<Card>[] hands = new List<Card>[0];
List<Card> dealerHand = new List<Card>();
List<Card> deck = new List<Card>();
List<Card> discardPile = new List<Card>();
int viewWidth;
int viewHeight;
int currentPlayer;
int[] playerStates;
int playerCount;
int totalCards;
float sw = 0;
float sh = 0;
bool gameOver = false;
Dictionary<int, string> cardNames = new Dictionary<int, string>() {
{1, "A"}, {2, "2"}, {3,"3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"}, {8, "8"}, {9, "9"}, {10, "10"}, {11, "J"}, {12, "Q"}, {13, "K"}
};
Dictionary<int, string> states = new Dictionary<int, string>() {
{0, ""}, {1, "Win"}, {-1, "Lose"}, {-2, "Bust"}
};
// Start is called before the first frame update
void Start()
{
Screen.orientation = ScreenOrientation.LandscapeLeft;
viewWidth = GetComponent<Camera>().pixelWidth;
viewHeight = GetComponent<Camera>().pixelHeight;
sw = viewWidth/100;
sh = viewHeight/100;
Reset(1);
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
DrawButtons();
DrawDealerStats(dealerHand, 25);
DrawAllPlyStats(hands);
DrawStats();
}
void DrawButtons() {
if (GUI.Button(new Rect(viewWidth - 19*sw, viewHeight - 7*sw, 18*sw, 6*sw), "HIT")) {
hit(currentPlayer);
}
if (GUI.Button(new Rect(viewWidth - 19*sw, viewHeight - 14*sw, 18*sw, 6*sw), "HOLD")) {
currentPlayer++;
}
if (GUI.Button(new Rect(1*sw, viewHeight - 14*sw, 18*sw, 6*sw), "RESET")) {
Reset(1);
}
if (GUI.Button(new Rect(1*sw, viewHeight - 7*sw, 18*sw, 6*sw), "NEXT ROUND")) {
NextRound();
}
}
void DrawStats() {
GUI.Label(new Rect(viewWidth-45*sw, 3*sw, 43.5f*sw, 3*sw), String.Format("Cards in discard pile: {0}", discardPile.Count));
GUI.Label(new Rect(viewWidth-45*sw, 6*sw, 43.5f*sw, 3*sw), String.Format("Cards in draw pile: {0}", deck.Count));
int[] cards = new int[13];
foreach (Card c in deck) {
cards[c.value-1]++;
}
for (int i = 1; i<=13; i++) {
/*if((i-1)%2==0) fill(0);
else fill(255,0,0);*/
GUI.Label(new Rect(viewWidth-45*sw+(i-1)*3*sw, 10.5f*sw, 43.5f*sw, 3*sw), /*cardNames.get(*/String.Format("{0}", i)/*)*/);
//fill(255);
GUI.Label(new Rect(viewWidth-45*sw+(i-1)*3*sw, 12.25f*sw, 43.5f*sw, 3*sw), String.Format("{0}", cards[i-1]));
}
double permille = deck.Count>0 ? Math.Round((double)((cards[9] + cards[10] + cards[11] + cards[12])/deck.Count*1000)) : -1;
GUI.Label(new Rect(viewWidth-45*sw, 18*sw, 45*sw, 3*sw), String.Format("Percentage face cards/tens: {0}% (average=30.8%)", (permille/10)));
}
void hit(int player) {
if (!gameOver) {
ShuffleIfNecessary();
if (currentPlayer<playerCount) {
hands[player].Add(deck[deck.Count-1]);
deck.RemoveAt(deck.Count-1);
if (GetScore(hands[player])>21 && hands[player].Count!=2) {
playerStates[currentPlayer] = -2;
currentPlayer++;
}
} else {
DealerHit();
if (GetScore(dealerHand)>=17) {
GameOver();
}
}
}
}
void hit(int player, int value) {
if (!gameOver) {
ShuffleIfNecessary();
if (currentPlayer<playerCount) {
hands[player].Add(deck[deck.Count-1]);
RemoveCard(value);
if (GetScore(hands[currentPlayer])>21 && hands[currentPlayer].Count!=2) {
playerStates[currentPlayer] = -2;
currentPlayer++;
}
} else {
DealerHit();
if (GetScore(dealerHand)>=17) {
GameOver();
}
}
}
}
void GameOver() {
gameOver = true;
int dealerScore = GetScore(dealerHand);
for (int i = 0; i<hands.Length; i++) {
int score = GetScore(hands[i]);
if (score>21 && score!=100) {
playerStates[i] = -2;
} else if (score>dealerScore) {
playerStates[i] = 1;
} else if (score==dealerScore) {
playerStates[i] = 0;
} else if (score<dealerScore) {
if (dealerScore>21 && dealerScore!=100) {
playerStates[i] = 1;
} else {
playerStates[i] = -1;
}
}
}
}
void DealerHit() {
ShuffleIfNecessary();
dealerHand.Add(deck[deck.Count-1]);
deck.RemoveAt(deck.Count-1);
}
void ShuffleIfNecessary() {
if (deck.Count<=52) {
ShuffleDeck();
}
}
void Hold() {
}
void Reset(int players) {
playerCount = players;
InitializeDeck();
ClearTable();
discardPile.Clear();
ShuffleDeck();
NextRound();
}
void NextRound() {
gameOver = false;
currentPlayer = 0;
ClearTable();
// initialize player states
playerStates = new int[playerCount];
DealerHit();
}
void ClearTable() {
discardPile.AddRange(dealerHand);
dealerHand.Clear();
foreach (List<Card> hand in hands) {
discardPile.AddRange(hand);
hand.Clear();
}
}
void InitializeDeck() {
deck.Clear();
// initialize player hands
hands = new List<Card>[playerCount];
for (int i = 0; i<hands.Length; i++) {
hands[i] = new List<Card>();
}
for (int numOfDecks = 0; numOfDecks<2; numOfDecks++) {
for (int i = 1; i<=13; i++) {
for (int j = 0; j<4; j++) {
deck.Add(new Card(i, j));
}
}
}
totalCards = deck.Count;
}
void ShuffleDeck() {
deck.AddRange(discardPile);
discardPile.Clear();
deck.Shuffle();
}
int GetScore(List<Card> hand) {
int score = 0;
int aces = 0;
foreach (Card c in hand) {
if (c.value == 1) {
aces++;
score += 11;
} else {
score += c.value.Clamp(2, 10);
}
}
for (int i = 0; i<aces; i++) {
if (score>21) {
score -= 10;
}
}
if(score==21 && hand.Count==2) {
score = 100;
}
return score;
}
void DrawDealerStats(List<Card> hand, int y) {
if(hand.Count>0) {
int score = GetScore(hand);
GUI.Label(new Rect(1.5f*sw, y-2.2f*sw, 30*sw, 3*sw), score==100 ? "Natural 21" : String.Format("{0}", score));
GUI.Label(new Rect(12*sw, y-2.2f*sw, 30*sw, 3*sw), String.Format("{0} (Dealer)", GetScore(hand)>21 ? states[-2] : states[0]));
for(int i = 0; i<hand.Count; i++) {
GUI.Label(new Rect(1.5f*sw+i*3*sw, y, 30*sw, 3*sw), String.Format("{0}", cardNames[hand[i].value]));
}
}
}
void DrawPlyStats(List<Card> hand, float y, int player) {
int score = GetScore(hand);
GUI.Label(new Rect(1.5f*sw, y-2.2f*sw, 30*sw, 3*sw), score==100 ? "Natural 21" : String.Format("{0}", score));
string state = states[playerStates[player]];
if(playerStates[player]==0 && gameOver) {
state = "Push";
}
GUI.Label(new Rect(12*sw, y-2.2f*sw, 30*sw, 3*sw), String.Format("{0} (Player {1})", state, player+1));
for(int i = 0; i<hand.Count; i++) {
GUI.Label(new Rect(1.5f*sw+i*3*sw, y, 30*sw, 3*sw), String.Format("{0}", cardNames[hand[i].value]));
}
}
void DrawAllPlyStats(List<Card>[] hands) {
for(int i = 0; i< hands.Length; i++) {
DrawPlyStats(hands[i], 11*sw+i*6*sw, i);
}
}
bool RemoveCard(int value) {
for(int i = 0; i<deck.Count; i++) {
if(deck[i].value==value) {
deck.RemoveAt(i);
return true;
}
}
// no card with given value
return false;
}
}
class Card : IComparable {
public int value;
public int colour;
public Card(int value, int colour) {
this.value = value;
this.colour = colour;
}
public int CompareTo(object o) {