Forum Moderators: coopster

Message Too Old, No Replies

program flow structure logic thing

         

appi2

7:54 am on May 4, 2008 (gmt 0)

10+ Year Member



I made a little pretty interactive crossword puzzle with HTML css and javascript and was a happy bunny. I then had the daft idea of populating the crossword from a word list.

so...

function attempt() {
$diditwork=$testeachword();

if ($diditwork) {
echo "wooo hooo";
}
else {
attempt();
}
}

Which does atcually work, sometimes. Other times PHP obviously times out. Thing is it restarts the whole process each time it fails. Would be better to have another go at the previous result before the function starts again.

Which leads me to this

<?php

func1();

function func1() {
$stack = new array{};
$test1 = somefunction();
array_push($stack, $test1);
funct2();
}

function func2() {
$test2 = somefunctionA(); //returns random value based on func1
if (!$test2) {
func1(); //start again
}
else {
array_push($stack, $test2);
}
funct3();
}

function func3() {
$test3 = somefunctionB(); //returns random value based on func2
if (!$test3) {
array_pop($stack);
func2(); //try 2 again
}
else {
array_push($answer, $test3);
}
funct4();
}

function func4() {
$test4 = somefunctionC(); //returns random value based on func3
if (!$test4) {
array_pop($stack);
func3(); //try 3 again
}
else {
array_push($answer, $test4);
}
funct5();
}

//and so on ...

// the somefunctions are basically

$data = file_get_contents ('xwordlist');

function somefunction() {
//find 15 letter words
$pattern15 = '/\b...............\b/';
preg_match_all($pattern15, $data, $matches15);

//Pick a random word
$rand_keys15 = array_rand($matches15[0], 2);
$a10 = $matches15[0][$rand_keys15[0]] ;
}

function somefunctionA() {
//does it intersect
$patternd4 = '/\b..'.$a10{6}.'..\b/';
preg_match_all($patternd4, $data, $matchesd4);
if ($matchesd4[0]) {
$rand_keysd4 = array_rand($matchesd4[0],2);
$d4 = $matchesd4[0][$rand_keysd4[0]];
}
}
?>

which then leads me to ask how to structure something like this.
Is there a better way to structure the above?
And does this sort of structure/thing have a name, apart from youvehadtomuchcoffeandforgottogotosleep.

1.pick a random word

2.pick another random word
do those words intersect
no => do 1 again
yes => step3

3.pick another random word
does that word intersect?
no => do 2 agian
now does it intersect?
no => try step2 x more times
now does it intersect?
no => do 1 again
now does it intersect?
yes => step4

4.pick a random word
does that instersect
no => do 3 again
now does it intersect?
no => try step3 x more times
now does it intersect?
no => do 2 agian
now does it intersect?
yes => step5

this post was not sponsered by nescaf, but it did have an influence.

vincevincevince

1:04 pm on May 4, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The word is 'recursive function'