📜 ⬆️ ⬇️

Livejack blackjack

Probably, many are already fed up with headers like “XXX in 30 lines”, but I would venture to write another post. In order to somehow smooth the anger of individual habraluy, I will tell you what each line does in this script.
image

As a game, I chose Blackjack . Let me remind the rules:

Link to jsfiddle .
The code itself:
cards = [['\ud83c\udca1', '\ud83c\udca2', '\ud83c\udca3', '\ud83c\udca4', '\ud83c\udca5', '\ud83c\udca6', '\ud83c\udca7', '\ud83c\udca8', '\ud83c\udca9', '\ud83c\udcaa', '\ud83c\udcab', '\ud83c\udcad', '\ud83c\udcae',] ['\ud83c\udcb1', '\ud83c\udcb2', '\ud83c\udcb3', '\ud83c\udcb4', '\ud83c\udcb5', '\ud83c\udcb6', '\ud83c\udcb7', '\ud83c\udcb8', '\ud83c\udcb9', '\ud83c\udcba', '\ud83c\udcbb', '\ud83c\udcbd', '\ud83c\udcbe',] ['\ud83c\udcc1', '\ud83c\udcc2', '\ud83c\udcc3', '\ud83c\udcc4', '\ud83c\udcc5', '\ud83c\udcc6', '\ud83c\udcc7', '\ud83c\udcc8', '\ud83c\udcc9', '\ud83c\udcca', '\ud83c\udccb', '\ud83c\udccd', '\ud83c\udcce',] ['\ud83c\udcd1', '\ud83c\udcd2', '\ud83c\udcd3', '\ud83c\udcd4', '\ud83c\udcd5', '\ud83c\udcd6', '\ud83c\udcd7', '\ud83c\udcd8', '\ud83c\udcd9', '\ud83c\udcda', '\ud83c\udcdb', '\ud83c\udcdd', '\ud83c\udcde']] deck=[[0 0 false ] [0 1 2] [0 2 3] [0 3 4] [0 4 5] [0 5 6] [0 6 7] [0 7 8] [0 8 9] [0 9 10] [0 10 10] [0 11 10] [0 12 10] [1 0 false ] [1 1 2] [1 2 3] [1 3 4] [1 4 5] [1 5 6] [1 6 7] [1 7 8] [1 8 9] [1 9 10] [1 10 10] [1 11 10] [1 12 10] [2 0 false ] [2 1 2] [2 2 3] [2 3 4] [2 4 5] [2 5 6] [2 6 7] [2 7 8] [2 8 9] [2 9 10] [2 10 10] [2 11 10] [2 12 10] [3 0 false ] [3 1 2] [3 2 3] [3 3 4] [3 4 5] [3 5 6] [3 6 7] [3 7 8] [3 8 9] [3 9 10] [3 10 10] [3 11 10] [3 12 10] ] sdeck=JSON.parse JSON.stringify deck shuffle = !-> for x,i in deck when (x=Math.floor(Math.random() * (i + 1));true) [deck[i],deck[x]] = [deck[x],deck[i]] gid=(id) -> document.getElementById(id) window.mca=mc=[];window.dca=dc=[];defbet=gid("defbet");m=gid("money");bet=gid("bet");mch=gid("mcards");dch=gid("dcards");l=gid("log") tod = !-> dc.push (c=deck.pop!); dch.innerHTML+=cards[c[0]][c[1]] log = (msg)!-> l.innerHTML=msg window.more = tom = !-> mc.push (c=deck.pop!); mch.innerHTML+=cards[c[0]][c[1]]; summ(mc)>21 and (log "";gid("d").disabled=gid("m").disabled=gid("e").disabled=true;gid("g").disabled=false) clear = !-> gid("g").disabled=!(gid("d").disabled=gid("m").disabled=gid("e").disabled=false);log "";bet.innerHTML=0;mch.innerHTML=dch.innerHTML="";mc.length=0;dc.length=0;deck:=JSON.parse JSON.stringify sdeck;shuffle! window.s=summ = (ar)-> sum=0;tzs=0 for c in ar (c[2] and sum+=c[2]) or tzs++ for i in [1 to tzs] (sum+11>21 and sum+=1) or sum+=11 sum betbet = -> if +m.innerHTML<+defbet.value then log "    !"; false else bet.innerHTML=+defbet.value;m.innerHTML-=+defbet.value;true window.dub = !-> if +m.innerHTML<+bet.innerHTML then log "    !" else m.innerHTML-=+bet.innerHTML;bet.innerHTML*=2;tom! window.give = !-> (clear!; betbet!) and (tom!;tom!;tod!;true) or gid("g").disabled=false;!mc[0][2] and !mc[1][2] and (log " !"; m.innerHTML = +m.innerHTML + +bet.innerHTML*3;gid("e").disabled=true;gid("g").disabled=false) window.enough = !-> win=true;nothing=false while summ(dc) < 17 tod! log if summ(dc)>21 or summ(dc)<summ(mc) then " !" else if summ(dc)==summ(mc) then (nothing=true; "") else (win=false; " ") gid("g").disabled=false gid("e").disabled=true m.innerHTML = +m.innerHTML + (+bet.innerHTML)*(if nothing then 1 else if win then 2 else 0) 


So, we will consider its each line.

The first line initializes an array with "images" of maps.
 cards = [['\ud83c\udca1', '\ud83c\udca2', '\ud83c\udca3', '\ud83c\udca4', '\ud83c\udca5', '\ud83c\udca6', '\ud83c\udca7', '\ud83c\udca8', '\ud83c\udca9', '\ud83c\udcaa', '\ud83c\udcab', '\ud83c\udcad', '\ud83c\udcae',] ['\ud83c\udcb1', '\ud83c\udcb2', '\ud83c\udcb3', '\ud83c\udcb4', '\ud83c\udcb5', '\ud83c\udcb6', '\ud83c\udcb7', '\ud83c\udcb8', '\ud83c\udcb9', '\ud83c\udcba', '\ud83c\udcbb', '\ud83c\udcbd', '\ud83c\udcbe',] ['\ud83c\udcc1', '\ud83c\udcc2', '\ud83c\udcc3', '\ud83c\udcc4', '\ud83c\udcc5', '\ud83c\udcc6', '\ud83c\udcc7', '\ud83c\udcc8', '\ud83c\udcc9', '\ud83c\udcca', '\ud83c\udccb', '\ud83c\udccd', '\ud83c\udcce',] ['\ud83c\udcd1', '\ud83c\udcd2', '\ud83c\udcd3', '\ud83c\udcd4', '\ud83c\udcd5', '\ud83c\udcd6', '\ud83c\udcd7', '\ud83c\udcd8', '\ud83c\udcd9', '\ud83c\udcda', '\ud83c\udcdb', '\ud83c\udcdd', '\ud83c\udcde']] 

')
The second line initializes the array with maps, each map represents an array with three elements: the coordinates of the map image and its value. In this case, false is stored for aces, since it is processed separately.
 deck=[[0 0 false ] [0 1 2] [0 2 3] [0 3 4] [0 4 5] [0 5 6] [0 6 7] [0 7 8] [0 8 9] [0 9 10] [0 10 10] [0 11 10] [0 12 10] [1 0 false ] [1 1 2] [1 2 3] [1 3 4] [1 4 5] [1 5 6] [1 6 7] [1 7 8] [1 8 9] [1 9 10] [1 10 10] [1 11 10] [1 12 10] [2 0 false ] [2 1 2] [2 2 3] [2 3 4] [2 4 5] [2 5 6] [2 6 7] [2 7 8] [2 8 9] [2 9 10] [2 10 10] [2 11 10] [2 12 10] [3 0 false ] [3 1 2] [3 2 3] [3 3 4] [3 4 5] [3 5 6] [3 6 7] [3 7 8] [3 8 9] [3 9 10] [3 10 10] [3 11 10] [3 12 10] ] 

In the third line, the array of maps is copied to another array, since with further actions the first array will be corrupted.
 sdeck=JSON.parse JSON.stringify deck 

The fourth and fifth lines declare the deck mixing function.
 shuffle = !-> for x,i in deck when (x=Math.floor(Math.random() * (i + 1));true) [deck[i],deck[x]] = [deck[x],deck[i]] 

The sixth line declares a short function to replace the long document.getElementById.
 gid=(id) -> document.getElementById(id) 

In the seventh line, the player and dealer card sets are announced, as well as links to the size of the bet, the amount of player money, the current con, the container with the player and dealer cards and the place to display messages.
 window.mca=mc=[];window.dca=dc=[];defbet=gid("defbet");m=gid("money");bet=gid("bet");mch=gid("mcards");dch=gid("dcards");l=gid("log") 

The eighth line. The function of adding a card to the dealer.
 tod = !-> dc.push (c=deck.pop!); dch.innerHTML+=cards[c[0]][c[1]] 

Ninth line. Message display function.
 log = (msg)!-> l.innerHTML=msg 

In the tenth line, the function of adding a card to a player is announced and rendered to the global scope.
 window.more = tom = !-> mc.push (c=deck.pop!); mch.innerHTML+=cards[c[0]][c[1]]; summ(mc)>21 and (log "";gid("d").disabled=gid("m").disabled=gid("e").disabled=true;gid("g").disabled=false) 

11-12 lines. The reset function for the new engine. The dealer and the player take the cards, a new deck is created and shuffled, a message box is cleared.
 clear = !-> gid("g").disabled=!(gid("d").disabled=gid("m").disabled=gid("e").disabled=false);log "";bet.innerHTML=0;mch.innerHTML=dch.innerHTML="";mc.length=0;dc.length=0;deck:=JSON.parse JSON.stringify sdeck;shuffle! 

13-19 lines. The function of counting the amount. We summarize all the cards, count the number of aces, at the end count them, trying to meet the 21.
 window.s=summ = (ar)-> sum=0;tzs=0 for c in ar (c[2] and sum+=c[2]) or tzs++ for i in [1 to tzs] (sum+11>21 and sum+=1) or sum+=11 sum 

Twentieth line. We make a bet, check that there is enough money.
 betbet = -> if +m.innerHTML<+defbet.value then log "    !"; false else bet.innerHTML=+defbet.value;m.innerHTML-=+defbet.value;true 

Twenty first line. Double the bet.
 window.dub = !-> if +m.innerHTML<+bet.innerHTML then log "    !" else m.innerHTML-=+bet.innerHTML;bet.innerHTML*=2;tom! 

Twenty-second line. We distribute initial cards.
 window.give = !-> (clear!; betbet!) and (tom!;tom!;tod!;true) or gid("g").disabled=false;!mc[0][2] and !mc[1][2] and (log " !"; m.innerHTML = +m.innerHTML + +bet.innerHTML*3;gid("e").disabled=true;gid("g").disabled=false) 

23-30. If there are enough cards, the dealer picks up his cards, after which the points are compared.
 window.enough = !-> win=true;nothing=false while summ(dc) < 17 tod! log if summ(dc)>21 or summ(dc)<summ(mc) then " !" else if summ(dc)==summ(mc) then (nothing=true; "") else (win=false; " ") gid("g").disabled=false gid("e").disabled=true m.innerHTML = +m.innerHTML + (+bet.innerHTML)*(if nothing then 1 else if win then 2 else 0) 

Source: https://habr.com/ru/post/202906/


All Articles