SyntaxSquad/DADProject#14 feature/frontend-gameboard-singleplayer #75
@@ -1,6 +1,7 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="relative flex items-center justify-center w-64 h-48">
|
<div class="relative flex items-center justify-center w-64 h-48">
|
||||||
<div
|
<div
|
||||||
|
v-if="trumpCard"
|
||||||
class="absolute rotate-90 origin-center transition-all duration-700"
|
class="absolute rotate-90 origin-center transition-all duration-700"
|
||||||
:class="[trumpReveal && 'scale-110']"
|
:class="[trumpReveal && 'scale-110']"
|
||||||
>
|
>
|
||||||
@@ -68,7 +69,7 @@ import GameCard from './GameCard.vue'
|
|||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
trumpCard: {
|
trumpCard: {
|
||||||
type: Object,
|
type: Object,
|
||||||
required: true,
|
default: null,
|
||||||
},
|
},
|
||||||
cardsRemaining: {
|
cardsRemaining: {
|
||||||
type: Number,
|
type: Number,
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
<template>
|
<template>
|
||||||
<div class="relative min-h-screen overflow-hidden">
|
<div class="relative min-h-screen overflow-hidden">
|
||||||
<div v-if="store.trumpCard">
|
|
||||||
|
<div v-if="store.isGameRunning">
|
||||||
<GameBoard
|
<GameBoard
|
||||||
:trump-card="store.trumpCard"
|
:trump-card="store.trumpCard"
|
||||||
:cards-remaining="store.deck.length"
|
:cards-remaining="store.deck.length + (store.trumpCard ? 1 : 0)"
|
||||||
:player-hand="store.playerHand"
|
:player-hand="store.playerHand"
|
||||||
:opponent-hand="store.opponentHand"
|
:opponent-hand="store.opponentHand"
|
||||||
:player-score="store.playerScore"
|
:player-score="store.playerScore"
|
||||||
@@ -15,7 +16,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div v-else class="flex h-screen items-center justify-center bg-green-900 text-white">
|
<div v-else class="flex h-screen items-center justify-center bg-green-900 text-white">
|
||||||
<div class="animate-pulse text-xl">A preparar baralho...</div>
|
<div class="animate-pulse text-xl">A preparar jogo...</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<GameOver
|
<GameOver
|
||||||
|
|||||||
@@ -11,15 +11,15 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
const deck = ref([])
|
const deck = ref([])
|
||||||
const playerHand = ref([])
|
const playerHand = ref([])
|
||||||
const opponentHand = ref([])
|
const opponentHand = ref([])
|
||||||
|
|
||||||
const trumpCard = ref(null)
|
const trumpCard = ref(null)
|
||||||
|
const trumpSuit = ref('')
|
||||||
|
|
||||||
const playerScore = ref(0)
|
const playerScore = ref(0)
|
||||||
const opponentScore = ref(0)
|
const opponentScore = ref(0)
|
||||||
|
|
||||||
const currentTurn = ref('player')
|
const currentTurn = ref('player')
|
||||||
|
|
||||||
const playerTricks = ref(0)
|
const playerTricks = ref(0)
|
||||||
const opponentTricks = ref(0)
|
const opponentTricks = ref(0)
|
||||||
|
|
||||||
const winner = ref(null)
|
const winner = ref(null)
|
||||||
|
|
||||||
const table = ref({
|
const table = ref({
|
||||||
@@ -41,33 +41,27 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
opponentScore.value = 0
|
opponentScore.value = 0
|
||||||
playerTricks.value = 0
|
playerTricks.value = 0
|
||||||
opponentTricks.value = 0
|
opponentTricks.value = 0
|
||||||
|
|
||||||
deck.value = []
|
deck.value = []
|
||||||
playerHand.value = []
|
playerHand.value = []
|
||||||
opponentHand.value = []
|
opponentHand.value = []
|
||||||
trumpCard.value = null
|
trumpCard.value = null
|
||||||
|
trumpSuit.value = ''
|
||||||
currentTurn.value = 'player'
|
currentTurn.value = 'player'
|
||||||
|
table.value = { playerCard: null, opponentCard: null, firstToPlay: null }
|
||||||
table.value = {
|
|
||||||
playerCard: null,
|
|
||||||
opponentCard: null,
|
|
||||||
firstToPlay: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
deck.value = createDeck()
|
deck.value = createDeck()
|
||||||
|
|
||||||
trumpCard.value = deck.value[0]
|
const lastCard = deck.value.pop()
|
||||||
|
trumpCard.value = lastCard
|
||||||
|
trumpSuit.value = lastCard.suit
|
||||||
|
|
||||||
await wait(500)
|
await wait(500)
|
||||||
|
|
||||||
await dealInitialCards()
|
await dealInitialCards()
|
||||||
|
isDealing.value = false
|
||||||
|
|
||||||
if (currentTurn.value === 'opponent') {
|
if (currentTurn.value === 'opponent') {
|
||||||
setTimeout(botPlayCard, 1000)
|
setTimeout(botPlayCard, 1000)
|
||||||
}
|
}
|
||||||
|
|
||||||
isDealing.value = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const playerPlayCard = (card) => {
|
const playerPlayCard = (card) => {
|
||||||
@@ -94,11 +88,8 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
|
|
||||||
if (table.value.playerCard) {
|
if (table.value.playerCard) {
|
||||||
const pCard = table.value.playerCard
|
const pCard = table.value.playerCard
|
||||||
const trump = trumpCard.value.suit
|
|
||||||
|
|
||||||
const winningCards = opponentHand.value.filter((botCard) =>
|
const winningCards = opponentHand.value.filter((botCard) => doesCardWin(botCard, pCard))
|
||||||
doesCardWin(botCard, pCard, trump),
|
|
||||||
)
|
|
||||||
|
|
||||||
if (winningCards.length > 0) {
|
if (winningCards.length > 0) {
|
||||||
winningCards.sort((a, b) => a.strength - b.strength)
|
winningCards.sort((a, b) => a.strength - b.strength)
|
||||||
@@ -112,9 +103,8 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const sortedHand = [...opponentHand.value].sort((a, b) => {
|
const sortedHand = [...opponentHand.value].sort((a, b) => {
|
||||||
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 !== trumpCard.value.suit && b.suit === trumpCard.value.suit) return -1
|
if (a.suit !== trumpSuit.value && b.suit === trumpSuit.value) return -1
|
||||||
|
|
||||||
return a.strength - b.strength
|
return a.strength - b.strength
|
||||||
})
|
})
|
||||||
cardToPlay = sortedHand[0]
|
cardToPlay = sortedHand[0]
|
||||||
@@ -144,15 +134,14 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
const resolveTrick = async () => {
|
const resolveTrick = async () => {
|
||||||
const pCard = table.value.playerCard
|
const pCard = table.value.playerCard
|
||||||
const oCard = table.value.opponentCard
|
const oCard = table.value.opponentCard
|
||||||
const trump = trumpCard.value.suit
|
|
||||||
|
|
||||||
let weWon = false
|
let weWon = false
|
||||||
|
|
||||||
if (table.value.firstToPlay === 'player') {
|
if (table.value.firstToPlay === 'player') {
|
||||||
if (doesCardWin(oCard, pCard, trump)) weWon = false
|
if (doesCardWin(oCard, pCard)) weWon = false
|
||||||
else weWon = true
|
else weWon = true
|
||||||
} else {
|
} else {
|
||||||
if (doesCardWin(pCard, oCard, trump)) weWon = true
|
if (doesCardWin(pCard, oCard)) weWon = true
|
||||||
else weWon = false
|
else weWon = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +158,7 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
|
|
||||||
table.value = { playerCard: null, opponentCard: null, firstToPlay: null }
|
table.value = { playerCard: null, opponentCard: null, firstToPlay: null }
|
||||||
|
|
||||||
if (deck.value.length > 0) {
|
if (deck.value.length > 0 || trumpCard.value !== null) {
|
||||||
if (weWon) {
|
if (weWon) {
|
||||||
await drawOneCard('player')
|
await drawOneCard('player')
|
||||||
await drawOneCard('opponent')
|
await drawOneCard('opponent')
|
||||||
@@ -188,24 +177,29 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const drawOneCard = async (who) => {
|
const drawOneCard = async (who) => {
|
||||||
if (deck.value.length === 0) return
|
let cardToDraw = null
|
||||||
|
|
||||||
let card
|
if (deck.value.length > 0) {
|
||||||
if (deck.value.length === 1) {
|
cardToDraw = deck.value.pop()
|
||||||
card = deck.value.pop()
|
} else if (trumpCard.value !== null) {
|
||||||
|
cardToDraw = trumpCard.value
|
||||||
trumpCard.value = null
|
trumpCard.value = null
|
||||||
} else {
|
} else {
|
||||||
card = deck.value.pop()
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (who === 'player') playerHand.value.push(card)
|
if (who === 'player') {
|
||||||
else opponentHand.value.push(card)
|
playerHand.value.push(cardToDraw)
|
||||||
|
} else {
|
||||||
|
opponentHand.value.push(cardToDraw)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const doesCardWin = (attacker, defender, trumpSuit) => {
|
const doesCardWin = (attacker, defender) => {
|
||||||
if (attacker.suit === trumpSuit && defender.suit !== trumpSuit) return true
|
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) {
|
if (attacker.suit === defender.suit) {
|
||||||
return attacker.strength > defender.strength
|
return attacker.strength > defender.strength
|
||||||
@@ -225,19 +219,15 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
|
|
||||||
const dealInitialCards = async () => {
|
const dealInitialCards = async () => {
|
||||||
const cardsToDeal = gameType.value === 9 ? 9 : 3
|
const cardsToDeal = gameType.value === 9 ? 9 : 3
|
||||||
|
|
||||||
const dealSpeed = gameType.value === 9 ? 200 : 500
|
const dealSpeed = gameType.value === 9 ? 200 : 500
|
||||||
|
|
||||||
for (let i = 0; i < cardsToDeal; i++) {
|
for (let i = 0; i < cardsToDeal; i++) {
|
||||||
if (deck.value.length > 0) {
|
if (deck.value.length > 0) {
|
||||||
const card = deck.value.pop()
|
playerHand.value.push(deck.value.pop())
|
||||||
playerHand.value.push(card)
|
|
||||||
await wait(dealSpeed)
|
await wait(dealSpeed)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deck.value.length > 0) {
|
if (deck.value.length > 0) {
|
||||||
const card = deck.value.pop()
|
opponentHand.value.push(deck.value.pop())
|
||||||
opponentHand.value.push(card)
|
|
||||||
await wait(dealSpeed)
|
await wait(dealSpeed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -281,28 +271,23 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
opponentScore.value = 120
|
opponentScore.value = 120
|
||||||
playerScore.value = 0
|
playerScore.value = 0
|
||||||
winner.value = 'opponent'
|
winner.value = 'opponent'
|
||||||
|
|
||||||
isGameRunning.value = false
|
isGameRunning.value = false
|
||||||
isGameOver.value = true
|
isGameOver.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
const canPlayCard = (card) => {
|
const canPlayCard = (card) => {
|
||||||
// 1. Se não for a minha vez, não posso jogar nada
|
|
||||||
if (currentTurn.value !== 'player') return false
|
if (currentTurn.value !== 'player') return false
|
||||||
|
|
||||||
if (!table.value.opponentCard) return true
|
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 suitToFollow = table.value.opponentCard.suit
|
||||||
|
|
||||||
const hasSuit = playerHand.value.some((c) => c.suit === suitToFollow)
|
const hasSuit = playerHand.value.some((c) => c.suit === suitToFollow)
|
||||||
|
|
||||||
if (hasSuit && card.suit !== suitToFollow) {
|
if (hasSuit && card.suit !== suitToFollow) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -317,6 +302,7 @@ export const useBiscaStore = defineStore('bisca', () => {
|
|||||||
opponentHand,
|
opponentHand,
|
||||||
isLoggingOut,
|
isLoggingOut,
|
||||||
trumpCard,
|
trumpCard,
|
||||||
|
trumpSuit,
|
||||||
playerScore,
|
playerScore,
|
||||||
opponentScore,
|
opponentScore,
|
||||||
currentTurn,
|
currentTurn,
|
||||||
|
|||||||
Reference in New Issue
Block a user