Vue
by sensei
HTML
<div id="app">
<div>deck: {{deck}}</div>
<div>batch: {{batch}}</div>
<div v-for="(hand, index) in hands">
<span v-if="index === currentHand">></span>
<span>hand {{index + 1}}: {{hand.set}} <b>{{hand.title}}</b></span>
</div>
<h4>winner: {{winner}}</h4>
<button @click="deal(currentHand, 1, true)" :disabled="hands[currentHand].value >= 21">deal</button>
<button @click="pass(true)" :disabled="winner">pass</button>
<button @click="setTable">reset</button>
</div>
Vue
new Vue({
el: "#app",
data () {
return {
deck: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
batch: [],
hands: [this.getHandModel(), this.getHandModel()],
currentHand: 0,
playerIndex: 1,
winner: null
}
},
methods: {
shuffle () {
let array = this.deck.slice()
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
this.batch = array
},
deal (hand = 0, count = 1, pass = false) {
let target = this.hands[hand], i = 0
while (i < count) {
let card = this.batch[0],
value = target.value + card,
title = ''
if (hand === this.playerIndex) {
title = value + ' / 21'
}
else {
title = '? + ' + (value - target.set[0] ) + ' / 21'
}
target.set.push(card)
target.value = value
target.title = title
this.batch.splice(0, 1)
i++
}
if (pass) this.pass()
},
pass (self) {
let nextHand = this.currentHand === (this.hands.length - 1) ? 0 : this.currentHand + 1
if (self) {
let current = this.hands[this.currentHand],
next = this.hands[nextHand]
if (next.pass) {
this.scoring()
}
else {
current.pass = true
next.pass = false
}
}
this.currentHand = nextHand
},
scoring () {
let winner = 'ничья', maxValue = 0
this.hands.forEach((hand, index) => {
if (hand.value <= 21 && hand.value > maxValue) {
maxValue = hand.value
winner = index + 1
}
...