temporary
This commit is contained in:
43
src/utils/bytebeatFormulas.ts
Normal file
43
src/utils/bytebeatFormulas.ts
Normal file
@ -0,0 +1,43 @@
|
||||
const operators = ['&', '|', '^', '+', '-', '*', '%']
|
||||
const shifts = ['>>', '<<']
|
||||
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 20, 25, 32, 42, 63, 64, 127, 128, 255]
|
||||
|
||||
function randomElement<T>(arr: T[]): T {
|
||||
return arr[Math.floor(Math.random() * arr.length)]
|
||||
}
|
||||
|
||||
function generateTerm(depth: number = 0): string {
|
||||
if (depth > 2 || Math.random() < 0.3) {
|
||||
const shift = randomElement(shifts)
|
||||
const num = randomElement(numbers)
|
||||
return `(t${shift}${num})`
|
||||
}
|
||||
|
||||
const op = randomElement(operators)
|
||||
const left = generateTerm(depth + 1)
|
||||
const right = Math.random() < 0.5 ? generateTerm(depth + 1) : randomElement(numbers).toString()
|
||||
return `(${left}${op}${right})`
|
||||
}
|
||||
|
||||
export function generateRandomFormula(): string {
|
||||
const numTerms = Math.floor(Math.random() * 3) + 1
|
||||
const terms: string[] = []
|
||||
|
||||
for (let i = 0; i < numTerms; i++) {
|
||||
terms.push(generateTerm())
|
||||
}
|
||||
|
||||
return terms.join(randomElement(operators))
|
||||
}
|
||||
|
||||
export function generateFormulaGrid(rows: number, cols: number): string[][] {
|
||||
const grid: string[][] = []
|
||||
for (let i = 0; i < rows; i++) {
|
||||
const row: string[] = []
|
||||
for (let j = 0; j < cols; j++) {
|
||||
row.push(generateRandomFormula())
|
||||
}
|
||||
grid.push(row)
|
||||
}
|
||||
return grid
|
||||
}
|
||||
Reference in New Issue
Block a user