All files / web/src/arcade-games/matching/utils matchValidation.ts

0% Statements 0/222
0% Branches 0/1
0% Functions 0/1
0% Lines 0/222

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                                                                                                                                                                                                                                                                                                                                                                                                                                                             
import type { GameCard, MatchValidationResult } from '../types'

// Validate abacus-numeral match (abacus card matches with number card of same value)
export function validateAbacusNumeralMatch(
  card1: GameCard,
  card2: GameCard
): MatchValidationResult {
  // Both cards must have the same number
  if (card1.number !== card2.number) {
    return {
      isValid: false,
      reason: 'Numbers do not match',
      type: 'invalid',
    }
  }

  // Cards must be different types (one abacus, one number)
  if (card1.type === card2.type) {
    return {
      isValid: false,
      reason: 'Both cards are the same type',
      type: 'invalid',
    }
  }

  // One must be abacus, one must be number
  const hasAbacus = card1.type === 'abacus' || card2.type === 'abacus'
  const hasNumber = card1.type === 'number' || card2.type === 'number'

  if (!hasAbacus || !hasNumber) {
    return {
      isValid: false,
      reason: 'Must match abacus with number representation',
      type: 'invalid',
    }
  }

  // Neither should be complement type for this game mode
  if (card1.type === 'complement' || card2.type === 'complement') {
    return {
      isValid: false,
      reason: 'Complement cards not valid in abacus-numeral mode',
      type: 'invalid',
    }
  }

  return {
    isValid: true,
    type: 'abacus-numeral',
  }
}

// Validate complement match (two numbers that add up to target sum)
export function validateComplementMatch(card1: GameCard, card2: GameCard): MatchValidationResult {
  // Both cards must be complement type
  if (card1.type !== 'complement' || card2.type !== 'complement') {
    return {
      isValid: false,
      reason: 'Both cards must be complement type',
      type: 'invalid',
    }
  }

  // Both cards must have the same target sum
  if (card1.targetSum !== card2.targetSum) {
    return {
      isValid: false,
      reason: 'Cards have different target sums',
      type: 'invalid',
    }
  }

  // Check if the numbers are actually complements
  if (!card1.complement || !card2.complement) {
    return {
      isValid: false,
      reason: 'Complement information missing',
      type: 'invalid',
    }
  }

  // Verify the complement relationship
  if (card1.number !== card2.complement || card2.number !== card1.complement) {
    return {
      isValid: false,
      reason: 'Numbers are not complements of each other',
      type: 'invalid',
    }
  }

  // Verify the sum equals the target
  const sum = card1.number + card2.number
  if (sum !== card1.targetSum) {
    return {
      isValid: false,
      reason: `Sum ${sum} does not equal target ${card1.targetSum}`,
      type: 'invalid',
    }
  }

  return {
    isValid: true,
    type: 'complement',
  }
}

// Main validation function that determines which validation to use
export function validateMatch(card1: GameCard, card2: GameCard): MatchValidationResult {
  // Cannot match the same card with itself
  if (card1.id === card2.id) {
    return {
      isValid: false,
      reason: 'Cannot match card with itself',
      type: 'invalid',
    }
  }

  // Cannot match already matched cards
  if (card1.matched || card2.matched) {
    return {
      isValid: false,
      reason: 'Cannot match already matched cards',
      type: 'invalid',
    }
  }

  // Determine which type of match to validate based on card types
  const hasComplement = card1.type === 'complement' || card2.type === 'complement'

  if (hasComplement) {
    // If either card is complement type, use complement validation
    return validateComplementMatch(card1, card2)
  } else {
    // Otherwise, use abacus-numeral validation
    return validateAbacusNumeralMatch(card1, card2)
  }
}

// Helper function to check if a card can be flipped
export function canFlipCard(
  card: GameCard,
  flippedCards: GameCard[],
  isProcessingMove: boolean
): boolean {
  // Cannot flip if processing a move
  if (isProcessingMove) return false

  // Cannot flip already matched cards
  if (card.matched) return false

  // Cannot flip if already flipped
  if (flippedCards.some((c) => c.id === card.id)) return false

  // Cannot flip if two cards are already flipped
  if (flippedCards.length >= 2) return false

  return true
}

// Get hint for what kind of match the player should look for
export function getMatchHint(card: GameCard): string {
  switch (card.type) {
    case 'abacus':
      return `Find the number ${card.number}`

    case 'number':
      return `Find the abacus showing ${card.number}`

    case 'complement':
      if (card.complement !== undefined && card.targetSum !== undefined) {
        return `Find ${card.complement} to make ${card.targetSum}`
      }
      return 'Find the matching complement'

    default:
      return 'Find the matching card'
  }
}

// Calculate match score based on difficulty and time
export function calculateMatchScore(
  difficulty: number,
  timeForMatch: number,
  isComplementMatch: boolean
): number {
  const baseScore = isComplementMatch ? 15 : 10 // Complement matches worth more
  const difficultyMultiplier = difficulty / 6 // Scale with difficulty
  const timeBonus = Math.max(0, (10000 - timeForMatch) / 1000) // Bonus for speed

  return Math.round(baseScore * difficultyMultiplier + timeBonus)
}

// Analyze game performance
export function analyzeGamePerformance(
  totalMoves: number,
  matchedPairs: number,
  totalPairs: number,
  gameTime: number
): {
  accuracy: number
  efficiency: number
  averageTimePerMove: number
  grade: 'A' | 'B' | 'C' | 'D' | 'F'
} {
  const accuracy = totalMoves > 0 ? (matchedPairs / totalMoves) * 100 : 0
  const efficiency = totalPairs > 0 ? (matchedPairs / (totalPairs * 2)) * 100 : 0 // Ideal is 100% (each pair found in 2 moves)
  const averageTimePerMove = totalMoves > 0 ? gameTime / totalMoves : 0

  // Calculate grade based on accuracy and efficiency
  let grade: 'A' | 'B' | 'C' | 'D' | 'F' = 'F'
  if (accuracy >= 90 && efficiency >= 80) grade = 'A'
  else if (accuracy >= 80 && efficiency >= 70) grade = 'B'
  else if (accuracy >= 70 && efficiency >= 60) grade = 'C'
  else if (accuracy >= 60 && efficiency >= 50) grade = 'D'

  return {
    accuracy,
    efficiency,
    averageTimePerMove,
    grade,
  }
}