Fix Sikinia Parliament game end
This commit is contained in:
parent
2e75dfc30e
commit
6acfc2748b
1 changed files with 49 additions and 33 deletions
|
|
@ -90,38 +90,39 @@ const SikiniaParliamentPuzzle: React.FC<SikiniaParliamentPuzzleProps> = ({ showS
|
|||
|
||||
// Check for violations and update edge colors
|
||||
const updateViolations = useCallback(() => {
|
||||
setEdges(currentEdges => {
|
||||
return currentEdges.map(edge => {
|
||||
const person1 = people.find(p => p.id === edge.from);
|
||||
const person2 = people.find(p => p.id === edge.to);
|
||||
|
||||
if (!person1 || !person2 || person1.color === 'grey' || person2.color === 'grey') {
|
||||
return { ...edge, isViolation: false };
|
||||
}
|
||||
|
||||
if (person1.color === person2.color) {
|
||||
// Check if either person has more than 1 enemy of their color
|
||||
const person1Enemies = currentEdges.filter(e =>
|
||||
(e.from === person1.id || e.to === person1.id) &&
|
||||
e !== edge
|
||||
).map(e => e.from === person1.id ? e.to : e.from)
|
||||
.map(id => people.find(p => p.id === id))
|
||||
.filter(p => p && p.color === person1.color);
|
||||
|
||||
const person2Enemies = currentEdges.filter(e =>
|
||||
(e.from === person2.id || e.to === person2.id) &&
|
||||
e !== edge
|
||||
).map(e => e.from === person2.id ? e.to : e.from)
|
||||
.map(id => people.find(p => p.id === id))
|
||||
.filter(p => p && p.color === person2.color);
|
||||
|
||||
return { ...edge, isViolation: person1Enemies.length >= 1 || person2Enemies.length >= 1 };
|
||||
}
|
||||
|
||||
const updatedEdges = edges.map(edge => {
|
||||
const person1 = people.find(p => p.id === edge.from);
|
||||
const person2 = people.find(p => p.id === edge.to);
|
||||
|
||||
if (!person1 || !person2 || person1.color === 'grey' || person2.color === 'grey') {
|
||||
return { ...edge, isViolation: false };
|
||||
});
|
||||
}
|
||||
|
||||
if (person1.color === person2.color) {
|
||||
// Check if either person has more than 1 enemy of their color
|
||||
const person1Enemies = edges.filter(e =>
|
||||
(e.from === person1.id || e.to === person1.id) &&
|
||||
e !== edge
|
||||
).map(e => e.from === person1.id ? e.to : e.from)
|
||||
.map(id => people.find(p => p.id === id))
|
||||
.filter(p => p && p.color === person1.color);
|
||||
|
||||
const person2Enemies = edges.filter(e =>
|
||||
(e.from === person2.id || e.to === person2.id) &&
|
||||
e !== edge
|
||||
).map(e => e.from === person2.id ? e.to : e.from)
|
||||
.map(id => people.find(p => p.id === id))
|
||||
.filter(p => p && p.color === person2.color);
|
||||
|
||||
return { ...edge, isViolation: person1Enemies.length >= 1 || person2Enemies.length >= 1 };
|
||||
}
|
||||
|
||||
return { ...edge, isViolation: false };
|
||||
});
|
||||
}, [people]);
|
||||
|
||||
setEdges(updatedEdges);
|
||||
return updatedEdges;
|
||||
}, [people, edges]);
|
||||
|
||||
// Check if puzzle is solved
|
||||
const checkCompletion = useCallback(() => {
|
||||
|
|
@ -142,9 +143,24 @@ const SikiniaParliamentPuzzle: React.FC<SikiniaParliamentPuzzleProps> = ({ showS
|
|||
}, [people, edges, gameStarted, isComplete, timer, bestTime]);
|
||||
|
||||
useEffect(() => {
|
||||
updateViolations();
|
||||
checkCompletion();
|
||||
}, [people, updateViolations, checkCompletion]);
|
||||
const updatedEdges = updateViolations();
|
||||
|
||||
// Check completion with the updated edges
|
||||
if (gameStarted && !isComplete) {
|
||||
const allColored = people.every(p => p.color !== 'grey');
|
||||
const noViolations = updatedEdges.every(e => !e.isViolation);
|
||||
|
||||
if (allColored && noViolations) {
|
||||
setIsComplete(true);
|
||||
setIsRunning(false);
|
||||
|
||||
if (!bestTime || timer < bestTime) {
|
||||
setBestTime(timer);
|
||||
localStorage.setItem('sikinia-best-time', timer.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [people, updateViolations, gameStarted, isComplete, timer, bestTime]);
|
||||
|
||||
// Load best time from localStorage
|
||||
useEffect(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue