Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

c++ - Why is my array initalized with 0 on every position?

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) << "


";
            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 << "
" << "--+---+---+---+---+---+---+---+---+--" << "
";
            }
        }
};
question from:https://stackoverflow.com/questions/66056305/why-is-my-array-initalized-with-0-on-every-position

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...