ya-ivanov
ya-ivanov

Reputation: 516

C++ 2D Array Ping Pong Board

I can't find my mistake is my code. I'm trying to create a "board" and draw something on it with X and Y as coordinates. The problem is that the char I want to draw appears more than once. Here is the code:

class Board
{
public:

    void fillBoard()
    {
        for (int i = 0; i < 22; i++)
        {
            for (int k = 0; k < 78; k++)
            {
                board[i][k] = ' ';
            }
        }
    }

    void drawBoard()
    {
        for (int i = 0; i < 22; i++)
        {
            for (int k = 0; k < 78; k++)
            {
                cout<<board[i][k];
            }
            cout<<"\n";
        }
    }

    void addView(int width, int height, char img, int tallness = 0)
    {
        /// board[height][width] = img;
        for (int i = 0; i <= tallness; i++, height++)
        {
            board[height][width] = img;
        }
    }

private:
   char board[78][22];
};


int main()
{
    Board board;
    board.fillBoard();

    ///board.addView(x, y, 'O');
    board.addView(0, 10, '|', 5); 
    board.drawBoard();

    return 0;
}

So as you see, the paddle appears 4 times. I want it to appear only once, at the given coordinates. What is my mistake?

Upvotes: 0

Views: 340

Answers (1)

yizzlez
yizzlez

Reputation: 8805

Steve Cox already pointed out that you are indexing the array backwards. This:

for (int i = 0; i < 22; i++)
{
    for (int k = 0; k < 78; k++)
    {
        board[i][k] = ' ';
    }
}

Would work on a array board[22][78], however you are declaring a board[78][22]

Upvotes: 3

Related Questions