// Zähler-Array-Klasse, Hauptprogramm: Lotto
//
// Aufruf: counter-main outfilename
//
// Klaus Kusche, 2012

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

#include "counter.h"

using namespace std;

const int kugeln = 49;
const int zahlen = 6;

ostream &operator<<(ostream &outFile, const Counter &a);
Counter ziehe();

ostream &operator<<(ostream &outFile, const Counter &a)
{  
  unsigned int i;
  
  for (i = 0; i < a.getSize(); ++i) {
    int c = a.getCnt(i);
    if (c > 0) {
      outFile << i << ':' << c << ' ';
    }
  }
  
  return outFile;
}

Counter ziehe()
{ 
  Counter res(kugeln);
  int i;
  unsigned int z;

  for (i = 0; i < zahlen; ++i) {
    do {
      z = rand() % kugeln;
    } while (res.getCnt(z) > 0);
    res = res + z;    
  }

  return res;
}

int main(int argc, const char *argv[])
{
  if (argc != 2) {
    cerr << "Aufruf: " << argv[0] << " outfilename" << endl;
    exit(EXIT_FAILURE);
  }
  ofstream outf(argv[1]);
  if (!outf) {
    cerr << argv[0] << ": Cannot open " << argv[2] << " for writing" << endl;
    exit(EXIT_FAILURE);
  }

  srand(time(nullptr));

  Counter ziehung(kugeln), gezogen(kugeln);
  int i;

  try {
    for (i = 0; ; ++i) {
      ziehung = ziehe();
      if (ziehung <= gezogen) break;
      gezogen = gezogen + ziehung;
    }

    outf << i << " Runden" << endl;
    outf << ziehung << endl;
    outf << gezogen << endl;
  }
  catch (const char *e) {
    cerr << e << endl;
    exit(EXIT_FAILURE);
  }

  exit(EXIT_SUCCESS);
}
