Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 758147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:26:42+00:00 2026-05-14T15:26:42+00:00

I’m attempting to create a text-based version of this game . Code: #include <iostream>

  • 0

I’m attempting to create a text-based version of this game.

Code:

#include <iostream>
#include <vector>
#include <ctime>

class Clickomania {
    public:
        Clickomania();
        std::vector<std::vector<int> > board;
        int move(int, int);
        bool isSolved();
        void print();
        void pushDown();
        bool isValid();
};

Clickomania::Clickomania()
    : board(12, std::vector<int>(8,0)) {

    srand((unsigned)time(0));

    for(int i = 0; i < 12; i++) {
        for(int j = 0; j < 8; j++) {
            int color = (rand() % 3) + 1;
            board[i][j] = color;
        }
    }
}

void Clickomania::pushDown() {
    for(int i = 0; i < 8; i++) {
        for(int j = 0; j < 12; j++) {
            if (board[j][i] == 0) {
                for(int k = j; k > 0; k--) {
                    board[k][i] = board[k-1][i];
                }
                board[0][i] = 0;
            }
        }
    }
}

int Clickomania::move(int row, int col) {
    bool match = false;
    int totalMatches = 0;
    if (row > 12 || row < 0 || col > 8 || col < 0) {
        return 0;
    }

    int currentColor = board[row][col];
    board[row][col] = 0;

    if ((row + 1) < 12) {
        if (board[row+1][col] == currentColor)
        {
            match = true;
            totalMatches++;
            totalMatches +=  move(row+1, col);
        }
    }

    if ((row - 1) >= 0) {
        if (board[row-1][col] == currentColor) {
            match = true;
            totalMatches++;
            totalMatches += move(row-1, col);
        }
    }

    if ((col + 1) < 8) {
        if (board[row][col+1] == currentColor) {
            match = true;
            totalMatches++;
            totalMatches += move(row, col+1);
        }
    }

    if ((col - 1) >= 0) {
        if (board[row][col-1] == currentColor) {
            match = true;
            totalMatches++;
            totalMatches += move(row, col-1);
        }
    }

    return totalMatches;
}

void Clickomania::print() {
    for(int i = 0; i < 12; i++) {
        for(int j = 0; j < 8; j++) {
            std::cout << board[i][j];
        }
        std::cout << "\n";
    }
}

int main() {
    Clickomania game;
    game.print();
    int row;
    int col;
    std::cout << "Enter row: ";
    std::cin >> row;
    std::cout << "Enter col: ";
    std::cin >> col;
    int numDestroyed = game.move(row,col);
    game.print();
    std::cout << "Destroyed: " << numDestroyed << "\n";
}

The method that is giving me trouble is my “move” method. This method, given a pair of coordinates, should delete all the squares at that coordinate with the same number and likewise with all the squares with the same number connected to it.

If you play the link I gave above you’ll see how the deletion works on a click.

int Clickomania::move(int row, int col) {
        bool match = false;
        int totalMatches = 0;

        if (row > 12 || row < 0 || col > 8 || col < 0) {
            return 0;
        }

        int currentColor = board[row][col];
        board[row][col] = 0;

        if ((row + 1) < 12) {
            if (board[row+1][col] == currentColor) {
                match = true;
                totalMatches++;
                totalMatches +=  move(row+1, col);
            }
        }

        if ((row - 1) >= 0) {
            if (board[row-1][col] == currentColor) {
                match = true;
                totalMatches++;
                totalMatches += move(row-1, col);
            }
        }

        if ((col + 1) < 8) {
            if (board[row][col+1] == currentColor)
            {
                match = true;
                totalMatches++;
                totalMatches += move(row, col+1);
            }
        }

        if ((col - 1) >= 0) {
            if (board[row][col-1] == currentColor) {
                match = true;
                totalMatches++;
                totalMatches += move(row, col-1);
            }
        }

        return totalMatches;
    }

My move() method above works fine, as in it will delete the appropriate “blocks” and replace them with zeros, however the number of destroyed (value returned) is always one off (too small). I believe this is because the first call of move() isn’t being counted, but I don’t know how to differentiate between the first call or subsequent calls in that recursive method.

How can I modify my move() method so it returns the correct number of destroyed blocks?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:26:43+00:00Added an answer on May 14, 2026 at 3:26 pm

    It looks like you’re incrementing totalMoves in the wrong place(s). You should count the match at the point where you set board[r][c] = 0 and remove the other references to totalMoves++.

    You are right that the first call isn’t being counted, it’s only counting the recursive calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer EDIT after comment: Simplest way is to make the search… May 16, 2026 at 7:08 am
  • Editorial Team
    Editorial Team added an answer Short answer: No. Longer answer: Unfortunately, ECB completely takes over… May 16, 2026 at 7:08 am
  • Editorial Team
    Editorial Team added an answer You cannot apply a shader effect on a particular Grid… May 16, 2026 at 7:08 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.