Archived

This topic is now archived and is closed to further replies.

SSJCORY

I need help with creating a game board.

Recommended Posts

Hi i know how to create a game board with an array by doing this char board[10][10]; then setting each variable to the char that i want by board[1][1]=''a''; and so on But I was wondering if there is an easier way to set all of them to one character so i could display them without goin cout<

Share this post


Link to post
Share on other sites

    
int ix,iy;
for(ix=0;ix<10;ix++)
{
for(iy=0;iy<10;iy++)
{
board[ix][iy] = 'a';
}
}

that will do it, but
use your noggin to make that same routine make a checker board

--Fireking

Owner/Leader
Genetics 3rd Dimension Development

[edited by - fireking on October 19, 2002 4:55:41 PM]

Share this post


Link to post
Share on other sites
You can loop through your board with 2 standard C++ loops. For every iteration it is just to set each board space to a specific piece.


  
char board[10][10];
int i, j;

for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
board[i][j] = ''a'';
}
}

Share this post


Link to post
Share on other sites
LOL fireking and I posted the same answer, I viewed the thread before his answer was there! Remember that you can use the same loop structure to draw the entire board of course; just make two loops and place a cout call to the according piece. Example:


  
char board[10][10];
int i, j;

for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
cout << board[i][j];
}
cout << "\n"; //Remember we wan''t a new line every row.

}

Share this post


Link to post
Share on other sites
You could use memset() to set the array, which would do the same job but with only one line of code.

EDIT: Oh wait, never mind. You wanted to display it, not set it. Ah well. Btw, you might have to use cout.flush() with the above code, or alternatively you could cout << endl; instead of cout << '\n';

[edited by - Alimonster on October 19, 2002 5:10:34 PM]

Share this post


Link to post
Share on other sites
memset() would only clear the array. You pass the first param as the address of a var to clear, then the value, then the size (you can use sizeof() for that).


    
#include <cstdlib>

#include <cstring>

#include <iostream>

using namespace std;

int main()
{
char board[10][10];

memset(board, 'a', sizeof(board)); // sets all elements of board to 'a'


// now display the board

for (int y = 0; y < 10; ++y)
{
for (int x = 0; x < 10; ++x)
{
cout << board[y][x];
}
cout << endl;
}

system("PAUSE");
return 0;
}


[edited by - Alimonster on October 19, 2002 5:14:54 PM]

Share this post


Link to post
Share on other sites
Thanks memset worked well. But now how do i set a piece of the array to a char.
Thanks I think i know but it''s better to hear from others

Cory Fisher

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites