Tuesday, March 27, 2012

CIS 217: Array Assignment [Tic-Tac-Toe] Part 2

I decided to post the code for the project now that I've finished it...but forgot to upload the project to my DropBox account...so I'll type this up from memory until I can post the code tonight after my other classes.

// Creates a 3x3 array.
int boardArray[3, 3]

Which makes an array that looks like this, each dimension of the array corresponding to an "x" or "y" value (like a grid):
[1, 1] [1, 2] [1, 3]
[2, 1] [2, 2] [2, 3]
[3, 1] [3, 2] [3, 3]

And I randomly generated the 0 or 1 result (for X or O) and inserted it into the array at the current spot:
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
boardArray[x,y] = rand.Next();
}
}
So the loop will run like this:
x=1/y=1, x=1/y=2, x=1/y=2
x=2/y=1, x=2/y=2, x=2/y=3
x=3/y=1, x=3/y=2, x=3/y=1

perfectly corresponding to the array.

I goofed on the loop that displayed the results, though, so the board was either all X's or all O's, based on the value stored in boardArray[3, 3]. Oops!

No comments:

Post a Comment