Thursday, January 17, 2013

A Danish/Morra/Elephant system?

As you may have guessed from my previous chess posts, I am a chess beginner and a gambiteer. I like to play the strange moves and see my opponents squirm in positions that they were not expecting and were not prepared for. That is why the proposition in this post is gambit galore and also to be taken with a grain of salt.

My idea is that there could be common themes for the three Gambits in the title, since they start almost the same way. You have the Smith-Morra Gambit, where White answers Black's Sicilian Defence with d4 and then c3 in the accepted version:1. e4 c5 2. d4 cxd4 3. c3 dxc3 4. Nxc3 * then you have the Danish Gambit, where White's move order is the same in response to King's pawn defence: 1. e4 e5 2. d4 exd4 3. c3 dxc3 4. Nxc3 * and then there is the similar response from Black to the King's pawn opening, called the Elephant gambit: 1. e4 e5 2. Nf3 d5 3. exd5 *

You might notice that in the Elephant, I did not offer up a second pawn on c6. This is because the main line is pawn to e4 and also White has the opportunity to take e5 with the knight, which is the computer recommendation as well. The problem is that after Black's response Qe2 to the main line or the Bd6 computer recommendation after Nxe5, the Elephant doesn't appear at all like the Danish/Morra and, instead, threads into its own territory, somewhere closer to the Latvian gambit, but not by much. However, in this post I will be stretching the imagination and will be trying to squish the big Elephant into the Morra mold and see where it takes me.

I have just finished watching a two hour video presentation of the Morra accepted line, by IM Andrew Martin, and there are also a lot of tutorials for the Danish, from beginner to very advanced levels. Not so for the Elephant, which seems to be even less favoured than the Latvian, to which GM Roman Dzindzichashvili answered with a refutation and some very rude words to its efficacity. All that I could find about it are lines that have no connection with the Danish/Morra style and that is because of that pesky White knight on f3.

If my thesis holds ground, then I will be talking here about a chess system that has some similar ideas and theory for at least three major openings: The Sicilian Defence and King's pawn for both White and Black! Also, threading on less travelled ground, there is a good chance online and club players will be unnerved by it.

So let's get into it. I will start with the Smith-Morra main line. This means the most played version in database games, by players who know theory and open that way because it was proven to be the best way. There is little chance you will see the same moves in club level games.

1. e4 c5 2. d4 cxd4 3. c3 dxc3 4. Nxc3 Nc6 5. Nf3 d6 6. Bc4 e6 7. O-O Nf6 8. Qe2 Be7 9. Rd1 e5 10. h3 O-O 11. Be3 a6 12. Rac1 *

This is the position in which White wants to get. I tweaked a little the game so that at move 11. Black moved to the second most used line in the database, the a6 system, rather than Be6 and exchange light bishops. What I want to evidence here is the position of the White pieces: both rooks are connected and on open or semi-open files, cramping Black's development, the bishops are out and about, aiming at the Black king, the knights are developed and the queen is on a very crucial square, controlling yet another file and the essential e2-c4 diagonal.Black has not finished development, has pinned pawns and weaknesses like b6 where a knight may find outpost. There is a lot of potential for attack and, even before reaching this position, a lot of possible traps in which Black could fall. Even the main line has only about 100 games in the database at this point, so it is not very common, even if it is a pretty solid opening.

Let's move to the Danish and compare.

1. e4 e5 2. d4 exd4 3. c3 dxc3 4. Nxc3 Nc6 5. Bc4 Nf6 6. Nf3 d6 7. Qb3 (7. O-O Be7 8. Qe2 O-O 9. h3 a6 10. Rd1 b5 11. Bb3) 7... Qd7 8. Ng5 Ne5 9. Bb5 c6 10. f4 cxb5 11. fxe5 *

Seems to be a different beast altogether. I've added a variation that plays like the Morra, the single game in the database like that (Sipek-Urbanec 1995). The b5 push is also found in the Morra. So, what are the differences here? The most important to me seems the absence of the Black pawn on e6. That means White can attack much quicker and Black must defend more aggressively, too. The e4 pawn is unopposed and, in some games, that proves decisive for White. The pawn on c7 is still there, so the Black queen will have to maneuver on light squares instead of dark.

