-
Notifications
You must be signed in to change notification settings - Fork 0
/
PieceBag.pde
executable file
·90 lines (76 loc) · 2.23 KB
/
PieceBag.pde
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Random;
public class PieceBag {
Piece pieceBag[] = new Piece[7]; // a bag of 7 tetris pieces
//int pieceBag[] = new int[7];
int pieceBagI;
float x; // piece origin
float y; // piece origin
public PieceBag(float x, float y) {
this.x = x;
this.y = y;
initPieces();
jumble();
pieceBagI = 0;
}
void initPieces() {
pieceBag[0] = new LPiece(this.x, this.y);
pieceBag[1] = new OPiece(this.x, this.y);
pieceBag[2] = new TPiece(this.x, this.y);
pieceBag[3] = new SPiece(this.x, this.y);
pieceBag[4] = new ZPiece(this.x, this.y);
pieceBag[5] = new IPiece(this.x, this.y);
pieceBag[6] = new JPiece(this.x, this.y);
/*
for (int i=1; i<7; i++) {
pieceBag[i] = new LPiece(this.x, this.y);
}*/
}
//pieceBag[0] = new LPiece(playField[1].getX()/2, 32.0f);
//pieceBag[1] = new LPiece(playField[1].getX()/2, 32.0f);
//pieceBagI = 0;
public void jumble() {
//println("JUMBLE");
Random rng = new Random(); // i.e., java.util.Random.
int n = pieceBag.length; // The number of items left to shuffle (loop invariant).
while (n > 1)
{
int k = rng.nextInt(n); // 0 <= k < n.
n--; // n is now the last pertinent index;
Piece temp = pieceBag[n]; // swap array[n] with array[k] (does nothing if k == n).
pieceBag[n] = pieceBag[k];
pieceBag[k] = temp;
}
}
// get piece from bag
public Piece getPiece() {
// if we are on piece 7 then jumble the bag again
if (pieceBagI == pieceBag.length) {
initPieces();
jumble();
pieceBagI = 0;
}
Piece piece = pieceBag[pieceBagI];
pieceBagI++;
return piece;
}
public void print() {
System.out.println("-------------");
for (int i=0; i<pieceBag.length; i++) {
System.out.print(pieceBag[i] + " ");
}
System.out.println("\n-------------");
}
/*
public static void main(String args[]) {
PieceBag pg = new PieceBag();
pg.print();
System.out.println(pg.getPiece());
pg.print();
System.out.println(pg.getPiece());
System.out.println(pg.getPiece());
System.out.println(pg.getPiece());
System.out.println(pg.getPiece());
System.out.println(pg.getPiece());
System.out.println(pg.getPiece());
}*/
}