Skip to content

Instantly share code, notes, and snippets.

@battis
Created March 23, 2020 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save battis/ffe73913d8af535bfec9866a7f9b1919 to your computer and use it in GitHub Desktop.
Save battis/ffe73913d8af535bfec9866a7f9b1919 to your computer and use it in GitHub Desktop.
Rethinking Levels as a doubly-indexed list
import java.util.ArrayList;
import java.util.List;
public class DoublyIndexedList {
private Sprite[][] level;
/* ... */
public void set(int x, int y, Sprite sprite) {
level[x][y] = sprite;
}
public Sprite get(int x, int y) {
return level[x][y];
}
public List<Sprite> getColumn(int column) {
List<Sprite> result = new ArrayList<>();
for (Sprite s : level[column]) {
if (s != null) {
result.add(s);
}
}
return result;
}
public List<Sprite> getRow(int row) {
List<Sprite> result = new ArrayList<>();
for (Sprite[] column : level) {
if (column[row] != null) {
result.add(column[row]);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment