C#

Fools ignore complexity; pragmatists suffer it; experts avoid it; CREATIVES remove it.

Monday, September 24, 2007

Design Pattern Books

Sometimes you get all information you need in one book only, but sometimes, even including all information directly from core, it's possible that book has not been written best way it could do, I'm mean format.
if you ask me a good book about Design Patterns I would like to suggest two, first one it's the bible:



I'm used to say if you want to learn about C start reading “The C programming language” from Kernighan and Ritchie or if you want to learn about C++ start reading “The C++ programming language” from Stroustrup because those books are bibles written by creators of those languages; to learn with creators really works for me, the same can be said about Design Patterns, you can start reading the book above, after a first contact with idea, in order to get some fluency, you will need practice, more research and more samples, internet is full of them, but you know internet is a free land, so take care with pseudo gurus; in order to help you, I would like to suggest a second book about patterns that worked for me, this second book has a practical approach, I love practical things, less academic, the only bad point about this book is about language, they have been using Java, I’m not a Java programmer but if you know C++ or even C+# and OO you can read without any reserve.



I've friends that confessed me they ignored this book because the cover, don’t do that, it’s a nice book!
Keep studying and keep creative.










Another very very nice work, this time, they've provided codes in other languages and all kind of examples; really excellent job.




Design Patterns: Simply


http://sourcemaking.com/design-patterns-simply-course

Good reading!

Thursday, September 20, 2007

Float random in 15 minutes

I was reading Gamasutra website when I saw an interesting job offer, please check out, http://jobs.gamasutra.com/jobseekerx/viewjobrss.asp?cjid=12163&accountno=219
I couldn't avoid write that function they are describing in the first part of test.

* The function rand_FloatRange(float a, float b) returns a random float number, 'x', such that a <= x <= b.


I’ve decided to invest 15 minutes from my spare time, so after 5 minutes trying the conventional approach I just decided change strategy in order to make that function work in the last 10 minutes; Pressing creative button all became clear: we need to return a number that needs match AB range, the difference between A and B can be considered a distance as well, a percent, that was my approach:


float rand_FloatRange(float a, float b)
{
return ((b-a)*((float)rand()/RAND_MAX))+a;
}


All I did was transform conventional rand() in percent, now that I’ve a percent I can apply that to range between A and B, it doesn't matter if they are negative or positive values, easy, huh?

that’s all!!! nothing bad for 15 minutes

Keep creative!

Saturday, August 25, 2007

Tic Tac Toe - checking winner without matrices



Recently I was interviewed for a big company, (no names here); it was an exciting and interesting interview by phone; the recruiter started asking me about linked lists, data structures, collisions trees, path finding, etc… It was strange talking about code by phone, I had never been interviewed that way before, not by phone; worst than that I was under strong medicines, I had caught a cold one night before, summarizing, that was not my day, during interview one of the questions allowed me show to recruiter exactly how I’m used to think, He asked me:

"How would you implement the rules of a Tic Tac Toe game?"

He asked me to think while telling him what I was thinking. I was under pressure and doesn’t matter how much recruiter tried to calm down it was still an interview for a big company, describing code techniques by phone, so I started answering conventional way, I meaning not the way I'm used to do, (creative way), I started creating a 3x3 matrices considering game positions, etc… suddenly some voice inside of me started screaming: “Wake up Flavio! What you are doing! that’s your opportunity to show how you think, don’t fear, don’t be conventional!” Instantly I started thinking the way as I’m used to do, I'm used to think visually first, metaphorical way second, design third and coding at last so I said to recruiter: “Forget everything you heard I would like to start again, Tic Tac Toe is a game which the rules are fixed, it never changes, you can visualize board so that, you can see results visually and map them all in any place, its about 8 possibilities of victory starting count after three choices, a bit field should work fine for that" so I suggested a bit field which would avoid matrices manipulation overhead. I felt some “what? show me the money” feeling coming from other phone line, and He asked me to tell him the code for this, by phone, He asked me to write the code in a piece of paper, or compiler, or any text editor and keep telling him what I was writing and thinking, well, I was not comfortable, I was sick and under pressure and I was not able to do the code that time, after sleep a little and wake up better here is the code.




