Jump to content
  • Advertisement

Archived

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

SSJCORY

I need help with creating a game board.

This topic is 5749 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

    
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!