*argh* Blackjack



  • Ich habe vorhin ja schonmal wegen einem Random-Befehl nachgefragt.
    Jetzt hab ich mein Blackjack fast fertig, aber ich komm an dem Punkt nicht weiter, wo er, wenn er über 21 geht dann sagt "YOU LOOSE". Könnte mir da jemand helfen?

    Ist ein bisschen Chaos im Code 🙂

    /*
    Blackjack made by Marc G. 2006
    */
    
    /*
    Ass = 1 oder 11
    König = 10
    Königin = 10
    Bube = 10
    */
    
    //Libarys
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    //Soweit unwichtig...
    using namespace std;
    
    //Haupt-Funktion
    int main()
    {
    
    //Deklarationen
    srand(time(0));
    int a = rand() % 9 + 2;
    int b = rand() % 9 + 2;
    int c = rand() % 9 + 2;
    int d = rand() % 9 + 2;
    int e = rand() % 9 + 2;
    int f = rand() % 9 + 2;
    int g = rand() % 9 + 2;
    int h = rand() % 9 + 2;
    int i = rand() % 9 + 2;
    int j = rand() % 9 + 2;
    int k = rand() % 9 + 2;
    char ch = 0;
    int insg, insg2, insg3, insg4, insg5, insg6, insg7, insg8, insg9, insg10, insg11, eing;
    
    //Ausgabe (Design)
    if(a<=21)
    {
    insg = a + b;
    }else{
    system("cls");
    cout<<"YOU LOST - You took to much"<<endl;
    }
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<" \n\nTogether: "<<insg<<endl;
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg2 = a + b + c;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<" \n\nTogether: "<<insg2<<endl;
    
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg3 = a + b + c + d;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<" \n\nTogether: "<<insg3<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg4 = a + b + c + d + e;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<"\n5th Card: "<<e<<" \n\nTogether: "<<insg4<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg5 = a + b + c + d + e + f;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<"\n5th Card: "<<e<<"\n6th Card: "<<f<<" \n\nTogether: "<<insg5<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg6 = a + b + c + d + e + f + g;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<"\n5th Card: "<<e<<"\n6th Card: "<<f<<"\n7th Card: "<<g<<" \n\nTogether: "<<insg6<<endl;
    }
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg7 = a + b + c + d + e + f + g + h;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<"\n5th Card: "<<e<<"\n6th Card: "<<f<<"\n7th Card: "<<g<<"\n8th Card:"<<h<<" \n\nTogether: "<<insg7<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg8 = a + b + c + d + e + f + g + h + i;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<"\n5th Card: "<<e<<"\n6th Card: "<<f<<"\n7th Card: "<<g<<"\n8th Card:"<<h<<"\n9th Card: "<<i<<" \n\nTogether: "<<insg8<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg9 = a + b + c + d + e + f + g + h + i + j;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<"\n5th Card: "<<e<<"\n6th Card: "<<f<<"\n7th Card: "<<g<<"\n8th Card: "<<h<<"\n9th Card: "<<i<<"\n10th Card:"<<j<<" \n\nTogether: "<<insg9<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg10 = a + b + c + d + e + f + g + h + i + j;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<"\n4th Card: "<<d<<"\n5th Card: "<<e<<"\n6th Card: "<<f<<"\n7th Card: "<<g<<"\n8th Card: "<<h<<"\n9th Card: "<<i<<"\n10th Card: "<<j<<"\n11th Card: "<<k<<" \n\nTogether: "<<insg10<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg11 = a + b + c + d + e + f + g + h + i + j + k;
    cout<<"This was too much, you lost."<<endl;
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //SCHLEIFE + Schließen
    do{
    ch = cin.get( );    
    } while(ch != 'Q'); 
    //SCHLEIFEN-ENDE
    
    cin.get();
    }
    


  • Ich sehe min. 50 "YOU LOST"... wo genau hängst du?



  • Und zwar an dem Punkt, nach dem er die 2te Zahl ausgegeben hat. Weil es ja sein kann, das man dann 20 Hat und jemand stop drücken will. Und genau da hänge ich, er soll dann, wenn man nach der 2ten Zahl 20 hat und dann noch eine Zahl bekommt und über 21 kommt, das er dann schreiben soll: "YOU LOST", also an dieser stelle ungefähr--->

    //Ausgabe (Design)
    if(a<=21)
    {
    insg = a + b;
    }else{
    system("cls");
    cout<<"YOU LOST - You took to much"<<endl;
    }
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<" \n\nTogether: "<<insg<<endl;
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    
    //Wenn zu viel - dann tod!
    if(insg > 21)
    {
    cout<<"You lost!"<<endl;
    }else if(insg2 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg3 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg4 > 21){
          system("cls");
          cout<<"You Lost!";    
    }else if(insg5 > 21){
          system("cls");
          cout<<"You Lost!";  
    }else if(insg6 > 21){
          system("cls");
          cout<<"You Lost!";  
    }
    
    if(eing = 1)
    {
    system("cls");
    insg2 = a + b + c;
    cout<<"1st Card: "<<a<<" \n2nd Card: "<<b<<"\n3rd Card: "<<c<<" \n\nTogether: "<<insg2<<endl;
    
    }
    
    cout<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nTake another Card, type: 1\n";
    cout<<"Stand with that Cards, type: 2\n";
    cout<<"To quit, type: Q + Enter\n";
    eing = 0;
    cin>>eing;
    


  • An der Stelle hast du 'insg2' und die weiteren Zwischensummen auch noch gar nicht initialisiert.

    (btw reicht eine Zwischensumme aus, in der du dir den jeweils aktuellen Stand merkst)

    PS: Und mit 2 Karten kann man noch gar nicht über 21 kommen 😉



  • @FreeR0M:

    du solltest mal die elementaren Grundbegriffe wie Arrays und Funktionen lernen und verwenden(!). Dann hättest du auch nicht so viele Fehler im Programm...

    "Für Programmier-Anfänger sollte man C&P verbieten!"



  • Überleg dir mal um das spiel ne schleife drumherumzusetzten.
    Ich sehe ca. 10 mal denselben code!
    Dein ganzes Programm könnte man mit schleifen also auf ca 1/10 des codes reduzieren. Dann wird dein Problem vll. auch besser ersichtlich.
    (Ich glaub keiner ließt sich so einen großen code ganz durch)

    Desweiteren solltest du uns sagen wo genau dein problem liegt.



  • ich sehe spontan solche fehler:

    if(eing = 1)

    wie geht der vergleichsoperator? 😉



  • @Th: Ich kann das ganze Zeug, weiss nur nicht, wie ich das in mein Spiel implementieren soll 😕

    @Storm: Ja das stimmt allerdings!

    @elise: "==" stimmt :S



  • @FreeROM:

    @Th: Ich kann das ganze Zeug, weiss nur nicht, wie ich das in mein Spiel implementieren soll 😕

    So definierst du also "Können"?

    hier mal ein kurzer Tipp:

    int wert = 0;
    int karte;
    do
    {
      karte = rand()%9+2;
      wert += karte;
    }while(wert <= 21);
    

    Ein- und Ausgabe mußt du nur noch ergänzen...



  • hä?

    Ich sagte, ich kann Schleifen und co, aber weiss nicht, wie ich sie mit Blackjack in Verbindung setzen soll.

    Deine kleine Schleife funktioniert auch nur halb, weil da kommt 20 ma hintereinander z.B. 7 raus, wenn ich jetzt aber 2 Karten ziehen will, dann kommt ja immer z.B. immer 14, bzw. das Doppelte der Random-Zahlen raus, weils ja immer das gleiche ist?!?!



  • Was rechnest du denn, daß da immer das selbe rauskommen soll? rand() liefert bei jedem Aufruf eine neue Zufallszahl. Und Th's Konstruktion addiert solange Zufallswerte, bis sie über 21 kommt (die Ausgabe der Zwischenwerte und die Abfrage, ob der Spieler weitermachen will, mußt du da noch selber einbauen).



  • @CStoll: Wenn ich TH's kleines Teil berechnen lasse und 2 mal ausgeben lasse, dann kommt 20 mal die gleiche Zahl.

    Wenn ich jetzt also eine Random-Zahl haben will OK, aber wenn ich jetzt die 2te KArte ziehen will, dann kommt ja wieder die selbe Random Zahl heraus?!



  • ooo also mit diesem Spaghetticode solltest lieber nicht weitermachen.
    Das ist ja grauslig.



  • nach rand immer das gleiche ergebnis: wahrscheinlich initialisierung
    mit srand vergessen.

    ausserdem sollte man sowieso sicherstellen das nicht z.b. die kreuz sieben
    mehr als drei mal nacheinandergezogen wird: sonst falschspieler. 😉

    und wenn du deine kenntnisse von schleifenkonstrukten nicht beim
    blackjack zum einsatz bringen kannst, ist die aussage blackjack ist
    fast fertig sehr optimistisch.
    obwohl ich kann mich noch waage an relativ aktuelle quelltexte erinnern die
    noch mit unnötigen "goto"-anweisungen arbeiten.



  • Ich habe mal ein anderes Kartenspiel geschrieben. Vielleicht geben dir folgende Programmausschnitte einen Eindruck wie man das ganze objektorientiert lösen könnte.

    karte.h

    #ifndef __BLACKJACKKARTE_H__
    #define __BLACKJACKKARTE_H__
    
    #include <string>
    #include "farbe.h"
    #include "wert.h"
    
    namespace blackjack {
    
    static const int max_wert_ass = 11;
    
    class Karte {
    private:
        Farbe farbe;
        Wert wert;
    
    public:
        Karte()
                : farbe(PICK), wert(ASS) {}
    
        explicit Karte(Farbe f, Wert w)
                : farbe(f), wert(w) {}
    
        ~Karte() {}
    
        Farbe getFarbe() const {
            return farbe;
        }
    
        std::string getFarbeStr() const;
    
        Wert getWert() const {
            return wert;
        }
    
        std::string getWertStr() const;
    
        int minValue() const;
        int maxValue() const;
    };
    
    }
    
    #endif
    

    kartenstapel.h

    #ifndef __BLACKJACKKARTENSTAPEL_H__
    #define __BLACKJACKKARTENSTAPEL_H__
    
    #include <vector>
    #include "karte.h"
    
    static const int anzahlDecks = 6;
    static const int kartenProDeck = 52;
    static const int anzahlKarten = anzahlDecks * kartenProDeck; 
    
    namespace blackjack {
    
    class Kartenstapel{
    private:
    	typedef std::vector<Karte*> vec_type;
    	vec_type v;
    
    public:
        void initStapel();
    
        Karte* getKarte();
    
    };
    
    }
    
    #endif
    

    kartenstapel.cpp

    #include "kartenstapel.h"
    #include <algorithm>
    
    #include <iostream>
    using namespace std;
    
    using namespace blackjack;
    
    struct generator_ {
        int f, w;
    
        generator_()
                : f(blackjack::PICK), w(0) {}
    
        Karte* operator()() {
            if (w == blackjack::KOENIG) {
                w = blackjack::ASS;
    
                if (f == blackjack::KARO)
                    f = blackjack::PICK;
                else
                    ++f;
            } else
                ++w;
    
            return new Karte(static_cast<Farbe>(f)
                             , static_cast<Wert>(w));
        }
    };
    
    namespace blackjack {
    
    void Kartenstapel::initStapel() {
        vec_type::iterator i = v.begin();
        while (i != v.end())
            delete *i++;
    
        v.resize(anzahlKarten, 0);
        generate(v.begin(), v.end(), generator_());
    
    #ifdef _GLIBCPP_HAVE_DRAND48
    
        srand48(time(0));
    #else
    
        srand(time(0));
    #endif
    
        random_shuffle(v.begin(), v.end());
    }
    
    Karte* Kartenstapel::getKarte() {
        if (v.empty())
            return 0;
    
        Karte* tmp = v.back();
        v.pop_back();
        return tmp;
    }
    
    }
    


  • auf so etwas kompeliziertes wäre ich zwar nicht gekommen, ist aber richtig geil
    danke! 🙂



  • auf so etwas kompeliziertes wäre ich zwar nicht gekommen, ist aber richtig geil
    danke!

    Kompliziert finde ich das ganz und garnicht. Nimm mir das jetzt nicht böse 🙂 - aber unübersichtlicher und redundanter Quelltext ist viel umständlicher zu lesen. Aber mit der Zeit und mit guten Büchern eignet man sich schon einen guten Programmierstiel an 😉



  • ich habe M+T easy c++

    aber das mit dem oop will einfach net in meinen kopf 😕



  • "Die C++ Programmiersprache" von Bjarne Stroustrup ist das ideale Buch für Fortgeschrittene C++-Programmierer. Es wird sehr detailliert auf alle wichtigen Konzepte eingegangen.

    Ansonsten finde ich spezielle Bücher über objektorientierte Analyse und Design recht gut, da die Konzepte der OOP unabhängig von der Programmiersprache behandelt werden.

    Ich würde mich auch mit der UML beschäftigen, so erlernt man nicht nur eine Sprache mit der man die objektorientierten Gedanken ausdrücken kann, sondern vertieft auch die Kenntnisse der OOP.

    Von den C++-Büchern von Markt & Technik bin ich eher weniger begeistert.



  • den code würde ich ganz schnell vergessen aber mach dir kein zu großen kopf darüber so haben wir fast alle angefangen. Mich zumindest erinner das an mein erstes VB Programm welches symbolisch ableiten sollte und da ich dahmals noch net wusste wie man das richtig macht (Parser Baum) habe ich so eine ähnlichen Code geschrieben um eine Klammer hirachie reinzubekommen das war echt graussam 🙂


Anmelden zum Antworten