0

「Mastermind」というゲームを支援するプログラムを作ろうとしています。このゲームに慣れていない場合は、単純に次のようになります。推測する必要があるコードがあります。1 人のプレイヤーがこのコードを作成し、別のプレイヤーが推測を試みます。推測に基づいて、プレイヤーはそれぞれに対して黒いペグを取得します。彼が推測した文字は正しい場所にあり、各文字の白いペグは間違った場所にあり、パターンの別の場所に属し、文字がまったく発生しない場合は最終的にペグはありません.

私のプログラムは、推測、白いペグの数、黒いペグの数を取り、すべての可能な解を出力することになっています。さらに、要件の 1 つは、プログラムが再帰を使用する必要があることです。

プログラムの実行方法のサンプル:

パターンの長さを入力してください: 3

推測パターンを入力してください: abc

フィードバックに黒いペグの数を入力してください: 2

フィードバックに白いペグの数を入力してください: 0

可能なキー パターンは次のとおりです。

aac

あば

アブ

アブド

阿部

abf

ACC

adc

aec

afc

BBC

CBC

dbc

ebc

fbc

私が書いたコードを実行するとわかるように、私のプログラムは明らかに同じ出力を与えません。(ちなみに私も入力されたchar配列をint配列に変換し、内容をcharとして出力しました)

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>


    void userEnter(int*pattern, int n);
    void print( int * s, int n);
    void recurs( int * s, int * a, int n, int wpegs, int bpegs);
    bool Done (int*s);
    bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n);
    bool wPegs(int* modcom, int* modoriginal, int*s, int wpegs, int w);
    void change(int*modoriginal, int*modcom, int i, int k, int w);

    int main(void)
    {
        int i, n, bpegs, wpegs;

        printf("Enter the pattern length: ");
        scanf("%d",&n);
        int *a = malloc(n * sizeof *a);
        printf("Input the guess pattern: ");
        int pattern[n];
        userEnter(pattern, n);  
        printf("Enter the number of black pegs in the feedback: ");
        scanf("%d",&bpegs);
        printf("Enter the number of white pegs in the feedback: ");
        scanf("%d",&wpegs);
        printf("The possible key patterns are: ");
        for(i=0; i<=n-1; i++)
        {
            a[i]=0;
        }
        print(a, n);
        recurs(a, pattern, n, wpegs, bpegs);

    }

    void userEnter(int*pattern, int n)
    {
        char input[n];
        scanf("%s",input);

        int i;
        for(i = 0; i < n-1; i++)
        {
            pattern[i] = input[i]-65;
        }
    }

    void print( int * s, int n)
    {
        int i; 
        printf( "\n" );
        for( i = n-1; i >= 0; i-- )
        {
            printf( "%c", ( s[ i ] + 65 ) );
        }
    }

    void recurs( int * s, int * a, int n, int wpegs, int bpegs)
    {

        int i;


        if(Done(s))
        {
            print( s, n);
            printf( "\nAccomplisshed!\n" );
        }

        else{
            s[ 0 ] += 1;
            for( i = 0; i < n-1; i++ )
            {
                if( s[ i ] == 6 ){
                    s[ i ] = 0;
                    s[ i + 1 ] += 1;
                }
            }
            if(bPegs(a ,s, bpegs, wpegs, n))
            {
            print( s, n);
            }
            recurs(s, a, n, wpegs, bpegs);
        }
    }

    bool Done (int*s)
        {
            int i;
            bool done=true;
            for (i=0;i<=11;i++)
            {
                if(s[i]!=5)
                {
                    done=false;
                }
            }
            return done;
        }


    bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n)
    {
        int i,j,c=0;
        bool d = false;
        for(i=0; i<n-1; i++)
        {
            if(a[i]=s[i])
            {
                c++;
            }
        }
        int x =n-c;
        int* modcom; 
        int*modoriginal;
        modcom=(int*)malloc((x)*(sizeof(int)));
        modoriginal=(int*)malloc((x)*(sizeof(int)));
        int w=0;
        for(j=0; j<n-1; j++)
        {
            if(a[j]!=s[j])
            {
                modcom[w]=s[j];
                modoriginal[w]=a[j];
                w++;
            }       
        }
        if(c=bpegs)
        {
            d = wPegs(modcom, modoriginal, s, wpegs, w);
        }

        return d;

    }

    bool wPegs(int*modcom, int*modoriginal, int*s, int wpegs, int w)
    {
        int i, k, count=0;
        for(i=0; i<=w; i++)
        {
            for(k=0; k<=w; k++)
            {
                if (modoriginal[i]==modcom[k])
                {
                    count++;
                    change(modoriginal, modcom, i, k, w);
                }
            }
        }
        if(wpegs=count)
        {
            return true;
        }

    }

    void change(int*modoriginal, int*modcom, int i, int k, int w)
    {
        int c, o;
        for(c=i-1; c<w-1; c++)
        {
            modoriginal[c]=modoriginal[c+1];
        }
        for(o=k-1;o<w-1;o++)
        {
            modcom[o]=modcom[o+1];
        }
    }
4

1 に答える 1

3

&あなたはあなたのscanfに欠けています:

int bpegs, wpegs;
...
scanf("%d",bpegs);
scanf("%d",wpegs);
于 2012-11-16T10:36:07.547 に答える