• Welcome to Valhalla Legends Archive.
 

Question About Multi-Dimensional Arrays.

Started by Dyndrilliac, December 29, 2004, 11:59 PM

Previous topic - Next topic

Dyndrilliac

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

Example: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");
}
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?
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Mephisto

#1
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.

Dyndrilliac

#2
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
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

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.

Dyndrilliac

#4
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.
Quote from: Edsger W. DijkstraIt is practically impossible to teach good programming to students that have had a prior exposure to BASIC; as potential programmers they are mentally mutilated beyond hope of regeneration.

Kp

Quote from: Dyndrilliac on December 30, 2004, 01:47 AMNow 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.

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!