all. I have a class defined as follows:
class Board {
int columns, rows;
bool board[10][10];
public:
Board(int, int);
void nextFrame();
void printFrame();
};
My void nextFrame() keeps giving me errors for [rows][columns] because " 'this' cannot be in a constant expression" for both of them. How can I redefine this so that it works? I understand the error. The definition of the function is below, and the error occurs on the 3rd line of the following code sample.
void Board::nextFrame() {
int numSurrounding = 0;
bool tempBoard[rows][columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if ((i + 1) < rows && board[i + 1][j] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && board[i - 1][j] == true)
{
numSurrounding++;
}
if ((j + 1) < columns && board[i][j + 1] == true)
{
numSurrounding++;
}
if ((j - 1) >= 0 && board[i][j - 1] == true)
{
numSurrounding++;
}
if ((i + 1) < rows && (j + 1) < columns && board[i + 1][j + 1] == true)
{
numSurrounding++;
}
if ((i + 1) < rows && (j - 1) >= 0 && board[i + 1][j - 1] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && (j + 1) < columns && board[i - 1][j + 1] == true)
{
numSurrounding++;
}
if ((i - 1) >= 0 && (j - 1) >= 0 && board[i - 1][j - 1] == true)
{
numSurrounding++;
}
if (numSurrounding < 2 || numSurrounding > 3)
{
tempBoard[i][j] = false;
}
else if (numSurrounding == 2)
{
tempBoard[i][j] = board[i][j];
}
else if (numSurrounding == 3)
{
tempBoard[i][j] = true;
}
numSurrounding = 0;
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
board[i][j] = tempBoard[i][j];
}
}
}
You need to use a collection from the STL.
Here's an example that nests vectors to get your board:
#include <vector>
#include <iostream>
using namespace std;
class Board {
int columns, rows;
vector<vector<bool>> board;
public:
Board(int x, int y) : board(vector<vector<bool>>(x, vector<bool>(y))) {
}
void nextFrame() {
// Fill in
}
void printFrame() {
// Fill in
}
size_t xdim() {
return board.size();
}
size_t ydim() {
if (board.size() == 0) {
return 0;
}
return board.at(0).size();
}
};
int main() {
Board b(10, 20);
cout << "Made the board" << endl;
cout << "X: " << b.xdim() << endl;
cout << "Y: " << b.ydim() << endl;
}
You can learn about the member initialization syntax for board(vector<vector<bool>>(x, vector<bool>(y))) here and here.
Related
#include<iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
int t, n;
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n;
vector<vector<int>> numbers;
for (int k = 1; k < n * n; k += n) {
vector<int> temp;
for (int j = 0; j < n; j++) {
temp.push_back(k + j);
}
numbers.push_back(temp);
}
for (int j = 0; j < numbers.size(); j++) //row
for (int k = 0; k < numbers.size(); k++) { //column
bool adjacent = false;
if (j - 1 > 0)
if (abs(numbers[j][k] - numbers[j - 1][k]) == 1)
adjacent = true;
if (j + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[j + 1][k]) == 1)
adjacent = true;
if (k - 1 > 0)
if (abs(numbers[j][k] - numbers[j][k - 1]) == 1)
adjacent = true;
if (k + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[j][k + 1]) == 1)
adjacent = true;
if (adjacent)
for (int l = 0; l < numbers.size(); l++)
for (int m = 0; m < numbers.size(); m++) {
bool adjacent2 = false, adjacent3 = false;
if (j - 1 > 0)
if (abs(numbers[l][m] - numbers[j - 1][k]) == 1)
adjacent2 = true;
if (j + 1 < numbers.size())
if (abs(numbers[l][m] - numbers[j + 1][k]) == 1)
adjacent2 = true;
if (k - 1 > 0)
if (abs(numbers[l][m] - numbers[j][k - 1]) == 1)
adjacent2 = true;
if (k + 1 < numbers.size())
if (abs(numbers[l][m] - numbers[j][k + 1]) == 1) {
adjacent2 = true;
cout << "hi " << adjacent2 << endl; //HERE
}
if (!adjacent2) {
cout << adjacent2 << endl;
if (l - 1 > 0)
if (abs(numbers[j][k] - numbers[l - 1][m]) == 1)
adjacent3 = true;
if (l + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[l + 1][m]) == 1)
adjacent3 = true;
if (m - 1 > 0)
if (abs(numbers[j][k] - numbers[l][m - 1]) == 1)
adjacent3 = true;
if (m + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[l][m + 1]) == 1)
adjacent3 = true;
if (!adjacent3) {
int temp = numbers[j][k];
numbers[j][k] = numbers[l][m];
numbers[l][m] = temp;
}
}
}
}
if (n > 2)
for (auto x : numbers) {
for (int y : x) {
cout << y << " ";
}
cout << endl;
}
else if (n == 1)
cout << 1 << endl;
else cout << -1 << endl;
}
}
I'm trying to solve this codeforces problem: https://codeforces.com/contest/1520/problem/C. The way I'm trying to solve it is by generating a 2d array from 1 to n squared. I then start from the first row, left to right, and work my way down to the last number. For each element, in the 2d array, I check for adjacent elements and see if their differences are equal to one. If there's at least one instance of that, I search for an element that can be swapped with the original element.
Swapping the elements works correctly, except for the first one.
For example, if n = 3, I should start with:
123
456
789
and then I should get
423
156
789
However, I get
213
456
789
It doesn't matter if n=3 or n=10. It will always start as 213...
I tried debugging and noticed that in the line denoted as "HERE", adjacent2 is true. However, the next if-statement, if(!adjacent2) runs. Why does it run? If adjacent2 is true, shouldn't !adjacent2 equal false? I also printed out adjacent2 after if(!adjacent2) and it's somehow false and I never changed it!
I believe this part might not be correct:
if (k + 1 < numbers.size())
if (abs(numbers[j][k] - numbers[j][k + 1]) == 1)
adjacent = true;
If the matrix is not square, numbers.size() might not be the same as numbers[j].size(). And then k + 1 should be compared to the latter.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I decided to make the 2048 game. It is a very popular game for beginners.
The game is almost complete now but there's this error because of which my game keeps getting closed again and again as soon as I make my first move. I have tested a lot thoroughly and couldn't trace the error(s). I am new to programming.
#include<iostream>
#include<conio.h>
#include<ctime>
#include<string>
#include<cstdlib>
#include<fstream>
#include<Windows.h>
using namespace std;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int limit = 4;
int board[limit][limit] = {};
int score = 0;
string name;
int highestScore;
void newGame();
void displayBoard();
bool gameOver();
int generateNum(); //generate random 2 or 4
void moveInDirection(int);
void generateNumInBoard();
int main()
{
int currentDirection;
char command, choice;
char direction[128];
string currentName;
direction['s'] = 0;
direction['w'] = 1;
direction['a'] = 3;
direction['d'] = 2;
ifstream inFile;
inFile.open("Score.txt");
inFile >> name >> highestScore;
inFile.close();
ofstream outFile;
outFile.open("Score.txt");
cout << "Please enter your name = ";
getline(cin, currentName);
newGame();
while (true)
{
system("CLS");
displayBoard();
cout << "\nEnter what you want to do = ";
command = _getche();
if (command == 'n')
{
newGame();
}
else if (command == 'e')
{
break;
}
else
{
currentDirection = direction[command];
moveInDirection(currentDirection);
}
if (gameOver())
{
if (score > highestScore)
{
outFile << currentName << " " << score;
}
do {
system("CLS");
cout << "YOU HAVE LOST :("
<< "Your score was " << score << endl
<< "Do you want to play again ( New game (n) / Exit (e) ) = ";
command = _getche();
} while (command != 'n' && command != 'e');
if (command == 'e')
{
exit(1);
}
}
}
return 0;
}
void newGame()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
board[i][j] = 0;
}
}
board[0][0] = 2;
}
void displayBoard()
{
SetConsoleTextAttribute(hConsole, 9);
cout << "\n2048 THE GAME\n\n a = left , s = down , w = up , d = right , n = newgame , e = exit\n\n If all boxes get filled you lose\n\nBest player = " << name << " Best score = " << highestScore << "\n\n Your Score = " << score << "\n\n" << endl;
for (int i = 0; i < limit; i++)
{
cout << "\t\t|";
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
cout << " - |";
}
else
{
cout << " " << board[i][j] << " |";
}
}
cout << endl;
}
}
void moveInDirection(int currentDirection)
{
int count = 0;
bool flag = false;
if (currentDirection == 2)
{
while (count != 4)
{
for (int i = 0; i < limit; i++)
{
for (int j = limit - 1; j > 0; j--)
{
if ((board[i][j] == board[i][j - 1]) && board[i][j] != 0 && board[i][j - 1] != 0)
{
board[i][j] += board[i][j - 1];
board[i][j - 1] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i][j - 1] != 0))
{
flag = true;
swap(board[i][j], board[i][j - 1]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
else if (currentDirection == 3)
{
while (count != 4)
{
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit - 1; j++)
{
if ((board[i][j] == board[i][j + 1]) && board[i][j] != 0 && board[i][j + 1] != 0)
{
board[i][j] += board[i][j + 1];
board[i][j + 1] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i][j + 1] != 0))
{
flag = true;
swap(board[i][j], board[i][j + 1]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
else if (currentDirection == 1)
{
while (count != 4)
{
for (int i = 0; i < limit - 1; i++)
{
for (int j = 0; j < limit; j++)
{
if ((board[i][j] == board[i + 1][j]) && board[i][j] != 0 && board[i + 1][j] != 0)
{
board[i][j] += board[i + 1][j];
board[i + 1][j] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i + 1][j] != 0))
{
flag = true;
swap(board[i][j], board[i + 1][j]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
else if (currentDirection == 0)
{
while (count != 4)
{
for (int i = limit - 1; i >= 0; i--)
{
for (int j = 0; j < limit; j++)
{
if ((board[i][j] == board[i - 1][j]) && board[i][j] != 0 && board[i - 1][j] != 0)
{
board[i][j] += board[i - 1][j];
board[i - 1][j] = 0;
score += board[i][j];
}
else if ((board[i][j] == 0) && (board[i - 1][j] != 0))
{
flag = true;
swap(board[i][j], board[i - 1][j]);
}
}
}
count++;
}
if (flag)
{
generateNumInBoard();
}
}
}
int generateNum()
{
srand(time(NULL));
int randomNum = rand() % 4;
if (randomNum <= 2)
{
return 2;
}
else
{
return 4;
}
}
void generateNumInBoard()
{
bool flag = true;
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
board[i][j] = generateNum();
flag = false;
break;
}
}
if (flag == false)
{
break;
}
}
}
bool gameOver()
{
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
return true;
}
}
}
return false;
}
You made a small blunder at the fucntion gameOver() as you are making the game end if the compiler finds any 0 in the 2D array and you are display the zero as - in front end , You just need to make he false part true and viceversa.The game will work AOK :-)
bool gameOver()
{
for (int i = 0; i < limit; i++)
{
for (int j = 0; j < limit; j++)
{
if (board[i][j] == 0)
{
return false; //You need to make this false to not end game as soon as it starts
}
}
}
return true; //You need to make this true as the end condition
}
This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 9 years ago.
Code in C++:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void check(int i,int j);
int main()
{
int n;
cin >> n;
int a[n][n];
int b[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = 0;
}
}
b[0][0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; i++)
{
cin >> a[i][j];
}
}
check(0, 0);
if (b[n - 1][n - 1] == 1)
cout << "yesssss";
else
cout << "no!!!!";
}
void check(int i, int j)
{
if((i - 1) >= 0 &&
(i - 1) <= (n - 1) &&
(abs(a[i][j] - a[i - 1][j]) >= 10) &&
b[i - 1][j] == 0)
{
b[i - 1][j] = 1;
check(i - 1, j);
}
if((i + 1) >= 0 &&
(i + 1) <= (n-1) &&
(abs(a[i][j] - a[i + 1][j]) >= 10) &&
b[i + 1][j] == 0)
{
b[i + 1][j] = 1;
check(i + 1, j);
}
if((j + 1) >= 0 &&
(j + 1) <= (n - 1) &&
(abs(a[i][j] - a[i][j + 1]) >= 10) &&
b[i][j + 1] == 0)
{
b[i][j + 1] = 1;
check(i, j + 1);
}
if((j - 1) >= 0 &&
(j-1) <= (n - 1) &&
(abs(a[i][j] - a[i][j - 1]) >= 10) &&
b[i][j - 1] == 0)
{
b[i][j - 1] = 1;
check(i, j - 1);
}
}
My code has a function named check inside which integer variable n and arrays a and b. I want them to be made available to this function. If i declare global variable, then I can't use cin outside the main function.
How can I make these variables available to my check function?
Since you do not know the size of the array before you pass it into check, you need to use a pointer to a pointer (and not a 2D array). Here's the modified code (you also had a typo in your nested for-loop where you should have had a j instead of an i):
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void check(int i, int j, int** a, int** b, int n);
int main() {
int n;
cin >> n;
int** a;
int** b;
a = new int* [n];
b = new int* [n];
for (int i = 0; i < n; i++)
a[i] = new int[n];
for (int i = 0; i < n; i++)
b[i] = new int[n];
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
b[i][j]=0;
}
}
b[0][0]=1;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cin >> a[i][j];
}
}
check(0,0,a,b,n);
if (b[n-1][n-1] == 1)
cout << "yesssss";
else
cout << "no!!!!";
}
void check(int i, int j, int** a, int** b, int n)
{
if((i-1)>=0 && (i-1)<=(n-1) && (abs(a[i][j]-a[i-1][j])>=10) && b[i-1][j]==0)
{
b[i-1][j]=1;
check(i-1,j,a,b,n);
}
if((i+1)>=0 && (i+1)<=(n-1) && (abs(a[i][j]-a[i+1][j])>=10) && b[i+1][j]==0)
{
b[i+1][j]=1;
check(i+1,j,a,b,n);
}
if((j+1)>=0 && (j+1)<=(n-1) && (abs(a[i][j]-a[i][j+1])>=10) && b[i][j+1]==0)
{
b[i][j+1]=1;
check(i,j+1,a,b,n);
}
if((j-1)>=0 && (j-1)<=(n-1) && (abs(a[i][j]-a[i][j-1])>=10) && b[i][j-1]==0)
{
b[i][j-1]=1;
check(i,j-1,a,b,n);
}
}
From looking at the below solution, can someone please tell me how can I implement a function to check whether the game has ended? I attempted this by creating "int checkEndGame" which loops through the board and returns either a 1 or 0 but it doesn't work.
Header:
#include <iostream>
#include <string>
#include <time.h>
int generations;
int boardWidth;
int boardHeight;
char** board;
char** boardTmp;
char deadOrAlive[] = {' ', 'x'};
char dead = deadOrAlive[0];
char alive = deadOrAlive[1];
char lookLeft(int i, int j)
{
if ( i == 0)
{
return dead;
}
return board[i - 1][j];
}
char lookRight(int i, int j)
{
if (i == boardWidth - 1)
{
return dead;
}
return board[i + 1][j];
}
char lookUp(int i, int j)
{
if (j == 0)
{
return dead;
}
return board[i][j - 1];
}
char lookDown(int i, int j)
{
if (j == boardHeight - 1)
{
return dead;
}
return board[i][j + 1];
}
char lookUpLeft(int i, int j)
{
if (i == 0 || j == 0)
{
return dead;
}
return board[i - 1][j - 1];
}
char lookUpRight(int i, int j)
{
if(i == boardWidth - 1 || j == 0)
{
return dead;
}
return board[i + 1][j + 1];
}
char lookDownLeft(int i, int j)
{
if (j == boardHeight - 1 || i == 0)
{
return dead;
}
return board[i - 1][j + 1];
}
char lookDownRight(int i, int j)
{
if (j == boardHeight - 1 || i == boardWidth - 1)
{
return dead;
}
return board[i + 1][j + 1];
}
char ans;
void init();
void setBoard();
void showBoard();
void verifyDeadOrAlive();
int getNeighbors(int i, int j);
void swap();
void sleep(unsigned int mseconds);
int checkEndGame();
void playGame();
c++:
#include "stdafx.h"
#include "gameoflife.h"
using namespace std;
void init()
{
cout << "How many generations would you like to cycle through? ";
cin >> generations;
cout << "Specify a width for the board: ";
cin >> boardWidth;
cout << "Specify a height for the board: ";
cin >> boardHeight;
}
void setBoard()
{
srand(time(0));
board = new char*[boardWidth];
boardTmp = new char*[boardWidth];
for (int i = 0; i < boardWidth; ++i)
{
board[i] = new char[boardHeight];
boardTmp[i] = new char[boardHeight];
for (int j = 0; j < boardHeight; ++j)
{
board[i][j] = deadOrAlive[rand() % generations];
boardTmp[i][j] = ' ';
}
}
}
void showBoard()
{
system("cls");
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
cout << board[i][j];
}
cout << endl;
}
}
void verifyDeadOrAlive()
{
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
int neighbors = getNeighbors(i, j);
if (board[i][j] == alive)
{
if (neighbors < 2 || neighbors > 3)
{
boardTmp[i][j] = dead;
}
else
{
boardTmp[i][j] = alive;
}
}
else
{
if (neighbors == 3)
{
boardTmp[i][j] = alive;
}
else
{
boardTmp[i][j] = dead;
}
}
}
}
swap();
}
int getNeighbors(int i, int j)
{
int x = 0;
if (lookLeft(i, j) == alive)
{
x++;
}
if (lookRight(i, j) == alive)
{
x++;
}
if (lookUp(i, j) == alive)
{
x++;
}
if (lookDown(i, j) == alive)
{
x++;
}
if (lookUpLeft(i, j) == alive)
{
x++;
}
if (lookUpRight(i, j) == alive)
{
x++;
}
if (lookDownLeft(i, j) == alive)
{
x++;
}
if (lookDownRight(i, j) == alive)
{
x++;
}
return x;
}
void swap()
{
char** tmp = board;
board = boardTmp;
boardTmp = tmp;
}
void sleep(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int checkEndGame()
{
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
if (board[i][j] == dead)
{
return 1;
}
else
{
return 0;
}
}
}
}
void playGame()
{
init();
setBoard();
do
{
showBoard();
sleep(500);
verifyDeadOrAlive();
checkEndGame();
}
while (checkEndGame == 0);
}
int main()
{
do
{
playGame();
cout << "Would you like to play again? y/n: ";
cin >> ans;
system("cls");
}
while (ans == 'Y' || ans == 'y');
return 0;
}
You want this:
int checkEndGame()
{
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
if (board[i][j] == alive)
return 0;
}
}
return 1;
}
Your function returns the moment it sees one dead cell, which is incorrect. You can abort if you see one live cell. But otherwise, you need to check every cell.
Also:
checkEndGame();
}
while (checkEndGame == 0);
is broken. checkEndGame will never be zero since it's a pointer to a function. You want:
}
while (checkEndGame() == 0);
a
You function should be like that:
int checkEndGame()
{
for (int j = 0; j < boardHeight; ++j)
{
for (int i = 0; i < boardWidth; ++i)
{
if (board[i][j] == alive){return 0; }
}
}
return 1;
}
Because you either find "alive" and say game is not over yet, or have to go through the hole board and then say the game is over because you haven't found "alive".
And as said before:
while(checkEndGame==0)
doesn't run the function to see what the returned value is, done this way all it does is checking if the memory address of you function is 0 (which it will most certainly never be unless you assign it).
to run the function and see the returned value you have to write:
while(checkEndGame()==0)
(notice the "()" at the end)
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?
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 based indexing. You dont want to check > you want >=
you wanna check row >= 12 col >= 8