并排打印两个二维数组

Printing two 2D arrays side by side

我刚开始用 C 编写一个单元,我的第一个任务是制作经典战舰游戏的简单控制台版本。

其中的一部分——也是我目前遇到麻烦的部分——是在每个回合向玩家显示信息。

这就是我们被要求的显示效果:

并且,经过最少的大惊小怪,我的看起来像这样:

(我的 Player 网格目前是空的,因为它的二维数组是空的,但如果我使用一个包含一些东西的数组,就没问题。)

我的头文件:

/* Header files. */
#include <stdio.h>
#include <stdlib.h>

#define UNKNOWN ' '

#define SIZE 10

/* Function prototypes. */
void init(char playerHidden[SIZE][SIZE], char playerReveal[SIZE][SIZE],
      char computHidden[SIZE][SIZE], char computReveal[SIZE][SIZE]);
void displayKnownInfo(char playerReveal[SIZE][SIZE],
                  char playerHidden[SIZE][SIZE],
                  char computReveal[SIZE][SIZE]);

我的 C 源文件:

#include "bship.h"

int main(void)
{
   /* Stores player ship position information secret to opponent. */
   char playerHidden[SIZE][SIZE];
   /* Stores player ship position information known to opponent. */
   char playerReveal[SIZE][SIZE];
   /* Stores computer ship position information secret to opponent. */
   char computHidden[SIZE][SIZE];
   /* Stores computer ship position information known to opponent. */
   char computReveal[SIZE][SIZE];

   init(playerHidden, playerReveal,
        computHidden, computReveal);


   displayKnownInfo(playerReveal, playerHidden, computReveal);

   return EXIT_SUCCESS;
}


/****************************************************************************
* Function init() initialises every cell in the four grids to a safe default
* value. The UNKNOWN constant is used for the initialisation value.
****************************************************************************/
void init(char playerHidden[SIZE][SIZE], char playerReveal[SIZE][SIZE],
          char computHidden[SIZE][SIZE], char computReveal[SIZE][SIZE])
{
   /*Variables i and j for each dimension of the Arrays*/
   int x,y;

   /*For each increment BETWEEN 0 and 'SIZE', firstly for i;*/
   for(y=0; y<SIZE; y++)
   {
      /*And then for j;*/
      for(x=0; x<SIZE; x++)
      {
         /*Populate that cell with the constant UNKNOWN*/
         playerHidden[x][y]=UNKNOWN;

         playerReveal[x][y]=UNKNOWN;

         computHidden[x][y]=UNKNOWN;

         computReveal[x][y]=UNKNOWN;

      }

   }

}




/****************************************************************************
* Function displayKnownInfo() presents revealed information about the game in
* the format below. In this example, both contestants have made five
* guesses.
* As you can see, the computer player got lucky with all five guesses and has
* sunk the human players' aircraft carrier. The identity of the ship was
* revealed when the aircraft carrier was HIT the fifth time.
* The human player has been less lucky. The first four guesses were a MISS.
* However, the fifth guess was a HIT on the computer players' submarine. The
* human player does not yet know the identity of this ship yet as it is still
* afloat.
* All other squares are still UNKNOWN.
*
*          Player         |         Computer
*    1 2 3 4 5 6 7 8 9 0  |    1 2 3 4 5 6 7 8 9 0
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* a |A| | | | | | | | | | | a | | | | | | | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* b |A| | | | | | | | | | | b | | | | | | | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* c |A| | | | | | | | | | | c | | | | | | |=| | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* d |A| | | | | | | | | | | d | | |x| | | | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* e |A| | | | | | | | | | | e | | | | | | | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* f | | | | | | | | | | | | f | | | | |=| | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* g | | | | | | | | | | | | g | | | | | | | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* h | | | | | | | | | | | | h | |=| | | | |=| | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* i | | | | | | | | | | | | i | | | | | | | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* j | | | | | | | | | | | | j | | | | | | | | | | |
*   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+
* Aircraft Carrier  (5/5) | 0/5 ships sunk.
* Battleship        (0/4) | 1 hits.
* Destroyer         (0/3) | 4 misses.
* Frigate           (0/3) |
* Submarine         (0/2) |
****************************************************************************/
void displayKnownInfo(char playerReveal[SIZE][SIZE],
                      char playerHidden[SIZE][SIZE],
                      char computReveal[SIZE][SIZE])
{
   /*Ints for stepping through the arrays*/
   int i,j;

   /*First row identifier*/
   char row='a';

   /*Printing first few lines.*/
   printf("          Player         |         Computer\n");
   printf("    1 2 3 4 5 6 7 8 9 0  |    1 2 3 4 5 6 7 8 9 0\n");
   printf("   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+\n");
   printf(" %c |", row);

   /*Loop through the arrays*/
   for(i=0; i<SIZE; i++)
   {
      for(j=0; j<SIZE; j++)
      {
         /*Print the char at [i][j] with a '|' afterwards.*/
         printf("%c|",playerReveal[i][j]);
      }

      /*After reaching column '0' on the display, increment the row identifier*/
      row++;

      /*And print the 'spacer' row.*/
      printf("\n   +-+-+-+-+-+-+-+-+-+-+ |   +-+-+-+-+-+-+-+-+-+-+");

      /*If the current row identifier is less than 'k', we want to print it*/
      if(row<'k')
      {
         printf("\n %c |",row);
      }
      else
      {
         printf("\n");
      }

      /*And continue until the array has been printed in full.*/
   }

}

}

最终,PlayerHidden 数组将覆盖在 PlayerReveal 数组上,当玩家的船被击沉且其身份 'revealed' 时,但现在我只想让电脑显示器的一半工作。

只需在打印内容的第二个循环之后将其添加到您的代码中即可:

printf("   %c |", row);

for(j=0; j<SIZE; j++)
{
   /*Print the char at [i][j] with a '|' afterwards.*/
   printf("%c|",computReveal[i][j]);
}

因此您的代码如下所示:

//...
/*Loop through the arrays*/
for(i=0; i<SIZE; i++)
{
   for(j=0; j<SIZE; j++)
   {
      /*Print the char at [i][j] with a '|' afterwards.*/
      printf("%c|",playerReveal[i][j]);
   }
   printf("   %c |", row);
   for(j=0; j<SIZE; j++)
   {
      /*Print the char at [i][j] with a '|' afterwards.*/
      printf("%c|",computReveal[i][j]);
   }
   //...

您的问题是您只打印了播放器部分。看一下代码:

   for(i=0; i<SIZE; i++)
   {
      for(j=0; j<SIZE; j++)
      {
         /*Print the char at [i][j] with a '|' afterwards.*/
         printf("%c|",playerReveal[i][j]);
      }

      // ...
    }

您需要有另一个循环来打印 computerReveal 数组

   for(i=0; i<SIZE; i++)
   {
      for(j=0; j<SIZE; j++)
      {
         /*Print the char at [i][j] with a '|' afterwards.*/
         printf("%c|",playerReveal[i][j]);
      }

      for(j=0; j<SIZE; j++)
      {
         /*Print the char at [i][j] with a '|' afterwards.*/
         printf("%c|",computReveal[i][j]);
      }

      // ...
    }

应该可以了。您正在打印全宽行分隔符,而不是创建单元格分隔符的数组部分