Last active
December 27, 2015 13:39
-
-
Save moomoohk/7335373 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package gridCheck; | |
import java.util.Scanner; | |
public class Grid | |
{ | |
private boolean[][] grid; | |
public Grid(int row, int col) | |
{ | |
grid = new boolean[row][col]; | |
for (int r = 0; r < grid.length; r++) | |
for (int c = 0; c < grid[0].length; c++) | |
grid[r][c] = false; //false = empty | |
} | |
public Coordinates findValidPos(int itemW, int itemH) | |
{ | |
//grid.length height | |
//grid[0].length width | |
if (itemW > grid[0].length || itemH > grid.length) | |
return null; | |
for (int r = 0; r < grid.length; r++) | |
for (int c = 0; c < grid[0].length; c++) | |
{ | |
Coordinates pos = new Coordinates(c, r); | |
if (grid[r][c] || pos.x + itemW > grid[0].length || pos.y + itemH > grid.length) | |
continue; | |
boolean valid = true; | |
for (int checkWidth = pos.x; checkWidth < pos.x + itemW; checkWidth++) | |
if (grid[r][checkWidth]) | |
valid = false; | |
if (!valid) | |
continue; | |
for (int checkHeight = pos.y; checkHeight < pos.y + itemH; checkHeight++) | |
if (grid[checkHeight][c]) | |
valid = false; | |
if (valid) | |
return pos; | |
} | |
return null; | |
} | |
public void addItem(int itemW, int itemH, int x, int y) | |
{ | |
for (int r = y; r < y + itemH; r++) | |
for (int c = x; c < x + itemW; c++) | |
grid[r][c] = true; | |
} | |
public String toString() | |
{ | |
String st = ""; | |
for (int r = 0; r < grid.length; r++) | |
{ | |
for (int c = 0; c < grid[0].length; c++) | |
st += "[" + (grid[r][c] ? "x" : " ") + "]"; | |
st += "\n"; | |
} | |
return st; | |
} | |
public static class Coordinates | |
{ | |
public int x, y; | |
public Coordinates(int x, int y) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
public String toString() | |
{ | |
return "(" + x + ", " + y + ")"; | |
} | |
} | |
public static void main(String[] args) | |
{ | |
System.out.println("Enter grid width:"); | |
int gridW = new Scanner(System.in).nextInt(); | |
System.out.println("Enter grid height:"); | |
int gridH = new Scanner(System.in).nextInt(); | |
Grid g = new Grid(gridH, gridW); | |
while (true) | |
{ | |
System.out.println(g); | |
System.out.println("Enter item width:"); | |
int itemW = new Scanner(System.in).nextInt(); | |
System.out.println("Enter item height:"); | |
int itemH = new Scanner(System.in).nextInt(); | |
Coordinates validPos = g.findValidPos(itemW, itemH); | |
if (validPos != null) | |
{ | |
System.out.println("Found a valid position at " + validPos + ". Would you like to add the item? (true/false)"); | |
if (new Scanner(System.in).nextBoolean()) | |
{ | |
g.addItem(itemW, itemH, validPos.x, validPos.y); | |
System.out.println("Added."); | |
} | |
else | |
System.out.println("Not added."); | |
} | |
else | |
System.out.println("No space found."); | |
System.out.println("--"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment