C++ 8 By 8 Board

C++ 8 By 8 Board

本文关键字:Board By C++      更新时间:2023-10-16

我试图实现n皇后问题棋盘游戏,我有问题与棋盘我在这个displayboard函数中做错了什么?它应该实现一个8 × 8的空板对不起,我只是一个初学者

#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;
 }
if (board[bRow][bCol] == 0)
      cout << " ";
else 
      cout << " ";

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

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

  1 2 3 4 5 6 7 8
  ---------------
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 
  . . . . . . . . 

代码
#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;
}