如何在2D阵列中存储数据并使用2个循环显示2D

时间:2012-07-24 13:59:18

标签: c++

我试图画一个拼字游戏。你可以参考附件图片,这是我的电路板设计。我的程序会要求用户输入行和列并插入单词。示例:ROW = A列= 5。但我不知道如何为每个变量声明变量。请帮助。感谢。

http://img824.imageshack.us/img824/516/capturecba.jpg

以下是我的代码:

void displayBoard()

{     int a = 0;     static string alpha [] = {“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”,“10”,“11” ,“12”,“13”,“14”,“15”,“”};

cout <<"   A B C D E F G H I J K L M N O P"<<endl;

for (int j=0; j<=14; j++)
{
    cout << alpha[a];
    for ( int i = 0; i <= 15; i++)
    {
        cout << "|_";
    }
    cout <<"|";
    cout <<endl;
    a++;
}

}


感谢您的帮助。我不太明白这一点。

for(i=0;i<word_length;i++)
{
board[word_i_offset][word_j_offset+i]=temporary_word_input[i];
}

顺便说一句,我尝试重新做整个并停止插入。

void displayBoard()
{
 int i=15;
 int j=15;
 int a=0;
   static string alpha[] = {"1 ","2 ","3 ","4 ","5 ","6 ","7 ","8 ","9    ","10","11","12","13","14","15"," "};

  cout <<"   A B C D E F G H I J K L M N O"<<endl;


  char board[i][j];

for(int k=0; k<j; k++)
{
    cout<<alpha[a];
    for(int k=0; k<j; k++)
    {
       board[i][j]=' ';
       cout<<"|_";
    }
    cout<<"|";
    cout<<endl;
    a++;
}
cout<<endl;

 }

 int main()
  {
  int i=15;
  int j=15;
   int l=0;
  char board[i][j];
  int words,column;
  char answer,rows;

  displayBoard();

  cout<<endl;
  cout<<"Enter how many words : ";
  cin>>words;
  cout<<endl;
  cout<<"Insert on which rows : ";
  cin>>i;
  cout<<endl;
  cout<<"Insert on which column : ";
  cin>>j;
  cout<<endl;
  cout<<"Insert what words : ";
  cin>>answer;
  cout<<endl;

  for(l=0;l<words;l++)
  {
  board[i][j]=answer[];
   }

  return 0;
   }

1 个答案:

答案 0 :(得分:0)

  char board[imax][jmax]; //declaration

初​​始化:

for(int i=0;i<imax;i++)
{
    for(int j=0;j<jmax;j++)
    {
        board[imax][jmax]=' ';
    }

}

打印:

 for(int i=0;i<imax;i++)
{
    for(int j=0;j<jmax;j++)
    {
        printf("%c",board[imax][jmax]);
    }
    printf("\n");//newline
}

在其中加入词语:

first check the bounds that word fits within the board's borders

check the horizontal borders and vertical borders, and check word length versus board length.

然后:

for(i=0;i<word_length;i++)
{
    board[word_i_offset][word_j_offset+i]=temporary_word_input[i];
}