blurner

BlackJackDealer

Dec 3rd, 2012
1,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. @name BlackJackDealer
  2. @inputs [DealerScreen PlayerScreen]:wirelink
  3. @persist [Cards MyCards Count Names]:table Flip
  4. @outputs Dealer
  5. Players = ioOutputEntities("Dealer")
  6. if(first()) {
  7. function void wirelink:centerHString(String:string, Y, Clr) {
  8. X = 15-round(String:length()/2)
  9. This:writeString(String, X, Y, Clr)
  10. }
  11. function void newDeck(Decks) {
  12. for(I=1, Decks*52) {
  13. Cards[I,vector2] = vec2(((I-1)%13), (floor(I/13)%4)+1)
  14. }
  15. }
  16. function void shuffleDeck() {
  17. for(I=1, Cards:count()) {
  18. Random = randint(1, Cards:count())
  19. Temp = Cards[Random,vector2]
  20. Cards[Random,vector2] = Cards[I,vector2]
  21. Cards[I,vector2] = Temp
  22. }
  23. }
  24. function string getCardName(Pos, Array:table) {
  25. Card = Array[Pos,vector2]
  26. if(Card:x()==10) {
  27. String = "Jack of "
  28. } elseif(Card:x()==11) {
  29. String = "Queen of "
  30. } elseif(Card:x()==12) {
  31. String = "King of "
  32. } elseif(Card:x()==0) {
  33. String = "Ace of "
  34. } else {
  35. String = (Card:x()+1)+" of "
  36. }
  37. if(Card:y()==1) {
  38. String += "Hearts"
  39. } elseif(Card:y()==2) {
  40. String += "Diamonds"
  41. } elseif(Card:y()==3) {
  42. String += "Clubs"
  43. } elseif(Card:y()==4) {
  44. String += "Spades"
  45. }
  46. return String
  47. }
  48. function number totalCount() {
  49. local Count = 0
  50. for(I=1, MyCards:count()) {
  51. Card = MyCards[I,vector2]
  52. Number = Card:x()+1
  53. if(Number>=2 & Number<=10) {
  54. Count += Number
  55. }
  56. if(Number==1) {
  57. if((Count+11)>21) {
  58. Count += 1
  59. } else {
  60. Count += 11
  61. }
  62. }
  63. if(Number>10) {
  64. Count += 10
  65. }
  66. }
  67. return Count
  68. }
  69. function number table:contains(Entity:entity) {
  70. for(I=1, This:count()) {
  71. if(This[I,entity]==Entity) { return I }
  72. }
  73. return 0
  74. }
  75. function void dealCard(Player) {
  76. if(Cards:count()) {
  77. dsSendDirect("Deal", Players[Player,entity], Cards[1, vector2])
  78. Cards:removeVector2(1)
  79. }
  80. }
  81. function void dealMyCard() {
  82. if(Cards:count()) {
  83. MyCards:pushVector2(Cards[1,vector2])
  84. Cards:removeVector2(1)
  85. }
  86. }
  87. function void updateDealerScreen() {
  88. DealerScreen[2041] = 1
  89. DealerScreen:centerHString("Robby's Blackjack", 0, 900)
  90. DealerScreen:centerHString("Cards left: "+Cards:count(), 1, 339)
  91. DealerScreen:centerHString("Your count: "+totalCount(), 2, 492)
  92. DealerScreen:centerHString("Counts", 3, 939)
  93. for(I=1, Players:count()) {
  94. if(Names[I,string]) {
  95. String = Names[I,string]+": "+Count[I,number]
  96. } else {
  97. String = "Players "+I+": "+Count[I,number]
  98. }
  99. DealerScreen:centerHString(String, I+3, 999)
  100. }
  101. }
  102. function void updatePlayerScreen() {
  103. PlayerScreen[2041] = 1
  104. PlayerScreen:centerHString("Robby's Blackjack", 0, 900)
  105. PlayerScreen:centerHString("Dealer", 1, 339)
  106. if(MyCards) {
  107. PlayerScreen:centerHString("Cards", 3, 933)
  108. if(MyCards:count()==2 & totalCount()!=21 & !Flip) {
  109. PlayerScreen:centerHString(getCardName(1, MyCards), 4, 999)
  110. PlayerScreen:centerHString("[Face down Card]", 5, 999)
  111. } else {
  112. for(I=1, MyCards:count()) {
  113. String = getCardName(I, MyCards)
  114. PlayerScreen:centerHString(String, I+3, 999)
  115. }
  116. PlayerScreen:centerHString("Total", MyCards:count()+5, 393)
  117. PlayerScreen:centerHString(totalCount():toString(), MyCards:count()+6, 999)
  118. if(MyCards:count()==2 & totalCount()==21) {
  119. PlayerScreen:centerHString("BlackJack", 17, 90)
  120. }
  121. }
  122. } else {
  123. PlayerScreen:centerHString("Make your bids.", 8, 9)
  124. }
  125. }
  126. function void printC(String:string, Color:vector) {
  127. printColor(vec(255,0,0), "BlackJackE2: ", Color, String)
  128. }
  129. }
  130.  
  131. if(first()) {
  132. Dealer = entity():id()
  133. updateDealerScreen()
  134. printC("Type !help to get started.", vec(255,255,0))
  135. runOnChat(1)
  136. }
  137.  
  138. if(dsClk("Count")) {
  139. for(I=1, Players:count()) {
  140. if(Players[I,entity]==dsGetSender()) {
  141. Count[I,number] = dsGetNumber()
  142. }
  143. }
  144. }
  145.  
  146. if(dsClk("Name")) {
  147. for(I=1, Players:count()) {
  148. if(Players[I,entity]==dsGetSender()) {
  149. Names[I,string] = dsGetString()
  150. }
  151. }
  152. }
  153.  
  154. if(chatClk(owner())) {
  155. Said = lastSaid():explode(" ")
  156. if(Said[1,string]=="!deal") {
  157. Player = Said[2,string]:toNumber()
  158. if(Player>=1 & Names[Player,string]) {
  159. dealCard(Said[2,string]:toNumber())
  160. printC("Card dealt to "+Names[Player,string], vec(0,255,0))
  161. }
  162. hideChat(1)
  163. }
  164. if(Said[1,string]=="!dealme") {
  165. dealMyCard()
  166. printC("Card dealt to you.", vec(0,255,0))
  167. hideChat(1)
  168. }
  169. if(Said[1,string]=="!start") {
  170. if(Cards:count()>=Names:count()*2+2) {
  171. if(Names:count()) {
  172. for(I=1, Players:count()) {
  173. if(Names[I,string]) {
  174. dealCard(I)
  175. dealCard(I)
  176. }
  177. }
  178. dealMyCard()
  179. dealMyCard()
  180. printC("Hands dealt.", vec(0,255,0))
  181. } else {
  182. printC("No bets received.", vec(0,255,0))
  183. }
  184. } else {
  185. printC("You need more cards.", vec(0,255,0))
  186. }
  187. hideChat(1)
  188. }
  189. if(Said[1,string]=="!reset") {
  190. for(I=1, Players:count()) {
  191. dsSendDirect("Reset", Players[I,entity], 1)
  192. }
  193. MyCards = table()
  194. Names = table()
  195. Flip = 0
  196. printC("Reset.", vec(0,255,0))
  197. hideChat(1)
  198. }
  199. if(Said[1,string]=="!newdeck") {
  200. Amount = Said[2,string]:toNumber()
  201. if(Amount>10) {
  202. newDeck(10)
  203. printC("Max amount exceeded. 10 decks created.", vec(0,255,0))
  204. } elseif(Amount<1) {
  205. newDeck(1)
  206. printC("Minimum exceeded. 1 deck created.", vec(0,255,0))
  207. } else {
  208. newDeck(Amount)
  209. printC(Amount+" deck"+(Amount>1 ? "s" : "")+" created.", vec(0,255,0))
  210. }
  211. hideChat(1)
  212. }
  213. if(Said[1,string]=="!shuffle") {
  214. if(Cards:count()) {
  215. shuffleDeck()
  216. printC(Cards:count()+" cards shuffled.", vec(0,255,0))
  217. } else {
  218. printC("No cards to shuffle!", vec(0,255,0))
  219. }
  220. hideChat(1)
  221. }
  222. if(Said[1,string]=="!flip") {
  223. Flip = 1
  224. }
  225. if(Said[1,string]=="!help") {
  226. printC("---Commands---", vec(0,255,255))
  227. printC("!start | Deals 2 cards to everyone.", vec(0,255,255))
  228. printC("!deal [N] | Deals 1 card to player N.", vec(0,255,255))
  229. printC("!dealme | Deals 1 card to the dealer.", vec(0,255,255))
  230. printC("!newdeck [N] | Creates N decks of cards.", vec(0,255,255))
  231. printC("!shuffle | Shuffles your cards.", vec(0,255,255))
  232. printC("!flip | Flips the face down card.", vec(0,255,255))
  233. hideChat(1)
  234. }
  235. }
  236. updateDealerScreen()
  237. updatePlayerScreen()
Advertisement
Add Comment
Please, Sign In to add comment