A possible conclusion would be that, even if they seem similar, the Danish and Morra gambits are quite different. But are they?

A White queen on e2 would support the e4 pawn in its push forward and make room for a rook on d1, just like in the Morra. The dark bishop can pin or eventually trade with the knight on f6 or even move to e3 or f4, supporting the e4 pawn as it moves to e5 and making room for a rook on c1. A computer analysis on a Danish game played in the Morra fashion shows equality when Black still has an extra pawn.

Now, some of the readers may scoff and conclude that I am trying to fit the proverbial triangle shape into the round hole using brute force, that attempting to take one opening and play it like another is an imperfect chimera, destined to be an abomination. However, I must remind you that I am not a master player, nor a professional one. I have no time to learn tons of theory just to win a game. My purpose for this research (which may still fail to achieve anything) is to find a gambit based system that uses the same principles for any opponent response. In time, each variation can be improved and branched off from the main system, but at the start all I need is for it to work.

Let's get back, then. How about playing a Morra game in the Danish way? Well, the Danish gambit is even rarer than the Smith-Morra and the games in my database are primarily focused on the exposed f7 square. It could work, I guess, but it would seem even more unnatural and, lacking proper theory, a beginner like me could easily mess it up. I will, therefore, use the Morra as the template to which all others must conform.

Besides, if you think fitting the Danish to the Morra was difficult, the Elephant comes next!

In the database there are only two games that start like a Morra Elephant and they both are won by White, which is not good for us. Only when it gets to the standard position of knight protecting the single center pawn, the transposed games suddenly reach 73! It seems this position can be more easily achieved by playing the Scandinavian defence! In the next board I will present the main line for the Elephant, then the Morra Elephant and at the same time the way to reach the same position from the Scandinavian. The rest will continue from the Elephant line, but, actually, it will be based more on Scandinavian games.

1. e4 e5 (1... d5 {The Scandinavian Defence} 2. exd5 c6 3. dxc6 Nxc6 4. Nf3 e5 {And we reach the position would would have liked from the Morra Elephant.}) 2. Nf3 d5 3. exd5 e4 (3... c6 {And here is a Morrafication of the Elephant} 4. dxc6 Nxc6 5. Bb5 Bd6 6. O-O Nge7 7. d4 e4 8. Ne5 Bxe5 9. dxe5 O-O 10. Qxd8 Rxd8 11. Nc3 Nxe5) 4. Qe2 {This is how the Elephant is mainly played.} Nf6 5. Nc3 Be7 6. Nxe4 O-O 7. d3 Nxd5 8. Qd1 Nc6 9. Be2 Bf5 10. O-O Qd7 *

Ooh! This seems completely different. White still has that extra tempo and he uses it to pin the Black knight on c6, which leaves e5 undefended. Black's bishop on f8 did not have time to get out, so moving the queen on e7 like in the Morra would block it and the entire king side. The move Bd6 is the only one that can defend the pawn and this gives White at least the opportunity to swap the bishop with the knight and mess up the Black pawn structure on the queen side. If White does not take, as in the example above, then the only possible move to protect the knight is to use the other knight on e7, thus forever altering the structure of the game.

It seems no amount of force will twist the Elephant into a Morra gambit a tempo behind. A Morra with a lost tempo doesn't even appear to work! Besides, in order to get here, White had to ignore the opportunity in the beginning to take on e5 with the knight, as suggested by the computer; a much safer route to the same dysfunctional position can be achieved from the Scandinavian defence.

The Elephant hides some interesting traps that have nothing to do with the Morra or the Danish and has more in common with the Latvian gambit that with the two systems above. The Latvian, if you remember, offers up two pawns in order to gain the tempo White is awarded in the start of a chess game. The Elephant can be played in the same way, only to lose two center pawns, so not so good. The similarities with the Morra/Danish are deceiving. A tempo behind, Black cannot use the same ideas, having to defend instead of attack.

Conclusion: It is a very difficult thing to find a defence for Black that works the same way as an opening for White, because of the extra tempo. Even so, the Elephant only begins like the Morra, it has nothing else in common. The Morra gambit itself is only similar to the Danish and, while I think they can be molded in the same shape, it would be a tortuous adventure that I am not sure will get me where I want.

I hope you have gained a little understanding of the differences between the three gambits and how simple differences like the position of a pawn or an extra tempo can change a game of chess.

Thursday, January 10, 2013

The Drowning Girl, by Caitlín R. Kiernan

Book coverWe live now in a world where people get the same education, see the same movies, read the same books - if at all. We then watch the ones around us and see only ourselves and we get bored. That is why, I believe, we start to see various mental illnesses or strange behaviours as interesting. That is why, I think, The Drowning Girl, by Caitlín R. Kiernan has received so wonderful reviews.

That doesn't mean the book is not brilliant. Kiernan paints the world as seen from the eyes of a lesbian paranoid schizophrenic, combining ideas from paintings, old legends and written stories into a whirlpool of staggering creativity. However, I do have to wonder, would the book have received the same amount of positive reviews if the main character was a straight man?

All that aside, I have tried to keep an open mind when reading the book and I have found that the way the author mingles stories and goes back and forth, keeping the reader on their tows, is both excellent and terribly irritating. It builds up a lot of tension that needs to be released into a grand finale. However, the climax of the book seemed to me to be somewhere in the middle, with the ending lagging and wasting into pointless mental delusions.

It is hard for me to recommend or not recommend this book. It is clearly well written and very inspired. It not only delightfully weird, but also draws information and data from all kinds of art fields and mingles them together in an interesting way. The construction of the book aside, though, leaves a plot that doesn't really mean anything. It's the maelstrom of thoughts and feelings of a mentally troubled person with a slight mystical component which, even till the end, is not really clear if it is only in her mind or has some factual truth.

I did enjoy one thing, though, the idea that something can be "true", but not "factual". If you think about it, it makes sense, but usually words like "truth" hold an objective mask on them, when most of the uses of those words are actually subjective. Yep, it's true :) I also liked the way details about the artists led to connections to other works and facts, that a thorough analysis of art can show hidden worlds and interesting perspectives.

As a conclusion, what leapt into mind when trying to find a book that is similar to this was Geek Love, by Katherine Dunn. In a word: freaky. The Drowning Girl is much more interesting, though, and doesn't try so hard to shock with the character's sexuality or personal weirdness. But in the end, having read it, I felt like it said nothing. An interesting journey towards nowhere in particular.

Wednesday, January 09, 2013

Why doubling single quotes is not enough to protect from SQL Injection

We all know that the best way to prevent SQL injection is to use parameters, either in stored procedures or in parameterized queries. Yet on some occasions we meet code that replaces every single quote with two single quotes. The question arises: OK, it's ugly, but isn't it enough?

I have recently found out of something called "Unicode Smuggling" which uses the database against itself to bypass protection as described above. More details here: SQL Smuggling , but the basic idea is this: if the replacement scheme is implemented in the database and uses VARCHAR or maybe the code uses some non-unicode string, then the protection is vulnerable to this by leveraging what is known as Unicode Homoglyphs. If you feel adventurous and want to examine thoroughly the ways Unicode can be used maliciously, check out UTR#36.

