Ot - storing game data

currently i have this setup to hold data about the board in a puzzle game

static char * board_one[] = {
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ", };

what i really want is an array of these so i dont have to have the _one so i can
read data in better and easier.

i understand i am not allowed to have 3d character arrays unless i know they are
all going to be the same size

currently the size is told to the reading part which then collects the dtat from
the board_one array.

i am informed i can have 3d character arrays as lon as they are all the same
size, so i am thinking just have a predfiened array size that tells me the size
of the rest of the array in the first line, so even if the board is only 5x5 it
still sits in a, say, 12x12 array

then i can collect my data like and here is where it all falls apart, how can i
hold the data i want easilly so it is as the board_one definition above, but
only board[1].

hope this all makes a little sence, any suggestions on a way of handling a 3d
character array or different data store soloutions always welcome.

Assuming you aren’t saving these to disk (if you are there are simpiler
solutions) then you can use something like the code below. I have attached
the CPP file since that will be better formatted than what gets posted.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

const int kBoardWidth = 8; // Whatever your board width is
const int kBoardHeight = 8; // Whatever your board height is
const int kMaxBoards = 3; // Max number of boards

// NOTE: I removed the commas after each string. This has the effect
// of creating a single string with everything concatonated. You now
// end up with a character array containing all of the characters in
// your board. The plus one is because we are using a C literal string
// which means the compiler will append a ‘\0’ at the end.
static char board_one[kBoardWidth*kBoardHeight+1] = {
“abcdefgh”
“ijklmnop”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
};

static char board_two[kBoardWidth*kBoardHeight+1] = {
“ABCDEFGH”
“IJKLMNOP”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
};

static char board_three[kBoardWidth*kBoardHeight+1] = {
"01234567"
"89012345"
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
};

static char * boards[kMaxBoards] = {
board_one,
board_two,
board_three,
};

// To access a board you can just use: boards[n] where n is the board
number.
// Sample to access data in a specific board at a specific x,y
char getBoardData( int nBoard, int x, int y ) {
char * pBoard = boards[nBoard]; // or you could call getBoardPtr()

return( *(pBoard+kBoardWidth*y+x) );

}

void test( void ) {

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

// Let's just grab 20 random values from the 3 boards and output them.
for( int i=0;i<20;++i ) {
    int board = rand()%kMaxBoards;
    int x = rand()%kBoardWidth;
    int y = rand()%kBoardHeight;

    printf( "The character for board %d at %d,%d is

%c\n",board,x,y,getBoardData(board,x,y) );
}
}

The output generated by the test() function is:

The character for board 2 at 0,5 is z
The character for board 2 at 4,6 is z
The character for board 2 at 0,2 is z
The character for board 2 at 6,0 is 6
The character for board 1 at 3,1 is L
The character for board 1 at 0,4 is y
The character for board 0 at 2,5 is x
The character for board 2 at 2,5 is z
The character for board 2 at 6,1 is 4
The character for board 2 at 3,4 is z
The character for board 2 at 7,4 is z
The character for board 0 at 1,7 is x
The character for board 2 at 1,1 is 9
The character for board 1 at 6,7 is y
The character for board 0 at 2,0 is c
The character for board 2 at 0,4 is z
The character for board 2 at 3,5 is z
The character for board 0 at 6,4 is x
The character for board 1 at 3,4 is y
The character for board 2 at 6,7 is z

Remember that all X,Y values are ZERO based (the first element is 0, not 1).

I hope that helps.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of neil at cloudsprinter.com
Sent: Saturday, September 01, 2007 3:40 PM
To: sdl at lists.libsdl.org
Subject: [SDL] ot - storing game data

currently i have this setup to hold data about the board in a puzzle game

static char * board_one[] = {
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ", };

what i really want is an array of these so i dont have to have the _one so i
can read data in better and easier.

i understand i am not allowed to have 3d character arrays unless i know they
are all going to be the same size

currently the size is told to the reading part which then collects the dtat
from the board_one array.

i am informed i can have 3d character arrays as lon as they are all the same
size, so i am thinking just have a predfiened array size that tells me the
size of the rest of the array in the first line, so even if the board is
only 5x5 it still sits in a, say, 12x12 array

then i can collect my data like and here is where it all falls apart, how
can i hold the data i want easilly so it is as the board_one definition
above, but only board[1].

hope this all makes a little sence, any suggestions on a way of handling a
3d character array or different data store soloutions always welcome.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.13.2/983 - Release Date: 9/1/2007
4:20 PM

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.13.2/983 - Release Date: 9/1/2007
4:20 PM

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: Source1.cpp
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070901/ad717fcd/attachment.txt

And if you are loading them, use something like this:

struct Board {
unsigned int w;
unsigned int h;
char *board;
}

std::vector<Board *> boards;

int LoadBoard(const char name) {
/
code to open file */
Board newboard = new Board;
/
code to load the board */
boards.push_back(newboard);
return 0;
}On 9/1/07, Ken Rogoway wrote:

Assuming you aren’t saving these to disk (if you are there are simpiler
solutions) then you can use something like the code below. I have
attached
the CPP file since that will be better formatted than what gets posted.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

const int kBoardWidth = 8; // Whatever your board width is
const int kBoardHeight = 8; // Whatever your board height is
const int kMaxBoards = 3; // Max number of boards

// NOTE: I removed the commas after each string. This has the effect
// of creating a single string with everything concatonated. You now
// end up with a character array containing all of the characters in
// your board. The plus one is because we are using a C literal string
// which means the compiler will append a ‘\0’ at the end.
static char board_one[kBoardWidth*kBoardHeight+1] = {
“abcdefgh”
“ijklmnop”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
};

static char board_two[kBoardWidth*kBoardHeight+1] = {
“ABCDEFGH”
“IJKLMNOP”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
};

static char board_three[kBoardWidth*kBoardHeight+1] = {
"01234567"
"89012345"
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
};

static char * boards[kMaxBoards] = {
board_one,
board_two,
board_three,
};

// To access a board you can just use: boards[n] where n is the board
number.
// Sample to access data in a specific board at a specific x,y
char getBoardData( int nBoard, int x, int y ) {
char * pBoard = boards[nBoard]; // or you could call getBoardPtr()

return( *(pBoard+kBoardWidth*y+x) );

}

void test( void ) {

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

// Let's just grab 20 random values from the 3 boards and output them.
for( int i=0;i<20;++i ) {
    int board = rand()%kMaxBoards;
    int x = rand()%kBoardWidth;
    int y = rand()%kBoardHeight;

    printf( "The character for board %d at %d,%d is

%c\n",board,x,y,getBoardData(board,x,y) );
}
}

The output generated by the test() function is:

The character for board 2 at 0,5 is z
The character for board 2 at 4,6 is z
The character for board 2 at 0,2 is z
The character for board 2 at 6,0 is 6
The character for board 1 at 3,1 is L
The character for board 1 at 0,4 is y
The character for board 0 at 2,5 is x
The character for board 2 at 2,5 is z
The character for board 2 at 6,1 is 4
The character for board 2 at 3,4 is z
The character for board 2 at 7,4 is z
The character for board 0 at 1,7 is x
The character for board 2 at 1,1 is 9
The character for board 1 at 6,7 is y
The character for board 0 at 2,0 is c
The character for board 2 at 0,4 is z
The character for board 2 at 3,5 is z
The character for board 0 at 6,4 is x
The character for board 1 at 3,4 is y
The character for board 2 at 6,7 is z

Remember that all X,Y values are ZERO based (the first element is 0, not
1).

I hope that helps.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

-----Original Message-----
From: sdl-bounces at lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org]
On
Behalf Of neil at cloudsprinter.com
Sent: Saturday, September 01, 2007 3:40 PM
To: sdl at lists.libsdl.org
Subject: [SDL] ot - storing game data

currently i have this setup to hold data about the board in a puzzle game

static char * board_one[] = {
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ", };

what i really want is an array of these so i dont have to have the _one so
i
can read data in better and easier.

i understand i am not allowed to have 3d character arrays unless i know
they
are all going to be the same size

currently the size is told to the reading part which then collects the
dtat
from the board_one array.

i am informed i can have 3d character arrays as lon as they are all the
same
size, so i am thinking just have a predfiened array size that tells me the
size of the rest of the array in the first line, so even if the board is
only 5x5 it still sits in a, say, 12x12 array

then i can collect my data like and here is where it all falls apart, how
can i hold the data i want easilly so it is as the board_one definition
above, but only board[1].

hope this all makes a little sence, any suggestions on a way of handling a
3d character array or different data store soloutions always welcome.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.13.2/983 - Release Date: 9/1/2007
4:20 PM

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.13.2/983 - Release Date: 9/1/2007
4:20 PM


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


su; cd /; rm -rf *

(not responsible for anything that happens if you’re foolish enough to type
that)

arrgh everone is throwing cpp at me gain! i can barkey cope with c!

(q the cpp is easier / better arguments… but i know c i like c, it
does it for
me :wink: )

anyway just for the record none of this data will be on disk, i’m keeping
everyhting in .h files to be compiled as part of the binary.

i’m just about to try setting up the stuff ken homebrew dot com mailed in so
hopefully i can have some sane code soon.

Quoting James Buchwald :> And if you are loading them, use something like this:

struct Board {
unsigned int w;
unsigned int h;
char *board;
}

std::vector<Board *> boards;

int LoadBoard(const char name) {
/
code to open file */
Board newboard = new Board;
/
code to load the board */
boards.push_back(newboard);
return 0;
}

On 9/1/07, Ken Rogoway wrote:

Assuming you aren’t saving these to disk (if you are there are simpiler
solutions) then you can use something like the code below. I have
attached
the CPP file since that will be better formatted than what gets posted.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

const int kBoardWidth = 8; // Whatever your board width is
const int kBoardHeight = 8; // Whatever your board height is
const int kMaxBoards = 3; // Max number of boards

// NOTE: I removed the commas after each string. This has the effect
// of creating a single string with everything concatonated. You now
// end up with a character array containing all of the characters in
// your board. The plus one is because we are using a C literal string
// which means the compiler will append a ‘\0’ at the end.
static char board_one[kBoardWidth*kBoardHeight+1] = {
“abcdefgh”
“ijklmnop”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
“xxxxxxxx”
};

static char board_two[kBoardWidth*kBoardHeight+1] = {
“ABCDEFGH”
“IJKLMNOP”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
“yyyyyyyy”
};

static char board_three[kBoardWidth*kBoardHeight+1] = {
"01234567"
"89012345"
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
“zzzzzzzz”
};

static char * boards[kMaxBoards] = {
board_one,
board_two,
board_three,
};

// To access a board you can just use: boards[n] where n is the board
number.
// Sample to access data in a specific board at a specific x,y
char getBoardData( int nBoard, int x, int y ) {
char * pBoard = boards[nBoard]; // or you could call getBoardPtr()

return( *(pBoard+kBoardWidth*y+x) );

}

void test( void ) {

/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

// Let's just grab 20 random values from the 3 boards and output them.
for( int i=0;i<20;++i ) {
    int board = rand()%kMaxBoards;
    int x = rand()%kBoardWidth;
    int y = rand()%kBoardHeight;

    printf( "The character for board %d at %d,%d is

%c\n",board,x,y,getBoardData(board,x,y) );
}
}

The output generated by the test() function is:

The character for board 2 at 0,5 is z
The character for board 2 at 4,6 is z
The character for board 2 at 0,2 is z
The character for board 2 at 6,0 is 6
The character for board 1 at 3,1 is L
The character for board 1 at 0,4 is y
The character for board 0 at 2,5 is x
The character for board 2 at 2,5 is z
The character for board 2 at 6,1 is 4
The character for board 2 at 3,4 is z
The character for board 2 at 7,4 is z
The character for board 0 at 1,7 is x
The character for board 2 at 1,1 is 9
The character for board 1 at 6,7 is y
The character for board 0 at 2,0 is c
The character for board 2 at 0,4 is z
The character for board 2 at 3,5 is z
The character for board 0 at 6,4 is x
The character for board 1 at 3,4 is y
The character for board 2 at 6,7 is z

Remember that all X,Y values are ZERO based (the first element is 0, not
1).

I hope that helps.

Ken Rogoway
Homebrew Software
http://www.homebrewsoftware.com/

-----Original Message-----
From: sdl-bounces at lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org]
On
Behalf Of neil at cloudsprinter.com
Sent: Saturday, September 01, 2007 3:40 PM
To: sdl at lists.libsdl.org
Subject: [SDL] ot - storing game data

currently i have this setup to hold data about the board in a puzzle game

static char * board_one[] = {
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ",
"xxxxxxxx ", };

what i really want is an array of these so i dont have to have the _one so
i
can read data in better and easier.

i understand i am not allowed to have 3d character arrays unless i know
they
are all going to be the same size

currently the size is told to the reading part which then collects the
dtat
from the board_one array.

i am informed i can have 3d character arrays as lon as they are all the
same
size, so i am thinking just have a predfiened array size that tells me the
size of the rest of the array in the first line, so even if the board is
only 5x5 it still sits in a, say, 12x12 array

then i can collect my data like and here is where it all falls apart, how
can i hold the data i want easilly so it is as the board_one definition
above, but only board[1].

hope this all makes a little sence, any suggestions on a way of handling a
3d character array or different data store soloutions always welcome.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.13.2/983 - Release Date: 9/1/2007
4:20 PM

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.484 / Virus Database: 269.13.2/983 - Release Date: 9/1/2007
4:20 PM


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


su; cd /; rm -rf *

(not responsible for anything that happens if you’re foolish enough to type
that)

e-mail provided by Moose Internet Services
http://www.moose.co.uk/

So here is some C then. :wink:

Makes it very easy to add more boards. You of course want to add some
sanity checking before you use the board data.
One thing that needs checking is the length of the data String of each
boards. And of course you want to check that they contain only valid
chars.

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

struct board {
int width;
int height;
char* data;
} Board;
// “12345678901234567890123456789012"
static struct board boards[] = { {5,5, “here is w*h map chars=25.” },
{3,3, “OXO” // Easier to do it like this
"OXX” // So that we see the map layout
"XOX" },
{15,8,“xxxxxxxxxxxxxxx”
“x x”
“x xxxxxxxxx x x”
“x x x x x x”
“x x x x x x”
“x x xxxxxxxxx x”
“x x”
“xxxxxxxxxxxxxxx” } };

// How to access data in a specific board at specific x,y position
int getBoardData( int n, int x, int y ) {
char* data = NULL;
if( x < 0 || y < 0 ||
x >= boards[n].width || y >= boards[n].height )
return -1; // not inside the board

data = boards[n].data;
return ((int) (data+yboards[n].width+x));
}

int
main( int argc, char* argv[] ) {
int i,j,k;
int board_count = sizeof(boards)/sizeof(struct board);
// Print out the boards.
for( i=0; i<board_count; ++i ) {
printf( “Board: %d\n”, i );
for( j=0; j<boards[i].height; j++ ) {
printf("%c",’’’);
for( k=0; k<boards[i].width; k++ )
printf( “%c”, (char)getBoardData( i, k, j ) );
printf( “’%c”, ‘\n’ );
}
printf( “%s”, “\n\n” );
}
return 0;
}On Monday 03 September 2007, neil at cloudsprinter.com wrote:

arrgh everone is throwing cpp at me gain! i can barkey cope with c!

(q the cpp is easier / better arguments… but i know c i like c, it
does it for
me :wink: )

anyway just for the record none of this data will be on disk, i’m
keeping everyhting in .h files to be compiled as part of the binary.

i’m just about to try setting up the stuff ken homebrew dot com
mailed in so hopefully i can have some sane code soon.