Created
March 23, 2020 17:36
-
-
Save battis/ffe73913d8af535bfec9866a7f9b1919 to your computer and use it in GitHub Desktop.
Rethinking Levels as a doubly-indexed list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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