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
| public class Board extends JPanel {
private static final long serialVersionUID = 1L;
public static boolean Selected = false;
public static Pion PionClicked = null;
public static Case CaseClicked = null;
private Pion[][] game = new Pion[8][8];
GridLayout gl = new GridLayout(8, 8);
public Board() {
createGame();
refreshLayout();
}
//Créer le tableau en 2D pour avoir l'emplacement de chaque Pion au début de la game.
private void createGame() {
Deck deckEnemy = new Deck();
Deck deck = new Deck();
for (int c = 0; c < 16; c++) {
deckEnemy.array.get(c).nom += "e";
if (c < 8) {
game[0][c] = deckEnemy.array.get(c);
} else {
game[1][c - 8] = deckEnemy.array.get(c);
}
}
int i = 0;
for (int r = 2; r < 6; r++) {
for (int c = 0; c < 8; c++) {
game[r][c] = new Case("case" + i, true);
i++;
}
}
for (int c = 15; c > -1; c--) {
if (c > 7) {
game[6][c - 8] = deck.array.get(c);
} else {
game[7][c] = deck.array.get(c);
}
}
}
//Rafraichir le Layout pour mettre les Pion à la bonne position
private void refreshLayout() {
setLayout(new GridLayout(8, 8));
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
add("Case", game[r][c]);
}
}
revalidate();
}
//Lorsqu'un Pion est cliqué et une Case vide par la suite, ils changent de place
public void deplacement() {
int posP1 = 0, posP2 = 0, posC1 = 0, posC2 = 0;
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 8; c++) {
if (game[r][c].nom.equals(PionClicked.nom)) {
posP1 = r;
posP2 = c;
} else if (game[r][c].nom.equals(CaseClicked.nom)) {
posC1 = r;
posC2 = c;
}
}
}
game[posP1][posP2] = CaseClicked;
game[posC1][posC2] = PionClicked;
PionClicked.deselectionner();
CaseClicked = null;
setLayout(null);
game[posP1][posP2].revalidate();
game[posP1][posP2].repaint();
game[posC1][posC2].revalidate();
game[posC1][posC2].repaint();
refreshLayout();
}
//Créer le background
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r = 0; r < 8; r++) {
for (int c = 0; c < 4; c++) {
if (r % 2 == 0) {
g.fillRect(c * 200, r * 100, 100, 100);
} else {
g.fillRect(c * 200 + 100, r * 100, 100, 100);
}
}
}
}
} |
Partager