Refine AI strategy in guessing game
The AI's strategy for answering questions in the guessing game has been refined to be more strategically optimal. Previously, when asked "is the number not divisible by 7?" and the answer was "No", the AI would grey out all numbers not divisible by 7. This commit corrects the logic to grey out only the multiples of 7, aligning with the "No" answer to the negated question. This ensures that the AI's responses are more precise and lead to a more efficient search space reduction.
This commit is contained in:
parent
591b73c071
commit
85b73a97c2
1 changed files with 51 additions and 7 deletions
|
|
@ -55,17 +55,31 @@ const QuestionGuessingMode: React.FC<QuestionGuessingModeProps> = ({
|
||||||
if (baseQuestion.includes('is') && baseQuestion.includes('number')) {
|
if (baseQuestion.includes('is') && baseQuestion.includes('number')) {
|
||||||
// Is the number...
|
// Is the number...
|
||||||
if (baseQuestion.includes('even')) {
|
if (baseQuestion.includes('even')) {
|
||||||
// Count how many valid numbers are even vs odd
|
|
||||||
const evenCount = Array.from(validNumbers).filter(n => n % 2 === 0).length;
|
const evenCount = Array.from(validNumbers).filter(n => n % 2 === 0).length;
|
||||||
const oddCount = validNumbers.size - evenCount;
|
const oddCount = validNumbers.size - evenCount;
|
||||||
// Choose answer that keeps more numbers
|
|
||||||
const answer = evenCount > oddCount;
|
let answer: boolean;
|
||||||
|
if (isNegated) {
|
||||||
|
// Question is "Is it NOT even?" - answer "Yes" if odd numbers are more
|
||||||
|
answer = oddCount > evenCount;
|
||||||
|
} else {
|
||||||
|
// Question is "Is it even?" - answer "Yes" if even numbers are more
|
||||||
|
answer = evenCount > oddCount;
|
||||||
|
}
|
||||||
return { understood: true, answer, questionType: 'even', isNegated };
|
return { understood: true, answer, questionType: 'even', isNegated };
|
||||||
}
|
}
|
||||||
if (baseQuestion.includes('odd')) {
|
if (baseQuestion.includes('odd')) {
|
||||||
const oddCount = Array.from(validNumbers).filter(n => n % 2 !== 0).length;
|
const oddCount = Array.from(validNumbers).filter(n => n % 2 !== 0).length;
|
||||||
const evenCount = validNumbers.size - oddCount;
|
const evenCount = validNumbers.size - oddCount;
|
||||||
const answer = oddCount > evenCount;
|
|
||||||
|
let answer: boolean;
|
||||||
|
if (isNegated) {
|
||||||
|
// Question is "Is it NOT odd?" - answer "Yes" if even numbers are more
|
||||||
|
answer = evenCount > oddCount;
|
||||||
|
} else {
|
||||||
|
// Question is "Is it odd?" - answer "Yes" if odd numbers are more
|
||||||
|
answer = oddCount > evenCount;
|
||||||
|
}
|
||||||
return { understood: true, answer, questionType: 'odd', isNegated };
|
return { understood: true, answer, questionType: 'odd', isNegated };
|
||||||
}
|
}
|
||||||
if (baseQuestion.includes('prime')) {
|
if (baseQuestion.includes('prime')) {
|
||||||
|
|
@ -78,13 +92,29 @@ const QuestionGuessingMode: React.FC<QuestionGuessingModeProps> = ({
|
||||||
};
|
};
|
||||||
const primeCount = Array.from(validNumbers).filter(n => isPrime(n)).length;
|
const primeCount = Array.from(validNumbers).filter(n => isPrime(n)).length;
|
||||||
const nonPrimeCount = validNumbers.size - primeCount;
|
const nonPrimeCount = validNumbers.size - primeCount;
|
||||||
const answer = primeCount > nonPrimeCount;
|
|
||||||
|
let answer: boolean;
|
||||||
|
if (isNegated) {
|
||||||
|
// Question is "Is it NOT prime?" - answer "Yes" if non-primes are more
|
||||||
|
answer = nonPrimeCount > primeCount;
|
||||||
|
} else {
|
||||||
|
// Question is "Is it prime?" - answer "Yes" if primes are more
|
||||||
|
answer = primeCount > nonPrimeCount;
|
||||||
|
}
|
||||||
return { understood: true, answer, questionType: 'prime', isNegated };
|
return { understood: true, answer, questionType: 'prime', isNegated };
|
||||||
}
|
}
|
||||||
if (baseQuestion.includes('perfect square')) {
|
if (baseQuestion.includes('perfect square')) {
|
||||||
const perfectSquareCount = Array.from(validNumbers).filter(n => Math.sqrt(n) % 1 === 0).length;
|
const perfectSquareCount = Array.from(validNumbers).filter(n => Math.sqrt(n) % 1 === 0).length;
|
||||||
const nonPerfectSquareCount = validNumbers.size - perfectSquareCount;
|
const nonPerfectSquareCount = validNumbers.size - perfectSquareCount;
|
||||||
const answer = perfectSquareCount > nonPerfectSquareCount;
|
|
||||||
|
let answer: boolean;
|
||||||
|
if (isNegated) {
|
||||||
|
// Question is "Is it NOT a perfect square?" - answer "Yes" if non-perfect squares are more
|
||||||
|
answer = nonPerfectSquareCount > perfectSquareCount;
|
||||||
|
} else {
|
||||||
|
// Question is "Is it a perfect square?" - answer "Yes" if perfect squares are more
|
||||||
|
answer = perfectSquareCount > nonPerfectSquareCount;
|
||||||
|
}
|
||||||
return { understood: true, answer, questionType: 'perfectSquare', isNegated };
|
return { understood: true, answer, questionType: 'perfectSquare', isNegated };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -96,7 +126,21 @@ const QuestionGuessingMode: React.FC<QuestionGuessingModeProps> = ({
|
||||||
const divisor = parseInt(matches[1]);
|
const divisor = parseInt(matches[1]);
|
||||||
const divisibleCount = Array.from(validNumbers).filter(n => n % divisor === 0).length;
|
const divisibleCount = Array.from(validNumbers).filter(n => n % divisor === 0).length;
|
||||||
const nonDivisibleCount = validNumbers.size - divisibleCount;
|
const nonDivisibleCount = validNumbers.size - divisibleCount;
|
||||||
const answer = divisibleCount > nonDivisibleCount;
|
|
||||||
|
// For negated questions, we need to think about which answer keeps more numbers
|
||||||
|
let answer: boolean;
|
||||||
|
if (isNegated) {
|
||||||
|
// Question is "Is it NOT divisible by X?"
|
||||||
|
// If we answer "Yes" (it is NOT divisible), we keep non-divisible numbers
|
||||||
|
// If we answer "No" (it IS divisible), we keep divisible numbers
|
||||||
|
answer = nonDivisibleCount > divisibleCount; // Answer "Yes" if non-divisible are more
|
||||||
|
} else {
|
||||||
|
// Question is "Is it divisible by X?"
|
||||||
|
// If we answer "Yes" (it is divisible), we keep divisible numbers
|
||||||
|
// If we answer "No" (it is NOT divisible), we keep non-divisible numbers
|
||||||
|
answer = divisibleCount > nonDivisibleCount; // Answer "Yes" if divisible are more
|
||||||
|
}
|
||||||
|
|
||||||
return { understood: true, answer, questionType: 'divisible', value: divisor, isNegated };
|
return { understood: true, answer, questionType: 'divisible', value: divisor, isNegated };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue