[SDL] ot - storing game data
James Buchwald
jamestheprogrammer at gmail.com
Sun Sep 2 19:34:22 PDT 2007
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 <Ken at homebrewsoftware.com> 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)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070902/b3ed8823/attachment.html
More information about the SDL
mailing list