/* 
 * File:   bingo.cpp
 * Author: as
 *
 */

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int anzSpieler = 3;
const int anzZahlen = 20;
const int anzTipps = 5;
int status = 0;
int *spieler[anzSpieler]; // anzSpieler Spieler
int ziehung[anzZahlen]= {0};


void init() { 
    srand (time(NULL)); /* initialize random seed: */
    for (int s=0; s < anzSpieler; s++) {
        spieler[s] = new int[anzZahlen]; // Tippzettel Spieler s
        for (int j=0; j<anzZahlen; j++)
            spieler[s][j] = 0;
    }
} // alle Tippzettel mit 0 besetzen

int zaehlen(int f[], int max) {
    int res=0;
    for (int i=0; i< max; i++)
        res += f[i];
    return res;
} // zaehle true-Werte in f

void zufall(int f[], int x) { 
    // for (int i=0; i<x; i++)  // Aufgabe Note 4
    while (zaehlen(f, anzZahlen) < x)      // Aufgabe Note 2
      f[rand() % anzZahlen] = 1;
} // Feld f mit x Zufallszahlen beschreiben

void setzen() {
    for (int s=0; s < anzSpieler; s++) {
        zufall(spieler[s],anzTipps);  // alle Spieler bekommen Zahlen "gewuerfelt"
                               // hier waere Dialog zu programmieren
    }
    status = 1;
}

int richtige(int f[], int l) {
    int c=0;
    for (int i=0; i<l; i++)
        if (ziehung[i] == 1 && f[i] == 1)
            c++;
    return c;
} // Anzahl richtige returnieren

void gewinner() {
   int max;
   if (status == 0 || status == 1) {
        cout << "bitte zuerst setzen und spielen!" << endl;
        return;
    }
   cout << "          ";
       for (int i=0; i<anzZahlen; i++)
         cout << setw(2) << i << " " ;
       cout << endl;
   cout << "Ziehung:  ";
       for (int i=0; i<anzZahlen; i++)
         cout << setw(2) << ziehung[i] << " " ;
       cout << endl;
   for (int s=0; s < anzSpieler; s++) {
       cout << "Spieler" << s << ": ";
       for (int i=0; i<anzZahlen; i++)
         cout << setw(2) << spieler[s][i] << " " ;
       max = richtige(spieler[s], anzZahlen);
       cout << "  richtige: " << max << endl;
       if (max == anzTipps)  {
           cout << "Spieler " << s << " hat gewonnen!" << endl;
           exit(0);
       }
    }
    status = anzSpieler;
}

void spielen() {
    int gezZahl;
    if (status == 0) {
        cout << "bitte zuerst setzen!" << endl;
        return;
    }
    do {
      gezZahl = rand() % anzZahlen;
    } while (ziehung[gezZahl] == 1);
    ziehung[gezZahl] = 1;
    cout << "gezogene Zahl: " << gezZahl << endl;
    status = 2;
    gewinner();
}

int main(int argc, char** argv) {
    char eingabe[80];
    int auswahl;
    init();
    while (1) {
        cout << "----- Bingo -----" << endl;
        cout << "1 setzen         " << endl;
        cout << "2 spielen        " << endl;
        cout << "3 gewinner       " << endl;
        cout << "0 ende           " << endl << endl;
        cout << "Auswahl (0-3): ";
        cin >> eingabe;
        if (sscanf(eingabe,"%d", &auswahl) != 1) {
            cout << "falsche Eingabe: " << eingabe << endl;
            sleep(2);
            continue;
        }
        switch (auswahl) {
            case 0: exit(0);
            case 1: setzen();
                break;
            case 2: spielen();
                break;
            case 3: gewinner();
                break;
            default:  cout << "falsche Eingabe: " << eingabe << endl;
            sleep(2);
        }
    }
    return (EXIT_SUCCESS);
}

