1 / 34

DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON

Internet Software Design. DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON Michael Moshell University of Central Florida. SnakeWall: let's play the game. Well, it's a bit of a LAME GAME, but I expect we can improve on it.

kin
Download Presentation

DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Internet Software Design DIG 3134 – Lecture 19: Intellectual Overload: Flood Filling and RECURSION and JSON Michael Moshell University of Central Florida

  2. SnakeWall: let's play the game • Well, it's a bit of a • LAME GAME, but • I expect we can • improve on it

  3. Goal: Build a bridge to other side • X • Y • 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. 3

  4. 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 play • the color $color at location ($x,$y) • And it returns FALSE (or 0) if not. 4 4 4 4 4

  5. Can black draw at (4,6)? • X • Y Exercise: Talk to people around you. Talk to the people In front of, to the Left of, to the right of, Behind you. Can you think up the Algorithm, in English? ? 5 5

  6. Can black draw at (4,6)? • X • Y Answer #1: No, because It's Gold's turn! (But that's a trick Answer. A more Legitimate answer Is ….) ? 6 6 6

  7. Can black draw at (4,6)? • X • Y $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; ? 7 7 7

  8. 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 you Modify this rule If diagonal moves Were allowed? 8 8 8 8

  9. Now for the ultimate challenge Advanced players might Not require contiguous moves. Can we find a way to AUTOMATICALLY Detect a winner? We need software that can Detect the existence of a complete path, however crooked and forked! 9 9 9 9 9

  10. Now for the ultimate challenge: Like this one, Where Black has just Completed a path. If we could 'flood fill' The path to test it… 10 10 10 10 10 10

  11. Flood-filling to find a win-path Like this one, Where Black has just Completed 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! 11 11 11 11 11 11 11

  12. 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); 12 12 12 12 12 12

  13. 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) 13 13 13 13 13 13

  14. 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) 14 14 14 14 14 14

  15. 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) 15 15 15 15 15 15

  16. 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 16 16 16 16 16 16

  17. 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 17 17 17 17 17 17

  18. 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=60 returns 60 18 18 18 18 18 18

  19. 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 19 19 19 19 19 19 19 19

  20. 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 will hang up and run forever. "down the rabbit hole" 20 20 20 20 20 20 20 20 20

  21. 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 x is an operator (+ - * /) then compute it else break it into parts $a and $b, and apply evaluator () to both parts. 21 21 21 21 21 21 21 21 21

  22. 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 ... 22 22 22 22 22 22 22 22 22

  23. 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 ... 23 23 23 23 23 23 23 23 23

  24. 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) 24 24 24 24 24 24 24 24 24

  25. 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" } 25 25 25 25 25 25 25 25 25

  26. JSON – JavaScript Object Notation Self-documenting data – similar to XML but simpler and more compact { "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" } } 26 26 26 26 26 26 26 26 26

  27. 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>> 27 27 27 27 27 27 27 27 27

  28. 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>> 28 28 28 28 28 28 28 28 28

  29. 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", 29 29 29 29 29 29 29 29 29

  30. 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 ... 30 30 30 30 30 30 30 30 30

  31. 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 perform a quick switcheroo:$xml = simplexml_load_file('xml_file.xml');$json_string = json_encode($xml);$result_array = json_decode($json_string, TRUE); 31 31 31 31 31 31 31 31 31

  32. 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-write the encoded JSON string; etc. Take-away: JSON provides convenient storage for complex objects (like arrays-of-arrays). 32 32 32 32 32 32 32 32 32

  33. That's it for today. Next (and last) Lecture: Strategies for Success as a Programmer and a Human Being AND – a raffle! Win good junk. 33 33 33 33 33 33 33 33 33

More Related