340 likes | 480 Views
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.
E N D
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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