SyntaxSquad/DADProject#14 Bot works and play works, still has some bugs
This commit is contained in:
@@ -74,6 +74,10 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
if (!isGameRunning.value || currentTurn.value !== 'player') return
|
||||
if (table.value.playerCard) return
|
||||
|
||||
if (!canPlayCard(card)) {
|
||||
console.warn('Jogada Inválida! Tens de assistir ao naipe.')
|
||||
return
|
||||
}
|
||||
playerHand.value = playerHand.value.filter((c) => c.id !== card.id)
|
||||
table.value.playerCard = card
|
||||
|
||||
@@ -137,57 +141,67 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
}
|
||||
}
|
||||
|
||||
const resolveTrick = () => {
|
||||
const resolveTrick = async () => {
|
||||
const pCard = table.value.playerCard
|
||||
const oCard = table.value.opponentCard
|
||||
const trumpSuit = trumpCard.value.suit
|
||||
let trickWinner = ''
|
||||
const trump = trumpCard.value.suit
|
||||
|
||||
if (pCard.suit === trumpSuit && oCard.suit !== trumpSuit) trickWinner = 'player'
|
||||
else if (oCard.suit === trumpSuit && pCard.suit !== trumpSuit) trickWinner = 'opponent'
|
||||
else if (pCard.suit === oCard.suit) {
|
||||
trickWinner = getStrength(pCard.rank) > getStrength(oCard.rank) ? 'player' : 'opponent'
|
||||
let weWon = false
|
||||
|
||||
if (table.value.firstToPlay === 'player') {
|
||||
if (doesCardWin(oCard, pCard, trump)) weWon = false
|
||||
else weWon = true
|
||||
} else {
|
||||
trickWinner = table.value.firstToPlay
|
||||
if (doesCardWin(pCard, oCard, trump)) weWon = true
|
||||
else weWon = false
|
||||
}
|
||||
|
||||
const points = pCard.points + oCard.points
|
||||
|
||||
if (trickWinner === 'player') {
|
||||
if (weWon) {
|
||||
playerScore.value += points
|
||||
playerTricks.value++
|
||||
currentTurn.value = 'player'
|
||||
} else {
|
||||
opponentScore.value += points
|
||||
opponentTricks.value++
|
||||
currentTurn.value = 'opponent'
|
||||
}
|
||||
|
||||
table.value = { playerCard: null, opponentCard: null, firstToPlay: null }
|
||||
|
||||
if (
|
||||
deck.value.length === 0 &&
|
||||
playerHand.value.length === 0 &&
|
||||
opponentHand.value.length === 0
|
||||
) {
|
||||
if (deck.value.length > 0) {
|
||||
if (weWon) {
|
||||
await drawOneCard('player')
|
||||
await drawOneCard('opponent')
|
||||
} else {
|
||||
await drawOneCard('opponent')
|
||||
await drawOneCard('player')
|
||||
}
|
||||
} else if (playerHand.value.length === 0 && opponentHand.value.length === 0) {
|
||||
endGame()
|
||||
return
|
||||
}
|
||||
|
||||
if (deck.value.length > 0) {
|
||||
if (trickWinner === 'player') {
|
||||
playerHand.value.push(deck.value.pop())
|
||||
opponentHand.value.push(deck.value.pop())
|
||||
} else {
|
||||
opponentHand.value.push(deck.value.pop())
|
||||
playerHand.value.push(deck.value.pop())
|
||||
}
|
||||
}
|
||||
|
||||
currentTurn.value = trickWinner
|
||||
if (trickWinner === 'opponent') {
|
||||
if (currentTurn.value === 'opponent') {
|
||||
setTimeout(botPlayCard, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
const drawOneCard = async (who) => {
|
||||
if (deck.value.length === 0) return
|
||||
|
||||
let card
|
||||
if (deck.value.length === 1) {
|
||||
card = deck.value.pop()
|
||||
trumpCard.value = null
|
||||
} else {
|
||||
card = deck.value.pop()
|
||||
}
|
||||
|
||||
if (who === 'player') playerHand.value.push(card)
|
||||
else opponentHand.value.push(card)
|
||||
}
|
||||
|
||||
const doesCardWin = (attacker, defender, trumpSuit) => {
|
||||
if (attacker.suit === trumpSuit && defender.suit !== trumpSuit) return true
|
||||
|
||||
@@ -272,6 +286,26 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
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 suitToFollow = table.value.opponentCard.suit
|
||||
|
||||
const hasSuit = playerHand.value.some((c) => c.suit === suitToFollow)
|
||||
|
||||
if (hasSuit && card.suit !== suitToFollow) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
||||
|
||||
return {
|
||||
@@ -295,5 +329,6 @@ export const useBiscaStore = defineStore('bisca', () => {
|
||||
quitGame,
|
||||
resolveTrick,
|
||||
playerPlayCard,
|
||||
canPlayCard,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user