I'm trying to make a merging algorithm and it works fine except for one thing: a single tile can merge twice in one move so 4224 becomes 8400 instead of 4440 when I move left.
I've tried adding a break statement at the end of the else if block and adding a flag if a tile has merged but none of these things helped. I sincerely apologise for any mistakes that I might have made but this is my first post on stackoverflow.
#define TILES_X 4
#define TILES_Y 4
//Moving UP with SDL library
for (int i = 0; i < TILES_X; i++) {
for (int j = 1; j < TILES_Y; j++) {
if (gameBoard[i][j] != 0) {
for (int k = 0; k < j; k++) {
if (gameBoard[i][k] == 0) {
gameBoard[i][k] = gameBoard[i][j];
gameBoard[i][j] = 0;
}
else if (gameBoard[i][k] == gameBoard[i][j] && (gameBoard[i][k+1]==0||gameBoard[i][k+1]==gameBoard[i][k])) {
gameBoard[i][k] = 2 * gameBoard[i][j];
gameBoard[i][j] = 0;
}
}
}
}
}
Related
There are so many questions regarding Nqueens problem here. However, my implementation is different. My code checks with the board if queen placement is possible, instead of checking with the position of the previous queen.
It goes like this:
initially, the board has all zeros filled. The algorithm starts with the position (0,0). Then, it checks
row-wise per column to find the first 0. After finding the first zero, it changes the zero to one.
From this point onward, my logic differs. Instead of going to the next column, it first disables all the
positions, which the currently placed queen attacks, i.e. writes -1 on those places, i.e., row, column,
upper diagonal and lower diagonal. Now, the column value increments, and instead of check with the previous queen,
it simply has to find the first zero. Then again, relative positions get disabled.... you get the idea.
The code:
#include <iostream>
int board[8][8];
void printBoard() {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
std::cout << board[i][j] << " ";
}
std::cout << "\n";
}
}
void disablePositions(int row, int col) {
//disable row
for (int j = col + 1; j < 8; j++) {
board[row][j] = 2;
}
//disable column
for (int i = 0; i < 8; i++) {
if (board[i][col] == 1) {
continue;
}
board[i][col] = 2;
}
//disable upper diagonal
for (int i = row - 1, j = col + 1; i >= 0 || j < 8; i--, j++) {
board[i][j] = 2;
}
for (int i = row + 1, j = col + 1; i < 8 || j < 8; i++, j++) {
board[i][j] = 2;
}
}
void solve(int initial_row) {
int init = initial_row;
int row = 0;
int col = 0;
while (col != 8) {
for (row = init; row < 8; row++) {
if (board[row][col] == 0) {
board[row][col] = 1;
break;
}
}
if (row == 8) {
col = 0;
initial_row++;
init = initial_row;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
board[i][j] = 0;
}
}
}
else {
init = 0;
disablePositions(row, col);
col++;
}
printBoard();
std::cout << std::endl;
}
}
int main() {
solve(0);
std::cout << std::endl;
}
This code is for 8-queens. The problem is, after it reaches the stage where it starts from [5][0], it just crashes. What is causing the issue?
Also, as it tries to make an optimal choice at every stage, would we call it greedy algorithm?
In your disable upper diagonal loops, you have the condition wrong. Using an || operation, the looping continues when either condition is true, which will lead to out-of-bounds access to the array.
Change the conditions in both for loops to be && (and).
I am working on a small project and I am absolutely stuck. The purpose of the function I'm working on is to rearrange and change a Cstring based on a few preset rules. Where my issue lies is within the second portion of my swapping algorithm I came up with.
for(int i = 0; i < len; i++)
{
if(sentence[i] == SPACE)
{
space++;
spacePlace[counter] = i;
counter++;
}
}
for(int i = 0; i < space; i++)
{
if(i == 0)
{
count2 = 0;
for(int h = 0; h < 20; h++)
{
temp1[h] = NUL;
temp2[h] = NUL;
}
for(int j = 0; j < spacePlace[0]; j++)
temp1[j] = sentence[j];
for(int m = spacePlace[0]; m < spacePlace[1]; m++)
{
temp2[count2] = sentence[m];
count2++;
}
.
.
.
the first for loops executes perfectly and the output is great, but the second for loop always messes up and ends up sending me a stack smashing error. For more reference, sentence is a cstring passed to the function, and temp1 and temp2 are also cstrings. Any help or points in the right direction would be a godsend. Thanks!
I am trying to get the summary of CFG with given input. I have to list the terminals with the count of how many times it appears in the rule. However, I'm having trouble with it counting multiple terminals on the same rule.
for (int i = 0; i < cfg.size(); i++)
{
for (int j = 0; j < cfg[i].size(); j++)
{
for (int k = 0; k < terminal.size(); k++)
{
if (strcmp(cfg[i][j].c_str(), terminal[k].c_str()) == 0)
{
//TO-DO if counter[k] already incremented do not increment counter[k] again
counter[k]++;
break;
}
}
}
}
For example if the rule is
Z -> a b b b
It will return 3 for b when the correct answer would be 1.
Any help on how I can how I can leave that rule after it has already been counted would be great. Thank you
I'm not sure if I understand what you mean, but maybe changing the loops order would help?
for (int i = 0; i < cfg.size(); ++i)
{
for (int k = 0; k < terminal.size(); ++k)
{
for (int j = 0; j < cfg[i].size(); ++j)
{
if (strcmp(cfg[i][j].c_str(), terminal[k].c_str()) == 0)
{
++counter[k];
break;
}
}
}
}
I have a 2d array created of size n that holds 1s and 0s representing closed and open spaces respectively.
Now I need to test the 2d array to see if it percolates and I'm not sure how to go about this.
I have the following code for creating the array and randomly assigning each spot to a 1 or 0.
int** grid = new int*[boardSize];
for (int i = 0; i < boardSize; ++i) {
grid[i] = new int[boardSize];
}
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
if (i == 0) {
grid[i][j] = 1;
}
else if (i == boardSize - 1) {
grid[i][j] = 1;
}
else if (j == 0) {
grid[i][j] = 1;
}
else if (j == boardSize - 1) {
grid[i][j] = 1;
}
else
grid[i][j] = random(delta);
}
}
grid[0][enter] = 0;
grid[boardSize - 1][exit] = 0;
This will create an array with closed borders (1s) and put 2 random entry/exit points (0s) on the top and bottom. Only part I'm missing is to test for percolation.
Any help is appreciated, thanks!
I've created a simple game in C++ in which a player must avoid being bitten by zombies to survive.
The zombies are stored as Z characters in an array called zeds.
I am trying to write a function that can check if any zombies collide, and act accordingly.
I've written this if statement:
for (int i; i < MAXZEDS; ++i);
if (zeds[i].x == zeds[i].x && zeds[i].y == zeds[i].y)
{
--zlives;
updateLives();
}
Obviously it doesn't work, I'm just wondering if I can somehow rewrite this to only decrement zlives if a zombie collides with another zombie but NOT itself.
Apologies for the title I couldn't find a better way to summarize my issue. Thanks in advance for any help.
You need a double loop so you can compare two different zombies.
Something like this.
for (int i = 0; i < MAXZEDS - 1; ++i)
{
// Looping from i + 1 ensures that we only test each pair once.
for (int j = i + 1; j < MAXZEDS; ++j)
{
if (zeds[i].x == zeds[j].x && zeds[i].y == zeds[j].y)
{
--zlives;
updateLives();
}
}
}
The simplest way is 2 loops.
for (int i = 0; i < MAXZEDS; ++i) {
for (int j = i+1; j < MAXZEDS; ++j) {
if (zeds[i].x == zeds[j].x && zeds[i].y == zeds[j].y)
{
--zlives;
updateLives();
}
}
}
If the object order in zeds is not important, and operator<() is defined correctly for the element type of zeds, then you could sort zeds and check consecutive elements to get lower complexity.
Try using nested for loops:
for (int i = 0; i < MAXZEDS; i++)
{
for (int j = 0; j < MAXZEDS; j++)
{
if (i == j) continue; // Don't compare the current zed with itself
else if (zeds[i].x == zeds[j].x && zeds[i].y == zeds[j].y)
{
--zlives;
updateLives();
}
}
}
This iterates through zeds twice and compares each item in the array with each other item. I am sure there are more efficient ways, but this is the simplest.