import jlab.graphics.DWindow;
public class Card {
public static final int WIDTH = 60, HEIGHT = 60;
private int number; // หมายเลขของบัตรใบนี้
private int row, col; // ตำแหน่ง (หมายเลขแถว หมายเลขสดมภ์)
private boolean matched; // จับคู่ไปแล้วหรือยัง
private DWindow window; // วินโดว์ที่แสดงบัตรใบนี้
public Card(DWindow w, int r, int c, int n) {
window = w;
row = r; col = c;
number = n;
}
public int number() { // คืนหมายเลขของบัตรใบนี้
return number;
}
public void show() {
draw(DWindow.WHITE, true); // บัตรสีขาว แสดงหมายเลขของบัตร
}
public void hide() {
draw(DWindow.GRAY, false); // บัตรสีเทา ไม่แสดงหมายเลข
}
public void matched() {
matched = true; // บอกให้รู้ว่าบัตรใบนี้ถูกจับคู่แล้ว
draw(DWindow.PINK, true); // แสดงหมายเลข บนบัตรสีชมพู
}
public boolean isMatched() { // คืนสภาวะว่า บัตรใบนี้จับคู่แล้วหรือยัง
return matched;
}
private void draw(int color, boolean showNumber) {
int x = col * WIDTH, y = row * HEIGHT;
window.fillRect(color, x, y, WIDTH, HEIGHT);
window.drawRect(DWindow.BLACK, x, y, WIDTH, HEIGHT);
if (showNumber) window.drawString(""+number, 30, x+5, y+5);
}
}
|