Free chapter (and equally free critic) of the book “PHP in Action” – Design Patterns
http://devzone.zend.com/node/view/id/1684
http://devzone.zend.com/node/view/id/1734
http://devzone.zend.com/node/view/id/1755
Or the link to the pdfs:
http://devzone.zend.com/content/pdfs/PHPinAction_part1of3.pdf
http://devzone.zend.com/content/pdfs/PHPinAction_part2of3.pdf
http://devzone.zend.com/content/pdfs/PHPinAction_part3of3.pdf
But you always have to be careful to get a second opinion (and a third and fourth). I consider that there are some conceptual errors or that some of the explanations left incomplete.
For example, about the Strategy pattern, saying that the purpose is to create reusable components or plugins is an incomplete concept and even incorrect. Usually design patterns have the purpose of enhancing code reusability, conforming a common base of design strategies. Plugins creation is but one of the posibilities where the Strategy pattern can be used, however the purpose of Strategy is to decouple an algorithm from its host, for example, an object may have sorting capabilities, but we know there are many sorting algorithms, or, in other words, different sorting strategies. The object receives sorting strategy object which decides how to sort without knowing how it’s actually done, on the other hand the sorting strategy object does the sorting without knowing anything about the object where the sorting is actually done (this way the Strategy objects can be reused out the original context, and changed without refactoring the original object).
The Strategy pattern can be confused with the Bridge pattern, as their UML graphic is almost the same. The Bridge pattern has as purpose decoupling an abstract data type from its implementation, for example a “Colection” object can be is implemented using arrays, linked lists, trees, etc. Using Strategy and Bridge together is usually known as policy-based design.
I don’t recommend using the Null pattern, specially when the existence of null is recognized by the language, and specially if it’s used to overlook a null result on a function as suggested on the book. If a function has the option of returnin null is for a reason, and checkin the output is not only necessary but adequate. In the example given by the book is coherent that a search may give a null result, indicating that the object being searched doesn’t exist, you should be the one checking the nullity of the result and act accordingly. On the other hand, it’s not coherent that the function returns an object when ther is not one, even if the object represents the inexestence of an object (there is a null language construct for that purpose).
About the explanation of the Iterator pattern, it fails to fully explain the reason of this pattern, specially because it tries to compare iterators with arrays. The purpose of iterators is simply to keep a common interfase for going through item list one item at a time, without knowing what’s the actual implementation. An array is an implementation, it doesn’t make any sense to compare it with iterators.
Finally, the Composite pattern is confused with the Tree abstract data type. The use of Composite implies tree structures but is mora than just that, conceptually speaking. A composite is an object that allows to perform actions over more than one object as if it were only one, for example, a drawing is a composite of strokes, figures, colours and even other drawings.
The holy grail of videogames
Profiting from the fact that I was bedridden today, thanks to one of those virus that comes when seasons change, I red some of the many articles I left to be red later. Among those there was one I though it’ll be a profitable reading, as it was a thesis on storytelling in singleplayer action games, that promises to unveils some of the misteries around getting a game to work better with a story. The abstract and complete text of the thesis are here:
http://www.gamecareerguide.com/features/320/masters_thesis_storytelling_in_.php
In the beginning I was thrilled about it, because action games are a very interesting area of videogames, specially filled with controversies about stories inside games. But I had to leave its reading for later.
Now that I had the chance to read it fully (although I didn’t actually red every word, I skipped some parts), I notices first something that made most of my thrill go away: the list of analized games. The list is as follows: Just Cause, Gun, GTA: San Andres, Fable. The first three are so similar that it seems ridiculous not having taken only one as sample of the others. Fable I haven’t played it (yet), but from what the critics say, it seems to be a good choice (anyway, evidence in the writing suggest that it was added later on the thesis and on suggestion from someone else).
¿Can we really obtain substantial conclusions about the effects of storytelling in a videogame from these 3 titles (leaving Fable besides)? My opinion is simply NO. The only one worthy of analysis is GTA:SA, but not because of the game itself but because of the evolution of the whole series, from the first GTA to what it has become nowadays. The GTA series is a very good example of storytelling and gameplay evolving together to become a product that has broken many barriers, and even become succesfull in the most difficult market for foreigners game developers, Japan.
There are many titles to analyze on this topic, but there is one that can’t be left outside in an investigation of this kind, as it set a before and an after y the relationship between action games and storytelling: Half-Life. To understand why I say this, a good starting point is this article, which recounts the events of the key moments of the creation of this game:
http://www.gamespot.com/features/halflife_final/index.html
About the thesis, it turned out to be another critic voice in the debate narratology vs ludology, and as expected of such, it denies being part of the debate. You can see hard evidence of this right at the beginning, as the thesis starts with a direct critic to another writing from another voice of this debate: “Games telling stories?” form Jesper Juul, which can be found here:
http://www.gamestudies.org/0101/juul-gts/
Although I don’t deny that most of the criticism made to that article are right, it’s not a very good starting point for the objectives of the thesis, and shows that the author is biased, and the bias keeps showing up in the rest of the writing, making me doubt the objectivity of the thesis.
I leave my opinion of the narratology vs ludology bout for some other time.
Rock-Paper-Scissors
Don’t you just love capitalized titles? Anyway, I red the following a few days ago:
Rock Paper Scissors – A Method for Competitive Game Play Design
I strongly recommend its reading to anyone interested in game-design. It explains the RPS technique for multiplayer game design, and shows this model at work in videogames, sports and other types of competitive games.
Just to explain a bit:
A game applying the RPS model must have a set of different attack and defend moves which cancel each other in a way that no move has an advantege over the others.
This should allow the player to identify quickly and through experimentation which are the options available to him and how to counter them.
A player may choose to act randomly and quickly as an strategy which prevents the oponent from distinguishing an attack pattern, which would be easily countered. But this takes the point of the game, which becomes not one of strategy but one of simple luck. To stop this from happening “signals” are introduced to the model.
Signals announce an attack just before its occurence. They must be identifiable under normal conditions, that means not faster than the normal reaction of a human and neither as slow as to allow anyone to counter it without effort.
On these conditions the game may stall as players choose not to begin a strike, since skilled players develop conditioned reflexes to the signals. This kind of strategy (“turtling”) is not desirable, therefore separation between signal and attack is introduced.
Separation between signal and attack allows “fakes” to be thrown, that means signals which are not followed by its corresponding attack. “Faking” ruins the turtling strategy since one could fall to a fake leaving oneself open. The player has to read more into the game and into the opponents, and the game becomes more about the players and less about the game mechanichs. This is the scenario that RPS wants to achieve.
What I didn’t found explained in the article is how to apply RPS in competitive team play game design. The model should be appliable very simply just by using the RPS on one-on-one confrontations, but the team play conditions allows for richer strategies that aren’t considered in the article. It’s a doubt I have worthy of more research…
PHP Wizardry Lessons: Parsing CSV
The young apprentice asked fearfully the old and wise wizard how to parse a CSV file (plain text format used mostly in spreadsheet apps like MS Excel), because he knew how to use
but this only worked with files, not with variables already in memory. The old wizard looked him seriously and answered with a reproval – “no wizard that is proud of being such would fear such a trivial thing as parsing a plain text file”. Then he took from his magic hat his old book “the one thousand and one most powerful and dangerous regular expressions”. He went through the pages slowly and ceremonially. Lastly he closed the book and took a deep breath. Holding his breath he wrote the following on the floor (reciting aloud a regular expression would look stupid, wouldn’t it?):
/(?<=^|,)(“(.|\n)*(?<!”)”|.*)(,|$)/Um
The old wizard explain no more, that’s why we’ll look a little more into the mystics behind the power of this regexp.
On execution, this regexp applied sequentially to a string the way
does, the result will be each one the fields of the CSV. The content of each will be captured by the first parenthesis, but this must be processed first, because the CSV format allows fields to be enclosed by double quotes which can have double quotes enconded by duplicating them (“….”"…”).
To know when a row ends we must check the third parenthesis, which captures the end of the field (,|$). If the captured content is empty, a row end was found (the new line). Note that it is not safe to split first in lines and then parse each line, because there can be fields which hold a line break, using the double quotes to hold this character.
Each part of the expression has an specific purpose:
(?<=^|,) is a back assertion which assures that each regexp run starts where the one before ended, that is a comma or a line break, or the beginning of the string (for the first run).
“(.|\n)*(?<!”)” is the expression that looks for fields that use the double quotes. The can hold line breaks (look at .|\n), and it has a back assertion to prevent that a double quote followed by another double quote is interpreted as the end of the field.
|.* is the expression that capture fields that don’t follow the double quotes format, in case that fails.
(,|$) is the expression that captures the field end, which can be a comma or a line end.
U (ungreedy) and m (multiline) modifiers are necessary to prevent expressions to keep capturing beyond the intented end of it (ungreedy), and so that ^ and $ also match line breaks.
The full content of the spell, that builds the array using the results of preg_match_all can be found at:
http://www.martinalterisio.com.ar/php-spell-book/parse-csv.php
—-
But the old wizard left other less cryptic solutions untold, maybe because of his senililty, maybe because he couldn’t keep up with the latest spells. All those who have reached level 5 of PHP wizardry (PHP version 5.x) have new options in their skills menu. Just copy&paste (skill you should have probably acquired while being an apprentice) the example code for the custom stream wrappers in the PHP manual:
http://www.php.net/stream_wrapper_register
Once the summon of the new stream wrapper has been completed, we can use global variables as files:
$csv = “….”;
$fp = fopen(“var://csv”, “r”);
while ($data = fgetcsv($fp)) {
…etc…
}
For those who lack the copy&paste skill (shame on you) here you can look the stream wrapper code:
http://www.martinalterisio.com.ar/php-spell-book/VariableStream.phps
“Professional SEO With PHP” Free (as in “you-only-got-a-taste-of-it”) Chapter
In another most evident marketing tactic, we get a free chapter, which says a little but promises much more for those who buy the rest of the book, from “Professional SEO With PHP”. This book explains common SEO techniques applied using the PHP programming language. The chapter can be downloaded in:
http://www.seoegghead.com/attachments/00929c01.p1.pdf
This blog post talks about this:
http://www.seoegghead.com/blog/seo/f…-php-p196.html
The chapter begins with a nice, yet incomplete, introduction to the topic. It explains clearly what the problem is (what are we trying to achieve) and who are involved in the process of solving it. Likewise it explains what are our objectives and position as the ones in charge of SEO. Still, it doesn’t say much about HOW we can solve this problem.
I strongly suggest reading page 5, which establishes the most important thing about SEO: we’re not designing a communication only with humans, there are other readers in the web business that have a differente perception of the content we pusblish. Those are the one we less understand, since they are the algorithms that crawl our site compiling information for a search engine. The SEO must be involved since the early stages of the site conception, where communication decisions are taken that will decide which is the best course of action for developing the site. In this page, there is algo a link to an onlice tool that caught my eye:
http://www.seochat.com/seo-tools/spider-simulator/
… which presents us with what would look like the “view” a search engine robot would have of a specific url.
Following pages aren’t even worth reading. They are only technical instructions to setup the development enviroment required to do the exercises in the book, which we will have to buy if we want to do them.
Don’t read this blog
¿What’s a blog? ¿What’s its purpose? ¿Is there something beyond “let’s plant a blog and see it grow links”? ¿Is there any conscious soul listening and reasoning, or are we just screaming to each other, hoping our voice will be heard louder than the roar of the wind? ¿Are we just mutes crying out to deaf ears?
I never tried to be a voice of the democratic oligarchy that holds fort inside blogs and other forms of expression 2.0.7.1-forever-beta. I never meant to be listened to, I’m sorry if you did and it did not please you, it was never my intention and it never will, it’s just good enough to be able to express myself.
So, in a moment of doubt, and very scarce content, I ask myself what am I here, what I want to be. And these will be my principles version 0.1-dev, if you don’t like them, wait for the patch, I promise they will be released before Duke Nukem Forever goes gold.
I’m a conformist, I conform myself with mumbling my sorrows to the wind. Luckly the wind answers caressing the hair gently, and that’s enough. Sometimes even the casual echo answers my call, carrying my voice far away, where nobody hears, and nobody is a good chatting pal for those desperate for conversation. I prefer calling him on the phone, in a pay-to-receive call.
There are many things to say, this is just one of them and ends here. I leave you with the silence, a cautious and discreet monologuist, who, without a single word, says more than all of us together. With a bit of luck, I hope there’ll be another chance for me to talk to you again, talk about this fascinating ethereal world.
Never give up, keep debuging the code of life. Remember that there are no limits that do not come from our own mind.