4 Gewinnt Need Help



  • Alo ich sitze hier an einem 4 Gewinnt Spiel, das ich programmiert habe.
    Mir fehlt noch die " Gewinn Prozedur" um festzustellen wer gewonnen hat.
    Ich habe ein zweidimensionales Array (Größe 7x7) verwendet (für Zeile und Spalte) und jenachdem wer einwirft, wird an die jeweilige Stelle eine 1 oder eine 2 geschrieben. Anhand dessen wollte ich festellen wer gewonnen hat.

    Vielleicht kann mir jemand dabei helfen und stellt mal so eine prozedur rein.
    Bitte relativ einfach.



  • Hallo

    wo liegt denn das Problem?
    Du weißt welcher Spieler an welcher Stelle ein Steins setzt. Von dieser Stelle aus zähltst du die gleichfarbigen Steine in alle Richtungen (mit Diagonalen). Ist eine Summer zweier gegenüberliegender Richtungen >= 4, hat der Spieler gewonnen.

    bis bald
    akari



  • genau. außerdem bietet es sich an, das spielfenld um ein kästchen größer zu machen rundherum, als es eigentlich gebraucht wird. dann braucht man die dem menschen übliche zählung 1,2,3,4,5,6,7 nicht immer runterzurechnen auf 0,1,2... und man kann den toten rand mit einer dritten farbe belegen. dann sind die schleifen hübsche, sie zählen einfach ausgehend vom zuletzt geworfenen stien in alle richtungen, bis sie auf nen andersfarbigen stein treffen.
    hab sogar ein wenig code übrig.

    #ifndef STELLUNG_H
    #define STELLUNG_H
    
    #include "Farbe.h"
    #include "Stack.h"
    #include "Wert.h"
    #include "Zug.h"
    #include "ZugReihenfolge.h"
    
    #include <string>
    
    class Stellung
    {
    private:
    	Farbe brett[9][8];//jeweils 2 mehr für den Todesrand
    	int hoehe[9];
       Zug history[42];
       int zugNummer;
    public:
       Stellung(std::string hist);
    	~Stellung();
    
       void sageAlleZuege(Stack<Zug,7> *zuege) const;
       void sageAlleZuege(Stack<Zug,7> *zuege,const ZugReihenfolge &zugreihenfolge) const;
       Zug sageZufallsZug() const;
       Zug sageErstmoeglichenZug() const;
    	Zug sageLetztenZug() const;
    	bool istGueltig(Zug zug) const;
       Farbe werIstDran() const;
       Farbe werWarDran() const;
       int getZugNummer() const;
    
       bool spielAnfang() const;
       bool spielEnde() const;
       Wert bewerte() const;//Aus Sicht dessen, der gerade dran ist
       Wert bewerte(Farbe farbe) const;//Aus Sicht von farbe
    	bool istUnentschieden() const;
    
       int pruefung(Farbe farbe,int x,int y,int deltax,int deltay) const;
       int pruefungNO(Farbe farbe,int x,int y) const;//NordOst
       int pruefungO(Farbe farbe,int x,int y) const;//Ost
       int pruefungSO(Farbe farbe,int x,int y) const;//SüdOst
       int pruefungS(Farbe farbe,int x,int y) const;//Süd
       int pruefungSW(Farbe farbe,int x,int y) const;//SüdWest
       int pruefungW(Farbe farbe,int x,int y) const;//West
       int pruefungNW(Farbe farbe,int x,int y) const;//NordWest
       Farbe at(int x,int y) const;
       int getHoehe(int x) const
       {
    	   return hoehe[x];
       }
    
    	void setzeZug(Zug zug);
       void loescheZug(Zug zug);
    
       std::string getHistory() const;
    	void anzeige() const;
    };
    
    #endif
    
    #include "Stellung.h"
    
    #include <assert.h>
    #include <iostream>
    using namespace std;
    
    Stellung::Stellung(string hist)
    {
       for(int x=0;x<9;++x)
       {
          hoehe[x]=1;
          for (int y=0;y<8;++y)
             brett[x][y]=KEINER;
       }
       zugNummer=0;
       for(int i=0;i<hist.size();++i)
          setzeZug(hist[i]-'0');
    }
    Stellung::~Stellung()
    {
    }
    int Stellung::pruefung(Farbe farbe,int x,int y,int deltax,int deltay) const
    {
       int anzahl=0;
       x+=deltax;
       y+=deltay;
       while(brett[x][y]==farbe)
       {
          ++anzahl;
          x+=deltax;
          y+=deltay;
       }
       return anzahl;
    }
    int Stellung::pruefungNO(Farbe farbe,int x,int y) const
    {
       return pruefung(farbe,x,y,1,1);
    }
    int Stellung::pruefungO(Farbe farbe,int x,int y) const
    {
       return pruefung(farbe,x,y,1,0);
    }
    int Stellung::pruefungSO(Farbe farbe,int x,int y) const
    {
       return pruefung(farbe,x,y,1,-1);
    }
    int Stellung::pruefungS(Farbe farbe,int x,int y) const
    {
       return pruefung(farbe,x,y,0,-1);
    }
    int Stellung::pruefungSW(Farbe farbe,int x,int y) const
    {
       return pruefung(farbe,x,y,-1,-1);
    }
    int Stellung::pruefungW(Farbe farbe,int x,int y) const
    {
       return pruefung(farbe,x,y,-1,0);
    }
    int Stellung::pruefungNW(Farbe farbe,int x,int y) const
    {
       return pruefung(farbe,x,y,-1,1);
    }
    
    void Stellung::setzeZug(Zug zug)
    {
       int x=zug;
       int y=hoehe[x];
       brett[x][y]=werIstDran();
       ++hoehe[x];
       history[zugNummer]=zug;
       ++zugNummer;
    }
    void Stellung::loescheZug(Zug zug)
    {
       int x=zug;
       --hoehe[x];
       int y=hoehe[x];
       brett[x][y]=KEINER;
       --zugNummer;
    }
    bool Stellung::istUnentschieden() const
    {
       return zugNummer==42;
    }
    bool Stellung::istGueltig(Zug zug) const
    {
       return 1<=zug && zug<=7 && hoehe[zug]<7;
    }
    Zug Stellung::sageLetztenZug() const
    {
       return history[zugNummer-1];;
    }
    void Stellung::anzeige() const
    {
       for (int y=6;y>=1;--y)
       {
          cout<<"|---+---|---|---|---|---|---|"<<endl;
          cout<<"| ";
          for(int x=1;x<=7;++x)
          {
             cout<<farbAnzeige(brett[x][y])<<" | ";
          }
          cout<<endl;
       }
       cout<<"|---+---|---|---|---|---|---|"<<endl;
       cout<<"| ";
       for(int x=1;x<=7;++x)
       {
          cout<<x<<" | ";
       }
       cout<<getHistory()<<endl<<endl;
    }
    string Stellung::getHistory() const
    {
       string result;
       for(int i=0;i<zugNummer;++i)
       {
          char ch='0'+history[i];
          result+=ch;
       }
       return result;
    }
    Farbe Stellung::at(int x,int y) const
    {
       return brett[x][y];
    }
    void Stellung::sageAlleZuege(Stack<Zug,7> *alleZuege) const
    {
       for(int x=7;x>=1;--x)
          if(hoehe[x]<=6)
             alleZuege->push(x);
    }
    void Stellung::sageAlleZuege(Stack<Zug,7> *alleZuege,const ZugReihenfolge &zugreihenfolge) const
    {
       for(int i=6;i>=0;--i)
          if(hoehe[zugreihenfolge.at(i)]<=6)
             alleZuege->push(zugreihenfolge.at(i));
    }
    Zug Stellung::sageErstmoeglichenZug() const
    {
       for(int x=7;x>=1;--x)
          if(hoehe[x]<=6)
             return x;
          return UNMOEGLICH;
    }
    Zug Stellung::sageZufallsZug() const
    {
       int anzahl=0;
    
       for(int x=1;x<=7;++x)
          if(hoehe[x]<=6)
             ++anzahl;
    
          int zufall=rand()%anzahl;
    
          anzahl=0;
          for(x=1;x<=7;++x)
          {
             if(hoehe[x]<=6)
             {
                if(anzahl==zufall)
                   return x;
                ++anzahl;
             }
          }
          return UNMOEGLICH;
    }
    Farbe Stellung::werIstDran() const
    {
       if(zugNummer%2==0)
          return WEISS;
       else
          return SCHWARZ;
    }
    Farbe Stellung::werWarDran() const
    {
       if(zugNummer%2==0)
          return SCHWARZ;
       else
          return WEISS;
    }
    int Stellung::getZugNummer() const
    {
       return zugNummer;
    }
    Wert Stellung::bewerte() const//Aus Sicht dessen, der gerade dran ist
    {
       if(istUnentschieden())
          return UNENTSCHIEDEN;
       int x=sageLetztenZug();
       int y=hoehe[x]-1;
       Farbe farbe=werWarDran();
       if(at(x,y)==farbe)
       {
          if(pruefungS(farbe,x,y)>=3) return verlorenBeiZug(zugNummer);
          if(pruefungW(farbe,x,y)+pruefungO(farbe,x,y)>=3) return verlorenBeiZug(zugNummer);
          if(pruefungSW(farbe,x,y)+pruefungNO(farbe,x,y)>=3) return verlorenBeiZug(zugNummer);
          if(pruefungNW(farbe,x,y)+pruefungSO(farbe,x,y)>=3) return verlorenBeiZug(zugNummer);
       }
       return UNBEKANNT;
    }
    Wert Stellung::bewerte(Farbe farbe) const
    {
       if(werIstDran()==farbe)
          return bewerte();
       else
          return -bewerte();
    }
    bool Stellung::spielAnfang() const
    {
       return zugNummer==0;
    }
    bool Stellung::spielEnde() const
    {
       if(zugNummer==0)
          return false;
       if(istUnentschieden())
          return true;
    
       int x=sageLetztenZug();
       int y=hoehe[x]-1;
       Farbe farbe=brett[x][y];
       if(pruefungS(farbe,x,y)>=3) return true;
       if(pruefungW(farbe,x,y)+pruefungO(farbe,x,y)>=3) return true;
       if(pruefungSW(farbe,x,y)+pruefungNO(farbe,x,y)>=3) return true;
       if(pruefungNW(farbe,x,y)+pruefungSO(farbe,x,y)>=3) return true;
       return false;
    }
    


  • Dieser Thread wurde von Moderator/in Jansen aus dem Forum VCL/CLX (Borland C++ Builder) in das Forum Rund um die Programmierung verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.


Anmelden zum Antworten