import java.awt.*; import javax.swing.*; import java.util.*; import java.awt.event.*; public class Controleur extends JFrame implements KeyListener { private Case[][] tableauCase; private VuePlateau plateau; private int xHeros; private int yHeros; private int points; public Controleur() { this.xHeros = 1; this.yHeros = 1; this.points = 0; this.tableauCase = new Case[3][3]; VueCase[][] tabCase = new VueCase[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (i == this.xHeros && j == this.yHeros) { this.tableauCase[i][j] = new Heros(i, j); tabCase[i][j] = new VueCase(tableauCase[i][j]); tabCase[i][j].setLocation(i * 200, j * 200); tabCase[i][j].setSize(200, 200); this.add(tabCase[i][j]); } else { Case c = new Case(i, j, "temp"); this.tableauCase[i][j] = c.newRandomCase(i, j); tabCase[i][j] = new VueCase(tableauCase[i][j]); tabCase[i][j].setLocation(i * 200, j * 200); tabCase[i][j].setSize(200, 200); this.add(tabCase[i][j]); } } } this.plateau = new VuePlateau(tabCase); this.add(this.plateau); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } this.addKeyListener(this); } private void rencontre(int xNew, int yNew) { int tempX = xHeros; int tempY = yHeros; int tempZ; if (!this.tableauCase[xHeros][yHeros].rencontrer(this.tableauCase[xNew][yNew])) { if (this.tableauCase[xHeros][yHeros].getArme().getValue() > 0) { if (this.tableauCase[xHeros][yHeros].getArme().getValue() > this.tableauCase[xNew][yNew].getPv()) { tempZ=this.tableauCase[xNew][yNew].getPv(); this.tableauCase[xHeros][yHeros].setArme(this.tableauCase[xHeros][yHeros].getArme().getValue()-tempZ); this.tableauCase[xNew][yNew] = new Or(this.tableauCase[xNew][yNew].getPv() ,xNew ,yNew ); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } } else { this.tableauCase[xNew][yNew].setPv(this.tableauCase[xNew][yNew].getPv() - this.tableauCase[xHeros][yHeros].getArme().getValue()); this.tableauCase[xHeros][yHeros].setArme(0); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } } } } else { if (this.tableauCase[xNew][yNew].type.equals("or")) { this.points += this.tableauCase[xNew][yNew].getValue(); this.plateau.upPoint(this.tableauCase[xNew][yNew].getValue()); this.tableauCase[xNew][yNew] = this.tableauCase[xHeros][yHeros]; this.tableauCase[xHeros][yHeros] = new Case(xHeros, yHeros, "temp").newRandomCase(xHeros, yHeros); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } xHeros = xNew; yHeros = yNew; } if (this.tableauCase[xNew][yNew].type.equals("arme")) { if (this.tableauCase[xHeros][yHeros].getArme().getValue() < this.tableauCase[xNew][yNew].getValue()) { this.tableauCase[xHeros][yHeros].setArme(this.tableauCase[xNew][yNew].getValue()); this.points += this.tableauCase[xNew][yNew].getValue(); this.plateau.upPoint(this.tableauCase[xNew][yNew].getValue()); this.tableauCase[xNew][yNew] = this.tableauCase[xHeros][yHeros]; this.tableauCase[xHeros][yHeros] = new Case(xHeros, yHeros, "temp").newRandomCase(xHeros, yHeros); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } xHeros = xNew; yHeros = yNew; } else { this.points += this.tableauCase[xNew][yNew].getValue(); this.plateau.upPoint(this.tableauCase[xNew][yNew].getValue()); this.tableauCase[xNew][yNew] = this.tableauCase[xHeros][yHeros]; this.tableauCase[xHeros][yHeros] = new Case(xHeros, yHeros, "temp").newRandomCase(xHeros, yHeros); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } xHeros = xNew; yHeros = yNew; } } if (this.tableauCase[xNew][yNew].type.equals("potion")) { this.points += this.tableauCase[xNew][yNew].getValue(); this.plateau.upPoint(this.tableauCase[xNew][yNew].getValue()); this.tableauCase[xNew][yNew] = this.tableauCase[xHeros][yHeros]; this.tableauCase[xHeros][yHeros] = new Case(xHeros, yHeros, "temp").newRandomCase(xHeros, yHeros); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } xHeros = xNew; yHeros = yNew; } if (this.tableauCase[xNew][yNew].type.equals("monstre")) { this.tableauCase[xHeros][yHeros].setPv(this.tableauCase[xHeros][yHeros].getPv()-this.tableauCase[xNew][yNew].getPv()); if (this.tableauCase[xHeros][yHeros].getPv() <= 0) { System.out.println("GameOver, votre score final est de : " + this.points); this.dispose(); } else { this.points += this.tableauCase[xNew][yNew].getValue(); this.plateau.upPoint(this.tableauCase[xNew][yNew].getValue()); this.tableauCase[xNew][yNew] = this.tableauCase[xHeros][yHeros]; this.tableauCase[xHeros][yHeros] = new Case(xHeros, yHeros, "temp").newRandomCase(xHeros, yHeros); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } xHeros = xNew; yHeros = yNew; } } } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { int tempX = xHeros; int tempY = yHeros; if (e.getKeyCode() == KeyEvent.VK_DOWN) { tempX = xHeros; tempY = yHeros; if (tempY + 1 > 2) { System.out.println("Impossible d'aller plus bas."); } else { this.rencontre(tempX, tempY + 1); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } } } if (e.getKeyCode() == KeyEvent.VK_UP) { tempX = xHeros; tempY = yHeros; if (tempY - 1 < 0) { System.out.println("Impossible d'aller plus haut."); } else { this.rencontre(tempX, tempY - 1); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } } } if (e.getKeyCode() == KeyEvent.VK_LEFT) { tempX = xHeros; tempY = yHeros; if (tempX - 1 < 0) { System.out.println("Impossible d'aller plus à gauche."); } else { this.rencontre(tempX - 1, tempY); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { this.plateau.update(this.tableauCase[i][j], i, j); } } } } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { tempX = xHeros; tempY = yHeros; if (tempX + 1 > 2) { System.out.println("Impossible d'aller plus à droite."); } else { this.rencontre(tempX + 1, tempY); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { plateau.update(this.tableauCase[i][j], i, j); } } } } } }