Mini projet LOO
Dependencies: mbed APDS_9960 mbed-rtos
Home du projet
src/cJeu.cpp@15:3782eb2bc312, 2016-04-11 (annotated)
- Committer:
- clementdoreau
- Date:
- Mon Apr 11 13:29:31 2016 +0000
- Revision:
- 15:3782eb2bc312
- Parent:
- 14:6b6aa696680f
- Child:
- 16:5c3d61584f51
Ajout de la classe cCollision;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Willheisen | 8:92d0c4961a16 | 1 | #include "cJeu.h" |
Willheisen | 8:92d0c4961a16 | 2 | |
Willheisen | 8:92d0c4961a16 | 3 | cJeu::cJeu() |
Willheisen | 8:92d0c4961a16 | 4 | { |
Willheisen | 14:6b6aa696680f | 5 | cMatrice _mat(8,8); |
Willheisen | 8:92d0c4961a16 | 6 | //on initialise la générateur de nombres pseudo-aléatoires |
clementdoreau | 10:9ef3f520ff6c | 7 | srand(time(NULL)); |
Willheisen | 9:6f3d8b714a59 | 8 | } |
Willheisen | 8:92d0c4961a16 | 9 | |
Willheisen | 14:6b6aa696680f | 10 | cMatrice cJeu::get_matrice() |
Willheisen | 13:336220ac9a8b | 11 | { |
Willheisen | 13:336220ac9a8b | 12 | return _mat; |
Willheisen | 13:336220ac9a8b | 13 | } |
Willheisen | 13:336220ac9a8b | 14 | |
Willheisen | 13:336220ac9a8b | 15 | unsigned char cJeu::get_score() |
Willheisen | 13:336220ac9a8b | 16 | { |
Willheisen | 13:336220ac9a8b | 17 | return _score; |
Willheisen | 13:336220ac9a8b | 18 | } |
Willheisen | 13:336220ac9a8b | 19 | |
Willheisen | 14:6b6aa696680f | 20 | void cJeu::set_matrice(cMatrice mat) |
Willheisen | 13:336220ac9a8b | 21 | { |
Willheisen | 13:336220ac9a8b | 22 | _mat = mat; |
Willheisen | 13:336220ac9a8b | 23 | } |
Willheisen | 13:336220ac9a8b | 24 | |
Willheisen | 13:336220ac9a8b | 25 | void cJeu::set_score(unsigned char sc) |
Willheisen | 13:336220ac9a8b | 26 | { |
Willheisen | 13:336220ac9a8b | 27 | _score = sc; |
Willheisen | 13:336220ac9a8b | 28 | } |
Willheisen | 8:92d0c4961a16 | 29 | void cJeu::initialiser() |
Willheisen | 8:92d0c4961a16 | 30 | { |
Willheisen | 14:6b6aa696680f | 31 | _mat.clear(); |
Willheisen | 8:92d0c4961a16 | 32 | } |
Willheisen | 8:92d0c4961a16 | 33 | |
clementdoreau | 10:9ef3f520ff6c | 34 | // Description: |
clementdoreau | 10:9ef3f520ff6c | 35 | // Créé une nouvelle pièce et renvoi l'adresse de cette dernière |
Willheisen | 14:6b6aa696680f | 36 | void cJeu::nouvellePiece() |
Willheisen | 8:92d0c4961a16 | 37 | { |
clementdoreau | 10:9ef3f520ff6c | 38 | cForme *f; |
Willheisen | 8:92d0c4961a16 | 39 | //on génère un nombre aléatoire entre 1 et 5 pour la pièce |
Willheisen | 8:92d0c4961a16 | 40 | int nb = 1 + ((float)rand()/RAND_MAX) * 5; |
Willheisen | 8:92d0c4961a16 | 41 | //on génère un deuxième nombre pour choisir la couleur de la pièce |
Willheisen | 8:92d0c4961a16 | 42 | int couleur = 1 + ((float)rand()/RAND_MAX) * 8; |
Willheisen | 8:92d0c4961a16 | 43 | //selon le nombre généré, on va créer une nouvelle pièce |
clementdoreau | 10:9ef3f520ff6c | 44 | switch(nb) { |
Willheisen | 8:92d0c4961a16 | 45 | //on crée une pièce "L" |
Willheisen | 8:92d0c4961a16 | 46 | case 1: { |
clementdoreau | 10:9ef3f520ff6c | 47 | f = new cEl(3,0); |
Willheisen | 8:92d0c4961a16 | 48 | break; |
Willheisen | 8:92d0c4961a16 | 49 | } |
Willheisen | 8:92d0c4961a16 | 50 | //on crée une pièce "ligne" |
Willheisen | 8:92d0c4961a16 | 51 | case 2: { |
clementdoreau | 10:9ef3f520ff6c | 52 | f = new cLigne(3,0); |
Willheisen | 8:92d0c4961a16 | 53 | break; |
Willheisen | 8:92d0c4961a16 | 54 | } |
Willheisen | 8:92d0c4961a16 | 55 | //on crée une pièce "Z" |
Willheisen | 8:92d0c4961a16 | 56 | case 3: { |
clementdoreau | 10:9ef3f520ff6c | 57 | f = new cZed(3,0); |
Willheisen | 8:92d0c4961a16 | 58 | break; |
Willheisen | 8:92d0c4961a16 | 59 | } |
Willheisen | 8:92d0c4961a16 | 60 | //on crée une pièce "T" |
Willheisen | 8:92d0c4961a16 | 61 | case 4: { |
clementdoreau | 10:9ef3f520ff6c | 62 | f = new cTe(3,0); |
Willheisen | 8:92d0c4961a16 | 63 | break; |
Willheisen | 8:92d0c4961a16 | 64 | } |
Willheisen | 8:92d0c4961a16 | 65 | //on crée une pièce "carré" |
Willheisen | 8:92d0c4961a16 | 66 | case 5: { |
clementdoreau | 10:9ef3f520ff6c | 67 | f = new cCarre(3,0); |
Willheisen | 8:92d0c4961a16 | 68 | break; |
Willheisen | 8:92d0c4961a16 | 69 | } |
clementdoreau | 10:9ef3f520ff6c | 70 | default: |
clementdoreau | 10:9ef3f520ff6c | 71 | {} |
Willheisen | 14:6b6aa696680f | 72 | } |
Willheisen | 14:6b6aa696680f | 73 | //puis on affiche la nouvelle pièce |
Willheisen | 14:6b6aa696680f | 74 | _mat.afficherForme(f, couleur); |
Willheisen | 8:92d0c4961a16 | 75 | } |
Willheisen | 8:92d0c4961a16 | 76 | |
Willheisen | 9:6f3d8b714a59 | 77 | cJeu::~cJeu() |
Willheisen | 9:6f3d8b714a59 | 78 | { |
Willheisen | 14:6b6aa696680f | 79 | delete _ptrForme; |
Willheisen | 9:6f3d8b714a59 | 80 | } |
Willheisen | 12:7afdbc7465ac | 81 | |
Willheisen | 14:6b6aa696680f | 82 | void cJeu::nouvelleManche() |
Willheisen | 14:6b6aa696680f | 83 | { |
Willheisen | 14:6b6aa696680f | 84 | cJeu::nouvellePiece(); |
Willheisen | 14:6b6aa696680f | 85 | //tant que l'on peut descendre |
Willheisen | 14:6b6aa696680f | 86 | //while(frfd) |
Willheisen | 14:6b6aa696680f | 87 | |
Willheisen | 14:6b6aa696680f | 88 | |
Willheisen | 14:6b6aa696680f | 89 | //puis on teste si une ligne est complete |
Willheisen | 14:6b6aa696680f | 90 | cJeu::ligneComplete(); |
Willheisen | 14:6b6aa696680f | 91 | |
Willheisen | 14:6b6aa696680f | 92 | } |
Willheisen | 14:6b6aa696680f | 93 | void cJeu::ligneComplete() |
Willheisen | 12:7afdbc7465ac | 94 | { |
Willheisen | 13:336220ac9a8b | 95 | Serial pc(USBTX, USBRX); |
Willheisen | 14:6b6aa696680f | 96 | unsigned char total = 0, numLigne = 0; |
Willheisen | 12:7afdbc7465ac | 97 | /*on parcourt chaque ligne de la matrice. |
Willheisen | 12:7afdbc7465ac | 98 | Si une de ces lignes est complète (c-à-d pas de LED noire), alors |
Willheisen | 12:7afdbc7465ac | 99 | on fait descendre toutes les pièces au-dessus de la ligne(autres que celle de la manche) d'un cran. |
Willheisen | 12:7afdbc7465ac | 100 | */ |
Willheisen | 14:6b6aa696680f | 101 | for( unsigned char i=0; i < _mat.getLig(); i++) { |
Willheisen | 13:336220ac9a8b | 102 | total = 0; |
Willheisen | 14:6b6aa696680f | 103 | for( unsigned char j=0; j < _mat.getCol(); j++) { |
Willheisen | 14:6b6aa696680f | 104 | if(_mat.getValTab(j, i) != LED_NOIR) { |
Willheisen | 13:336220ac9a8b | 105 | total ++; |
Willheisen | 14:6b6aa696680f | 106 | } |
Willheisen | 14:6b6aa696680f | 107 | if(total == 8) { |
Willheisen | 13:336220ac9a8b | 108 | numLigne = i; |
Willheisen | 12:7afdbc7465ac | 109 | break; |
Willheisen | 12:7afdbc7465ac | 110 | } |
clementdoreau | 15:3782eb2bc312 | 111 | else{ |
clementdoreau | 15:3782eb2bc312 | 112 | return 0; // PAS DE LIGNE COMPLETE |
clementdoreau | 15:3782eb2bc312 | 113 | } |
Willheisen | 12:7afdbc7465ac | 114 | } |
Willheisen | 12:7afdbc7465ac | 115 | } |
clementdoreau | 15:3782eb2bc312 | 116 | |
Willheisen | 14:6b6aa696680f | 117 | pc.printf("numLigne: %d\n", numLigne); |
Willheisen | 13:336220ac9a8b | 118 | ////////////////////////////////////////////// |
Willheisen | 13:336220ac9a8b | 119 | //On descend toutes les LEDs allumées au dessus de numLigne d'un cran |
Willheisen | 14:6b6aa696680f | 120 | if(numLigne != 0) { |
Willheisen | 14:6b6aa696680f | 121 | for( unsigned char i = 0; i < numLigne; i++) { |
Willheisen | 14:6b6aa696680f | 122 | total = 0; |
Willheisen | 14:6b6aa696680f | 123 | for( unsigned char j = 0; j < _mat.getCol(); j++) { |
Willheisen | 14:6b6aa696680f | 124 | _mat.setValTab(j, i, _mat.getValTab(j-1, i)); |
Willheisen | 14:6b6aa696680f | 125 | } |
Willheisen | 14:6b6aa696680f | 126 | } |
Willheisen | 14:6b6aa696680f | 127 | //Et on augmente le score! |
Willheisen | 14:6b6aa696680f | 128 | _score+=10; |
Willheisen | 13:336220ac9a8b | 129 | } |
clementdoreau | 15:3782eb2bc312 | 130 | return 1; |
Willheisen | 12:7afdbc7465ac | 131 | } |
clementdoreau | 15:3782eb2bc312 | 132 | |
clementdoreau | 15:3782eb2bc312 | 133 | int partieEnCours(){ |
clementdoreau | 15:3782eb2bc312 | 134 | |
clementdoreau | 15:3782eb2bc312 | 135 | //////////////////////////////////////// |
clementdoreau | 15:3782eb2bc312 | 136 | // DEMANDE UTILISATEUR |
clementdoreau | 15:3782eb2bc312 | 137 | |
clementdoreau | 15:3782eb2bc312 | 138 | |
clementdoreau | 15:3782eb2bc312 | 139 | //////////////////////////////////////// |
clementdoreau | 15:3782eb2bc312 | 140 | // CONTROLE COLLISION |
clementdoreau | 15:3782eb2bc312 | 141 | if(_matrice.verificationMvt('b') != 0) |
clementdoreau | 15:3782eb2bc312 | 142 | { |
clementdoreau | 15:3782eb2bc312 | 143 | //////////////////////////////////////// |
clementdoreau | 15:3782eb2bc312 | 144 | // MOUVEMENT |
clementdoreau | 15:3782eb2bc312 | 145 | |
clementdoreau | 15:3782eb2bc312 | 146 | |
clementdoreau | 15:3782eb2bc312 | 147 | //////////////////////////////////////// |
clementdoreau | 15:3782eb2bc312 | 148 | // CONTROLE LIGNE COMPLETE |
clementdoreau | 15:3782eb2bc312 | 149 | |
clementdoreau | 15:3782eb2bc312 | 150 | |
clementdoreau | 15:3782eb2bc312 | 151 | |
clementdoreau | 15:3782eb2bc312 | 152 | } |