how can i do a function?
Dec 14, 2010 at 5:18am UTC
I would like to do a function just to output a gameboard instead of write the same code everytime when a change occurs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
using namespace std;
int gameboard();
int main () {
int gameboard[10][10];
int row, col;
srand(time(NULL));
for (row = 0; row < 10; row++) {
for (col = 0; col < 10; col++) {
gameboard[row][col] = 0;
cout << gameboard[row][col] << " " ;
}
cout << endl;
}
gameboard[0][0] = 6;
gameboard[9][9] = 9;
for (row = 0; row < 10; row++) {
for (col = 0; col < 10; col++)
cout << gameboard[row][col];
cout << endl;
}
getch();
return 0;
}
Dec 14, 2010 at 7:44am UTC
Hi
have put drawgameboard as a function
and moved gameboard [10][10] as a global variable
also have initialixed gameboard in main function.
I notice you have srand so assume you will be using random numbers to change
the board. If you are I would put this in a while loop to update until finished
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
using namespace std;
void drawGameboard();
int gameboard[10][10];
int main () {
for (int i=0;i<10;i++)
{
for (int j=0;j<10;j++)
{
gameboard[i][j] = 0;
}
}
srand(time(NULL));
drawGameboard();
gameboard[0][0] = 6;
gameboard[9][9] = 9;
drawGameboard();
getch();
return 0;
}
void drawGameboard()
{
int row, col;
for (row = 0; row < 10; row++) {
for (col = 0; col < 10; col++) {
cout << gameboard[row][col] << " " ;
}
cout << endl;
}
cout << endl;
}
Hoped This helps
Shredded
Dec 14, 2010 at 8:06am UTC
I don't mean to be rude, but this is covered in the tutorial, and its very basic. I HIGHLY suggest reading the tutorial from start to finish:
http://cplusplus.com/doc/tutorial/
Topic archived. No new replies allowed.