-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collage.java
129 lines (121 loc) · 4.45 KB
/
Collage.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package game;
import java.awt.Color;
import java.awt.Font;
/**
* Uses libraries and content from the Introduction to Computer Science book.
* Displays board interactively through rendering images with StdDraw.
* @author Kal Pandit
**/
public class Collage {
private int n;
/**
* Board collage constructor with default values.
*/
public Collage() {
n = 4;
StdDraw.setCanvasSize(600, 600);
StdDraw.setScale(0, 4);
StdDraw.enableDoubleBuffering();
}
/**
* Board collage constructor with an n x n size.
*
* @param size the number of rows and columns in the board
*/
public Collage(int size) {
n = size;
StdDraw.setScale(0, n);
StdDraw.enableDoubleBuffering();
}
/**
* Updates and refreshes the game board.
*
* @param gb the game board to reference
* @param mode whether losses are tracked
*/
public void updateBoard(Board gb, int mode) {
int[][] board = gb.getBoard();
StdDraw.clear();
if (mode == 2 && gb.isGameLost()) {
Font font = new Font("Arial", Font.BOLD, 64);
StdDraw.setPenColor(Color.BLACK);
String text = "Final score: " + gb.showScore();
StdDraw.setFont(font);
StdDraw.text((double) n / 2, (double) n / 2, text);
} else {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
replaceTile(i, j, board[i][j]);
}
}
}
StdDraw.show();
}
/**
* Replaces and draws tiles that need replacing.
*
* @param collageCol the column on collage's axis
* @param collageRow the row on collage's axis
* @param numToReplace the value of the tile
*/
public void replaceTile(int collageCol, int collageRow, int numToReplace) {
String text = Integer.toString(numToReplace);
if (numToReplace != 0) {
StdDraw.setPenRadius();
double centerX = collageRow + .5;
double centerY = ((n - 1) - collageCol) + .5;
StdDraw.setPenColor(Color.BLACK);
StdDraw.square(centerX, centerY, 0.5);
if (numToReplace == -1) {
StdDraw.setPenColor(Color.DARK_GRAY);
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.WHITE);
text = "-";
}
else if (numToReplace == 2) {
StdDraw.setPenColor(new Color(238, 228, 218));
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.BLACK);
}
else if (numToReplace == 4) {
StdDraw.setPenColor(new Color(255, 224, 200));
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.BLACK);
}
else if (numToReplace == 8) {
StdDraw.setPenColor(new Color(242, 177, 121));
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.WHITE);
}
else if (numToReplace == 16) {
StdDraw.setPenColor(new Color(245, 149, 99));
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.WHITE);
}
else if (numToReplace == 32) {
StdDraw.setPenColor(new Color(255, 160, 96));
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.WHITE);
}
else if (numToReplace == 64) {
StdDraw.setPenColor(new Color(246, 124, 96));
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.WHITE);
}
else {
StdDraw.setPenColor(new Color(237, 197, 63));
StdDraw.filledSquare(centerX, centerY, 0.5);
StdDraw.setPenColor(Color.WHITE);
}
Font font = new Font("Arial", Font.BOLD, 40);
StdDraw.setFont(font);
StdDraw.text(centerX, centerY, text);
} else {
double centerX = collageRow + .5;
double centerY = ((n - 1) - collageCol) + .5;
StdDraw.setPenColor(Color.BLACK);
StdDraw.setPenRadius(0.001);
StdDraw.square(centerX, centerY, 0.5);
}
}
}