Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @name BlackJackDealer
- @inputs [DealerScreen PlayerScreen]:wirelink
- @persist [Cards MyCards Count Names]:table Flip
- @outputs Dealer
- Players = ioOutputEntities("Dealer")
- if(first()) {
- function void wirelink:centerHString(String:string, Y, Clr) {
- X = 15-round(String:length()/2)
- This:writeString(String, X, Y, Clr)
- }
- function void newDeck(Decks) {
- for(I=1, Decks*52) {
- Cards[I,vector2] = vec2(((I-1)%13), (floor(I/13)%4)+1)
- }
- }
- function void shuffleDeck() {
- for(I=1, Cards:count()) {
- Random = randint(1, Cards:count())
- Temp = Cards[Random,vector2]
- Cards[Random,vector2] = Cards[I,vector2]
- Cards[I,vector2] = Temp
- }
- }
- function string getCardName(Pos, Array:table) {
- Card = Array[Pos,vector2]
- if(Card:x()==10) {
- String = "Jack of "
- } elseif(Card:x()==11) {
- String = "Queen of "
- } elseif(Card:x()==12) {
- String = "King of "
- } elseif(Card:x()==0) {
- String = "Ace of "
- } else {
- String = (Card:x()+1)+" of "
- }
- if(Card:y()==1) {
- String += "Hearts"
- } elseif(Card:y()==2) {
- String += "Diamonds"
- } elseif(Card:y()==3) {
- String += "Clubs"
- } elseif(Card:y()==4) {
- String += "Spades"
- }
- return String
- }
- function number totalCount() {
- local Count = 0
- for(I=1, MyCards:count()) {
- Card = MyCards[I,vector2]
- Number = Card:x()+1
- if(Number>=2 & Number<=10) {
- Count += Number
- }
- if(Number==1) {
- if((Count+11)>21) {
- Count += 1
- } else {
- Count += 11
- }
- }
- if(Number>10) {
- Count += 10
- }
- }
- return Count
- }
- function number table:contains(Entity:entity) {
- for(I=1, This:count()) {
- if(This[I,entity]==Entity) { return I }
- }
- return 0
- }
- function void dealCard(Player) {
- if(Cards:count()) {
- dsSendDirect("Deal", Players[Player,entity], Cards[1, vector2])
- Cards:removeVector2(1)
- }
- }
- function void dealMyCard() {
- if(Cards:count()) {
- MyCards:pushVector2(Cards[1,vector2])
- Cards:removeVector2(1)
- }
- }
- function void updateDealerScreen() {
- DealerScreen[2041] = 1
- DealerScreen:centerHString("Robby's Blackjack", 0, 900)
- DealerScreen:centerHString("Cards left: "+Cards:count(), 1, 339)
- DealerScreen:centerHString("Your count: "+totalCount(), 2, 492)
- DealerScreen:centerHString("Counts", 3, 939)
- for(I=1, Players:count()) {
- if(Names[I,string]) {
- String = Names[I,string]+": "+Count[I,number]
- } else {
- String = "Players "+I+": "+Count[I,number]
- }
- DealerScreen:centerHString(String, I+3, 999)
- }
- }
- function void updatePlayerScreen() {
- PlayerScreen[2041] = 1
- PlayerScreen:centerHString("Robby's Blackjack", 0, 900)
- PlayerScreen:centerHString("Dealer", 1, 339)
- if(MyCards) {
- PlayerScreen:centerHString("Cards", 3, 933)
- if(MyCards:count()==2 & totalCount()!=21 & !Flip) {
- PlayerScreen:centerHString(getCardName(1, MyCards), 4, 999)
- PlayerScreen:centerHString("[Face down Card]", 5, 999)
- } else {
- for(I=1, MyCards:count()) {
- String = getCardName(I, MyCards)
- PlayerScreen:centerHString(String, I+3, 999)
- }
- PlayerScreen:centerHString("Total", MyCards:count()+5, 393)
- PlayerScreen:centerHString(totalCount():toString(), MyCards:count()+6, 999)
- if(MyCards:count()==2 & totalCount()==21) {
- PlayerScreen:centerHString("BlackJack", 17, 90)
- }
- }
- } else {
- PlayerScreen:centerHString("Make your bids.", 8, 9)
- }
- }
- function void printC(String:string, Color:vector) {
- printColor(vec(255,0,0), "BlackJackE2: ", Color, String)
- }
- }
- if(first()) {
- Dealer = entity():id()
- updateDealerScreen()
- printC("Type !help to get started.", vec(255,255,0))
- runOnChat(1)
- }
- if(dsClk("Count")) {
- for(I=1, Players:count()) {
- if(Players[I,entity]==dsGetSender()) {
- Count[I,number] = dsGetNumber()
- }
- }
- }
- if(dsClk("Name")) {
- for(I=1, Players:count()) {
- if(Players[I,entity]==dsGetSender()) {
- Names[I,string] = dsGetString()
- }
- }
- }
- if(chatClk(owner())) {
- Said = lastSaid():explode(" ")
- if(Said[1,string]=="!deal") {
- Player = Said[2,string]:toNumber()
- if(Player>=1 & Names[Player,string]) {
- dealCard(Said[2,string]:toNumber())
- printC("Card dealt to "+Names[Player,string], vec(0,255,0))
- }
- hideChat(1)
- }
- if(Said[1,string]=="!dealme") {
- dealMyCard()
- printC("Card dealt to you.", vec(0,255,0))
- hideChat(1)
- }
- if(Said[1,string]=="!start") {
- if(Cards:count()>=Names:count()*2+2) {
- if(Names:count()) {
- for(I=1, Players:count()) {
- if(Names[I,string]) {
- dealCard(I)
- dealCard(I)
- }
- }
- dealMyCard()
- dealMyCard()
- printC("Hands dealt.", vec(0,255,0))
- } else {
- printC("No bets received.", vec(0,255,0))
- }
- } else {
- printC("You need more cards.", vec(0,255,0))
- }
- hideChat(1)
- }
- if(Said[1,string]=="!reset") {
- for(I=1, Players:count()) {
- dsSendDirect("Reset", Players[I,entity], 1)
- }
- MyCards = table()
- Names = table()
- Flip = 0
- printC("Reset.", vec(0,255,0))
- hideChat(1)
- }
- if(Said[1,string]=="!newdeck") {
- Amount = Said[2,string]:toNumber()
- if(Amount>10) {
- newDeck(10)
- printC("Max amount exceeded. 10 decks created.", vec(0,255,0))
- } elseif(Amount<1) {
- newDeck(1)
- printC("Minimum exceeded. 1 deck created.", vec(0,255,0))
- } else {
- newDeck(Amount)
- printC(Amount+" deck"+(Amount>1 ? "s" : "")+" created.", vec(0,255,0))
- }
- hideChat(1)
- }
- if(Said[1,string]=="!shuffle") {
- if(Cards:count()) {
- shuffleDeck()
- printC(Cards:count()+" cards shuffled.", vec(0,255,0))
- } else {
- printC("No cards to shuffle!", vec(0,255,0))
- }
- hideChat(1)
- }
- if(Said[1,string]=="!flip") {
- Flip = 1
- }
- if(Said[1,string]=="!help") {
- printC("---Commands---", vec(0,255,255))
- printC("!start | Deals 2 cards to everyone.", vec(0,255,255))
- printC("!deal [N] | Deals 1 card to player N.", vec(0,255,255))
- printC("!dealme | Deals 1 card to the dealer.", vec(0,255,255))
- printC("!newdeck [N] | Creates N decks of cards.", vec(0,255,255))
- printC("!shuffle | Shuffles your cards.", vec(0,255,255))
- printC("!flip | Flips the face down card.", vec(0,255,255))
- hideChat(1)
- }
- }
- updateDealerScreen()
- updatePlayerScreen()
Advertisement
Add Comment
Please, Sign In to add comment