basic story generator
//test for story writing program
//creates a basic character object
function character(name){
return {name:name, story:""};
}
//selects random element from array
function rselect(arr){
return arr[Math.floor((Math.random() * arr.length))];
}
//environment object
function forest(char){
function walkThrough(char){
return char.name + "walked slowly through the forest.";
}
function spotTree(char){
return char.name + "spot's a tree.";
}
function foundBranch(char){
return char.name + "find's a branch!";
}
function lookTree(char){
return char.name + "look's at the tree ahead.";
}
var funcs = [walkThrough, spotTree, foundBranch, lookTree];
for(var i=0;i<6;i++){
char.story += rselect(funcs)(char);
}
return char;
}
/*=> undefined
var b = character("thomas");
=> undefined
b
=> { name: 'thomas', story: '' }
forest(b)
=> { name: 'thomas',
story: 'thomasfind\'s a branch!thomaswalked slowly through the forest.thomaswalked slowly through the forest.thomasspot\'s a tree.thomaslook\'s at the tree ahead.thomaswalked slowly through the forest.' }*/