I need help with creating a game board.

Started by
14 comments, last by SSJCORY 21 years, 6 months ago
Also another thing i don''t understand what sizeof is?
Thanks for your help

Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
An array contains many variables. To set one, you simply say which part of the array surrounded by brackets. If you wanted to set the first element of your array, you would say:

board[0][0] = ''b'';

You can simply say your_array[someval][someotherval] to get or set a value.

Note that arrays start at element zero. For example, the declaration "int anArray[10]" declares elements 0 to 9. You can say "anArray[0] = ''b''"... and so on up until "anArray[9]". This is ten elements (count ''em - 0,1,2,3,4,5,6,7,8,9). Remember that the highest index of your array is always one less than the value you chucked in.

For example, if you wanted to set the 3rd value in a one-d array then you''d change the element[2], not [3]! Remember: 0, 1, 2 = three elements along.
Oh, sizeof() is an operator that returns the size of a value (or type) in bytes. For example, int = 4 bytes, so sizeof(int) = 4. I used in there because memset() wanted how many bytes to fill up with the given value. I said sizeof(board) - if you cout that then you''ll see that sizeof(board) = 100 (to be precise, it''s equal to sizeof(char) * 10 * 10, where sizeof(char) = 1).

It''s not something that you''ll need to use much early on.
Is there any way to make a game using this method? Say
the char is * and the walls are a.
and they have to get out of the maze.
Thanks


Cory Fisher
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
You'd have to generate your maze using some method (or load it up from a file if you're feeling confident). You'd set board[some_row][some_column] to either a '*' (for your location), 'a' (for a wall) or ' ' (for a blank space).

You couldn't really use memset() because that fills up consecutive locations in memory - but your maze won't be like that, so it won't help you much.

Here's example code to generate an easy maze:


    #include <cstring>#include <cstdlib>#include <iostream>using namespace std;int main(){    // first, we have to say where the player is...    int player_x = 1;    int player_y = 1;    int x,y;    char board[10][10];    memset(board, 0, sizeof(board));    // todo: clever maze generation.  I'll set the bottom row    // to 'a' so that you can see how to do it    for (x = 0; x < 10; ++x)    {        board[9][x] = 'a';    }        // now, let's set another bit of the array - for example, the 2nd row, 3rd element along    board[1][2] = 'a'; // note: one less each time    // now display the maze as before    for (y = 0; y < 10; ++y)    {        for (x = 0; x < 10; ++x)        {            if (x == player_x && y == player_y) // is the player here?                cout << '*'; // display the player's character            else                cout << board[y][x]; // display what's here        }        cout << endl;    }    system("pause");    return 0;}  


I'm using array[y][x] each time, not array[x][y]. This might confuse you, but it's just a common practise among programmers (there's a reason to do with efficiency when looping over it, but you don't have to concern yourself with that).

Think of it at the moment as array[row][colum].

Also, note the "one less than you think" way of setting it - we wanted to set row 2, column 3 - but the line said board[1][2]! This is as I said before - remember that the arrays start with element zero.

[edited by - Alimonster on October 19, 2002 5:51:41 PM]
By the way, you could store the player''s location in the board itself if you wanted - board[player_y][player_x] = ''*'';. This would make the drawing loop simpler:

for y...
for x...
cout << board[y][x];
cout << endl;

(no if there, notice)

You''d have to remember to remove the old player location from the board when they moved, though, because the player won''t be in multiple places at one.

This topic is closed to new replies.

Advertisement