Convert 1D array to 2D array C#

Started by
10 comments, last by FantasyVII 11 years, 7 months ago
Feel free to play around with this and learn how it works but this is a very basic example of writing and reading your map data from a file written in binary, I also wouldn't call myself an amazing programmer but this works.


public class MapLocation
{
public int TileNumber = 0;
public bool Blocked = false; //if the tile itself is blocked
}
public class MapManager
{
MapLocation[,] MapData = new MapLocation[100, 100]; //create a 2d Array with our map class

// ////////////////////////////////////////////////////////////////////////////////////////////////////////
public int GetTileAtLocation(int X, int Y)
{
//check bounds
if (X < 0 || X > 100 || Y < 0 || Y > 100)
{
return 0;
}
else //its in bounds of our array
{
return MapData[X, Y].TileNumber;
}
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
public void SaveMap()
{
using (FileStream stream = new FileStream("Map.dat", FileMode.Create)) //create our file
{
using (BinaryWriter writer1 = new BinaryWriter(stream)) //create writer
{
//write header
writer1.Write(1000); //File Version increment this anytime you change data below

for (int x = 0; x < 100; x++) //loop through all of our tiles
{
for (int y = 0; y < 100; y++)
{
writer1.Write(MapData[x,y].TileNumber); //write tile number
writer1.Write(MapData[x,y].Blocked); //write blocked flag
}
}
}
}
}//End SaveMap()
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
public void LoadMap()
{
if (!File.Exists("Map.dat"))
{
//file does not exist return
Console.Write("File Does not Exist\n");
return;
}

using (FileStream stream = new FileStream("Map.dat", FileMode.Open))
{
using (BinaryReader reader = new BinaryReader(stream))
{///------------------------------------------------------------------------------------------------------------------------
//Read Header
int ThisFileVersion = reader.ReadInt32(); //pull our version from the file and make sure it matches what we think we are reading
//Check our file version
if (ThisFileVersion != 1000)
{
Console.Write("Exp File Version Mismatch\n");
return;
}
for (int x = 0; x < 100; x++) //loop through all of our tiles
{
for (int y = 0; y < 100; y++)
{
MapData[x,y].TileNumber = reader.ReadInt32();
MapData[x,y].Blocked = reader.ReadBoolean();
}
}
}
}
}//end LoadMap()

}//end MapManager
Advertisement

Feel free to play around with this and learn how it works but this is a very basic example of writing and reading your map data from a file written in binary, I also wouldn't call myself an amazing programmer but this works.


public class MapLocation
{
public int TileNumber = 0;
public bool Blocked = false; //if the tile itself is blocked
}
public class MapManager
{
MapLocation[,] MapData = new MapLocation[100, 100]; //create a 2d Array with our map class

// ////////////////////////////////////////////////////////////////////////////////////////////////////////
public int GetTileAtLocation(int X, int Y)
{
//check bounds
if (X < 0 || X > 100 || Y < 0 || Y > 100)
{
return 0;
}
else //its in bounds of our array
{
return MapData[X, Y].TileNumber;
}
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
public void SaveMap()
{
using (FileStream stream = new FileStream("Map.dat", FileMode.Create)) //create our file
{
using (BinaryWriter writer1 = new BinaryWriter(stream)) //create writer
{
//write header
writer1.Write(1000); //File Version increment this anytime you change data below

for (int x = 0; x < 100; x++) //loop through all of our tiles
{
for (int y = 0; y < 100; y++)
{
writer1.Write(MapData[x,y].TileNumber); //write tile number
writer1.Write(MapData[x,y].Blocked); //write blocked flag
}
}
}
}
}//End SaveMap()
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
public void LoadMap()
{
if (!File.Exists("Map.dat"))
{
//file does not exist return
Console.Write("File Does not Exist\n");
return;
}

using (FileStream stream = new FileStream("Map.dat", FileMode.Open))
{
using (BinaryReader reader = new BinaryReader(stream))
{///------------------------------------------------------------------------------------------------------------------------
//Read Header
int ThisFileVersion = reader.ReadInt32(); //pull our version from the file and make sure it matches what we think we are reading
//Check our file version
if (ThisFileVersion != 1000)
{
Console.Write("Exp File Version Mismatch\n");
return;
}
for (int x = 0; x < 100; x++) //loop through all of our tiles
{
for (int y = 0; y < 100; y++)
{
MapData[x,y].TileNumber = reader.ReadInt32();
MapData[x,y].Blocked = reader.ReadBoolean();
}
}
}
}
}//end LoadMap()

}//end MapManager



I assume the save method will produce this file in binary:

0false 1false 2true 3false 2true
etc...

right?

anyways your code looks really good and believe it or not I learned 2 thing by looking at your good.

1- what and how and when to use "using" statement.
2- garbage collection. biggrin.png

The reason I want my Map File to look like this is because it easier to place tiles according to the file:-


1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,2,1,1,1,1,1,
1,1,1,1,1,2,1,1,1,1,1,
1,1,1,1,1,2,1,1,1,1,1,
1,1,1,1,1,2,2,2,2,2,2,
1,1,1,1,1,1,1,1,1,1,2,
1,1,1,1,1,1,1,1,1,1,2,
1,1,1,1,1,1,1,1,1,1,2,
2,2,2,2,2,2,2,2,2,2,2,



so I would do something like this

MapX = 100, MapY = 100;
TileX = 32, TileY = 32;

int[,] map = new int[100, 100];

GrassID = 1;
StoneID = 2;

for (int x = 0; x < MapX; x++)
{
for (int y = 0; y < MapY; y++)
{
TilePosition.X = (float)x * TileX;
TilePosition.Y = (float)y * TileY;

if (map[y, x] == this.GrassID)
{
spriteBatch.Draw(this.GrassTile, TilePosition + ScreenScroll.ScreenOffset, Color.White);
}

if (map[y, x] == this.StoneID)
{
spriteBatch.Draw(this.StoneTile, TilePosition + ScreenScroll.ScreenOffset, Color.White);
}



so if there is number 1 in the array it will take it position which lets say will be at map[1, 2]. so 1*32 = 32 and 2*32 = 64. So the tile will be placed at position(32, 64).

anyways I like your code and I'll actually use some parts of it to put in my code.

Thank you very much :)

This topic is closed to new replies.

Advertisement