generated from skedwards88/template-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc70739
commit 797ad37
Showing
5 changed files
with
441 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
function sharedWordsQ(words1, words2) { | ||
for (const word of words1) { | ||
if (words2.has(word)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
export function findIncompatibleShapeIdIfAny( | ||
wordsForShape1, | ||
wordsForShape2, | ||
shapeId1, | ||
shapeId2, | ||
) { | ||
// If the shapes share a word, remove the shape that has more words | ||
if (sharedWordsQ(wordsForShape1, wordsForShape2)) { | ||
if (wordsForShape1.size <= wordsForShape2.size) { | ||
return shapeId2; | ||
} else { | ||
return shapeId1; | ||
} | ||
} | ||
|
||
// If the singular and plural form of a word are both in the game, | ||
// remove the shape that has the plural form. | ||
// This isn't perfect because it doesn't account for irregular plurals | ||
// and because it doesn't account for words that end in "S" that aren't plural, | ||
// but it should catch most cases that are likely to be present in the game. | ||
for (const word of wordsForShape1) { | ||
if (wordsForShape2.has(word + "S")) { | ||
return shapeId2; | ||
} | ||
} | ||
for (const word of wordsForShape2) { | ||
if (wordsForShape1.has(word + "S")) { | ||
return shapeId1; | ||
} | ||
} | ||
|
||
// Otherwise, the shapes are compatible. Return undefined. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {findIncompatibleShapeIdIfAny} from "./findIncompatibleShapeIdIfAny"; | ||
|
||
test("If two shapes share a word, the ID for the shape with the most words is returned", () => { | ||
const wordsForShape1 = new Set(["CAT", "DOG"]); | ||
const wordsForShape2 = new Set(["COW", "HORSE", "DOG"]); | ||
|
||
expect( | ||
findIncompatibleShapeIdIfAny( | ||
wordsForShape1, | ||
wordsForShape2, | ||
"1-1-1", | ||
"2-2-2", | ||
), | ||
).toBe("2-2-2"); | ||
|
||
expect( | ||
findIncompatibleShapeIdIfAny( | ||
wordsForShape2, | ||
wordsForShape1, | ||
"1-1-1", | ||
"2-2-2", | ||
), | ||
).toBe("1-1-1"); | ||
}); | ||
|
||
test("If two shapes share a word, and the shapes are the same length, the ID for the second shape is returned", () => { | ||
const wordsForShape1 = new Set(["CAT", "DOG", "ANIMAL"]); | ||
const wordsForShape2 = new Set(["COW", "HORSE", "DOG"]); | ||
|
||
expect( | ||
findIncompatibleShapeIdIfAny( | ||
wordsForShape1, | ||
wordsForShape2, | ||
"1-1-1", | ||
"2-2-2", | ||
), | ||
).toBe("2-2-2"); | ||
|
||
expect( | ||
findIncompatibleShapeIdIfAny( | ||
wordsForShape2, | ||
wordsForShape1, | ||
"1-1-1", | ||
"2-2-2", | ||
), | ||
).toBe("2-2-2"); | ||
}); | ||
|
||
test("If two shapes share a word with 'S' appended to one, the ID for the shape with the 'S' appended to the word is returned", () => { | ||
const wordsForShape1 = new Set(["CAT", "DOGS"]); | ||
const wordsForShape2 = new Set(["COW", "HORSE", "DOG"]); | ||
|
||
expect( | ||
findIncompatibleShapeIdIfAny( | ||
wordsForShape1, | ||
wordsForShape2, | ||
"1-1-1", | ||
"2-2-2", | ||
), | ||
).toBe("1-1-1"); | ||
|
||
expect( | ||
findIncompatibleShapeIdIfAny( | ||
wordsForShape2, | ||
wordsForShape1, | ||
"1-1-1", | ||
"2-2-2", | ||
), | ||
).toBe("2-2-2"); | ||
}); | ||
|
||
test("If two shapes don't share a word, no ID is returned", () => { | ||
const wordsForShape1 = new Set(["CAT", "DOGS"]); | ||
const wordsForShape2 = new Set(["COW", "HORSE", "DOGGIE"]); | ||
|
||
expect( | ||
findIncompatibleShapeIdIfAny( | ||
wordsForShape1, | ||
wordsForShape2, | ||
"1-1-1", | ||
"2-2-2", | ||
), | ||
).toBe(undefined); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.