SyntaxSquad/DADProject#14 Added final touches to the gameplay
This commit is contained in:
@@ -11,15 +11,15 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
const deck = ref([])
|
||||
const playerHand = ref([])
|
||||
const opponentHand = ref([])
|
||||
|
||||
const trumpCard = ref(null)
|
||||
const trumpSuit = ref('')
|
||||
|
||||
const playerScore = ref(0)
|
||||
const opponentScore = ref(0)
|
||||
|
||||
const currentTurn = ref('player')
|
||||
|
||||
const playerTricks = ref(0)
|
||||
const opponentTricks = ref(0)
|
||||
|
||||
const winner = ref(null)
|
||||
|
||||
const table = ref({
|
||||
@@ -41,33 +41,27 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
opponentScore.value = 0
|
||||
playerTricks.value = 0
|
||||
opponentTricks.value = 0
|
||||
|
||||
deck.value = []
|
||||
playerHand.value = []
|
||||
opponentHand.value = []
|
||||
trumpCard.value = null
|
||||
|
||||
trumpSuit.value = ''
|
||||
currentTurn.value = 'player'
|
||||
|
||||
table.value = {
|
||||
playerCard: null,
|
||||
opponentCard: null,
|
||||
firstToPlay: null,
|
||||
}
|
||||
table.value = { playerCard: null, opponentCard: null, firstToPlay: null }
|
||||
|
||||
deck.value = createDeck()
|
||||
|
||||
trumpCard.value = deck.value[0]
|
||||
const lastCard = deck.value.pop()
|
||||
trumpCard.value = lastCard
|
||||
trumpSuit.value = lastCard.suit
|
||||
|
||||
await wait(500)
|
||||
|
||||
await dealInitialCards()
|
||||
isDealing.value = false
|
||||
|
||||
if (currentTurn.value === 'opponent') {
|
||||
setTimeout(botPlayCard, 1000)
|
||||
}
|
||||
|
||||
isDealing.value = false
|
||||
}
|
||||
|
||||
const playerPlayCard = (card) => {
|
||||
@@ -94,11 +88,8 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
|
||||
if (table.value.playerCard) {
|
||||
const pCard = table.value.playerCard
|
||||
const trump = trumpCard.value.suit
|
||||
|
||||
const winningCards = opponentHand.value.filter((botCard) =>
|
||||
doesCardWin(botCard, pCard, trump),
|
||||
)
|
||||
const winningCards = opponentHand.value.filter((botCard) => doesCardWin(botCard, pCard))
|
||||
|
||||
if (winningCards.length > 0) {
|
||||
winningCards.sort((a, b) => a.strength - b.strength)
|
||||
@@ -112,9 +103,8 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
}
|
||||
} else {
|
||||
const sortedHand = [...opponentHand.value].sort((a, b) => {
|
||||
if (a.suit === trumpCard.value.suit && b.suit !== trumpCard.value.suit) return 1
|
||||
if (a.suit !== trumpCard.value.suit && b.suit === trumpCard.value.suit) return -1
|
||||
|
||||
if (a.suit === trumpSuit.value && b.suit !== trumpSuit.value) return 1
|
||||
if (a.suit !== trumpSuit.value && b.suit === trumpSuit.value) return -1
|
||||
return a.strength - b.strength
|
||||
})
|
||||
cardToPlay = sortedHand[0]
|
||||
@@ -144,15 +134,14 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
const resolveTrick = async () => {
|
||||
const pCard = table.value.playerCard
|
||||
const oCard = table.value.opponentCard
|
||||
const trump = trumpCard.value.suit
|
||||
|
||||
let weWon = false
|
||||
|
||||
if (table.value.firstToPlay === 'player') {
|
||||
if (doesCardWin(oCard, pCard, trump)) weWon = false
|
||||
if (doesCardWin(oCard, pCard)) weWon = false
|
||||
else weWon = true
|
||||
} else {
|
||||
if (doesCardWin(pCard, oCard, trump)) weWon = true
|
||||
if (doesCardWin(pCard, oCard)) weWon = true
|
||||
else weWon = false
|
||||
}
|
||||
|
||||
@@ -169,7 +158,7 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
|
||||
table.value = { playerCard: null, opponentCard: null, firstToPlay: null }
|
||||
|
||||
if (deck.value.length > 0) {
|
||||
if (deck.value.length > 0 || trumpCard.value !== null) {
|
||||
if (weWon) {
|
||||
await drawOneCard('player')
|
||||
await drawOneCard('opponent')
|
||||
@@ -188,24 +177,29 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
}
|
||||
|
||||
const drawOneCard = async (who) => {
|
||||
if (deck.value.length === 0) return
|
||||
let cardToDraw = null
|
||||
|
||||
let card
|
||||
if (deck.value.length === 1) {
|
||||
card = deck.value.pop()
|
||||
if (deck.value.length > 0) {
|
||||
cardToDraw = deck.value.pop()
|
||||
} else if (trumpCard.value !== null) {
|
||||
cardToDraw = trumpCard.value
|
||||
trumpCard.value = null
|
||||
} else {
|
||||
card = deck.value.pop()
|
||||
return
|
||||
}
|
||||
|
||||
if (who === 'player') playerHand.value.push(card)
|
||||
else opponentHand.value.push(card)
|
||||
if (who === 'player') {
|
||||
playerHand.value.push(cardToDraw)
|
||||
} else {
|
||||
opponentHand.value.push(cardToDraw)
|
||||
}
|
||||
}
|
||||
|
||||
const doesCardWin = (attacker, defender, trumpSuit) => {
|
||||
if (attacker.suit === trumpSuit && defender.suit !== trumpSuit) return true
|
||||
const doesCardWin = (attacker, defender) => {
|
||||
const suit = trumpSuit.value
|
||||
|
||||
if (defender.suit === trumpSuit && attacker.suit !== trumpSuit) return false
|
||||
if (attacker.suit === suit && defender.suit !== suit) return true
|
||||
if (defender.suit === suit && attacker.suit !== suit) return false
|
||||
|
||||
if (attacker.suit === defender.suit) {
|
||||
return attacker.strength > defender.strength
|
||||
@@ -225,19 +219,15 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
|
||||
const dealInitialCards = async () => {
|
||||
const cardsToDeal = gameType.value === 9 ? 9 : 3
|
||||
|
||||
const dealSpeed = gameType.value === 9 ? 200 : 500
|
||||
|
||||
for (let i = 0; i < cardsToDeal; i++) {
|
||||
if (deck.value.length > 0) {
|
||||
const card = deck.value.pop()
|
||||
playerHand.value.push(card)
|
||||
playerHand.value.push(deck.value.pop())
|
||||
await wait(dealSpeed)
|
||||
}
|
||||
|
||||
if (deck.value.length > 0) {
|
||||
const card = deck.value.pop()
|
||||
opponentHand.value.push(card)
|
||||
opponentHand.value.push(deck.value.pop())
|
||||
await wait(dealSpeed)
|
||||
}
|
||||
}
|
||||
@@ -281,28 +271,23 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
opponentScore.value = 120
|
||||
playerScore.value = 0
|
||||
winner.value = 'opponent'
|
||||
|
||||
isGameRunning.value = false
|
||||
isGameOver.value = true
|
||||
}
|
||||
|
||||
const canPlayCard = (card) => {
|
||||
// 1. Se não for a minha vez, não posso jogar nada
|
||||
if (currentTurn.value !== 'player') return false
|
||||
|
||||
if (!table.value.opponentCard) return true
|
||||
|
||||
if (deck.value.length > 0) return true
|
||||
|
||||
const hasCardsToDraw = deck.value.length > 0 || trumpCard.value !== null
|
||||
if (hasCardsToDraw) return true
|
||||
|
||||
const suitToFollow = table.value.opponentCard.suit
|
||||
|
||||
const hasSuit = playerHand.value.some((c) => c.suit === suitToFollow)
|
||||
|
||||
if (hasSuit && card.suit !== suitToFollow) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -317,6 +302,7 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
opponentHand,
|
||||
isLoggingOut,
|
||||
trumpCard,
|
||||
trumpSuit,
|
||||
playerScore,
|
||||
opponentScore,
|
||||
currentTurn,
|
||||
|
||||
Reference in New Issue
Block a user