Cube Reverse

一些枚举类题目会提到某种特殊的密码锁,比如按下一个钮旁边的按钮状态也会跟着变之类的。这个小玩应跟枚举关系不大,只是一个3×3的方块,我开始叹服我的无聊程度了。
更新了,在最后,控制方法依然原始但是区域变成了4*4

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<windows.h>

#define MULTIPLE 5  // 一个点放大为若干乘以若干的方块
#define CUBE 3  // 这里不要改除非你愿意替我重做按键映射
typedef char cTable[MULTIPLE * CUBE][MULTIPLE * CUBE];

bool** CreateCube(int N)    // 一个 N*N 的空方块
{
    int i;
    bool** c = (bool**)calloc(N, sizeof(bool*));

    for(i = 0; i < N; i++)
        c[i] = (bool*)calloc(N, sizeof(bool));

    return c;
}

void DelCube(bool** cube, int N)
{
    int i;
    for(i = 0; i < N; i++)
        free(cube[i]);
    free(cube);
}

bool creverse(bool a)
{
    return a?false:true;
}

void Push(bool** c, int x, int y)
{
    int ix, iy;
    int minx = (x == 0)?0:x-1, maxx = (x == CUBE-1)?(CUBE-1):x+1;
    int miny = (y == 0)?0:y-1, maxy = (y == CUBE-1)?(CUBE-1):y+1;

    for(iy = miny; iy <= maxy; iy++)
        c[x][iy] = creverse(c[x][iy]);
    for(ix = minx; ix <= maxx; ix++)
        c[ix][y] = creverse(c[ix][y]);

    c[x][y] = creverse(c[x][y]);
    return;
}

bool** Mult(bool** c, int mltp, bool** ch)  // 小方块映射到大方块
{// c[mx][my] -> ch[x][y], with mltp times.
    int N = CUBE;
    int x, y, mx, my;

    for(my = 0; my < N; my++)
        for(mx = 0; mx < N; mx++)
        {
            if(c[mx][my])
            {
                for(y = my*mltp; y<my*mltp+mltp; y++)
                    for(x = mx*mltp; x<mx*mltp+mltp; x++)
                        ch[x][y] = true;
            }
            else
            {
                for(y = my*mltp; y<my*mltp+mltp; y++)
                    for(x = mx*mltp; x<mx*mltp+mltp; x++)
                        ch[x][y] = false;
            }
        }
    return;
}

void bShow(bool** ch, cTable cc, int width, char t)
{// 同边长方块, 布尔->字符 映射
    int x, y;
    for(y = 0; y < width; y++)
        for(x = 0; x < width; x++)
        {
            if(ch[x][y])
                cc[x][y] = t;
            else
                cc[x][y] = ' ';
        }
    return;
}

void cShow(cTable cc, int width)
{
    int x, y;
    for(y = 0; y < width; y++)
        for(x = 0; x < width; x++)
        {
            putchar(cc[x][y]);
            if(x == width-1)
                putchar('\n');
        }
}

int check(bool** cube)
{
    int x, y;
    int sum = 0;
    for(x = 0; x < CUBE; x++)
        for(y = 0; y < CUBE; y++)
        {
            if(cube[x][y])
                sum++;
        }
    return sum;
}

int main(void)
{
    bool** cube = CreateCube(CUBE);
    bool** ch = CreateCube(MULTIPLE * CUBE);
    cube[1][1] = true;

    cTable table;
    Mult(cube, MULTIPLE, ch);
    bShow(ch, table, MULTIPLE * CUBE, '*');
    cShow(table, MULTIPLE * CUBE);

    char input = '\0', c;
    printf("\n[q,w,e,a,s,d,z,x,c] to push.\n");
    while(input != EOF)
    {
        input = getch();
        switch (input)  // 按键映射
        {
            case 'q': { Push(cube, 0,0); break;}
            case 'w': { Push(cube, 1,0); break;}
            case 'e': { Push(cube, 2,0); break;}
            case 'a': { Push(cube, 0,1); break;}
            case 's': { Push(cube, 1,1); break;}
            case 'd': { Push(cube, 2,1); break;}
            case 'z': { Push(cube, 0,2); break;}
            case 'x': { Push(cube, 1,2); break;}
            case 'c': { Push(cube, 2,2); break;}
        }
        Mult(cube, MULTIPLE, ch);
        system("cls");
        bShow(ch, table, MULTIPLE * CUBE, '*');
        cShow(table, MULTIPLE * CUBE);
        printf("\n[q,w,e,a,s,d,z,x,c] to push.\n");
        if(check(cube) == 0)
            printf("\aGood...you have destroyed all the cube! Just kidding.\n");
        else if(check(cube) == CUBE*CUBE)
            printf("\aYou highlight all the cube!\n");
    }
    DelCube(cube, CUBE);
    DelCube(ch, MULTIPLE * CUBE);
    printf("~Cubes were be destroyed!\n");

    return 0;
}

vol.2:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<windows.h>

#define MULTIPLE 5  // 一个点放大为若干乘以若干的方块
#define CUBE 4  // 这里不要改除非你愿意替我重做按键映射
typedef char cTable[MULTIPLE * CUBE][MULTIPLE * CUBE];

