![Page 1: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/1.jpg)
1
DIG 3134 – Lecture 19:
Intellectual Overload:Flood Filling
andRECURSION
and JSON
Michael MoshellUniversity of Central Florida
Internet Software Design
![Page 2: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/2.jpg)
2
SnakeWall: let's play the game
• Well, it's a bit of a• LAME GAME, but-I expect we can -improve on it
![Page 3: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/3.jpg)
33
Goal: Build a bridge to other side
• Players take turns• enter x, y, (b or g)OR• Check the radio button + color.
PLAY THE GAME(wall/wall04.php)
Develop Ideas About game play.
•X• Y
![Page 4: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/4.jpg)
444444
Implementing a Rule
WHAT IF we had a rule:
Each new tile (black or gold) must be NEXT TO a tile that's already black or gold?
Define a function like this:
function goodplay($color,$x,$y)
That returns TRUE (or 1) if it is legal to playthe color $color at location ($x,$y)
And it returns FALSE (or 0) if not.
![Page 5: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/5.jpg)
555
Can black draw at (4,6)?
Exercise:Talk to people around you.
Talk to the peopleIn front of, to the Left of, to the right of,Behind you.
Can you think up theAlgorithm, in English?
•X• Y
?
![Page 6: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/6.jpg)
6666
Can black draw at (4,6)?
Answer #1:No, becauseIt's Gold's turn!
(But that's a trickAnswer. A moreLegitimate answerIs ….)
•X• Y
?
![Page 7: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/7.jpg)
7777
Can black draw at (4,6)?
$b=BLACK;If (($Grid[$x-1][[$y]==$b)||($Grid[$x+1][$y]==$b)||($Grid[$x][$y-1]==$b)||($Grid[$x][$y+1]==$b) ) return 1; else return 0;
•X• Y
?
![Page 8: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/8.jpg)
88888
An exam practice question
$b=BLACK;If (($Grid[$x-1][[$y]==$b)||($Grid[$x+1][$y]==$b)||($Grid[$x][$y-1]==$b)||($Grid[$x][$y+1]==$b) ) return 1; else return 0;
How would youModify this rule If diagonal movesWere allowed?
![Page 9: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/9.jpg)
999999
Now for the ultimate challenge
Advanced players mightNot require contiguous moves.
Can we find a way toAUTOMATICALLYDetect a winner?
We need software that canDetect the existence of a complete path, however crooked and forked!
![Page 10: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/10.jpg)
10101010101010
Now for the ultimate challenge:
Like this one,Where Black has justCompleted a path.
If we could 'flood fill'The path to test it…
![Page 11: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/11.jpg)
1111111111111111
Flood-filling to find a win-path
Like this one,Where Black has justCompleted a path.
If we could 'flood fill'The path to test it…
Then we would just put a test in the floodfill function, to see if we ever try to fill a cell where $y>$Tablewidth.
If so, declare a winner!
![Page 12: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/12.jpg)
12121212121212
A Diversion: RECURSION!
A recursive function is one that calls a copy of itself!
consider this famous example:
function factorial($n){
if ($n<=1) return 1;else return $n*factorial($n-1);
}print factorial(5);
![Page 13: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/13.jpg)
13131313131313
A Diversion: RECURSION!
A recursive function is one that calls a copy of itself!
consider this famous example:
function factorial($n){
if ($n<=1) return 1;else return $n*factorial($n-1);
}print factorial(5);
factorial(5)
![Page 14: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/14.jpg)
14141414141414
A Diversion: RECURSION!
A recursive function is one that calls a copy of itself!
consider this famous example:
function factorial($n){
if ($n<=1) return 1;else return $n*factorial($n-1);
}print factorial(5);
factorial(5) 5*factorial(4)
![Page 15: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/15.jpg)
15151515151515
A Diversion: RECURSION!
A recursive function is one that calls a copy of itself!
consider this famous example:
function factorial($n){
if ($n<=1) return 1;else return $n*factorial($n-1);
}print factorial(5);
factorial(5) 5*factorial(4) 4*factorial(3)
3*factorial(2) 2*factorial(1)
![Page 16: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/16.jpg)
16161616161616
A Diversion: RECURSION!
A recursive function is one that calls a copy of itself!
consider this famous example:
function factorial($n){
if ($n<=1) return 1;else return $n*factorial($n-1);
}print factorial(5);
factorial(5) 5*factorial(4) 4*factorial(3)
3*factorial(2) 2*factorial(1)which is 2*1=2
![Page 17: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/17.jpg)
17171717171717
A Diversion: RECURSION!
A recursive function is one that calls a copy of itself!
consider this famous example:
function factorial($n){
if ($n<=1) return 1;else return $n*factorial($n-1);
}print factorial(5);
factorial(5) 5*factorial(4) 4*factorial(3)
3*factorial(2) 2*factorial(1)which is 2*1=2
yielding 3*2=6 yielding 4*3=12
![Page 18: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/18.jpg)
18181818181818
A Diversion: RECURSION!
A recursive function is one that calls a copy of itself!
consider this famous example:
function factorial($n){
if ($n<=1) return 1;else return $n*factorial($n-1);
}print factorial(5);
factorial(5) 5*factorial(4) 4*factorial(3)
3*factorial(2) 2*factorial(1)which is 2*1=2
yielding 3*2=6 yielding 4*3=12 yielding 5*12=60returns 60
![Page 19: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/19.jpg)
191919191919191919
Flood-filling
function floodfill($oldcol,$newcol,$x,$y) { global $Grid;
$thiscolor=$Grid[$x][$y];if (($x<0)||($y<0)||($thiscolor==BLUE))
return; if ($thiscolor==$oldcol) // time to go to work!{ $Grid[$x][$y]=$newcol;
floodfill($oldcol,$newcol,$x-1,$y);floodfill($oldcol,$newcol,$x+1,$y);floodfill($oldcol,$newcol,$x,$y-1);floodfill($oldcol,$newcol,$x,$y+1);
} } #floodfill
![Page 20: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/20.jpg)
20202020202020202020
Recursion
A function which calls itself is called a recursive function.
This is NOT the same as repetition (like a FOR loop).It is much more powerful (and dangerous!)
Powerful: it can figure out many tree shaped problems,like playing chess (or flood filling)
Dangerous: if you don't terminate it properly, it willhang up and run forever. "down the rabbit hole"
![Page 21: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/21.jpg)
21212121212121212121
Recursion
Why is this so powerful?Consider an expression like
$x=($z + 4*$y)/(*$z-3*$y);
It seems complex to analyze it. But most computer languages
use a recursive system, like this:evaluator($expression){
If $expression is of form $n x $b where xis an operator (+ - * /) then compute it
else break it into parts $a and $b, andapply evaluator () to both parts.
![Page 22: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/22.jpg)
22222222222222222222
JSON – wazzat?
If you need to store complex data in a text file(for instance, a string indexed array)
You COULD role your own code (I did, before JSON)and debug it, and maintain it, etc...
OR you could meet ...
![Page 23: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/23.jpg)
23232323232323232323
JSON – wazzat?
If you need to store complex data in a text file(for instance, a string indexed array)
You COULD role your own code (I did, before JSON)and debug it, and maintain it, etc...
OR you could meet ...
![Page 24: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/24.jpg)
24242424242424242424
JSON – wazzat?
If you need to store complex data in a text file(for instance, a string indexed array)
You COULD role your own code (I did, before JSON)and debug it, and maintain it, etc...
OR you could meet ...
(oh ... sorry, wrong JASON)
![Page 25: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/25.jpg)
25252525252525252525
JSON – JavaScript Object Notation
Example from wikipedia:{"firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "height_cm": 167.64, "address": {
"streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100"
}
![Page 26: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/26.jpg)
26262626262626262626
JSON – JavaScript Object Notation
{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "height_cm": 167.64, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }}
Self-documenting data – similar to XMLbut simplerand more
compact
![Page 27: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/27.jpg)
27272727272727272727
JSON for Problem Solving
I was building a complex application for my grad student, Dave Moran
I needed to store and recover a big messy record called a Picture Dictionary
JSON built into PHP was the perfect tool.
<<Demonstration of Moran-Vision Software>>
![Page 28: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/28.jpg)
28282828282828282828
JSON for Problem Solving
I was building a complex application for my grad student, Dave Moran
I needed to store and recover a big messy record called a Picture Dictionary
JSON built into PHP was the perfect tool.
<<Demonstration of Moran-Vision Software>>
![Page 29: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/29.jpg)
29292929292929292929
JSON for Problem Solving
The actual JSON file looks like a MESS:
{"File Name":{"wordlist":[["tag 2",0,0],["tag 3",0,0],["tag 4",0,0],["tag 5",0,0],["tag 6",0,0],["tag 7",0,0],["tag 8",0,0],["tag 9",0,0],["tag 10",0,0],["tag 11",0,0]],"zone":"Page"},"245":{"pictureid":245,"filename":"245","wordlist":[["hellafaya",0,0],["i",0,0],["am",0,0],["raphael",0,0],["iamraphael",0,0],["pattern",0,0],["of",0,0],["behavior",0,0],["patternofbehavior",0,0],["teenage",0,0],["mutant",0,0],["ninja",0,0],["turtles",0,0],["teenagemutantninjaturtles",0,0],["running",0,0],["with",0,0],["scissors",0,0],["runningwithscissors",0,0],["wired",0,0],["fabric",0,0],["softener",0,0],["fabricsoftener",0,0],["black",0,0],["and",0,0],["white",0,0],["blackandwhite",0,0],["from",0,0],["where",0,0],["i",0,0],["stand",0,0],["fromwhereistand",0,0],["scissors",
![Page 30: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/30.jpg)
30303030303030303030
JSON for Problem Solving
The data, viewed by print_r, is nicer (if you VIEW SOURCE)
[245] => stdClass Object (
[pictureid] => 245[filename] => 245 [wordlist] => Array ( [0] => Array (
[0] => hellafaya [1] => 0 [2] => 0 )
[1] => Array ( [0] => i [1] => 0 .. etc etc ...
![Page 31: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/31.jpg)
31313131313131313131
JSON for Trickery
Remember XML? Its loader creates a complex OBJECT.But what if we wanted an ARRAY?
json_encode and decode is a sneaky way to performa quick switcheroo:
$xml = simplexml_load_file('xml_file.xml'); $json_string = json_encode($xml); $result_array = json_decode($json_string, TRUE);
![Page 32: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/32.jpg)
32323232323232323232
JSON for Trickery
$xml = simplexml_load_file('xml_file.xml'); $json_string = json_encode($xml); $result_array = json_decode($json_string, TRUE);
These are built-in PHP functions. You can simply file-writethe encoded JSON string; etc.
Take-away: JSON provides convenient storage for complexobjects (like arrays-of-arrays).
![Page 33: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/33.jpg)
33333333333333333333
That's it for today.
Next (and last) Lecture:
Strategies for Success asa Programmer
anda Human Being
AND – a raffle! Win good junk.
![Page 34: DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON](https://reader035.vdocuments.us/reader035/viewer/2022070406/56814093550346895dac27d9/html5/thumbnails/34.jpg)
34