patternjavaMinor
Tiling an nxn grid with L-shaped Trominoes
Viewed 0 times
gridtilingwithshapedtrominoesnxn
Problem
This is some code I wrote in order to see if it was possible to tile a 9x9 grid using trominoes that are shaped like this:
_
| |_
|___|
(They can be rotated.)
One issue I had while writing this is that I had to fix too many small errors (like having a 0 instead of a -1); is there some way to reduce the number of errors?
Here is the code:
```
public class LTiling {
public static void main(String[] args) {
int size = 9;
int[][] board = new int[size][size];
Piece[] pieces = new Piece[size*size/3+1];
for(int i = 0; i = size) {
minrow++;
mincol = 0;
}
while(board[minrow][mincol] != -1) {
if(mincol>=size-1) {
mincol = 0;
minrow++;
if(minrow == size) {
print(board);
return;
}
} else {
mincol++;
}
}
//print(board);
//System.out.println();
//System.out.println();
//System.out.println("");
} else {
pieces[piecenum].state++;
}
}
if(pieces[piecenum].state == 1) {
if(board[minrow][mincol+1] == -1 && board[minrow+1][mincol] == -1) {
board[minrow][mincol+1] = piecenum;
board[minrow+1][mincol] = piecenum;
mincol += 2;
if(mincol >= size) {
minrow++;
mincol = 0;
}
while(board[minrow][mincol] != -1) {
if(mincol>=size-1) {
mincol = 0;
minrow++;
_
| |_
|___|
(They can be rotated.)
One issue I had while writing this is that I had to fix too many small errors (like having a 0 instead of a -1); is there some way to reduce the number of errors?
Here is the code:
```
public class LTiling {
public static void main(String[] args) {
int size = 9;
int[][] board = new int[size][size];
Piece[] pieces = new Piece[size*size/3+1];
for(int i = 0; i = size) {
minrow++;
mincol = 0;
}
while(board[minrow][mincol] != -1) {
if(mincol>=size-1) {
mincol = 0;
minrow++;
if(minrow == size) {
print(board);
return;
}
} else {
mincol++;
}
}
//print(board);
//System.out.println();
//System.out.println();
//System.out.println("");
} else {
pieces[piecenum].state++;
}
}
if(pieces[piecenum].state == 1) {
if(board[minrow][mincol+1] == -1 && board[minrow+1][mincol] == -1) {
board[minrow][mincol+1] = piecenum;
board[minrow+1][mincol] = piecenum;
mincol += 2;
if(mincol >= size) {
minrow++;
mincol = 0;
}
while(board[minrow][mincol] != -1) {
if(mincol>=size-1) {
mincol = 0;
minrow++;
Solution
This is not an extensive list, but there are several ways your naming could improved that I can see:
-
The parameters for your Piece constructor don't have to be shorted to their initials if you use the
What
-
You use camel case your method names (e.g. findMinRow) but not for your variable names (e.g. lowerleftrow). Apart from the fact that the most widely used naming convention for Java is CamelCase, it's good to have a consistant naming style.
-
I hope this helps!
-
The parameters for your Piece constructor don't have to be shorted to their initials if you use the
this keyword. i.e.public Piece(int piecenum, int state, int lowerleftrow, int lowerleftcol) {
this.state = state;
this.piecenum = piecenum;
this.lowerleftrow = lowerleftrow;
this.lowerleftcol = lowerleftcol;
}What
this does is reference the current object so that the fields of Piece are referenced instead of the arguments.-
You use camel case your method names (e.g. findMinRow) but not for your variable names (e.g. lowerleftrow). Apart from the fact that the most widely used naming convention for Java is CamelCase, it's good to have a consistant naming style.
-
flag should be renamed to something that better explains what it is a flag for. On its own flag is not meaningful enough for anyone else to easily understand what it will be used for without having to first read through our code and look for places where it is used.I hope this helps!
Code Snippets
public Piece(int piecenum, int state, int lowerleftrow, int lowerleftcol) {
this.state = state;
this.piecenum = piecenum;
this.lowerleftrow = lowerleftrow;
this.lowerleftcol = lowerleftcol;
}Context
StackExchange Code Review Q#147403, answer score: 3
Revisions (0)
No revisions yet.