C++ 我正在尝试创建一个c++;蛇的游戏,但我似乎不能画第二个高度边界,我如何解决这个问题?

C++ 我正在尝试创建一个c++;蛇的游戏,但我似乎不能画第二个高度边界,我如何解决这个问题?,c++,C++,我不知道如何打印第二个高度边框,如何实现这一点?您的voidsetup()函数应该如下图所示。同时打印左右两侧的边界。迭代高度20次,然后在每次迭代中迭代每列(即宽度),当列值为1和19时,即第一个和最后一个,然后只打印“#” void setup(){ 内部高度=20,宽度=20; 对于(int x=1;x

我不知道如何打印第二个高度边框,如何实现这一点?

您的void
setup()
函数应该如下图所示。同时打印左右两侧的边界。迭代高度20次,然后在每次迭代中迭代每列(即宽度),当列值为1和19时,即第一个和最后一个,然后只打印“#”

void setup(){
内部高度=20,宽度=20;
对于(int x=1;x难道你需要考虑成行打印吗

void setup() {
int height = 20, width = 20;
for (int x = 1; x < width; x++) {
    cout << "#";
}
cout<<endl;
for (int x = 1; x < height; x++) {
    for(int i=1;i<width;i++){
        if(i==19 || i==1){
            cout<<"#";
        }
        else{
            cout<<" ";
        }
    }
    cout<<endl;
}
   for (int x = 1; x < width; x++) {
       cout << "#";
   }
}
void setup()
{
内部高度=20,宽度=20;
//打印顶行
如前所述,考虑每行的内容会有所帮助

我们正在尝试创建一个NxN网格。NxN网格由N行组成,其中每行包含N个元素,每个元素可以是#或空白

第一行和最后一行完全由#组成,在它们之间的每一行的开始和结束处都有一个#

这个网格的3x3版本如下所示。我使用了+符号而不是空格字符,这样更容易理解

void setup()
{
    int height = 20, width = 20;

    // print top row
    std::cout << std::string(width, '#') << '\n';

    // print middle rows
    for (int i = 0; i < height - 2; ++i)
         std::cout << '#' << std::string(width - 2, ' ') << "#\n";

    // print bottom row
    std::cout << std::string(width, '#') << '\n';

    std::cout << std::flush;
}
4x4网格如下所示:

###
#+#
###
####
#++#
#++#
####
5x5网格如下所示:

###
#+#
###
####
#++#
#++#
####
在第一行中,我们对每个元素使用#

#####
#+++#
#+++#
#+++#
#####
最后,我们创建与第一行相同的最后一行

for (int c = 0; c < height - 2; ++c)
{
  std::cout << '#';

  for (int i = 0; i < width - 2; ++i)
  {
    std::cout << ' ';
  }

  std::cout << '#' << std::endl;
}
for(int i=0;istd::我不建议在内存中构建2D数组。将棋盘和游戏中的项目写入该内存,然后将该内存写入屏幕
for (int c = 0; c < height - 2; ++c)
{
  std::cout << '#';

  for (int i = 0; i < width - 2; ++i)
  {
    std::cout << ' ';
  }

  std::cout << '#' << std::endl;
}
for (int i = 0; i < width; ++i)
{
  std::cout << '#';
}