I think @M.M already pointed the issue. The problem was in your checking the status of board cell (i.e., boardArr.at(i).at(j)
) in your printBoard()
function. Here, I am adding the updated code, please have a look and compare it with your one:
//main.cpp
#include <iostream>
#include <array>
//#include "board.hpp"
//board.cpp
class board
{
private:
std::array<std::array<int, 8>, 8> boardArr;
int const white = -1;
int const black = 0;
int const player1 = 1;
int const player2 = 2;
public:
board()
{
for( int i = 0; i < 8; i++ )
{
for( int j = 0; j < 8; j++ )
{
if( ((i + j) % 2) == 1 )
{
boardArr.at(i).at(j) = black;
}
else
{
boardArr.at(i).at(j) = white;
}
}
}
for( int i = 0; i < 3; i++ )
{
for( int j = 0; j < 8; j++)
{
if( boardArr.at(i).at(j) == black )
{
boardArr.at(i).at(j) = player2;
}
}
}
for( int i = 5; i < 8; i++ )
{
for( int j = 0; j < 8; j++)
{
if( boardArr.at(i).at(j) == black )
{
boardArr.at(i).at(j) = player1;
}
}
}
}
void printBoard()
{
/*
std::cout << "(TEST) on (white) 0.0::: " << boardArr.at(0).at(0) << "
";
std::cout << "(TEST) on (black) 3.0::: " << boardArr.at(3).at(0) << "
";
std::cout << "(TEST) on (O) 0.1::: " << boardArr.at(0).at(1) << "
";
std::cout << "(TEST) on (X) 7.0::: " << boardArr.at(7).at(0) << "
";
*/
std::cout << " | A | B | C | D | E | F | G | H |" << "
"
<< "--+---+---+---+---+---+---+---+---+--" << "
";
for( int i = 0; i < 8; i++ )
{
std::cout << -i + 8 << " |";
for( int j = 0; j < 8; j++ )
{
if( boardArr.at(i).at(j) == white )
{
std::cout << " |";
}
else if( boardArr.at(i).at(j) == black )
{
std::cout << "###|";
}
else if( boardArr.at(i).at(j) == player2 )
{
std::cout << " O |";
}
else if( boardArr.at(i).at(j) == player1 )
{
std::cout << " X |";
}
}
std::cout << "
" << "--+---+---+---+---+---+---+---+---+--" << "
";
}
}
};
int main()
{
board Checkers;
Checkers.printBoard();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…