3

I'm struggling to think of the most elegant/simple way of doing this. Perhaps I'm overthinking it a little.

Lets say I've got a 5x5 array of integers that looks like this:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

And another 2x2 array of integers that looks like this:

5 1 2 3 

I want to pick a location in the 5x5 array, say [2][2], and place the values from the second array into the first, so it looks like this:

0 0 0 0 0 0 0 0 0 0 0 0 5 1 0 0 0 2 3 0 0 0 0 0 0 

My initial thought was to use a for loop after determining the number of rows/columns in the array to be copied, but I can't seem to puzzle out a way in my head to do that this morning.

Any suggestions?

Edit:

Sorry, here's the way I'm doing it currently. Just wondering if there's a better way.

This is actually part of a unity thing I'm doing - "RoomDoors" is our smaller array, and "map" is the larger array it's being moved into. It's part of a random map generator that needs to know which "edges" of tiles in rooms have doors on them that can connect to other rooms. RoomDoors stores 4 booleans, one for each direction telling me if there's a door there.

 roomDoors = previousRoom.GetComponent<RoomDataInterface> ().rooms; //2d array of room/door arrangement in new room sizeCol = roomDoors.GetLength (0); sizeRow = roomDoors.GetLength (1); map [10, 10] = roomDoors [0, 0]; // top left of the room goes in the spot for (int i = 0; i < sizeCol; i ++){ for (int j = 0; j < sizeRow; j ++) { map [i + 10,j + 10] = roomDoors[i,j]; } 
4
  • 7
    Welcome to Stack Overflow. Sorry but this is not a good way to ask a question. Did you try anything so far to solve your problem? Show your effort first so people might show theirs. Please read FAQ, How to Ask and help center as a start.. Commented Jan 22, 2015 at 12:42
  • What about two nested for loops, one for the columns and one for the rows? Some index checking can also be helpful. Commented Jan 22, 2015 at 12:45
  • 1
    Sorry, I've edited my current code into the post. Commented Jan 22, 2015 at 12:55
  • The question asks for beautification of working code, which makes it also suitable for codereview.stackexchange.com Commented Jan 22, 2015 at 13:15

2 Answers 2

2

I think you can't do much better, just remove the unnecessary assignment:

roomDoors = previousRoom.GetComponent<RoomDataInterface> ().rooms; sizeCol = roomDoors.GetLength (0); sizeRow = roomDoors.GetLength (1); for (int i = 0; i < sizeCol; i ++) for (int j = 0; j < sizeRow; j ++) map [i + 10, j + 10] = roomDoors[i, j]; 
Sign up to request clarification or add additional context in comments.

Comments

1

The line

map [10, 10] = roomDoors [0, 0]; 

is redundant, as the same assignment will be performed by the first iteration of the loop. Removal of the line will result in a solution that is smaller and more efficient.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.