Should I go for the command line display via cout or printf ?
or would using simple graphics libraries be better(&&faster??) ?
btw..I was trying to implement a Conway's Game of Life program..with the simple rules..
Should I go for the command line display via cout or printf ?
or would using simple graphics libraries be better(&&faster??) ?
btw..I was trying to implement a Conway's Game of Life program..with the simple rules..
Why are you asking about the boolean only? Because it sounds like you would care that this is a boolean array.
There are some methods to optimize boolean array operations (like in special case of vector - vector<bool>), but when it comes to "displaying", it will not matter, since most of the execution time will be spent on displaying (drawing) itself.
You should not ask about displaying boolean arrays, but about displaying anything in general, if I understand.
How big is your game grid?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I am thinking of making it grow on demand.
Else a toroidal 100x100.
for boolean ...maybe the matrix can exactly fit into an implementation of some display function of some graphics library..._.But my knowledge in graphics...is yet very immature.. ..so I can't decide for sure..displaying anything in general
Well if you can manually cope with creating a console with a suitable font/size, then you should be able to get that kind of resolution just by printing using cout.
As for graphics, you need to say which OS/Compiler you're using. There's just too much choice to even hazard a guess as to where you might start.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
o..sorry.. It is Linux Mint(Debian)... && gcc-4.6
I don't know about printing, but this should not matter much. In terms of storing a matrix of Boolean consider creating an array of unsigned in of size the number of columns in the matrix. Each row can than be represented by an integer in binary representation. You have to check any bit of the integer to get the value in the column of the matrix. This is what i did (assuming a square array but this is easily changed)
Code:#include <iostream> // BIT MANIPULATIONS #define BOOL(x) ( !(!(x)) ) #define BIT_TEST( arg , pos ) BOOL( (arg)&(1L << (pos)) ) #define BIT_FLIP( arg , pos ) ( (arg)^(1L << (pos)) ) inline unsigned int BIT_SUM( unsigned int v ) { unsigned int sum; unsigned int tmp = v; tmp = tmp - ( ( tmp >> 1 ) & 0x55555555 ); tmp = ( tmp & 0x33333333 ) + (( tmp >> 2 ) & 0x33333333 ); sum = ( ( tmp + ( tmp >> 4 ) & 0xF0F0F0F ) * 0x1010101 ) >> 24; return( sum ); } int main() { const unsigned column = 10; unsigned int row[ column ] = {column}; // FILL ARRAY TO TEST, JUST ANYTHING for( int i = column; i --; ) { row[ i ] = i; } // READ AND PRINT VALUES for( int i = column; i --; ) { printf( "%u : " , row[ i ] ); for( int j = column; j --; ) { printf( "%u " , BIT_TEST( row[ i ], j ) ); } printf( " : %u \n" , BIT_SUM( i ) ); } // READ FLIP AND PRINT VALUES for( int i = column; i --; ) { printf( "%u : " , row[ i ] ); for( int j = column; j --; ) { printf( "%u " , BIT_TEST( BIT_FLIP( row[ i ], j ), j ) ); } printf( " : %u \n" , BIT_SUM( i ) ); } return 0; }
What advantage would this low level approach to storage have over creating a (x*y) sized boolean array?
I (maybe erroneously) understand that they are exactly same as far as the computer is concerned...but increases the effort needed by the user...
I haven't looked into this code much, but he probably wants to use one bit per boolean. He forgot that this is already implemented in the STL:
bitset - C++ Reference
for fixed size. For dynamic, use vector<bool>.
Also, wtf is this:
Firstly, it collides with WinAPI (he might want to move to Windows?) and possibly with other implementations. To be honest, this is the first time I see someone trying to convert int to bool this way.Code:#define BOOL(x) ( !(!(x)) )
looks better to me.Code:static_cast<bool>(x)
I heard that the use of vector<bool> wastes memory,time..everything(!) ....but never found out a reason other than that they are high level and doesn't do anything concerning bits.... Is there anything except that?
As for storage, I created my own class storing the bools with all the necessary means for dynamic operations..(I implemented a kind of linked lists for linking the 1d arrays together...but the O(n) should not be a problem because the problem typically requires fetching only the data immediately next to the current item..)
But the problem seems to be in the display...I'm having a bad experience ..trying to line out the output in the terminal..So any idea about implementing a graphical frontend (I'm thinking of learning a bit of QT) would be gold...
Me?...nope.. *nix all the way...he might want to move to Windows?
Last edited by manasij7479; 05-13-2011 at 12:20 PM.