C++ :: Aufgabe #361

1 Lösung Lösung öffentlich

Bingo-Lotterie (Spielerweiterung)

Fortgeschrittener - C++ von JKooP - 19.04.2021 um 20:17 Uhr
Ich habe die bereits bestehende Aufgabe
"Klassisches Bingo mit verzögert gezogenen Nummern (C# 243, C++ 220, PY 208, …) von maxi72501"
noch einmal aufgegriffen.

Das Spiel soll dahingehend erweitert werden, dass 22 Zahlen gezogen werden und
der vorab erstelle Spielschein (Aufgabe besteht bereits) damit verglichen wird.

Finden sich fünf Treffer in waagerechter, senkrechter oder diagonaler Reihe heißt das BINGO (siehe Bilder).

Als Ergebnis soll die Anzahl der Gewinne (Einfach-, Zweifach- bzw. Dreifach-Bingo) ausgegeben werden.

Viel Spaß

Lösungen:

vote_ok
von JKooP (18070 Punkte) - 23.05.2021 um 17:59 Uhr
C++ 17
Quellcode ausblenden C-Code
#include "Bingo_Lottery.h"

int main()
{
    Bingo_Lottery bl;
    bl.print_results();
}

.h
Quellcode ausblenden C-Code
#pragma once
#include <iostream>
#include <vector>
#include <random>
#include <chrono>
#include <algorithm>
#include <string>
#include <iomanip>
#include <map>
using namespace std;

enum class BingoRange
{
	B = 15, I = 30, N = 45, G = 60, O = 75
};

class Bingo_Lottery
{
private:
	vector<vector<int>> lottery_ticket_{ get_lottery_ticket() };
	vector<int> lottery_numbers_{ get_lottery_numbers() };
	void transpose(vector<vector<int>>& v);
	vector<vector<int>> get_lottery_ticket();
	vector<int> get_lottery_numbers();
	void compare_results();
	int number_of_bingos_rows();
	int number_of_bingos_diagonals();
	int number_of_bingos_all();
	void print_number_of_bingos();
	void print_lottery_ticket(bool is_result = false);
	void print_lottery_numbers();

public:
	Bingo_Lottery() {}
	void print_results();
};

.cpp
Quellcode ausblenden C-Code
#include "Bingo_Lottery.h"

void Bingo_Lottery::transpose(vector<vector<int>>& v)
{
    if (v.size() == 0)
        return;

    vector<vector<int>> tv(v[0].size(), vector<int>());

    for (size_t i = 0; i < v.size(); i++)
        for (size_t j = 0; j < v[i].size(); j++)
            tv[j].push_back(v[i][j]);

    v = tv;
}

vector<vector<int>> Bingo_Lottery::get_lottery_ticket()
{
    vector<vector<int>> lt;
    for (size_t i = 0; i < 5; i++)
    {
        vector<int> randoms{};
        for (size_t k = i * 15 + 1; k < i * 15 + 16; k++)
            randoms.push_back(k);

        auto s{ (int)chrono::system_clock::now().time_since_epoch().count() };
        auto r{ default_random_engine(s) };
        shuffle(randoms.begin(), randoms.end(), r);
        randoms.resize(5);
        lt.push_back(randoms);
    }
    transpose(lt);
    return lt;
}

vector<int> Bingo_Lottery::get_lottery_numbers()
{
    vector<int> ln;
    for (size_t i = 1; i <= 75; i++)
        ln.push_back(i);

    auto s{ (int)chrono::system_clock::now().time_since_epoch().count() };
    auto r{ default_random_engine(s) };
    shuffle(ln.begin(), ln.end(), r);
    ln.resize(22);
    sort(ln.begin(), ln.end());
    return ln;
}

void Bingo_Lottery::compare_results()
{
    for (int i = 0; i < 5; i++)
        for (int k = 0; k < 5; k++)
            if (find(lottery_numbers_.begin(), lottery_numbers_.end(), lottery_ticket_[i][k]) != lottery_numbers_.end())
                lottery_ticket_[i][k] = 0;
}

int Bingo_Lottery::number_of_bingos_rows()
{
    auto counter{ 0 };
    for (const auto& i : lottery_ticket_) {
        auto sum{ 0 };

        for (const auto& k : i)
            sum += k;

        if (sum == 0)
            counter++;
    }
    return counter;
}

int Bingo_Lottery::number_of_bingos_diagonals()
{
    auto max{ lottery_ticket_[0].size() };
    auto sum_left{ 0 };
    auto sum_right{ 0 };

    for (size_t i = 0; i < max; i++)
    {
        sum_left += lottery_ticket_[i][i];
        sum_right += lottery_ticket_[i][max - 1 - i];
    }

    return (sum_left == 0) + (sum_right == 0);
}

int Bingo_Lottery::number_of_bingos_all()
{
    auto rows{ number_of_bingos_rows() };
    auto diagonals{ number_of_bingos_diagonals() };
    transpose(lottery_ticket_);
    auto columns{ number_of_bingos_rows() };
    return rows + diagonals + columns;
}

void Bingo_Lottery::print_number_of_bingos()
{
    cout << "\n";
    switch (number_of_bingos_all()) {
    case 1: cout << "Einfach-Bingo" << "\n"; break;
    case 2: cout << "Zweifach-Bingo" << "\n"; break;
    case 3: cout << "Dreifach-Bingo" << "\n"; break;
    default: cout << "Kein Gewinn" << "\n"; break;
    }
}

void Bingo_Lottery::print_lottery_ticket(bool is_result)
{
    cout << "  B |  I |  N |  G |  O |\n";
    cout << string(25, '-') << "\n";

    for (const auto& i : lottery_ticket_) {
        cout << " ";
        for (const auto& k : i) {
            if (is_result)
                if (k == 0)
                    cout << "XX" << " | ";
                else
                    cout << "  " << " | ";
            else
                cout << setfill('0') << setw(2) << k << " | ";
        }
        cout << "\n";
        cout << string(25, '-') << "\n";
    }
}

void Bingo_Lottery::print_lottery_numbers()
{
    map<BingoRange, vector<string>>m{};

    for (const auto& i : lottery_numbers_) {
        if (i <= (int)BingoRange::B) m[BingoRange::B].push_back("B" + to_string(i));
        else if (i <= (int)BingoRange::I) m[BingoRange::I].push_back("I" + to_string(i));
        else if (i <= (int)BingoRange::N) m[BingoRange::N].push_back("N" + to_string(i));
        else if (i <= (int)BingoRange::G) m[BingoRange::G].push_back("G" + to_string(i));
        else m[BingoRange::O].push_back("O" + to_string(i));
    }

    for (const auto& i : m) {
        for (const auto& k : i.second)
            cout << k << ", ";
        cout << endl;
    }
}

void Bingo_Lottery::print_results()
{
    cout << "Bingo-Losschein:\n\n";
    print_lottery_ticket();
    cout << "\nGewinnzahlen:\n\n";
    print_lottery_numbers();
    compare_results();
    cout << "\nGewinnuebersicht:\n\n";
    print_lottery_ticket(true);
    cout << "\nGewinn:\n";
    print_number_of_bingos();
}