双面2D阵列打印

Reversi 2D array print

本文关键字:打印 阵列 2D 双面      更新时间:2023-10-16

(这是家庭作业)我正在尝试打印一个带有行和列标题的Reversi板。我的问题是,为什么没有打印句号,而是打印"B"answers"W"。有什么建议吗?

#include <iostream>
#include <cstdlib> 
using namespace std;
const int BOARD_SIZE = 8, MOVE_SYMBOL = 3;
void PrintBoard(char board[8][8]);
int main(int argc, char* argv[]) {
   const int BOARD_SIZE = 8;
   char board[BOARD_SIZE][BOARD_SIZE] =
 {
  { 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 1, -1, 0, 0, 0 },
  { 0, 0, 0, -1, 1, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0 },
  { 0, 0, 0, 0, 0, 0, 0, 0 }
 };
PrintBoard(board);
}
// Prints current state of Reversi Board 15L
void PrintBoard(char board[BOARD_SIZE][BOARD_SIZE]) { 
char output[MOVE_SYMBOL] = { 'W', ' . ', 'B' };
cout << "n- 0  1  2  3  4  5  6  7";
for (int i = 0; i < BOARD_SIZE; i++) {
  cout << "n" << i;
  for (int j = 0; j < BOARD_SIZE; j++) {
     cout << output[board[i][j] + 1];
   }
}

输出:

- 0  1  2  3  4  5  6  7
0
1
2
3   BW
4   WB
5
6
7        Press any key to continue . . .

这是因为witespace会影响你的经期。一个字符是一个单独的位置,所以'.'而不是' . '