RinoDrummer icon

Gioco delle coppie (semplice)

RinoDrummer | PRO | 05/10/17 06:33:35 PM UTC | 0 ⭐ | 327 👁️ | Never ⏰ | []
C |

6.13 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
 
#define ROWS 2
#define COLS 9
#define QUANT 2
#define PAIRS ((ROWS * COLS) / QUANT)
 
#define HIDE 'X'
 
typedef enum {false, true} bool;
 
enum messageID {WRONG_CHOICE, ODD_QUANT, WRONG_SEL, KNOWN_CARD, WRONG_PAIR, ADD_SEL = 5, REM_SEL, FOUND_PAIR};
enum userSelection {SEL_ROW, SEL_COL};
 
void printMessage(int msgID) {
    const char messages[8][70] = {
        "La scelta indicata non esiste.",
        "La quantita' indicata non e' corretta. Il valore deve essere pari.",
        "Il valore indicato non e' corretto.",
        "La carta selezionata e' gia' stata scoperta.",
        "Le carte indicate non coincidono.",
        "Carta selezionata.",
        "Carta non selezionata.",
        "Complimenti! Hai trovato una coppia!"
    };
 
    printf("%s\n\n", messages[msgID]);
    system("pause");
    system("cls");
    printf("\n");
    
    return;
}
 
// Array Related:
 
void hideArray(char arr[ROWS][COLS]) {
    int r;
    int c;
    
    for (r = 0; r < ROWS; r++) {
        for (c = 0; c < COLS; c++) {
            arr[r][c] = HIDE;
        }
    }
    
    return;
}
 
// Funzioni meccaniche:
 
void fillArray(int arr[ROWS][COLS]) {
    if ((QUANT % 2) == 0) {
        const int elements = ROWS * COLS;
    
        const int maxVal = PAIRS;
        const int minVal = 1;
        
        int q = QUANT; // Quantity of pairs
        int val;
        int filledElems = 0;
    
        srand(time(NULL));
    
        while (filledElems < elements) {
            for (val = minVal; val <= maxVal; val++) {
                int currPos;
                
                while (q > 0) {
                    currPos = rand() % elements;
                    
                    if (*(*(arr) + currPos) == 0) {
                        *(*(arr) + currPos) = val;
                        
                        filledElems++;
                        q--;
                    }
                }
                
                q = QUANT;
            }
        }
    }
    else {
        printMessage(ODD_QUANT);
    }
    
    return;
}
 
// TUI:
 
void printHiddenArray(char arr[ROWS][COLS]) {
    int r;
    int c;
    
    printf(" \t");
    for (c = 0; c < COLS; c++) {
        printf("%2d ", c);
    }
    printf("\n\n");
    
    for (r = 0; r < ROWS; r++) {
        printf("%2d ", r);
        printf("\t");
        
        for (c = 0; c < COLS; c++) {
            printf("%2c ", arr[r][c]);
        }
        printf("\n");
    }
    printf("\n");
    
    return;
}
 
bool readSelection(int coppie[ROWS][COLS], char coppieTrovate[ROWS][COLS], int *errors) {
    int prevSel[2] = {-1};
    int selection[2] = {0};
    
    int times = 2;
    
    int *sel;
    sel = selection;
    
    printf("%d\n", *errors);
    
    do {
        printf("- Errori commessi: %d;\n", *errors);
        
        if (times < 2) {
           printf("- Selezione precedente: %d %d;\n", prevSel[SEL_ROW], prevSel[SEL_COL]);
        }
        
        printf("Quale carta scegli? (R C) > ");
        scanf("%d %d", &selection[SEL_ROW], &selection[SEL_COL]);
        
        if ((selection[SEL_ROW] < 0) || (selection[SEL_ROW] > ROWS) || (selection[SEL_COL] < 0) || (selection[SEL_COL] > COLS)) {
            printMessage(WRONG_SEL);
        }
        else if (coppieTrovate[sel[SEL_ROW]][sel[SEL_COL]] != HIDE) {
            printMessage(KNOWN_CARD);
        }
        else {
            bool isConfirmed = true;
            
            do {
                char confirm;
                
                printf("\nConfermi la tua scelta? (Y/N) > ");
                scanf("\n%c", &confirm);
                
                if ((confirm == 'Y') || (confirm == 'y')) {
                    printMessage(ADD_SEL);
                    
                    coppieTrovate[sel[SEL_ROW]][sel[SEL_COL]] = coppie[sel[SEL_ROW]][sel[SEL_COL]] + '0';
                    
                    if (times != 1) {
                        prevSel[SEL_ROW] = sel[SEL_ROW];
                        prevSel[SEL_COL] = sel[SEL_COL];
                    }
                    
                    times--;
                }
                else if ((confirm == 'N') || (confirm == 'n')) {
                    printMessage(REM_SEL);
                }
                else {
                    printMessage(KNOWN_CARD);
                    
                    isConfirmed = false;
                }
            }
            while (!isConfirmed);
        }
        
        printHiddenArray(coppieTrovate);
    }
    while (times > 0);
    
    if (times == 0) {
        if (coppieTrovate[sel[SEL_ROW]][sel[SEL_COL]] != coppieTrovate[prevSel[SEL_ROW]][prevSel[SEL_COL]]) {
            printMessage(WRONG_PAIR);
            
            coppieTrovate[prevSel[SEL_ROW]][prevSel[SEL_COL]] = coppieTrovate[sel[SEL_ROW]][sel[SEL_COL]] = HIDE;
            *errors += 1;
            
            return false;
        }
        else {
            printMessage(FOUND_PAIR);
        }
    }
    
    return true;
}
 
bool startGame(int coppie[ROWS][COLS], char coppieTrovate[ROWS][COLS], int *errors) {
    int completition = 0;
    bool isCompleted = false;
 
    system("cls");
    printf("\n\t\t\tAdesso inizia la tua partita!\a\n\n");
    system("pause");
    
    while (completition < PAIRS) {
        printf("\n");
        printHiddenArray(coppieTrovate);
        
        if (readSelection(coppie, coppieTrovate, errors)) {
             completition++;
        }
    }
    
    if (completition == PAIRS) {
        system("cls");
        printf("\nComplementi! Hai finito la partita!\a\a\a\n");
        printf("Hai commesso un totale di %d errori!\n\n", *errors);
        system("pause");
        
        isCompleted = true;
    }
    
    return isCompleted;
}
 
int main() {
    int coppie[ROWS][COLS] = {0};
    char coppieTrovate[ROWS][COLS];
    int errors = 0;
    
    fillArray(coppie);
    hideArray(coppieTrovate);
    
    startGame(coppie, coppieTrovate, &errors);
    
    printf("\nAlla prossima!\n\n");
    system("pause");
    return;
}

Comments