Home > Game Development > New Team, New Task

New Team, New Task

UPDATED 10/29/07: Added guard to remove same first name used as middle name.

So I finally got myself into a project, you can check out an unattended, intended for public viewing page here.

My first assignment is this:

This tool will simply automate the task of producing a long list of trivial character names for the game; by mixing a list of first, second and last names to form unique male and female names for use. No interface is necessary, it should just load XML/INI or other data formats within a fixed file containing the individual names and should create a new file containing the long list of full names.

So the program will read in 3 files: male first names, female first names, and male last names. Now the output will be:

female names in the form of [female first name] + [male first name (used as a middle name)] + [male last name], and
male names in the form [male first name] + [male first name (used as a middle name)] + [male last name].

The program will create every possibility, store them in a vector, then shuffle the vector. After submission and review, here is my final solution:

#include “stdafx.h”

void printFemale(string);
void printMale(string);

ofstream of_completeFemaleNames (“completeFemaleNames.txt”);
ofstream of_completeMaleNames (“completeMaleNames.txt”);

int _tmain(int argc, _TCHAR* argv[])
{
srand(time(NULL));

ifstream if_femaleNames (“femaleNames.txt”);
ifstream if_maleNames (“maleNames.txt”);
ifstream if_malelastNames (“malelastNames.txt”);

vector v_femaleNames;
vector v_maleNames;
vector v_mLastNames;
vector v_completeFemaleNames;
vector v_completeMaleNames;

if(if_femaleNames.is_open())
{
string name;
while( getline(if_femaleNames, name) )
v_femaleNames.push_back(name);
if_femaleNames.close();
}

if(if_maleNames.is_open())
{
string name;
while( getline(if_maleNames, name) )
v_maleNames.push_back(name);
if_maleNames.close();
}

if(if_malelastNames.is_open())
{
string name;
while( getline(if_malelastNames, name) )
v_mLastNames.push_back(name);
if_malelastNames.close();
}

// Create iterators to grab the names
vector::iterator iter1;
vector::iterator iter2;
vector::iterator iter3;

// For each female name, append a male middle name and a male last name
for(iter1 = v_femaleNames.begin(); iter1 != v_femaleNames.end(); ++iter1)
{
string name = *iter1;

for(iter2 = v_maleNames.begin(); iter2 != v_maleNames.end(); ++iter2)
{
name += ” ” + *iter2;

for(iter3 = v_mLastNames.begin(); iter3 != v_mLastNames.end(); ++iter3)
{
v_completeFemaleNames.push_back(name + ” ” + *iter3);
}

name = *iter1;
}
}

// Shuffle the completed female names
random_shuffle(v_completeFemaleNames.begin(), v_completeFemaleNames.end());

// And print them to the output file
for_each(v_completeFemaleNames.begin(), v_completeFemaleNames.end(), printFemale);

// For each male name, append a male middle name and male last name
for(iter1 = v_maleNames.begin(); iter1 != v_maleNames.end(); ++iter1)
{
string name = *iter1;

for(iter2 = v_maleNames.begin(); iter2 != v_maleNames.end(); ++iter2)
{
// This will remove the same First Name being used as the Middle Name
if(*iter2 == name) continue;

name += ” ” + *iter2;

for(iter3 = v_mLastNames.begin(); iter3 != v_mLastNames.end(); ++iter3)
{
v_completeMaleNames.push_back(name + ” ” + *iter3);
}

name = *iter1;
}
}

// Shuffle the completed male names
random_shuffle(v_completeMaleNames.begin(), v_completeMaleNames.end());

// And print them to the output file
for_each(v_completeMaleNames.begin(), v_completeMaleNames.end(), printMale);

return 0;
}

void printFemale(string s)
{
of_completeFemaleNames << s << endl; } void printMale(string s) { of_completeMaleNames << s << endl; }[/sourcecode]

Advertisements
  1. October 28, 2007 at 11:35 pm

    I’ve tried something similar to this before, but it was never under 100 lines =) Looks like an awesome project you guys are working on, good luck.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: