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
|
public class Grid extends JPanel {
public Player[][] state;// le type Player est une enumeration (EMPTY, DOT, CROSS). Le tableau state est la représentation du plateau de jeu (3x3)
int nbTurn = 0;
public Grid() {
setBackground(Color.BLACK);
setPreferredSize(new Dimension(300, 300));
state = new Player[3][3];// toutes les cases de state sont mises a Player.EMPTY.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
state[i][j] = Player.EMPTY;
}
}
}
public void drawGrid(Graphics g) { // dessine le plateau de jeu (#)
g.setColor(Color.WHITE);
g.fillRect(95, 0, 10, 300);
g.fillRect(195, 0, 10, 300);
g.fillRect(0, 95, 300, 10);
g.fillRect(0, 195, 300, 10);
}
public void drawDot(Graphics g, int i, int j) {// la grille de jeu et représentée par un tableau 3x3. drawDot() trace un point en position (i,j).
if (state[i][j].equals(Player.DOT)) {
g.setColor(Color.WHITE);
int xShift=j * 100;
int yShift=i*100;
g.fillOval( xShift+ 5, yShift + 5, 85, 85);
}
}
public void drawCross(Graphics g, int i, int j) {//trace une croix en position (i,j).
if (state[i][j].equals(Player.CROSS)) {
g.setColor(Color.WHITE);
int xShift = j * 100;
int yShift = i * 100;
int[] xPoints = { 20 + xShift, 15 + xShift, 85 + xShift, 90 + xShift, 20 + xShift };
int[] yPoints = { 15 + yShift, 20 + yShift, 90 + yShift, 85 + yShift, 15 + yShift };
g.fillPolygon(xPoints, yPoints, 5);
int[] sPoints = { 15 + xShift, 20 + xShift, 90 + xShift, 85 + xShift, 15 + xShift };
int[] tPoints = { 85 + yShift, 90 + yShift, 20 + yShift, 15 + yShift, 85 + yShift };
g.fillPolygon(sPoints, tPoints, 5);
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawGrid(g);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
drawDot(g, i, j);
drawCross(g, i, j);
}
}
}
public int eval() { // teste à chaque tour si il y a un gagant; retourne 1 si le joueur gagne, -1 si l'ordi gagne, 0 sinon.
if (nbTurn < 5) {
return 0;
}
for (int i = 0; i < 3; i++) {
if (state[i][0].equals(state[i][1]) && state[i][0].equals(state[i][2]) && state[i][0].equals(Player.DOT)) {
return -1;
}
if (state[i][0].equals(state[i][1]) && state[i][0].equals(state[i][2]) && state[i][0].equals(Player.CROSS)) {
return 1;
}
}
for (int j = 0; j < 3; j++) {
if (state[0][j].equals(state[1][j]) && state[0][j].equals(state[2][j]) && state[0][j].equals(Player.DOT)) {
return -1;
}
if (state[0][j].equals(state[1][j]) && state[0][j].equals(state[2][j]) && state[0][j].equals(Player.CROSS)) {
return 1;
}
}
if (state[0][0].equals(state[2][2]) && state[0][0].equals(state[1][1]) && state[0][0].equals(Player.DOT)) {
return -1;
}
if (state[0][0].equals(state[2][2]) && state[0][0].equals(state[1][1]) && state[0][0].equals(Player.CROSS)) {
return 1;
}
if (state[0][2].equals(state[2][0]) && state[0][2].equals(state[1][1]) && state[0][2].equals(Player.DOT)) {
return -1;
}
if (state[0][2].equals(state[2][0]) && state[0][2].equals(state[1][1]) && state[0][2].equals(Player.CROSS)) {
return 1;
}
return 0;
}
public void printG() {
for (int i = 0; i < 3; i++) {
System.out.println(state[i][0].toString() + state[i][1].toString() + state[i][2].toString());
}
}
public Grid makeCopy() {
Grid copyOf = new Grid();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
copyOf.state[i][j] = state[i][j];
}
copyOf.nbTurn = nbTurn;
}
return copyOf;
}
} |
Partager