Here is an example:
@newtitle NVARCHAR(100)
Double up any single quotes
SET @newtitle = REPLACE(@newtitle, '''','''''')


SET @UpdateStatement = 'UPDATE myTable SET title='''  + @newtitle + ''''


Note the use of VARCHAR as the type of @UpdateStatement. This procedure receives a string, doubles all single quotes, then creates an SQL string that then is executed. This procedure would be vulnerable to this:
EXEC UpdateMyTable N'ʼ;DROP TABLE myTable--'

The first character in the provided string is not a single quote, but the Unicode character U+02BC . SQL will silently convert this into a single quote when stored in a VARCHAR. The injection will work.

Small demo in MS-SQL:
DECLARE @nhack NVARCHAR(100) = N'ʼ;DROP TABLE myTable--'
DECLARE @hack VARCHAR(100) = N'ʼ;DROP TABLE myTable--'
SELECT UNICODE(@nhack),UNICODE(@hack) -- Results: 700      39

More discussing this here: Can I protect against SQL Injection by escaping single-quote and surrounding user input with single-quotes?

Monday, January 07, 2013

T-SQL conditional sort

I've often encountered this situation: a stored procedure needs to display a list of records ordered by a dynamic parameter. In Transact SQL, the Microsoft SQL server, one cannot do this elegantly in any way. I will list them all and tell you what the problem with each is.

First of all, let's start with an example. Assume we have a table called Test with a lot of rows, which has a datetime column which has an index on it. Let's call that TheDate to avoid any SQL keywords. We want to do something like this:

Notice that I want to get the top 10 rows, which means I only need a small part of the total. I also order directly by TheDate. In order to release a piece of code we also need to test it for performance issues. Let's look at the execution plan:

Now, let's try to order it dynamically on a string parameter which determines the type of the sort:

As you see, I've used CASE to determine the sort order. There is no option to give a parameter as the sort order. The execution plan is this:

Surprise! The execution plan for the second query shows it is ten times slower. What actually happens is that the entire table is sorted by the case expression in a intermediate table result, then 10 items are extracted from it.

There must be a solution, you think, and here is an ingenious one:

I transform the datetime value into a float and then I use a mathematical expression on it, multiplying it with 1 or -1. It is the simplest expression possible under the circumstances. The execution plan is:

Bottom line, there is no exception to the rule: when you order by an expression, SQL Server does not use indexes, even if the expression is easily decompilable. Don't get mislead by the apparent functional programming style of SQL syntax. It doesn't really optimize the execution plan in that way.. Even if the column is an integer, it will not work. Ordering by TheInteger is fundamentally faster than ordering by -TheInteger.

And now the solution, ugly as it may be (imagine the select is a large one, with joins and where conditions):
IF @sort='ASC' 

Yes, the dreaded duplication of code. But the execution plans are now equivalent: 50%/50%.

This post was inspired by real events, where the production SQL server went into overdrive trying to create and manage many temporary result tables from a stored procedure that wanted to avoid duplication using the CASE method.

Update: there is, of course, another option: creating an SQL string in the stored procedure that is dynamically modified based on the sort parameter, then the SQL executed. But I really dislike that method, for many reasons.

More information from another blogger: Conditional Order By. They also explore rank using windows functions and in one of the comments there is a reference to SQL 2008 "Grouping Sets" which I have not covered yet.

Thursday, January 03, 2013

TV Series I've Been Watching - Part 13

You know the format, let's get this out of the way. First, the series I've already been talking about:

  • Doctor Who - a weird Christmas special introduced another companion. She is cute and perky and doesn't take commands well. Also, she dies twice already. It doesn't seem to stop her.
  • Torchwood - no Christmas special and no news on a fifth season. I wonder if it will ever reappear.
  • Criminal Minds - still on hold, but not totally rejected yet.
  • Dexter - A weird seventh season sees Dexter in love, running away from mobsters and being helped by his police sergeant to cover up kills or even commit them. Then it all ended in a ridiculous fashion. Hint: watch Dexter's face when Deb jumps at the recently shot person in the last episode: it seems to say That is NOT how this is done! Stop it, just stooop!
  • Fringe - Fringe is not better. I still watch it in order to avoid sci-fi withdrawal symptoms.
  • True Blood - I can't wait for season 6. I do fear for its quality, but I also have high hopes.
  • Weeds - the series has ended, finally, when all life has been squeezed out of it. I am glad it is over.
  • The Good Wife - season 4 is running strong... ish. There are some new ideas introduced in the show that don't really fit well or seem to be needed in the picture, like Kalinda's husband.
  • Haven - season 3 of Haven pits Audrey against a killer that takes people's skins. And she is also to disappear on a certain date in order to stop the troubles. And she is also courted by two guys who are insanely in love with her. What is a girl to do?
  • Lost Girl - removed from my watching list.
  • Falling Skies - season three has not started yet. I am curious how it will continue. Plus it's sci-fi.
  • Southpark - some funny episodes in the October batch. Could have been better. Still good.
  • The Killing - still on my watch list, being a police show and all.
  • Suits - third season is about to start. It doesn't make much sense, but I like it.
  • Breaking Bad - the fifth season has ended and there will not be a sixth. Still haven't had the inclination to watch it.
  • Californication - the sisth season is about to begin. I can't wait.
  • Beavis&Butt-head - It seems the show was quietly axed. I haven't heard anything about it for a long time.
  • Homeland - I've seen two seasons of Homeland and it's pretty cool. Claire Danes is a bipolar CIA agent that fights to prove an American soldier rescued from the Talibans has been turned. She is also in love with him. Not much sense when you put it that way, but a lot of tension. The show is another American adaptation from an Israeli show.
  • The Walking Dead - pretty hard stuff added this season, making it interesting again. Some characters even die! Almost got to "want" status.
  • Game of Thrones - the show follows the books faithfully, even when removing some bits or rearranging others. However I feel it failed to fully capture the atmosphere of the book. We'll see how it goes.
  • L5 - no second episode for a long time now and with the current economic climate, I doubt it will continue. Too bad.
  • Mad Men - season six is to premier in 2013. I will keep watching it, because it is just great.
  • Misfits - all new characters in Misfits, with some stories interesting, but now more towards shocking and/or disgusting. I've removed the "want" status from it, because the quality of the show is not as great as when it started.
  • Sherlock - the third season of the series will begin probably late 2013. I liked it, even if a bit too... Moffaty? I really don't want to see more and more people acting like Doctor Who. One is enough.
  • Spartacus - Vengeance - War of the Damned will start soon, in January. Too bad it ends the show, but then again, maybe it will be something meaningful and slim, without boring filler episodes.
  • My Babysitter's a Vampire - vampire teens, with seer and wizard friends and fighting against the evil in their highschool? It's silly, but at least it knows it is silly. I watch it because it is easy fun.
  • Continuum - a second season has been confirmed, but I don't know when it will start. Rather boring, but sci-fi, you know.
  • Copper - a BBC America drama about Irish immigrants set "Five Points". I really liked it, with its depiction of rampant corruption and racism and classism at the beginnings of the city of New York.
  • Longmire - a second season has been confirmed. I really like the show, even if about a rural cop in the middle of nowhere.
  • Political Animals - it feels interesting and profound, but it is not really so. The impression is strong, though, and I may still had watched it if it weren't cancelled.
  • The Newsroom - my wife loves this. I will watch the second season, but I can't decide if I like it or not.

And now for new shows:

  • 666 Park Avenue - A show about the devil! I haven't started watching it, and it was cancelled already.
  • A Young Doctor's Notebook - This is not really a series, but a mini series. Four episodes, each 20 minute long, about a Russian freshly graduated doctor coming to work in a really remote village. The show is brutal and funny at the same time. I doubt there will be a second season, but this one is worth it. Starring John Hamm and Daniel Radcliff.
  • Arrow - I watch this, even if I don't really know why. It is yet another superhero series where the main strength of the guy is that he shoots arrows. I can't recommend it.
  • Beauty and the Beast - Incredibly beautiful people acting as both beauty and beast in this ridiculous show that attempts to place the story in modern times. The beauty is, obviously, a cop.
  • Blackout - God know I tried to like this show about political and personal corruption in England, but I couldn't. It starts bleak and intense, but quickly devolves into the surreal.
  • Battlestar Galactica - Blood and Chrome - so far William Adama is introduced as a young and cocky pilot in a series of webisodes. The show has everything I loved about the first two seasons of BSG 2004. I can't wait for more.
  • Elementary - the American Sherlock doesn't look or feel at all like a Sherlock. Instead it looks and feels exactly like so many US shows about gifted people helping the police. I still watch it, but the show is really not what it should have been.
  • Emily Owens M.D. - I was caught into one of those times when one wants to see a true doctor show, with actual cases and medical dilemmas. No. Instead this crappy show seems like the poor brother of Gray's Anatomy, with those silly happy songs in the background whenever the female protagonist speaks with other women that are not her evil boss or highschool nemesis (I am not kidding, they added the highschool nemesis thing)
  • Hatfields and McCoys - This American Civil War miniseries was filmed in Romania and stars Kevin Costner. I really wanted to see it, but didn't get around to it, yet.
  • Hit and Miss - A new show I really know nothing about. Six episodes so far.
  • Hunted - Oh, Melissa George as a secret undercover agent (and I do mean undercover). Beautiful, smart, trained to kill and yet fragile as a woman, she leads the show well until... it is put on hold! BBC really screwed up abandoning this show. Cinemax is in negotiations to continue doing it, in collaboration with BBC. Fingers crossed.
  • Last Resort - The show started really well. An American submarine is ordered to nuke Pakistan, but through a secondary network. They request that the order be sent via the primary network, and they are shot upon. They manage to escape and hold fort on a remote island, threatening to launch on any country that even approaches the island. A great premise with very good actors. Unfortunately, the show started to lag a little, then suddenly was cancelled. I would like to believe that it was a nerve they touched with the script, but more likely there are too many stupid people deciding to watch reality shows instead.
  • Made in Jersey - they tried to make "Working Girl" a series, with a New Jersey girl, no less, making it as a lawyer in the big city. It was a complete flop and it was quickly cancelled.
  • Parade's End - another miniseries. The trailer looks really promising and I haven't read the book. As soon as I watch it you will know.
  • Primeval - New World - I can't really say I don't want to see it. It is the American continuation or spin off from the British Primeval show. But it just feels bad in every way, even if Zane from Eureka stars as the lead character.
  • Restless - a miniseries. A young woman finds out that her mother worked as a spy for the British Secret Service during World War II and has been on the run ever since. The synopsis sounds interesting. Two episodes so far, that I have yet to watch.
  • Revolution - can you imagine a sci-fi show that I refused to watch? This is a horrible show, something that combines Xena with Flash Forward. Yes, it is possible. And it sucks!
  • Ripper Street - this is not yet another Jack the Ripper show, instead it is set just after the killings stopped. A bit like Copper, with the police force of the time solving gruesome murders. Haven't got around to watch it, but it might be nice.
  • Secret State - a British miniseries starring Gabriel Byrne. Four episodes, rather captivating, but lacking a proper resolution. Not to mention a happy ending, which British seem to avoid completely :)
  • The Fear - A Brighton crime boss turns entrepreneur and then he goes crazy. Like mentally ill crazy. I haven't started watching this miniseries, but it might be interesting.
  • The Mob Doctor - another promising series that gets cancelled for no good reason. This doctor woman is forced to work for the mod in order to get her younger brother out of debt. She is a brilliant doctor and she has to jumble her official cases with the off the ledger ones. Really interesting, even if a bit bland.
  • Transporter - they decided to do a Transporter series. I couldn't get through half of the first episode. Everything that Jason Statham did well in the show, the ridiculous and pompous ass they placed as a lead did wrong. Every good redeeming quality of a movie that, let's face it, wasn't that great is lacking here. Avoid this waster of time!
  • Vegas - Well, if we have to watch series about cops, at least let them be good ones. Vegas is a good alternative to Longmire, with the action set in the early years of Las Vegas where a farmer and former MP gets to become the sheriff of the town. Interpreted by Dennis Quaid, his character and the local mobster, also well interpreted by Michael Chiklis, make this series interesting and worth watching.
  • Wizards versus Aliens - OK, go ahead: laugh! I did watch a TV series made exclusively for young children where a family of wizards fights a ship of evil aliens set to consume every bit of magic in the galaxy. It is an alternative to The Sarah Jane Adventures, only even sillier. I had fun.

There are several miniseries and shows that just arrived on my radar. No point in discussing something I know nothing about, yet. So far a lot of the shows I loved were cancelled, while stinking refuse of TV series thrive. I am almost to the point of not caring anymore. I hope my short list (yes, I was humblebragging) will help you decide which shows to watch or not watch.

Until the next post in the series about series!