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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
| package projet;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ColtExpress {
public static void main(String[] args){
EventQueue.invokeLater(() -> {
/** Voici le contenu qui nous intéresse. */
Train modele = new Train();
Personne b1 = new Personne(modele, "Bandito");
Personne b2 = new Personne(modele, "Zorro");
Personne b3 = new Personne(modele, "Garcia");
Personne mars = new Personne(modele, "Marshall");
CVue vue = new CVue(modele);
});
}
}
abstract class Observable {
/**
* On a une liste [observers] d'observateurs, initialement vide, à laquelle
* viennent s'inscrire les observateurs via la méthode [addObserver].
*/
private ArrayList<Observer> observers;
public Observable() {
this.observers = new ArrayList<Observer>();
}
public void addObserver(Observer o) {
observers.add(o);
}
/**
* Lorsque l'état de l'objet observé change, il est convenu d'appeler la
* méthode [notifyObservers] pour prévenir l'ensemble des observateurs
* enregistrés.
* On le fait ici concrètement en appelant la méthode [update] de chaque
* observateur.
*/
public void notifyObservers() {
for(Observer o : observers) {
o.update();
}
}
}
interface Observer {
/**
* Un observateur doit posséder une méthode [update] déclenchant la mise à
* jour.
*/
public void update();
/**
* La version officielle de Java possède des paramètres précisant le
* changement qui a eu lieu.
*/
}
enum Direction { AVANT, ARRIERE, HAUT, BAS }
class Train extends Observable{
public static final int nb_wagons = 4;
public static final int nb_actions = 4;
public static final int nb_joueurs = 3;
protected Position[][] wagons;
protected ArrayList<Personne> joueurs;
protected ArrayList<Action> liste;
public Train(){
this.wagons = new Position[2][Train.nb_wagons];
this.joueurs = new ArrayList<Personne>();
this.liste = new ArrayList<Action>();
for (int i = 0; i < 2; i++){
for(int j = 0; j < Train.nb_wagons; j++){
this.wagons[i][j] = new Position(i, j);
}
}
}
public Position getPos(int x, int y) {
return wagons[x][y];
}
}
class Position {
protected ArrayList<Personne> bandits;
protected ArrayList<Butin> but;
protected int x,y;
public Position(int x, int y) {
this.x = x;
this.y = y;
this.bandits = new ArrayList<Personne>();
this.but = new ArrayList<Butin>();
if (x == 1 && y == Train.nb_wagons - 1) {
Magot m = new Magot(this);
this.but.add(m);
}
if (x != 0){
Random rnd = new Random();
int nb = rnd.nextInt(4);
for (int i = 0; i < nb +1; i++){
double nb2 = rnd.nextDouble();
if (nb2 > 0.5){
Butin b = new Bijou(this);
this.but.add(b);
} else {
Butin b = new Bourse(this);
this.but.add(b);
}
}
}
}
}
class Personne {
protected String nom;
protected Position pos;
protected ArrayList<Butin> but;
protected int nbTir;
public static final double NERVOSITE_MARSHALL = 0.3;
public Personne(Train train, String s){
this.nom = s;
if (s == "Marshall"){
this.pos = train.wagons[1][Train.nb_wagons - 1];
train.wagons[this.pos.x][this.pos.y].bandits.add(this);
train.joueurs.add(0, this);
this.nbTir=999999;
} else {
this.pos = train.wagons[0][0];
train.wagons[this.pos.x][this.pos.y].bandits.add(this);
this.but = new ArrayList<Butin>();
train.joueurs.add(this);
this.nbTir=6;
}
}
}
class Butin {
protected int valeur;
protected String nom;
protected Position pos;
}
class Bourse extends Butin {
public Bourse(Position pos){
this.nom ="Bourse";
this.pos = pos;
Random rnd = new Random();
this.valeur = rnd.nextInt(501);
}
}
class Bijou extends Butin {
public Bijou(Position pos){
this.nom ="Bijou";
this.valeur = 500;
this.pos = pos;
}
}
class Magot extends Butin {
public Magot(Position pos){
this.nom = "Magot";
this.valeur = 1000;
this.pos = pos;
}
}
abstract class Action {
public abstract void execute(Train t, Personne b);
}
class TirHaut extends Action {
public void execute(Train t, Personne b){
ArrayList<Integer> AutresBandits = new ArrayList<Integer>();
if(b.nbTir > 0) {
if (b.pos.y == 0) {
if(t.wagons[b.pos.x][b.pos.y].bandits.size() > 1) {
for(int i = 0; i < t.wagons[b.pos.x][b.pos.y].bandits.size(); i++) {
if(t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != "Marshall" && t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != b.nom) {
AutresBandits.add(i);
}
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x][b.pos.y].but.add((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché).nom + "touché.");
}
}
if (b.pos.y == 1) {
if(t.wagons[b.pos.x][b.pos.y-1].bandits.size() >= 1) {
for(int i = 0; i < t.wagons[b.pos.x][b.pos.y-1].bandits.size(); i++) {
AutresBandits.add(i);
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x][b.pos.y-1].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x][b.pos.y-1].but.add((t.wagons[b.pos.x][b.pos.y-1].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x][b.pos.y-1].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x][b.pos.y-1].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x][b.pos.y-1].bandits.get(banditTouché).nom + "touché.");
}
}
b.nbTir = b.nbTir-1;
}
if(b.nbTir == 0) {
System.out.println(b.nom + "n'a plus de balle.");
}
}
}
class Braquage extends Action {
public void execute(Train t, Personne b){
if(t.wagons[b.pos.x][b.pos.y].but.isEmpty() == false){
Random rnd = new Random();
int nb = rnd.nextInt(t.wagons[b.pos.x][b.pos.y].but.size());
b.but.add(t.wagons[b.pos.x][b.pos.y].but.get(nb));
t.wagons[b.pos.x][b.pos.y].but.remove(t.wagons[b.pos.x][b.pos.y].but.get(nb));
System.out.println(b.nom + " braque.");
}
}
}
class TirBas extends Action {
public void execute(Train t, Personne b){
ArrayList<Integer> AutresBandits = new ArrayList<Integer>();
if(b.nbTir > 0) {
if (b.pos.y == 1) {
if(t.wagons[b.pos.x][b.pos.y].bandits.size() > 1) {
for(int i = 0; i < t.wagons[b.pos.x][b.pos.y].bandits.size(); i++) {
if(t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != "Marshall" && t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != b.nom) {
AutresBandits.add(i);
}
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x][b.pos.y].but.add((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché).nom + "touché.");
}
}
if (b.pos.y == 0) {
if(t.wagons[b.pos.x][b.pos.y+1].bandits.size() >= 1) {
for(int i = 0; i < t.wagons[b.pos.x][b.pos.y+1].bandits.size(); i++) {
if(t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != "Marshall") {
AutresBandits.add(i);
}
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x][b.pos.y+1].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x][b.pos.y+1].but.add((t.wagons[b.pos.x][b.pos.y+1].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x][b.pos.y+1].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x][b.pos.y+1].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x][b.pos.y+1].bandits.get(banditTouché).nom + "touché.");
}
}
b.nbTir = b.nbTir-1;
}
if(b.nbTir == 0) {
System.out.println(b.nom + "n'a plus de balle.");
}
}
}
class TirGauche extends Action {
public void execute(Train t, Personne b){
ArrayList<Integer> AutresBandits = new ArrayList<Integer>();
if(b.nbTir > 0) {
if (b.pos.x == 0) {
if(t.wagons[b.pos.x][b.pos.y].bandits.size() > 1) {
for(int i = 0; i < t.wagons[b.pos.x][b.pos.y].bandits.size(); i++) {
if(t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != "Marshall" && t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != b.nom) {
AutresBandits.add(i);
}
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x][b.pos.y].but.add((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché).nom + "touché.");
}
}
if (b.pos.x == 1 || b.pos.x == 2 || b.pos.x == 3) {
if(t.wagons[b.pos.x-1][b.pos.y].bandits.size() >= 1) {
for(int i = 0; i < t.wagons[b.pos.x-1][b.pos.y].bandits.size(); i++) {
if(t.wagons[b.pos.x-1][b.pos.y].bandits.get(i).nom != "Marshall") {
AutresBandits.add(i);
}
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x-1][b.pos.y].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x-1][b.pos.y].but.add((t.wagons[b.pos.x-1][b.pos.y].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x-1][b.pos.y].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x-1][b.pos.y].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x-1][b.pos.y].bandits.get(banditTouché).nom + "touché.");
}
}
b.nbTir = b.nbTir-1;
}
if(b.nbTir == 0) {
System.out.println(b.nom + "n'a plus de balle.");
}
}
}
class TirDroit extends Action {
public void execute(Train t, Personne b){
ArrayList<Integer> AutresBandits = new ArrayList<Integer>();
if(b.nbTir > 0) {
if (b.pos.x == 3) {
if(t.wagons[b.pos.x][b.pos.y].bandits.size() > 1) {
for(int i = 0; i < t.wagons[b.pos.x][b.pos.y].bandits.size(); i++) {
if(t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != "Marshall" && t.wagons[b.pos.x][b.pos.y].bandits.get(i).nom != b.nom) {
AutresBandits.add(i);
}
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x][b.pos.y].but.add((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x][b.pos.y].bandits.get(banditTouché).nom + "touché.");
}
}
if (b.pos.x == 0 || b.pos.x == 1 || b.pos.x == 2) {
if(t.wagons[b.pos.x+1][b.pos.y].bandits.size() >= 1) {
for(int i = 0; i < t.wagons[b.pos.x+1][b.pos.y].bandits.size(); i++) {
if(t.wagons[b.pos.x+1][b.pos.y].bandits.get(i).nom != "Marshall") {
AutresBandits.add(i);
}
}
int random = (int)(Math.random() * (AutresBandits.size()));
int banditTouché = AutresBandits.get(random);
int random2 = (int)(Math.random() * ((t.wagons[b.pos.x+1][b.pos.y].bandits.get(banditTouché)).but.size()));
t.wagons[b.pos.x+1][b.pos.y].but.add((t.wagons[b.pos.x+1][b.pos.y].bandits.get(banditTouché)).but.get(random2));
(t.wagons[b.pos.x+1][b.pos.y].bandits.get(banditTouché)).but.remove((t.wagons[b.pos.x+1][b.pos.y].bandits.get(banditTouché)).but.get(random2));
System.out.println(t.wagons[b.pos.x+1][b.pos.y].bandits.get(banditTouché).nom + "touché.");
}
}
b.nbTir = b.nbTir-1;
}
if(b.nbTir == 0) {
System.out.println(b.nom + "n'a plus de balle.");
}
}
}
class Deplacement extends Action {
private Direction d;
public Deplacement(Direction d){
this.d = d;
}
public void estArrete(Train t, int x, int y){
int mar = -1;
for(int i = 0; i < t.wagons[x][y].bandits.size(); i++){
if(t.wagons[x][y].bandits.get(i).nom == "Marshall" && t.wagons[x][y].bandits.size() > 1){
mar = i;
}
}
if(mar != -1){
for (int j = 0; j < t.wagons[x][y].bandits.size(); j++){
if(j != mar && t.wagons[x][y].bandits.get(j).nom != "Marshall"){
System.out.println(t.wagons[x][y].bandits.get(j).nom + " a été arrêté.");
t.joueurs.remove(t.wagons[x][y].bandits.get(j));
t.wagons[x][y].bandits.remove(j);
}
}
}
}
public void execute(Train t, Personne b){
switch(d){
case AVANT:
try {
b.pos = t.wagons[b.pos.x][b.pos.y + 1];
t.wagons[b.pos.x][b.pos.y - 1].bandits.remove(b);
t.wagons[b.pos.x][b.pos.y].bandits.add(b);
System.out.println(b.nom + " avance.");
this.estArrete(t, b.pos.x, b.pos.y);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(b.nom + " ne bouge pas.");
}
break;
case ARRIERE:
try {
b.pos = t.wagons[b.pos.x][b.pos.y - 1];
t.wagons[b.pos.x][b.pos.y + 1].bandits.remove(b);
t.wagons[b.pos.x][b.pos.y].bandits.add(b);
System.out.println(b.nom + " recule.");
this.estArrete(t, b.pos.x, b.pos.y);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(b.nom + " est au bout du train.");
}
break;
case HAUT:
try {
b.pos = t.wagons[b.pos.x - 1][b.pos.y];
t.wagons[b.pos.x + 1][b.pos.y].bandits.remove(b);
t.wagons[b.pos.x][b.pos.y].bandits.add(b);
System.out.println(b.nom + " monte sur le toit.");
this.estArrete(t, b.pos.x, b.pos.y);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(b.nom + " est déjà sur le toit.");
}
break;
case BAS:
try {
b.pos = t.wagons[b.pos.x + 1][b.pos.y];
t.wagons[b.pos.x - 1][b.pos.y].bandits.remove(b);
t.wagons[b.pos.x][b.pos.y].bandits.add(b);
System.out.println(b.nom + " descend à l'intérieur.");
this.estArrete(t, b.pos.x, b.pos.y);
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println(b.nom + " est déjà à l'intérieur.");
}
break;
}
}
}
class CVue {
private JFrame frame;
private VueTrain vtrain;
private VueCommandes commandes;
public CVue(Train train) {
frame = new JFrame();
frame.setTitle("Colt Express");
frame.setLayout(new FlowLayout());
vtrain = new VueTrain(train);
frame.add(vtrain);
commandes = new VueCommandes(train);
frame.add(commandes);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class VueTrain extends JPanel implements Observer {
private Train train;
private final static int largeur = 150;
private final static int hauteur = 100;
public VueTrain(Train train) {
this.train = train;
train.addObserver(this);
Dimension dim = new Dimension(largeur*Train.nb_wagons + 1,hauteur*2 + 1);
this.setPreferredSize(dim);
}
/**
* L'interface [Observer] demande de fournir une méthode [update], qui
* sera appelée lorsque la vue sera notifiée d'un changement dans le
* modèle. Ici on se content de réafficher toute la grille avec la méthode
* prédéfinie [repaint].
*/
public void update() { repaint(); }
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i=0; i<2; i++) {
for(int j=0; j<Train.nb_wagons; j++) {
paint(g, train.getPos(i, j), j*largeur, i*hauteur);
}
}
}
private void paint(Graphics g, Position c, int x, int y) {
g.drawRect(x, y, 150, 100);
if(c.bandits.isEmpty() == false){
int i = 0;
int y1 = y;
while (i < c.bandits.size()){
g.drawString(c.bandits.get(i).nom, x + 5, y1 + 15);
i++;
y1 = y1 + 15;
}
}
if(c.but.isEmpty() == false){
int i = 0;
int y1 = y;
while (i < c.but.size()){
g.drawString(c.but.get(i).nom, x + 75, y1 + 15);
i++;
y1 = y1 + 15;
}
}
}
}
class VueCommandes extends JPanel {
private Train train;
public VueCommandes(Train train) {
this.train = train;
JButton boutonAction = new JButton("Action");
this.add(boutonAction);
JButton H = new JButton("Monter");
this.add(H);
JButton B = new JButton("Descendre");
this.add(B);
JButton G = new JButton("Reculer");
this.add(G);
JButton D = new JButton("Avancer");
this.add(D);
JButton Br = new JButton("Braquage");
this.add(Br);
JButton TH = new JButton("Tir en Haut");
this.add(TH);
JButton TB= new JButton("Tir en Bas");
this.add(TB);
JButton TG = new JButton("Tir à Gauche");
this.add(TG);
JButton TD = new JButton("Tir à Droite");
this.add(TD);
BtAction action = new BtAction(train, boutonAction);
boutonAction.addActionListener(action);
BtDep haut = new BtDep(train,H);
H.addActionListener(haut);
BtDep bas = new BtDep(train,B);
B.addActionListener(bas);
BtDep gauche = new BtDep(train,G);
G.addActionListener(gauche);
BtDep droite = new BtDep(train,D);
D.addActionListener(droite);
BtBraq braq = new BtBraq(train);
Br.addActionListener(braq);
BtTH TirHaut = new BtTH(train);
TH.addActionListener(TirHaut);
BtTB TirBas = new BtTB(train);
TB.addActionListener(TirBas);
BtTG TirGauche = new BtTG(train);
TG.addActionListener(TirGauche);
BtTD TirDroit = new BtTD(train);
TD.addActionListener(TirDroit);
if(action.acc <= train.liste.size()){
boutonAction.setEnabled(true);
} else {
boutonAction.setEnabled(false);
}
}
}
abstract class Controleur implements ActionListener {
Train train;
abstract public void actionPerformed(ActionEvent e);
}
class BtBraq extends Controleur {
public BtBraq(Train train){
this.train = train;
}
public void actionPerformed(ActionEvent e) {
Braquage b = new Braquage();
this.train.liste.add(b);
}
}
class BtTH extends Controleur {
public BtTH(Train train){
this.train = train;
}
public void actionPerformed(ActionEvent e) {
TirHaut TH = new TirHaut();
this.train.liste.add(TH);
}
}
class BtTB extends Controleur {
public BtTB(Train train){
this.train = train;
}
public void actionPerformed(ActionEvent e) {
TirBas TB = new TirBas();
this.train.liste.add(TB);
}
}
class BtTG extends Controleur {
public BtTG(Train train){
this.train = train;
}
public void actionPerformed(ActionEvent e) {
TirGauche TG = new TirGauche();
this.train.liste.add(TG);
}
}
class BtTD extends Controleur {
public BtTD(Train train){
this.train = train;
}
public void actionPerformed(ActionEvent e) {
TirDroit TD = new TirDroit();
this.train.liste.add(TD);
}
}
class BtDep extends Controleur {
protected JButton b;
public BtDep(Train train, JButton j){
this.train = train;
this.b = j;
}
public void actionPerformed(ActionEvent e) {
String s = this.b.getText();
if (s == "Monter"){
Deplacement d = new Deplacement(Direction.HAUT);
this.train.liste.add(d);
} else if (s == "Descendre"){
Deplacement d1 = new Deplacement(Direction.BAS);
this.train.liste.add(d1);
} else if (s == "Avancer"){
Deplacement d2 = new Deplacement(Direction.AVANT);
this.train.liste.add(d2);
} else {
Deplacement d3 = new Deplacement(Direction.ARRIERE);
this.train.liste.add(d3);
}
}
}
class BtAction extends Controleur {
public int acc = 0;
protected JButton b;
protected int joueur = 1;
public BtAction(Train train, JButton bt){
this.train = train;
this.b = bt;
}
public void actionPerformed(ActionEvent e) {
if (this.train.liste.isEmpty() == false){
if(this.acc < this.train.liste.size()){
if (this.acc == 4){
joueur++;
this.acc = 0;
}
if (joueur > Train.nb_joueurs){
joueur = 1;
}
Random rnd = new Random();
double nb = rnd.nextDouble();
if(nb < Personne.NERVOSITE_MARSHALL){
if (nb < Personne.NERVOSITE_MARSHALL/2){
Deplacement d = new Deplacement(Direction.AVANT);
d.execute(train, train.joueurs.get(0));
} else {
Deplacement d = new Deplacement(Direction.ARRIERE);
d.execute(train, train.joueurs.get(0));
}
}
this.train.liste.get(this.acc).execute(this.train, this.train.joueurs.get(joueur));
train.notifyObservers();
this.acc++;
}
}
}
} |
Partager