C ++俄罗斯方块函数

时间:2019-01-20 21:30:10

标签: c++

我正在尝试启动制作俄罗斯方块的游戏。对于这一部分,我已经有七个表示Tetriminos(俄罗斯方块)的独特二维数组,并且我需要帮助创建两个函数来旋转这些二维数组:rotateLeft()和rotateRight()。它将沿指定方向旋转90度。例如。 RotateLeft()会将第1行的值移至col 1;第2列到第2列;并具有一个称为printTetrimino的功能,可将2-d tetrimino数组的内容打印到控制台窗口。我只是在如何完成rotateRight函数以及如何启动rotateLeft函数方面遇到问题。对于这个项目,我不想使用类和向量。请让我知道如何执行此操作,因为我正在努力。我需要了解它是如何工作的。谢谢!代码在下面。

#include <iostream>

using namespace std;

// function implementations
void rotateRight();

int main() {

};

int tetromino[7][4][4][4] =
{
    // Square piece
    {
    {
    { 1,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1, 1, 0, 0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // I piece
    {
    {
    { 1,1,1,1 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1, 0, 0, 0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    },
    {
    { 1,1,1,1 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    },
    },
    // L piece
    {
    {
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,1,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,0,1,0 },
    { 1,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // J piece
    {
    {
    { 0,1,0,0 },
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,1,0 },
    { 0,0,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // S piece
    {
    {
    { 0,1,1,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,1,0 },
    { 1,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    },
    // N piece
    {
    {
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 0,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,0,0 },
    { 0,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
    // T piece
    {
    {
    { 1,0,0,0 },
    { 1,1,0,0 },
    { 1,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 1,1,1,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,0,0 },
    { 1,1,0,0 },
    { 0,1,0,0 },
    { 0,0,0,0 },
    },
    {
    { 0,1,0,0 },
    { 1,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    },
    },
};

void rotateRight() {
    for (int i = 0; i < 16; i++) {
        int temp = tetromino[i][0];
        tetromino[i][0] = tetromino[i][1];
        tetromino[i][1] = -temp;
    }
}

2 个答案:

答案 0 :(得分:0)

您需要问自己的第一个问题是您要旋转哪个4x4阵列?您有28个4x4数组,目前rotateRight将如何知道要旋转哪个数组?答案是向rotateRight添加一些参数

// piece is a value from 0 to 6, e.g. 0 for square piece, 1 for I piece etc
// orientation is 0 to 4, its the version of the piece to rotate
void rotateRight(int piece, int orientation) {
    ...
}

接下来的一点,当您尝试旋转2D 4x4数组时,从0到15循环是没有意义的。您需要两个分别从0到3的循环

// piece is a value from 0 to 6, e.g. 0 for square piece, 1 for I piece etc
// orientation is 0 to 4, its the version of the piece to rotate
void rotateRight(int piece, int orientation) {
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            tetromino[piece][orientation][i][j] = ...
            ...
        }
    }
}

这为您提供了一个起点,希望您可以填写其余部分。

答案 1 :(得分:0)

结果为rotating 2d arrays inplace is much more complicated than you first think. Please read the tutorial carefully before you proceed。现在,将该教程与您的代码放在一起似乎几乎是完美的,除了您必须自己弄清楚如何顺时针进行操作。

#include <stdio.h>

#define N 4

void rotateMatrix(int mat[][N]) { 
    // Consider all squares one by one 
    for (int x = 0; x < N / 2; x++) { 
        // Consider elements in group of 4 in  
        // current square 
        for (int y = x; y < N-x-1; y++) { 
            // and so on
        } 
    } 
} 

void main() {
    int tetromino[4][4]={
    { 0,1,0,0 },
    { 1,1,1,0 },
    { 0,0,0,0 },
    { 0,0,0,0 },
    };

    rotateMatrix(tetromino);

    for (int y = 0; y < 4; y++) {
        for(int x=0; x<4; x++) {
            printf("%i", tetromino[y][x]);
        }
        printf("\n");
    }
}

然后,您是否再次注意到数组具有第四维并包含所有旋转?因此,无论谁编写您的代码,都为这种情况做好了准备。只需使用tetromino[num][rotation]

选择您的tetromino