Monday, February 7, 2011

Regex are AWESOME

So today in class we`ve seen regular expressions ( widely known as Regex ). They are expressions used to recognize if a piece of code follows a certain pattern. A regex is usually a string of character representing the expression. Regex'es are language agnostic, meaning they will work with whatever language you are using provided it uses the right libraries.

A regex looks like this : "^[a-z]\d[a-z][\s-]?\d[a-z]\d$" .
This particular regex is used to check a string to see if it represent a canadian postal code (they look like this H2Y 8D7 with a space, dash or nothing in the middle).

How does it works ? Like this : A regular expression is filled with special alphanumeric characters and symbols. How you place these characters determines how the target string will be evaluated. Rather like programming languages with keywords and expressions.

The first set of symbols is the [a-z] interval. An interval is used to represent any value comprised between those two. In this instance I mean any lowercase letter between a to z.

The second set of symbol is \d . \d is a pre-defined character. It means whatever numeric character. It is the same as [0-9] but shorter. Then is another a to z interval. These three expressions are used to check for the first three characters of the targeted string. The last three is checked the same way.

A simple problem we are faced is the middle space, or the absence of it. Indeed we have to check that the fourth character is a space, dash or nothing. Several operators are used to check occurrences of several characters.

  • * : The asterisk is used to check 0, 1 or several occurrences of the previous character. A* will return true to A, AA, AAA, AAAA and basically every A in the text.
  • + : The plus sign is used to check 1 or several occurrences of the previous character (or expression between brackets [ ] a "class" ).  H+ will return true to HH, HHH and every other series of two consecutive H. Not simply one though.
  • ? : The interrogation mark is used to check 0 or 1 occurrence of the previous class. It will test if a class is there or not. This what we need to test if there is a space, a dash or none.
The expression used to test this condition is [\s-]? . This checks if there is, or not, a white space or a dash. White space being represented by \s.

Lastly, you can see symbols in the first and last position of the regex. These are to make sure the value we check does not contain any other characters before and after the string. The ^ symbol is used at the beginning of an expression to check if the string *starts* with the tested expression and the $ is used at the end to check if it *ends* with the expression. Using both we make sure the tested string starts and ends with our pattern.

An example :
We want to check if a string is a Canadian permanent code. These codes are made of four letters and 8 numbers with spaces(or not) at the 5th and 8th position.

Regular Expression :
^[a-z]{4}[-\s]?\d{4}[-\s]?\d{4}$

Test strings : 
SIGH03129117
DUG712345673
SIGH 0312 9117
SIGH 03129117
SIGH0312 9117
1234DUGH9154

Using the expression above, we can see that the second and last expression does NOT fit and we did that only by checking it against a string of symbols. Much less hassle than going by letter by letter with a loop.

Here is the program I made in class today, it contains many good examples. Available with the .net framework 4, as are the majority of what I do anyway. Don't forget to grab the latest version.

EDIT : Testing a regex can be a pain in the ass. This is why I use a tool, its here if anyone would like to try it. It is an AWESOME regex testing tool and I recommended it to anyone interested. 

Saturday, February 5, 2011

We need a new prog post !

Ok well, since this is a blog about prog and I'm starting on a very boring and prog-less path. So ! My next post is going to be about some programming stuff, still in VB.net because I like it very much and its easy to explain.

Not sure when I'll make another post, probably in less than 6 months though.

First things first : I need a name

OK, I already have a name. MY name, and I'm not using it for this. I need a company/team name. Its not lerouxtechnologies anymore because it has to be a name to represent multiple people, I intend to make this a team. I have a few candidates I can choose from, this should be fun.

I only have a few choices yet :

  • Dragon Roost + Software, Team (Not sure which one yet)
  • Drunk Gamers
  • CodeHackers
  • Team Coders
As you can see the list is quite small, and I did not ask my future teammates about it. I could wait but it could take a while to choose a name we all want. Dragon roost software is my favorite to date, I took it from Zelda : The Wind Waker when I was listening to its soundtrack on ZREO. Its just such a good song.

The trouble of finding a new idea

They say ideas flow miles a second in our head. Thats probably why they get out so fast, and why we can't hold on to one. Its a shame really, having abandoned so many projects and having a hard time finding a new one.

So here I am, in search of an idea. One that I can, hopefully, complete in order to learn the right development process. It has been 272 days since my last post and I have created some many project stubs and samples. So many empty test projects. Of course I learned a lot in programming since then, learned new languages (or at least parts) and concepts. With that I hope to create another project with a tad more complexity, or at least something with fun features.

Also my urge to write some new stuff is growing again. So I'll try post my idea gathering here, maybe a couple posts a week. That'd be fun.

Sunday, May 9, 2010

Design update !!

Hey, I told you all that I would take my time and post something eventually. Well now is the time !

I have been working on the way my main form looks like. I had a few problems: 
First I wanted to get rid of the ugly conversation panel I have. The idea of a form popping whenever you talk to someone is crap, and having to close it again and again is a pain sometimes. I wanted to have some kind of control that would show messages for me. I have recently learned to make my own custom controls and found the idea of having a portable "messages & communication" box quite good. And here is the control and the code: 

The images speaks for themselves. The messagebar has a sub named addMSG() used to send messages to the textbox. The textbox is readonly to prevent the user disturbing the text and because the keys I use to move are the arrow keys I have made a sub to replace the caret (the little horizontal line standing where you clicked) every time a key is pressed. That way you cannot scroll through the textbox inadvertently when moving, only with the scroll bar. 

I have also begun to look into the code involved in moving around. The same 4 subs ( one for each direction) are both used to move and initiate a transition and I think this is a flawed way of getting around since the complexity is very case-by-case and thus need a lot of if's to verify stupid stuff. Due to the complexity of the overhaul this could take some time but I am still working on other parts while I'm digging around for a better solution. 

I have downloaded some tiles on a website because I don't really have *that* much time on my hand for designing .jpegs, time that could be better used somewhere else. They look good and I plan to make use of them someday to showoff a better version of the game. 

The game now looks thus: 



As you see I have made some subs to handle conversation. Now every sprite has an NPC class that is used to store conversation and other relevant stuff. I'll explain it in a later post. On this note see you all at the next post !


Saturday, April 17, 2010

Blog is NOT dead.

Like I said in an earlier post. I won't be posting a lot these days. I have a shitload of work to do for school and friends to take care of, so the little time I get to program stuff is spent in fruitless attempts to re-engineer my movement system. That or creating sprites or tiles. I'm hoping to be able to post a bit more in the coming weeks, I just need some new material to blog about. I'll find something new to implement in my game and work on it.


Until then, take care !

Thursday, April 8, 2010

Ha, been a week already !

Yeah no kidding, more than a week without post. You guys must be really bored. Well as you know I've been doing design lately and since I'm doing this on paper ( with a pen yes ! Like old times ) because it illustrates ideas better when you can freely draw and cross things out.


I have nearly a page of story and some pages of mechanics for the game that I'd like to implement. The story will be put in a .doc format and sent online so it can be browsed. Its NOT finished yet and I might just take another story that I started from scratch.


Beside that I'm pretty busy with other stuff, school and friends have successfully managed in keeping me away from concentrating on programming ( or sprite creation ), I'll be sure to make a couple big updates when I get a breather.


I'm also looking for books to read on subjects like A.I. , game design, story writing ( for games), graphical design ( Pixel art preferred, I don't do 3d ) and programming ( vb.net, of course ^.^ ).