Skip to content

Commit

Permalink
refactor utils
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-hu committed Nov 17, 2023
1 parent 23072bf commit 20ad0e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
35 changes: 2 additions & 33 deletions src/Connections/ConnectionsPlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,13 @@ import {
getDateString,
toInt,
normalizeGame,
correctFontSizeForAnswers,
calcOffBy,
} from './utils';
import VictoryModal from './VictoryModal';
import ConnectionsMenu from './ConnectionsMenu';
import ConnectionsBackButton from './ConnectionsBackButton';

const correctFontSizeForAnswers = (guess: string[]) => correctFontSize(guess.join(', '), isMobile() ? 200 : 300, 14);

const minChangesToEqualLists = (list1: string[], list2: string[]): number => {
const freq1: { [key: string]: number } = {};

// Count the frequency of strings in list1
for (const string of list1) {
freq1[string] = (freq1[string] || 0) + 1;
}

// Calculate the total changes needed
let changesNeeded = 0;

// Iterate through the strings in list2
for (const string of list2) {
if (freq1[string] && freq1[string] > 0) {
freq1[string] -= 1; // Mark the string as used
} else {
changesNeeded += 1; // Count strings that need to be changed
}
}

return changesNeeded;
};

const calcOffBy = (words: string[][], guess: string[]): number => {
const offs = [];
for (const word of words) {
offs.push(minChangesToEqualLists(word, guess));
}
return Math.min(...offs);
};

function Box({
word, selected, solved, onClick,
}: { word: string, selected: boolean, solved: boolean, onClick: () => void }) {
Expand Down
59 changes: 46 additions & 13 deletions src/Connections/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,6 @@ export const generateLink = (game: ConnectionsGame): string => {
return `/connections/play?game=${encodedBase64String}`;
};

export function shuffleArray(array: string[]): string[] {
const shuffledArray = [...array]; // Create a copy of the original array

for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1)); // Generate a random index

// Swap elements at i and j
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}

return shuffledArray;
}

export const decodeCategories = (encodedValue: string | null): ConnectionsGame | null => {
if (!encodedValue) {
return null;
Expand All @@ -105,6 +92,19 @@ export const decodeCategories = (encodedValue: string | null): ConnectionsGame |
return parsedGame;
};

export function shuffleArray(array: string[]): string[] {
const shuffledArray = [...array]; // Create a copy of the original array

for (let i = shuffledArray.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1)); // Generate a random index

// Swap elements at i and j
[shuffledArray[i], shuffledArray[j]] = [shuffledArray[j], shuffledArray[i]];
}

return shuffledArray;
}

export const correctFontSize = (str: string, elementWidth: number, originalSize = 16) => {
const { length } = str;
const mult = elementWidth / (originalSize * length);
Expand All @@ -113,6 +113,39 @@ export const correctFontSize = (str: string, elementWidth: number, originalSize
return Math.round(fontSize);
};

export const correctFontSizeForAnswers = (guess: string[]) => correctFontSize(guess.join(', '), isMobile() ? 200 : 300, 14);

export const minChangesToEqualLists = (list1: string[], list2: string[]): number => {
const freq1: { [key: string]: number } = {};

// Count the frequency of strings in list1
for (const string of list1) {
freq1[string] = (freq1[string] || 0) + 1;
}

// Calculate the total changes needed
let changesNeeded = 0;

// Iterate through the strings in list2
for (const string of list2) {
if (freq1[string] && freq1[string] > 0) {
freq1[string] -= 1; // Mark the string as used
} else {
changesNeeded += 1; // Count strings that need to be changed
}
}

return changesNeeded;
};

export const calcOffBy = (words: string[][], guess: string[]): number => {
const offs = [];
for (const word of words) {
offs.push(minChangesToEqualLists(word, guess));
}
return Math.min(...offs);
};

export const isMobile = () => window.innerWidth < 768;

export const COLORS_BY_DIFFICULTY = ['#e3bf02', '#84a63a', '#719eeb', '#bd70c4'];
Expand Down

0 comments on commit 20ad0e6

Please sign in to comment.