Message Board
| Message Board > Others > c# help with arrays | 
| June 9, 2013, 19:46 | |
| 
                	
                                        	DoctorN	
                                         Whiskered 91 posts  | 
        		
                	In my c++ game I have this Code:  #define lvlWidth 20  #define lvlHeight 15 int level[lvlHeight][lvlWidth] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1}, {1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} }; In my c# level editor I have this Code: const int lvlWidth = 20;  const int lvlHeight = 15; int[,] level = new int[ lvlWidth, lvlHeight ]; What on earth do I do from here so that I can bring the row/columns thing into c#? Thanks ____________  | 
            
| # | |
| June 10, 2013, 06:36 | |
| 
                	
                                        	Zomg	
                                         None 641 posts  | 
        		
                	Here's how to do it (100% working code): C code: using System;  using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Test { /** This code takes a text file (.txt). The text file gets *read line by line, by the ReadIt() method (specifically in *the while() loop. *When LoopArr() is called, each line is split into multiple *values (forming effectively an array of values per line), *and these values are then fed into each slot of the *levels[,] array. For debugging purposes, a couple of *Console.Write() statements were added. *Zomg */ public partial class Form1 : Form { public Form1() { InitializeComponent(); ReadIt(); } int[,] level = null; private void ReadIt() { const int lvlWidth = 20; const int lvlHeight = 15; level = new int[lvlWidth, lvlHeight]; int counter = 0; string line; System.IO.StreamReader file = new System.IO.StreamReader("test.txt"); while ((line = file.ReadLine()) != null) { LoopArr(lvlHeight, lvlWidth, line,counter); counter++; } for (int x = 0; x < lvlHeight; x++) { Console.Write("\n"); for (int y = 0; y < lvlWidth; y++) { Console.Write(level[y,x]); } } file.Close(); // Suspend the screen. Console.ReadLine(); } private void LoopArr(int lvlHeight, int lvlWidth, string line, int linenumber) { string[] theLine = line.Split(','); for (int column = 0; column < lvlWidth; column++) { level[column, linenumber] = Convert.ToInt32(theLine[column]); } } private void Form1_Load(object sender, EventArgs e) { } } } input.txt: Code: 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 Output (in the Output window) is the same. Notes: Place test.txt in \bin\debug in your C# project. You may download the C# project I made (with the help of a fellow programmer. We spent an hour on this): https://dl.dropboxusercontent. … 8/TestWorks.zip References: http://msdn.microsoft.com/en-us/library/aa287535(v=vs.71).aspx http://www.cplusplus.com/forum/beginner/42045/ Regards, Zomg [Edited on June 10, 2013 by Zomg] ____________  | 
            
| # | |
| June 10, 2013, 10:10 | |
| 
                	
                                        	Zomg	
                                         None 641 posts  | 
        		
                	 [Edited on June 10, 2013 by Zomg] ____________  | 
            
| # | |
| June 10, 2013, 21:38 | |
| 
                	
                                        	DoctorN	
                                         Whiskered 91 posts  | 
        		
                	thank you so much                                    
                     ____________  | 
            
| # | |
| June 10, 2013, 22:20 | |
| 
                	
                                        	Zomg	
                                         None 641 posts  | 
        		
                	Welcomeness. =)                                    
                     ____________  | 
            
| # | |
| July 5, 2013, 18:08 | |
| 
                	
                                        	DoctorN	
                                         Whiskered 91 posts  | 
        		
                	How would I make it read something like this? Quote: 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01  01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 01 01 01 01 00 00 00 00 00 01 01 01 01 01 00 00 01 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 00 00 01 00 00 00 01 01 01 00 00 00 00 00 00 01 00 00 01 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 thats what my game uses now. And I can add 0's to each thing and it will still count that spot as either 1 or 0. thanks ____________  | 
            
| # | |
| July 5, 2013, 18:14 | |
| 
                	
                                        	Zomg	
                                         None 641 posts  | 
        		
                	I don't have the time for this.. sadly. Studying for my re-exams.                                    
                     ____________  | 
            
| # | |
| July 7, 2013, 06:57 | |
| 
                	
                                        	DoctorN	
                                         Whiskered 91 posts  | 
        		
                	Thats okay  ____________  | 
            
| # | |
Message Board > Others > c# help with arrays