0

So i am a total beginner. Basically my goal is it to print out a board with the opening position of a checkers game, but i can only draw a blank board without any pieces on it. I don't understand why because i declared it should only do this when the array position equals white (-1). But if try to do a test and print out any random position, every position equals 0. So at this point i feel very lost because the code i wrote makes total sense to me.

//main.cpp #include <iostream> #include <array> #include "board.hpp" int main() { board Checkers; Checkers.printBoard(); } //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) << "\n\n\n"; std::cout << "(TEST) on (black) 3.0::: " << boardArr.at(3).at(0) << "\n\n\n"; std::cout << "(TEST) on (O) 0.1::: " << boardArr.at(0).at(1) << "\n\n\n"; std::cout << "(TEST) on (X) 7.0::: " << boardArr.at(7).at(0) << "\n\n\n"; */ std::cout << " | A | B | C | D | E | F | G | H |" << "\n" << "--+---+---+---+---+---+---+---+---+--" << "\n"; 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 << "\n" << "--+---+---+---+---+---+---+---+---+--" << "\n"; } } }; 
4
  • 1
    = is the assignment operator . Use == to do equality comparison Commented Feb 5, 2021 at 1:25
  • Most compilers will warn you about this, if you enable warnings (which you should!) Commented Feb 5, 2021 at 1:28
  • Tip: Learn about enum. Commented Feb 5, 2021 at 1:29
  • It's also worth noting that the layout of a checkers board is unchanging so that the black/white or red/black pattern can be reproduced in the display logic based on simple modulo math like you've already done. The pieces themselves do need to be tracked, but you can do that with a simple player enum. The problem with this approach is each time you move a piece you need to re-figure-out what "color" the square is, which is a complete waste of time. Commented Feb 5, 2021 at 1:31

1 Answer 1

1

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) << "\n\n\n"; std::cout << "(TEST) on (black) 3.0::: " << boardArr.at(3).at(0) << "\n\n\n"; std::cout << "(TEST) on (O) 0.1::: " << boardArr.at(0).at(1) << "\n\n\n"; std::cout << "(TEST) on (X) 7.0::: " << boardArr.at(7).at(0) << "\n\n\n"; */ std::cout << " | A | B | C | D | E | F | G | H |" << "\n" << "--+---+---+---+---+---+---+---+---+--" << "\n"; 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 << "\n" << "--+---+---+---+---+---+---+---+---+--" << "\n"; } } }; int main() { board Checkers; Checkers.printBoard(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.