0

Java から来た C++ を学ぼうとしていますが、配列を渡すのが面倒です。メインにこのコードがあります:

int *newTry;
    bool won = false;
    while(!won && tries < maxTries){
        
        newTry = makeTry();

        printw("= %d =",newTry[3]); //seems to give me the 4th value okay...
        refresh();
        won = checkTry(newTry,correctSequence);
        printw("\n");

        tries++;
    }

これらは私を悩ませている機能です。

bool checkTry(int attempt[],int correct[])
{

    for(int i =0; i < columns; i++)
    {
        printw(" %d against %d\n", attempt[i],correct[i]); // not values from array!
        refresh();
        if (attempt[i] == correct[i])
        {
            refresh();
        }
   }
   return false;

}

Attempt[i] 呼び出しは、私が期待または望んでいるものとはかけ離れた奇妙な値を私に与えます。ある種のポインタ値か何かだと思いますが、どうすれば値を抽出できますか? ポインターなどをグーグルで検索しましたが、値が得られない理由がよくわかりません。

以下の完全なコード。

<pre>
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    #include <ncurses.h>


using namespace std;
int numberOfColors, columns, maxTries;


void SetupGame()
{
    //asks some questions about the game setup.
    printw("How many colours do you want? \n[1-7]");
    refresh();
    numberOfColors = getch()-'0';
    printw("and how many columns?\n");
    refresh();
    columns= getch()-'0';;
    refresh();
    printw("and how many tries do you get?\n");
    refresh();
    maxTries= getch()-'0';;
    refresh();
}
int randomBetween(int min, int max)
{
    int random_integer;
    random_integer =  min + (rand() % (int)(max - min + 1));
    return random_integer;

}
int* randomizeSequence(int colours, int colus)
{
    int randomSequence [colus-1];
    for(int i =0; i < colus; i++)
    {
        randomSequence[i] = randomBetween(0,colours);
        printw("%d",randomSequence[i]);
        refresh();
    }
    printw("\n");
    return randomSequence;
}


void repeatWrite(string str, int times)
{
    const char * c = str.c_str();
    for(int i =0; i<times; i++)
    {
        printw(c);
    }
}
void printStart()
{
    printw (" ");
    repeatWrite("--", columns);
    printw ("\n");
    refresh();
    printw ("| ");
    repeatWrite("¤ ",columns);
    printw("|\n");
    refresh();
    printw(" ");
    repeatWrite("--", columns);
    printw("\n");
    refresh();

}
void printIntColour(int integer)
{
    // TODO (kristoffer#1#): define all colours

    init_pair(1,COLOR_BLACK,COLOR_GREEN);
    attron(COLOR_PAIR(1));
    printw("%d",integer);
    attroff(COLOR_PAIR(1));
}

int* makeTry()
{
    int trySequence[columns-1];
    printw("| ");
    repeatWrite("  ",columns);
    printw("|\r| ");
    noecho();

    for(int i =0; i < columns; i++)
    {
        trySequence[i] = getch()-'0';
        while(trySequence[i] > numberOfColors)
        {
            trySequence[i] = getch()-'0';
        }
        printIntColour(trySequence[i]);
        printw(" ");

    }
    printw("|");
    refresh();
    echo();
    return trySequence;
}
bool checkTry(int attempt[],int correct[])
{

        for(int i =0; i < columns; i++)
        {
            printw(" %d against %d\n", attempt[i],correct[i]);
            refresh();
            if (attempt[i] == correct[i])
            {
                refresh();
            }
       }
       return false;
}


int main(void)
{
    initscr();          //initiate screen from ncurses
    start_color();      //color mode on!

    srand((unsigned)time(0)); // sett the clock time as seed for the random function.

    numberOfColors = 7;
    columns =5;
    maxTries =12;

    int yes =1;
    printw ("Use standard settings 1/0? : \n");
    refresh();

    noecho();
    yes = getch()-'0';
    echo();
    refresh();

    if(yes ==0)
    {
        SetupGame();
    }
    printw("You chose %d colours %d columns %d maxtries\n" ,numberOfColors,columns,maxTries);

    refresh();
    int play =1;
    while(play==1 )
    {
        int *correctSequence = randomizeSequence(numberOfColors,columns);
        int tries = 0;
        printStart();
        bool won = false;
        while(!won && tries < maxTries){
            int *newTry;
            newTry = makeTry();

            printw("= %d =",newTry[3]);
            refresh();
            won = checkTry(newTry,correctSequence);
            printw("\n");

            tries++;
        }
        refresh();
        printw("\nplay again? 1/0\n");
        getch();
    }
    endwin();
    return 0;
}
4

1 に答える 1

0

makeTry または randomizeSequence で配列を返したくありません。代わりに、次の方法でヒープに割り当てます。

int *trySequence = new int[columns - 1];
int *randomSequence = new int[colus - 1];

その後、通常の配列として扱うことができます。一般に、配列を返すことはなく、常にポインターを返します。配列はスタックから割り当て解除されます。これは、一般に、配列の古いメモリ空間が再び使用されるまで機能することを意味します (次の関数呼び出しで可能性が高い)。コード内の他の場所で配列を返す場合は、それも修正してください。

また、1を引く必要があるかどうかも正確にはわかりません。配列のサイズは正確に列になります-最初のケースでは1ですが、サイズを列の量と同じにしたい場合は、1を引く必要はありません(配列の最後の要素は依然としてこの場合、columns-1)。そのため、必要に応じて配列のサイズと for ループを調整してそれらにアクセスします (コンパイラは通常、配列へのアクセスに使用される値が範囲内にあるかどうかを確認しません)。

無関係な修正として、while ループのコードの最後の行を次のように変更します。

play = getch() - '0';

それ以外の場合と同様に、while ループは単なる無限ループです。最後の注意: すべての入力をチェックして、ユーザーがばかげたことを入力していないことを確認することをお勧めします。

于 2013-03-15T15:19:42.857 に答える