Mini projet LOO
Dependencies: mbed APDS_9960 mbed-rtos
Home du projet
src/cJeu.cpp@13:336220ac9a8b, 2016-04-10 (annotated)
- Committer:
- Willheisen
- Date:
- Sun Apr 10 21:54:27 2016 +0000
- Revision:
- 13:336220ac9a8b
- Parent:
- 12:7afdbc7465ac
- Child:
- 14:6b6aa696680f
M?thode de test de ligne compl?te partiellement OK
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 | 13:336220ac9a8b | 5 | _mat = new cMatrice(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 | 13:336220ac9a8b | 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 | 13:336220ac9a8b | 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 | 13:336220ac9a8b | 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 |
clementdoreau | 10:9ef3f520ff6c | 36 | cForme *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 | {} |
clementdoreau | 10:9ef3f520ff6c | 72 | } |
clementdoreau | 10:9ef3f520ff6c | 73 | //puis on renvoi la nouvelle pièce |
clementdoreau | 10:9ef3f520ff6c | 74 | return f; |
Willheisen | 8:92d0c4961a16 | 75 | } |
Willheisen | 8:92d0c4961a16 | 76 | |
Willheisen | 9:6f3d8b714a59 | 77 | cJeu::~cJeu() |
Willheisen | 9:6f3d8b714a59 | 78 | { |
Willheisen | 13:336220ac9a8b | 79 | delete _mat; |
Willheisen | 9:6f3d8b714a59 | 80 | } |
Willheisen | 12:7afdbc7465ac | 81 | |
Willheisen | 12:7afdbc7465ac | 82 | unsigned char cJeu::ligneComplete() |
Willheisen | 12:7afdbc7465ac | 83 | { |
Willheisen | 13:336220ac9a8b | 84 | Serial pc(USBTX, USBRX); |
Willheisen | 13:336220ac9a8b | 85 | unsigned char res = 0, total = 0, numLigne = 0; |
Willheisen | 12:7afdbc7465ac | 86 | /*on parcourt chaque ligne de la matrice. |
Willheisen | 12:7afdbc7465ac | 87 | Si une de ces lignes est complète (c-à-d pas de LED noire), alors |
Willheisen | 12:7afdbc7465ac | 88 | on fait descendre toutes les pièces au-dessus de la ligne(autres que celle de la manche) d'un cran. |
Willheisen | 12:7afdbc7465ac | 89 | */ |
Willheisen | 13:336220ac9a8b | 90 | for( unsigned char i=0; i < _mat->getLig(); i++) { |
Willheisen | 13:336220ac9a8b | 91 | total = 0; |
Willheisen | 13:336220ac9a8b | 92 | for( unsigned char j=0; j < _mat->getCol(); j++) { |
Willheisen | 13:336220ac9a8b | 93 | |
Willheisen | 13:336220ac9a8b | 94 | if(_mat->getValTab(j, i) != LED_NOIR) { |
Willheisen | 13:336220ac9a8b | 95 | total ++; |
Willheisen | 13:336220ac9a8b | 96 | } |
Willheisen | 13:336220ac9a8b | 97 | if(total == 8) |
Willheisen | 13:336220ac9a8b | 98 | { |
Willheisen | 12:7afdbc7465ac | 99 | res = 1; |
Willheisen | 13:336220ac9a8b | 100 | numLigne = i; |
Willheisen | 12:7afdbc7465ac | 101 | break; |
Willheisen | 12:7afdbc7465ac | 102 | } |
Willheisen | 12:7afdbc7465ac | 103 | } |
Willheisen | 12:7afdbc7465ac | 104 | } |
Willheisen | 13:336220ac9a8b | 105 | pc.printf("numLigne: %d\n", numLigne); |
Willheisen | 13:336220ac9a8b | 106 | ////////////////////////////////////////////// |
Willheisen | 13:336220ac9a8b | 107 | //On descend toutes les LEDs allumées au dessus de numLigne d'un cran |
Willheisen | 13:336220ac9a8b | 108 | if(numLigne != 0) |
Willheisen | 13:336220ac9a8b | 109 | { |
Willheisen | 13:336220ac9a8b | 110 | } |
Willheisen | 13:336220ac9a8b | 111 | |
Willheisen | 13:336220ac9a8b | 112 | |
Willheisen | 13:336220ac9a8b | 113 | |
Willheisen | 13:336220ac9a8b | 114 | |
Willheisen | 13:336220ac9a8b | 115 | //Et on augmente le score! |
Willheisen | 13:336220ac9a8b | 116 | _score+=10; |
Willheisen | 13:336220ac9a8b | 117 | |
Willheisen | 12:7afdbc7465ac | 118 | return res; |
Willheisen | 12:7afdbc7465ac | 119 | } |