Valhalla Legends Forums Archive | C/C++ Programming | Question About Multi-Dimensional Arrays.

AuthorMessageTime
Dyndrilliac
Say I have a 2 index multi-dimensional array, like so:[code]char Grid[4][4];[/code]Can I think of this like line plotting in math (a Grid with an X/Y axis)?

Example:[code]inline void DrawBoard()
{
int x = 0;
int y = 0;
char GameBoard[3][3];

for (x = 0;x < 3;x++) {
for (y = 0;y < 3;y++) {
if (IsNull(GameBoard[x][y]) == true) {
GameBoard[x][y] = ' ';
}
}
}

printf("|-----|\n");
printf("|%s|%s|%s|\n", GameBoard[0][0], GameBoard[1][0], GameBoard[2][0]);
printf("|-----|\n");
printf("|%s|%s|%s|\n", GameBoard[0][1], GameBoard[1][1], GameBoard[2][1]);
printf("|-----|\n");
printf("|%s|%s|%s|\n", GameBoard[0][2], GameBoard[1][2], GameBoard[2][2]);
printf("|-----|\n");
}[/code]I guess my real question is if I think about them as a Grid with an X/Y axis, can I also assume that if I follow mathematical rules and always put the X axis as the first of the two indexes, will it perform the example function correctly?
December 30, 2004, 5:59 AM
Mephisto
Did you test your function?

The way I like to think of bidimensional arrays is as a chart.  Think of it as a multipalcation chart if you want, where on the left side you have numbers 0-12 for instance, and on the top 0-12 so your bidimensional array could be MultipalcationChart[12][12].  You could access 8 * 5 by doing: MultipalcationChart[7][4] or MultipalcationChart[4][7] since multipalcation arithmetic is commutaitive.  This of course would only work if in fact your bidimensional array is set to the values of a multipalcation chart which begins at 0 and goes through 12.  IIRC the ones you see in 2nd grade are 1-12.  :)

Edit: Your code will crash if you attempt to print the characters of your bidimensional array with %s as your type specifier for printf().  Change %s to %c.
December 30, 2004, 7:05 AM
Dyndrilliac
But otherwise it would work? If I changed the type specifier? I'm trying to make a Tic Tac Toe game.

I haven't tested it yet because I haven't finished everything O.o
December 30, 2004, 7:24 AM
Mephisto
I suppose your function works...It's not really doing anything useful though.  You're not initializing your array to anything, and even if you did, you're just setting it to empty characters in your loop iterations.
December 30, 2004, 7:41 AM
Dyndrilliac
Well the idea is that as players put their playing pieces 'X' or 'O' on the board, they're actually sticking them in one of the indexes which is outputted on the board. The only difference between my code and what I posted is that GameBoard is defined globally. The empty characters are to make the GameBoard look empty but keep the actual placeholders full so it keeps the proper shape and looks nice.

Now I'm lost as to how to figure out who won or not. I don't want to cycle through all possible winning combonations, either.
December 30, 2004, 7:47 AM
Kp
[quote author=Dyndrilliac link=topic=10047.msg93846#msg93846 date=1104392848]Now I'm lost as to how to figure out who won or not. I don't want to cycle through all possible winning combonations, either.[/quote]

If your program does not check all winning combinations, how do you expect it to know when someone has won?  There's only 4 distinct ways to win, and you can generalize a bit further if you're clever.
December 30, 2004, 5:20 PM

Search