MM Spiel



  • Danke Wutz



  • akito schrieb:

    , wie integrierte Funktionen. Sollte sie auch nicht benutzen, sondern das ganze eher mit den Basics umsetzen.

    In Standard-C gibt es keine integrierten Funktionen sondern nur Funktionen, und Funktionen sind zum Benutzen da.

    /* solche nichtsignifikanten Namen vergibt man nicht, schon gar nicht per define */
    #define MIN 'a'
    #define MAX 'f'
    #define LENGTH 4
    
    void creatModel ( char*,int,char,char );
    //void outputModel ( char*,int );
    void inputModel ( char*,int );
    int outputRating ( const char*,const char*,int );
    
    int main()
    {
        char secret[LENGTH], guessed[LENGTH];
        int ready = 0, attempts = 0;
        srand( time(NULL) );
        printf( "MASTERMIND\n\n" );
    
        creatModel( secret,LENGTH,MIN,MAX );
        //outputModel( secret, LENGTH );
    
        do
        {
            attempts++;
    
            printf( "\n%2d Try: ",attempts );
            inputModel( guessed,LENGTH );
            //outputModel( guessed,LENGTH );
    
            ready = outputRating( secret, guessed, LENGTH );
    
        }while( !ready );
    
        printf( "%2d attempts!\n", attempts );
    
        return 0;
    
    }
    
    void creatModel( char* secret,int length,char min,char max )
    {
        int i;
    
        for( i=0;i<length;i++ )
        {
            secret[i] = MIN + rand()%(MAX-MIN+1);
            putchar( secret[i] );
        }
    }
    
    void inputModel ( char* guessed,int length )
    {
        char s[10];
        sprintf(s,"%%%dc",length); /* "%4c" -> 4 Zeichen aber keinen String einlesen */
        scanf(s,guessed); while(getchar()!='\n'); /* standardkonform stdin leeren */
    }
    
    int outputRating( const char* secret,const char* guessed,int length )
    {
        int i;
        int j;
        int a;
        for( i=0;i<length;i++ )
        {
            if( secret[i]==guessed[i] )
            {
                printf( "s" );
            }
        }
    
        for( i=0;i<length;i++ )
        {
            for( j=0;j<length;j++ )
            {
                if( (secret[j]==guessed[i]) && (secret[j]!=guessed[j]) )
                printf( "w" );
                break;
            }
        }
    
        printf("\n");
    
        for( i=0;i<length;i++ )
        {
            if( secret[i]==guessed[i] )
            {
                a=1;
            }
            else
            {
                return 0;
            }
        }
        return a;
    }
    

Anmelden zum Antworten