/
Piece.java
74 lines (74 loc) · 1.98 KB
/
Piece.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
public class Piece {
// constants for the starting position for each piece
private final Point[][] pieces = {{mp(0, 4), mp(0, 5), mp(1, 4), mp(1, 5)},
{mp(1, 3), mp(1, 4), mp(1, 5), mp(1, 6)},
{mp(0, 3), mp(1, 3), mp(1, 4), mp(1, 5)},
{mp(0, 5), mp(1, 3), mp(1, 4), mp(1, 5)},
{mp(1, 3), mp(1, 4), mp(0, 4), mp(0, 5)},
{mp(0, 3), mp(0, 4), mp(1, 4), mp(1, 5)},
{mp(1, 3), mp(1, 4), mp(1, 5), mp(0, 4)}};
// auxiliary method to create a new Point object
private Point mp (int x, int y) {
return new Point(x, y);
}
@Deprecated
// generates a random piece -- not true to the mechanics behind tetris
public Active getActive () {
int id = (int)(Math.random()*7);
Point[] newPiece = new Point[4];
for (int i = 0; i < 4; i++)
newPiece[i] = new Point(pieces[id][i].r, pieces[id][i].c);
return new Active(newPiece, id+1);
}
// returns a piece with a specific id
public Active getActive (int id) {
Point[] newPiece = new Point[4];
for (int i = 0; i < 4; i++)
newPiece[i] = new Point(pieces[id][i].r, pieces[id][i].c);
return new Active(newPiece, id+1);
}
// generates a permutation of the seven pieces and returns it
public int[] getPermutation () {
int[] res = new int[7];
for (int i = 0; i < 7; i++)
res[i] = i;
permute(0, res);
return res;
}
// auxiliary function to permute the pieces
private void permute (int i, int[] a) {
if (i == 6)
return;
int swap = (int)(Math.random()*(6-i) + i + 1);
int temp = a[i];
a[i] = a[swap];
a[swap] = temp;
permute(i+1, a);
}
// represents the active piece
static class Active {
Point[] pos;
int id;
int lor, hir, loc, hic;
int state = 0;
Active (Point[] pos, int id) {
this.pos = pos;
this.id = id;
if (id != 2) {
lor = 0; hir = 2;
loc = 3; hic = 5;
} else {
lor = 0; hir = 3;
loc = 3; hic = 6;
}
}
}
// represents a point on the grid
static class Point {
int r, c;
Point (int r, int c) {
this.r = r;
this.c = c;
}
}
}