并排打印两个2D阵列

时间:2015-01-04 00:42:22

标签: c console printf multidimensional-array

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

此部分内容 - 以及我此刻遇到问题的部分 - 在每个回合都向玩家显示信息。

这就是我们要求显示的内容:

This is what we've been asked to have our display look like

而且,最小的麻烦,我看起来像这样:

And, with a minimum of fuss, I've got mine looking like this

(我的Player网格目前是空的,因为它的2D数组是空的,但如果我使用包含某些内容的数组,那就很好了。)

我的标题文件:

/* 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阵列上,但现在我和#39;我只想让计算机的一半显示工作。

2 个答案:

答案 0 :(得分:2)

在打印东西的第二个循环之后,只需将它添加到您的代码中:

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]);
   }
   //...

答案 1 :(得分:1)

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

   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]);
      }

      // ...
    }

应该这样做。您打印出全宽行分隔符,但不打印创建单元格分隔符的数组部分