Jump to content
  • Advertisement
Sign in to follow this  
Estauns

C# Array Troubles....

This topic is 4969 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

I'm having some problems following understanding C# arrays:
// Inside Render
		for(int iY = 0; iY < 3; ++iY)
			{
				Console.WriteLine(" {0} | {1} | {2} ", m_cBoard[0, iY], m_cBoard[1, iY], m_cBoard[2, iY]);
				if(iY != 2)
					Console.WriteLine("---|---|---");
			}

// In Class, outside Render

		private char[,] m_cBoard = new char[3, 3] { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };


I end up with: 147 258 369 though, instead of: 123 456 789 I don't understand why though? 0,0 would be top left to me. 1,0 would be top middle. 2,0 would be top right. Just like on a graph or whatever, but it's like it's making: 0,0 top left 0,1 top middle 0,2 top right Which is like some sort of sideways graph. Am I right? Why does it do it this way? I'm writing a Tic Tac Toe game if that changes anything.

Share this post


Link to post
Share on other sites
Advertisement
You've got the X's and Y's backwards. Here's how the program sees it:


// 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2
private char[,] m_cBoard = new char[3, 3] { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} };




Share this post


Link to post
Share on other sites
Conceptually (I don't think you can actually define arrays this way in code) it's like saying this:

m_cBoard[0, *] = {1, 2, 3};
m_cBoard[1, *] = {4, 5, 6};
m_cBoard[2, *] = {7, 8, 9};


Each level of braces gets deeper into the array's rank and the highest level rank comes first, then sub-ranks follow:

private char[,] m_cBoard = new char[,] {/*Array 0:*/ {1, 2, 3}, /*Array 1:*/ {4, 5, 6}, /*Array 2:*/ {7, 8, 9}};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!