// tictactoe.cpp : This code is only for illustrate an idea.
// 08/26/2007 by flavio.rodriguez@gmail.com

#include "stdafx.h"
#include <iostream>

#define TEST_IF_PLAYER_WIN(playerMoves, bitTest) (playerMoves&bitTest) == bitTest

using namespace std;

/*

Mapping Tic Tac Toe victory possibilities

X|X|X = 111
0|0|0 = 000
0|0|0 = 000
-----------
111000000

0|0|0 = 000
X|X|X = 111
0|0|0 = 000
-----------
000111000

0|0|0 = 000
0|0|0 = 000
X|X|X = 111
-----------
000000111

X|0|0 = 100
X|0|0 = 100
X|0|0 = 100
-----------
100100100

0|X|0 = 010
0|X|0 = 010
0|X|0 = 010
-----------
010010010

0|0|X = 001
0|0|X = 001
0|0|X = 001
-----------
001001001

X|0|0 = 100
0|X|0 = 010
0|0|X = 001
-----------
100010001

0|0|X = 001
0|X|0 = 010
X|0|0 = 100
-----------
001010100

*/

typedef struct
{
unsigned long _LD : 9; // 111000000
unsigned long _LM : 9; // 000111000
unsigned long _LU : 9; // 000000111

unsigned long _CL : 9; // 100100100
unsigned long _CM : 9; // 010010010
unsigned long _CR : 9; // 001001001

unsigned long _LR : 9; // 100010001
unsigned long _RL : 9; // 001010100

}WINNING;

int _tmain(int argc, _TCHAR* argv[])
{
WINNING win;

//The 8 victory possibilites
win._LD = 0x1C0;
win._LM = 0x38;
win._LU = 0x7;
win._CL = 0x124;
win._CM = 0x92;
win._CR = 0x49;
win._LR = 0x111;
win._RL = 0x54;

/*
* Although this code works it was made as pseudo, I mean no structure or functions,
* the objective here is just check victory
* then imagine a player called A
*/
unsigned long playerA = 0;

/*
each quad represents a position in game

| | | |
|--|--|--|
| | | |
|--|--|--|
| | | |

and here is their relative values

|1 | 2| 4|
|--|---|---|
|8 | 16| 32|
|--|---|---|
|64|128|256|
*/

/*
* Now you can simulate any player move;
* in case of two players, we should just to create a player called B
* each time player click a quad, a bit is incremented
* and that would begin to be tested after the third move, once
* before that time we don't know winner
*/

//player move - Good choice
playerA |= 64;
//play move - Good Choice
playerA |= 128;
//play move - Bad choice
playerA |= 2;
//play move - good choice you won!
playerA |= 256;

//CHECKING VICTORY
//please, FOR SURE it would be in a function
//its only to illustrate
if (TEST_IF_PLAYER_WIN( playerA, win._LD ))
cout << "WON LINE DOWN" << endl;
else if (TEST_IF_PLAYER_WIN( playerA, win._LM ))
cout << "WON LINE MIDDLE" << endl;
else if (TEST_IF_PLAYER_WIN( playerA, win._LU ))
cout << "WON LINE UP" << endl;
else if (TEST_IF_PLAYER_WIN( playerA, win._CL ))
cout << "WON COL LEFT" << endl;
else if (TEST_IF_PLAYER_WIN( playerA, win._CM ))
cout << "WON COL MIDDLE" << endl;
else if (TEST_IF_PLAYER_WIN( playerA, win._CR ))
cout << "WON COL RIGHT" << endl;
else if (TEST_IF_PLAYER_WIN( playerA, win._LR ))
cout << "WON LEFT TO RIGHT" << endl;
else if (TEST_IF_PLAYER_WIN( playerA, win._RL ))
cout << "WON RIGHT TO LEFT" << endl;

return 0;
}



Learned lessons, never to be interviewed sick!

That's it! stay creative!