首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ 8 x 8电路板

C++ 8 x 8电路板
EN

Stack Overflow用户
提问于 2011-12-16 15:55:49
回答 2查看 2.2K关注 0票数 0

我正在尝试实现n皇后问题棋盘游戏,我在棋盘上遇到了问题,那么我在这个displayboard函数中做错了什么?假设实现一个8 x 8的空白板对不起,我只是个初学者

代码语言:javascript
复制
#include <iostream>
#include <limits>

using namespace std;

const int rows = 8;
const int columns =8;

int board[rows][columns] = {0,0};

void displayboard();

int main(){

displayboard();

system("pause");

}
void displayboard ()
{

cout << "  1 2 3 4 5 6 7 8" << endl;
cout << "  ---------------";

for (int bRow = 0; bRow<rows; bRow++)
 {
 for (int bCol = 0; bCol<columns; bCol++)

  if (board[bRow][bCol] == 0)
         cout << " ";
  else 
      cout << " ";
  } 
 cout << endl;

  return;
 }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-16 16:01:11

代码语言:javascript
复制
if (board[bRow][bCol] == 0)
      cout << " ";
else 
      cout << " ";

??两者做的都是一样的事情!打印空白区域。此外,除了0之外,您还没有填充数组board[8][8]

票数 3
EN

Stack Overflow用户

发布于 2011-12-16 16:11:20

您错过了换行符和可能的每行空格。这是一个固定的版本:(我使用了一个‘’表示一个(空)字段-因为它对人工调试更友好)

代码语言:javascript
复制
  1 2 3 4 5 6 7 8
  ---------------
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 

代码

代码语言:javascript
复制
#include <iostream>
#include <limits>

using namespace std;

const int rows = 8;
const int columns =8;

int board[rows][columns] = {0,0};

void displayboard();

int main()
{
    displayboard();
}

void displayboard ()
{
    cout << "  1 2 3 4 5 6 7 8" << endl;
    cout << "  ---------------";

    for (int bRow = 0; bRow<rows; bRow++)
    {
        cout << "\n  ";
        for (int bCol = 0; bCol<columns; bCol++)
        {
            if (board[bRow][bCol] == 0)
            {
                cout << ".";
            }
            else
            {
                cout << ".";
            }
            cout << " "; // delimiter
        }
    }
    cout << endl;

    return;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8531318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档