我正在尝试学习来自 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;
}
尝试[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;
}