I think there would be a lot of improvements on how it generates the name, like defining better sets of rules. For example now it cannot make names like aya, since it only puts 1 or 2 vowels in a row. Another thing that would probably need to be considered would be possible starting / ending letters and letters that can be in middle of name. I am not sure what kind of ruleset would work for determining these, but from what I've tested out, some combinations of letters just feel awkward in middle of names like for example 'j'.
Anyways here is the code:
Code: Select all
-- Namey, a name generator
math.randomseed(os.time()) -- to avoid setting same seed on every launch
local NameGenerator = {}
-- The names are bit odd ones but atleast it gives something.
-- Random rules for creating weird names!
local vowels = "aeiouy"
local consonants = "bcdfghjklmnpqrstvwxz"
local doubleConsonantFirst = "bcdghklmnprstvw"
local doubleConsonantSecond = "dhklmnrst"
local function pickOne(t)
local pos = math.random( string.len(t) )
return t:sub( pos, pos )
end
-- Generates some weird names
function NameGenerator:generateName( parts, isFirstLetterCapital )
local tName = {}
local pos = 0
local isConsonant = math.random(2) == 1
for i = 1, parts do
if isConsonant then
if math.random(2) == 1 then -- double consonant
tName[ i ] = pickOne(doubleConsonantFirst) .. pickOne(doubleConsonantSecond)
else
tName[ i ] = pickOne( consonants )
end
else
tName[i] = ""
for j = 1, math.random(2) do -- 1 to 2 vowels in a row
tName[ i ] = tName[ i ] .. pickOne( vowels )
end
end
isConsonant = not isConsonant
end
-- capitalize first letter
if isFirstLetterCapital then
tName[1] = string.upper( tName[1]:sub(1,1) ) .. tName[1]:sub(2)
end
local sName = table.concat( tName )
return sName
end
return NameGenerator
Code: Select all
local namey = require( "namey" )
local names = {}
local font = love.graphics.newFont( 16 )
local fontHeight = font:getHeight()
function love.load()
for i = 1, 20 do
-- names with 2 to 4 parts,
-- so the name can be 2 to 8 letter long
names[i] = namey:generateName( math.random(2,4),true)
end
end
function love.draw()
for i = 1, #names do
love.graphics.print( names[i], 10, i * fontHeight )
end
love.graphics.print( "Press R to regenerate names", 10, (#names+1) * fontHeight )
love.graphics.print( "Press Q / esc to quit", 10, (#names + 2) * fontHeight )
end
function love.keypressed(key)
if key == "r" then
love.load()
elseif key == "escape" or key == "q" then
love.event.quit()
end
end
Edit: Fixed a bug where I used math.random(1) == 1 where math.random(1) returns 1 always, instead of 0 or 1.