Back to Coding

AdoptMePetSpawner

Prompt

-- Lua script for Adopt Me game to spawn pets from a predefined list -- Define the list of valid pet names local validPets = { "Dog", "Cat", "Dragon", "Turtle", "Parrot", "Frog", "Rabbit", "Lion", "Elephant", "Cheetah" } -- Create a lookup table with lowercase keys for efficient validation local petLookup = {} for _, pet in ipairs(validPets) do petLookup[pet:lower()] = pet end -- Helper function to convert string to lowercase local function toLower(str) return string.lower(str) end -- Compute Levenshtein distance between two strings local function levenshtein(str1, str2) local len1 = #str1 local len2 = #str2 local matrix = {} -- Initialize matrix for i = 0, len1 do matrix[i] = {} matrix[i][0] = i end for j = 0, len2 do matrix[0][j] = j end for i = 1, len1 do for j = 1, len2 do local cost = (str1:sub(i, i) == str2:sub(j, j)) and 0 or 1 matrix[i][j] = math.min( matrix[i-1][j] + 1, -- deletion matrix[i][j-1] + 1, -- insertion matrix[i-1][j-1] + cost -- substitution ) end end return matrix[len1][len2] end -- Suggest closest matching pet names based on Levenshtein distance threshold local function suggestPets(input, threshold) local suggestions = {} input = toLower(input) for petLower, petOriginal in pairs(petLookup) do local dist = levenshtein(input, petLower) if dist <= threshold then table.insert(suggestions, petOriginal) end end return suggestions end -- Function to spawn pet using Adopt Me API - placeholder local function spawnPet(petName) -- TODO: Replace this placeholder with actual Adopt Me pet spawning API call -- Example: AdoptMeAPI.spawnPet(petName) print("Spawning pet: " .. petName) -- Placeholder print statement end -- Function to get user input - placeholder local function getUserInput(promptMessage) -- TODO: Replace this placeholder with actual input reading method in Adopt Me environment io.write(promptMessage) local input = io.read() return input end -- Main interaction loop while true do local inputPet = getUserInput("Enter the name of the pet you want to spawn: ") if inputPet == nil or inputPet:match("^%s*$") then print("Input cannot be empty. Please enter a pet name.") else local inputLower = toLower(inputPet) local validPetName = petLookup[inputLower] if validPetName then spawnPet(validPetName) break -- Exit loop after successful spawn else print("Invalid pet name: '" .. inputPet .. "'.") local suggestions = suggestPets(inputPet, 2) -- Levenshtein distance threshold = 2 if #suggestions > 0 then print("Did you mean one of these?") for _, sug in ipairs(suggestions) do print(" - " .. sug) end else print("No similar pet names found.") end end end end

Related Coding Prompts

Write Code

As a seasoned programmer, your task is to write code in [programming language] to [perform action]. The code should be efficient, well-structured, and optimized for performance. Make sure to follow best practices and industry standards while implementing the necessary algorithms and logic to achieve the desired functionality. Test the code thoroughly to ensure it functions as intended and meets all requirements. Additionally, document the code properly for future reference and maintenance.

Debug Code

Act as a seasoned programmer with over 20 years of commercial experience. Analyze the provided [piece of code] that is causing a specific [error]. Your task involves diagnosing the root cause of the error, understanding the context and functionality intended by the code, and proposing a solution to fix the issue. Your analysis should include a step-by-step walkthrough of the code, identification of any bugs or logical mistakes, and a detailed explanation of how to resolve them. Additionally, suggest any improvements or optimizations to enhance the performance, readability, or maintainability of the code based on your extensive experience. Ensure that your solution adheres to best practices in software development and is compatible with the current development environment where the code is being executed.

Do Code Review

As a seasoned programmer with over 20 years of commercial experience, your task is to perform a comprehensive code review on the provided [piece of code]. Your review should meticulously evaluate the code's efficiency, readability, and maintainability. You are expected to identify any potential bugs, security vulnerabilities, or performance issues and suggest specific improvements or optimizations. Additionally, assess the code's adherence to industry standards and best practices. Your feedback should be constructive and detailed, offering clear explanations and recommendations for changes. Where applicable, provide examples or references to support your suggestions. Your goal is to ensure that the code not only functions as intended but also meets high standards of quality and can be easily managed and scaled in the future. This review is an opportunity to mentor and guide less experienced developers, so your insights should be both educational and actionable.