All files / web/src/arcade-games/rithmomachia/utils relationEngine.ts

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

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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import type { RelationKind } from '../types'

/**
 * Relation checking engine for Rithmomachia captures.
 * All arithmetic uses BigInt for precision with large values.
 */

export interface RelationCheckResult {
  valid: boolean
  relation?: RelationKind
  explanation?: string
}

/**
 * Check if two values satisfy the EQUAL relation.
 * a == b
 */
export function checkEqual(a: number, b: number): RelationCheckResult {
  if (a === b) {
    return {
      valid: true,
      relation: 'EQUAL',
      explanation: `${a} == ${b}`,
    }
  }
  return {
    valid: false,
    explanation: `${a} ≠ ${b} (values are not equal)`,
  }
}

/**
 * Check if two values satisfy the MULTIPLE relation.
 * a % b == 0 (a is a multiple of b)
 */
export function checkMultiple(a: number, b: number): RelationCheckResult {
  if (b === 0) return { valid: false, explanation: 'Cannot check multiple with zero' }
  if (a % b === 0) {
    return {
      valid: true,
      relation: 'MULTIPLE',
      explanation: `${a} is a multiple of ${b} (${a}÷${b}=${a / b})`,
    }
  }
  return {
    valid: false,
    explanation: `${a} is not a multiple of ${b} (${a}÷${b}=${(a / b).toFixed(2)}...)`,
  }
}

/**
 * Check if two values satisfy the DIVISOR relation.
 * b % a == 0 (a is a divisor of b)
 */
export function checkDivisor(a: number, b: number): RelationCheckResult {
  if (a === 0) return { valid: false, explanation: 'Cannot divide by zero' }
  if (b % a === 0) {
    return {
      valid: true,
      relation: 'DIVISOR',
      explanation: `${a} divides ${b} (${b}÷${a}=${b / a})`,
    }
  }
  return {
    valid: false,
    explanation: `${a} does not divide ${b} evenly (${b}÷${a}=${(b / a).toFixed(2)}...)`,
  }
}

/**
 * Check if three values satisfy the SUM relation.
 * a + h == b OR b + h == a
 */
export function checkSum(a: number, b: number, h: number): RelationCheckResult {
  if (a + h === b) {
    return {
      valid: true,
      relation: 'SUM',
      explanation: `${a} + ${h} = ${b}`,
    }
  }
  if (b + h === a) {
    return {
      valid: true,
      relation: 'SUM',
      explanation: `${b} + ${h} = ${a}`,
    }
  }
  return {
    valid: false,
    explanation: `Helper ${h} doesn't satisfy sum (need ${Math.abs(b - a)} but got ${h})`,
  }
}

/**
 * Check if three values satisfy the DIFF relation.
 * |a - h| == b OR |b - h| == a
 */
export function checkDiff(a: number, b: number, h: number): RelationCheckResult {
  const abs = (x: number) => (x < 0 ? -x : x)

  const diff1 = abs(a - h)
  if (diff1 === b) {
    return {
      valid: true,
      relation: 'DIFF',
      explanation: `|${a} - ${h}| = ${b}`,
    }
  }

  const diff2 = abs(b - h)
  if (diff2 === a) {
    return {
      valid: true,
      relation: 'DIFF',
      explanation: `|${b} - ${h}| = ${a}`,
    }
  }

  return {
    valid: false,
    explanation: `Helper ${h} doesn't satisfy difference (|${a}-${h}|=${diff1}, |${b}-${h}|=${diff2})`,
  }
}

/**
 * Check if three values satisfy the PRODUCT relation.
 * a * h == b OR b * h == a
 */
export function checkProduct(a: number, b: number, h: number): RelationCheckResult {
  if (a * h === b) {
    return {
      valid: true,
      relation: 'PRODUCT',
      explanation: `${a} × ${h} = ${b}`,
    }
  }
  if (b * h === a) {
    return {
      valid: true,
      relation: 'PRODUCT',
      explanation: `${b} × ${h} = ${a}`,
    }
  }
  const needed1 = a === 0 ? 'undefined' : (b / a).toFixed(2)
  const needed2 = b === 0 ? 'undefined' : (a / b).toFixed(2)
  return {
    valid: false,
    explanation: `Helper ${h} doesn't satisfy product (need ${needed1} or ${needed2})`,
  }
}

/**
 * Check if three values satisfy the RATIO relation.
 * a * r == b OR b * r == a (where r is the helper value)
 * This is similar to PRODUCT but with explicit ratio semantics.
 */
export function checkRatio(a: number, b: number, r: number): RelationCheckResult {
  if (r === 0) return { valid: false, explanation: 'Cannot use zero as ratio helper' }

  if (a * r === b) {
    return {
      valid: true,
      relation: 'RATIO',
      explanation: `${a} × ${r} = ${b}`,
    }
  }
  if (b * r === a) {
    return {
      valid: true,
      relation: 'RATIO',
      explanation: `${b} × ${r} = ${a}`,
    }
  }
  const needed1 = a === 0 ? 'undefined' : (b / a).toFixed(2)
  const needed2 = b === 0 ? 'undefined' : (a / b).toFixed(2)
  return {
    valid: false,
    explanation: `Helper ${r} doesn't satisfy ratio (need ${needed1} or ${needed2})`,
  }
}

/**
 * Check if a relation holds between mover and target values.
 * Returns the first valid relation found, or null if none.
 */
export function checkRelation(
  relation: RelationKind,
  moverValue: number,
  targetValue: number,
  helperValue?: number
): RelationCheckResult {
  switch (relation) {
    case 'EQUAL':
      return checkEqual(moverValue, targetValue)

    case 'MULTIPLE':
      return checkMultiple(moverValue, targetValue)

    case 'DIVISOR':
      return checkDivisor(moverValue, targetValue)

    case 'SUM':
      if (helperValue === undefined) {
        return { valid: false, explanation: 'SUM requires a helper' }
      }
      return checkSum(moverValue, targetValue, helperValue)

    case 'DIFF':
      if (helperValue === undefined) {
        return { valid: false, explanation: 'DIFF requires a helper' }
      }
      return checkDiff(moverValue, targetValue, helperValue)

    case 'PRODUCT':
      if (helperValue === undefined) {
        return { valid: false, explanation: 'PRODUCT requires a helper' }
      }
      return checkProduct(moverValue, targetValue, helperValue)

    case 'RATIO':
      if (helperValue === undefined) {
        return { valid: false, explanation: 'RATIO requires a helper' }
      }
      return checkRatio(moverValue, targetValue, helperValue)

    default:
      return { valid: false, explanation: 'Unknown relation type' }
  }
}

/**
 * Find all valid relations between two values (without helper).
 * Returns an array of valid relations.
 */
export function findValidRelationsNoHelper(a: number, b: number): RelationKind[] {
  const valid: RelationKind[] = []

  if (checkEqual(a, b).valid) valid.push('EQUAL')
  if (checkMultiple(a, b).valid) valid.push('MULTIPLE')
  if (checkDivisor(a, b).valid) valid.push('DIVISOR')

  return valid
}

/**
 * Find all valid relations between two values WITH a helper.
 * Returns an array of valid relations.
 */
export function findValidRelationsWithHelper(a: number, b: number, h: number): RelationKind[] {
  const valid: RelationKind[] = []

  // First check no-helper relations
  valid.push(...findValidRelationsNoHelper(a, b))

  // Then check helper-based relations
  if (checkSum(a, b, h).valid) valid.push('SUM')
  if (checkDiff(a, b, h).valid) valid.push('DIFF')
  if (checkProduct(a, b, h).valid) valid.push('PRODUCT')
  if (checkRatio(a, b, h).valid) valid.push('RATIO')

  return valid
}

/**
 * Check if ANY relation holds between mover and target (no helper).
 * Returns the first valid relation or null.
 */
export function findAnyValidRelation(a: number, b: number): RelationCheckResult | null {
  const relations = findValidRelationsNoHelper(a, b)
  if (relations.length > 0) {
    return checkRelation(relations[0], a, b)
  }
  return null
}

/**
 * Check if ANY relation holds between mover and target WITH a helper.
 * Returns the first valid relation or null.
 */
export function findAnyValidRelationWithHelper(
  a: number,
  b: number,
  h: number
): RelationCheckResult | null {
  const relations = findValidRelationsWithHelper(a, b, h)
  if (relations.length > 0) {
    return checkRelation(relations[0], a, b, h)
  }
  return null
}

/**
 * Format a BigInt value for display (commas for readability).
 */
export function formatValue(value: number): string {
  return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}