Last active
November 25, 2024 07:27
-
-
Save HoraceBury/b267b7f675ec3e9ba66cd5a54dcce761 to your computer and use it in GitHub Desktop.
Word search generator
This file contains hidden or 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
-- Word Search Generator for Solar2D | |
-- Configurable variables | |
local across = 15 -- Number of columns (minimum 8) | |
local down = 15 -- Number of rows (minimum 8) | |
local reversible = false -- Allow words to be placed backwards | |
local width = 600 -- Width of the grid area in pixels | |
local height = 600 -- Height of the grid area in pixels | |
local show = true -- Highlight words from the list in green | |
-- List of words to include in the word search | |
local words = {"HOT", "CHOCOLATE", "GINGER", "LATTE", "pine", "RED", "GREEN", "BERRIES", "WARM", "COOKIE", "SCARF", "MITTENs", "fireplace", "gloves"} | |
-- Ensure minimum grid size | |
across = math.max(across, 8) | |
down = math.max(down, 8) | |
-- capitalise all words | |
for w=1, #words do | |
words[w] = string.upper(words[w]) | |
end | |
-- Initialize the grid with empty spaces | |
local grid = {} | |
for y = 1, down do | |
grid[y] = {} | |
for x = 1, across do | |
grid[y][x] = " " -- Fill each cell with a space initially | |
end | |
end | |
-- Utility function to check if a word fits in the grid at a given position and direction | |
local function fits(word, x, y, dx, dy) | |
for i = 1, #word do | |
local nx, ny = x + (i - 1) * dx, y + (i - 1) * dy | |
-- Check if the position is out of bounds or conflicts with existing letters | |
if nx < 1 or ny < 1 or nx > across or ny > down or (grid[ny][nx] ~= " " and grid[ny][nx] ~= word:sub(i, i)) then | |
return false | |
end | |
end | |
return true | |
end | |
-- Utility function to place a word in the grid at a given position and direction | |
local function placeWord(word, x, y, dx, dy) | |
for i = 1, #word do | |
local nx, ny = x + (i - 1) * dx, y + (i - 1) * dy | |
grid[ny][nx] = word:sub(i, i) -- Place each letter of the word | |
end | |
end | |
-- Directions in which words can be placed | |
local directions = { | |
{1, 0}, -- Right | |
{0, 1}, -- Down | |
} | |
-- Add reverse directions if reversible is true | |
if reversible then | |
table.insert(directions, {-1, 0}) -- Left | |
table.insert(directions, {0, -1}) -- Up | |
end | |
-- Attempt to place each word in the grid | |
for _, word in ipairs(words) do | |
local placed = false | |
for attempt = 1, 100 do -- Try up to 100 random placements per word | |
if placed then break end | |
local x, y = math.random(across), math.random(down) -- Random starting position | |
local dx, dy = unpack(directions[math.random(#directions)]) -- Random direction | |
if fits(word, x, y, dx, dy) then | |
placeWord(word, x, y, dx, dy) -- Place the word if it fits | |
placed = true | |
end | |
end | |
end | |
-- Fill empty cells with random letters | |
local letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
for y = 1, down do | |
for x = 1, across do | |
if grid[y][x] == " " then | |
local randomIndex = math.random(#letters) | |
if not show then | |
grid[y][x] = letters:sub(randomIndex, randomIndex) -- Assign a random letter | |
end | |
end | |
end | |
end | |
-- Display the grid on the Solar2D screen | |
local cellWidth = width / across -- Calculate the width of each cell | |
local cellHeight = height / down -- Calculate the height of each cell | |
local startX = (display.contentWidth - width) / 2 -- Center the grid horizontally | |
local startY = (display.contentHeight - height) / 2 -- Center the grid vertically | |
for y = 1, down do | |
for x = 1, across do | |
-- Draw a rectangle for each grid cell | |
local rect = display.newRect(startX + (x - 0.5) * cellWidth, startY + (y - 0.5) * cellHeight, cellWidth, cellHeight) | |
rect.strokeWidth = 1 -- Set border width for the rectangle | |
rect:setFillColor(1, 1, 1) -- Set fill color to white | |
rect:setStrokeColor(0, 0, 0) -- Set border color to black | |
-- Display the letter in the cell | |
local letter = display.newText({ | |
text = grid[y][x], -- The letter to display | |
x = rect.x, -- Center of the rectangle | |
y = rect.y, -- Center of the rectangle | |
font = native.systemFontBold, -- Use a bold font | |
fontSize = math.min(cellWidth, cellHeight) * 0.6, -- Scale font size to fit cell | |
}) | |
-- Set the text color based on whether 'show' is true and the letter belongs to a word | |
local isWordLetter = false | |
if show then | |
for _, word in ipairs(words) do | |
if word:find(grid[y][x]) then | |
isWordLetter = true | |
break | |
end | |
end | |
end | |
-- if show and isWordLetter then | |
-- letter:setFillColor(0, 1, 0) -- Set text color to green | |
-- else | |
letter:setFillColor(0, 0, 0) -- Set text color to black | |
-- end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment