Skip to main content
deleted 11 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Tetris  - likelike algorithm [2D matrix](2D matrix)

The block is always the same shape

.##....:

..##....

 .##....

   ..##....

Keep in mind I wrote this in 1 hour - all the time we had for the task. How tocan I make it more optimized? :)

The code is

Tetris  - like algorithm [2D matrix]

The block is always the same shape

.##....

..##....

Keep in mind I wrote this in 1 hour - all the time we had for the task. How to make it more optimized? :)

The code is

Tetris-like algorithm (2D matrix)

The block is always the same shape:

 .##....

   ..##....

Keep in mind I wrote this in 1 hour - all the time we had for the task. How can I make it more optimized?

Source Link
Petar
  • 21
  • 1

Tetris - like algorithm [2D matrix]

The algorithm should be checking if the block fits perfectly when it falls.

The block is always the same shape

.##....

..##....

Keep in mind I wrote this in 1 hour - all the time we had for the task. How to make it more optimized? :)

The code is

#include <stdio.h>
#include <stdlib.h>
int check(char *k, int r, int s){
    int i, j, flag = 0;
    for (i = 0; i < r; i ++){
        for (j = 0; j < s; j ++){
            if (k[i * s + j] == '*' && k[(i + 1*2) * s + j + 1] == '.'){
                flag = 1;
            }
        }
    }
    return flag;
}
void toLadders(char *k, int r, int s){
    int i, j;
    for (i = 0; i < r; i ++){
        for (j = 0; j < s; j ++){
            if (k[i * s + j] == '*')
             k[i * s + j] = '#';
        }
    }

}
int main()
{
    int i, j, r, s;
    int flag = 0;

    do{
        scanf("%d %d", &r, &s);
    }while(r < 1 || s > 15);

    char matrix[r][s];

    for (i = 0; i < r; i ++){
        for(j = 0; j < s; j++){
            scanf(" %c", &matrix[i][j]);
        }
    }
 
    for (i = 0; i < r; i ++){
        for(j = 0; j < s; j++){
            if (matrix[i][j - 1] == '.' && matrix[i][j] == '#' && matrix[i][j + 2] == '.'
                && matrix[i + 1][j] == '.' && matrix[i + 1][j + 1] == '#' && matrix[i + 1][j + 2] == '#' && matrix[i + 1][j + 3] == '.'){

                matrix[i][j] = '*';
                matrix[i][j + 1] = '*';
                matrix[i + 1][j + 1] = '*';
                matrix[i + 1][j + 2] = '*';

            }
        }
    }

    for (i = 0; i < r; i ++){
        for (j = 0; j < s; j ++){
            if (matrix[i][j] == '*' && matrix[i + 1][j] == '.' && matrix[i + 2][j + 1] == '.' && matrix[i + 2][j + 2] == '.'){
                    matrix[i + 1][j] = '*';
                    matrix[i][j] = '.';
                    matrix[i + 1][j + 1] = '*';
                    matrix[i][j + 1] = '.';
                    matrix[i + 2][j + 1] = '*';
                    matrix[i + 1][j + 1] = '*';
                    matrix[i + 2][ j + 2] = '*';
                    matrix[i + 1][j + 2] = '.';
            }
        }
    }

    flag = check(&matrix[0][0], r, s);

    toLadders(&matrix[0][0], r, s);

    for (i = 0; i < r; i ++){
        for(j = 0; j < s; j++){
            printf("%c", matrix[i][j]);
        }
        printf("\n");
    }

    if (flag)
        printf("\nNot perfect!");
    else
        printf("\nPerfect!");

    return 0;
}