bool** CreateCube(int N)    // 一个 N*N 的空方块
{
    int i;
    bool** c = (bool**)calloc(N, sizeof(bool*));

    for(i = 0; i < N; i++)
        c[i] = (bool*)calloc(N, sizeof(bool));

    return c;
}

void DelCube(bool** cube, int N)
{
    int i;
    for(i = 0; i < N; i++)
        free(cube[i]);
    free(cube);
}

bool creverse(bool a)
{
    return a?false:true;
}

void Push(bool** c, int x, int y)
{
    int ix, iy;
    int minx = (x == 0)?0:x-1, maxx = (x == CUBE-1)?(CUBE-1):x+1;
    int miny = (y == 0)?0:y-1, maxy = (y == CUBE-1)?(CUBE-1):y+1;

    for(iy = miny; iy <= maxy; iy++)
        c[x][iy] = creverse(c[x][iy]);
    for(ix = minx; ix <= maxx; ix++)
        c[ix][y] = creverse(c[ix][y]);

    c[x][y] = creverse(c[x][y]);
    return;
}

void Control(char ch, bool** cube)
{
    switch (ch)  // 按键映射
        {
            case '1': { Push(cube, 0,0); break;}
            case '2': { Push(cube, 1,0); break;}
            case '3': { Push(cube, 2,0); break;}
            case '4': { Push(cube, 3,0); break;}

            case 'q': { Push(cube, 0,1); break;}
            case 'w': { Push(cube, 1,1); break;}
            case 'e': { Push(cube, 2,1); break;}
            case 'r': { Push(cube, 3,1); break;}

            case 'a': { Push(cube, 0,2); break;}
            case 's': { Push(cube, 1,2); break;}
            case 'd': { Push(cube, 2,2); break;}
            case 'f': { Push(cube, 3,2); break;}

            case 'z': { Push(cube, 0,3); break;}
            case 'x': { Push(cube, 1,3); break;}
            case 'c': { Push(cube, 2,3); break;}
            case 'v': { Push(cube, 3,3); break;}
        }
    return;
}

bool** Mult(bool** c, int mltp, bool** ch)  // 小方块映射到大方块
{// c[mx][my] -> ch[x][y], with mltp times.
    int N = CUBE;
    int x, y, mx, my;

    for(my = 0; my < N; my++)
        for(mx = 0; mx < N; mx++)
        {
            if(c[mx][my])
            {
                for(y = my*mltp; y<my*mltp+mltp; y++)
                    for(x = mx*mltp; x<mx*mltp+mltp; x++)
                        ch[x][y] = true;
            }
            else
            {
                for(y = my*mltp; y<my*mltp+mltp; y++)
                    for(x = mx*mltp; x<mx*mltp+mltp; x++)
                        ch[x][y] = false;
            }
        }
    return;
}

void bShow(bool** ch, cTable cc, int width, char t)
{// 同边长方块, 布尔->字符 映射
    int x, y;
    for(y = 0; y < width; y++)
        for(x = 0; x < width; x++)
        {
            if(ch[x][y])
                cc[x][y] = t;
            else
                cc[x][y] = ' ';
        }
    return;
}

void cShow(cTable cc, int width)
{
    int x, y;
    for(y = 0; y < width; y++)
        for(x = 0; x < width; x++)
        {
            putchar(cc[x][y]);
            if(x == width-1)
            {
                putchar('|');
                putchar('\n');
            }

        }
    for(x = 0; x < width; x++)
        putchar('-');
    putchar('+');
    return;
}

int check(bool** cube)
{
    int x, y;
    int sum = 0;
    for(x = 0; x < CUBE; x++)
        for(y = 0; y < CUBE; y++)
        {
            if(cube[x][y])
                sum++;
        }
    return sum;
}

int main(void)
{
    bool** cube = CreateCube(CUBE);
    bool** ch = CreateCube(MULTIPLE * CUBE);
    cube[1][1] = true;
    cube[2][2] = true;
    char cubeType = '@';

    cTable table;
    Mult(cube, MULTIPLE, ch);
    bShow(ch, table, MULTIPLE * CUBE, cubeType);
    cShow(table, MULTIPLE * CUBE);

    char input = '\0', c;
    printf("\nPush \'1\' to \'v\' in keyboard\nto change a 4*4 cube.\n");
    while(input != EOF)
    {
        input = getch();

        Control(input,cube);

        Mult(cube, MULTIPLE, ch);
        system("cls");
        bShow(ch, table, MULTIPLE * CUBE, cubeType);
        cShow(table, MULTIPLE * CUBE);
        printf("\nPush \'1\' to \'v\' in keyboard\nto change a 4*4 cube.\n");
        if(check(cube) == 0)
            printf("\aGood... You have destroyed all the cube! Just kidding.\n");
        else if(check(cube) == CUBE*CUBE)
            printf("\aYou highlight all the cube!\n");
    }
    DelCube(cube, CUBE);
    DelCube(ch, MULTIPLE * CUBE);
    printf("~Cubes were be destroyed!\n");

    return 0;
}

猜你喜欢

转载自blog.csdn.net/asura319/article/details/78638221