---
# System prepended metadata

title: Tetris第一週進度
tags: [程式設計 IV]

---

# Tetris第一週進度

## 加入課程 🧑‍💻
![image](https://hackmd.io/_uploads/B1cFfLlfA.png)

[GitHubClass 課程連結](https://classroom.github.com/a/pSWxfdGV)

## EXAMPLE
![tetris_example](https://hackmd.io/_uploads/HkVcEo1MC.gif)


<!-- 逐一列出各項功能 -->
## 第一周功能
1. 結構- 每一個方塊的樣式
    ```c=
    typedef struct {
        ShapeId shape;
        Color color;
        int size;
        char rotates[4][4][4];
    }Shape;
    ```
2. 枚舉 - 每一種顏色
    ```c=
    typedef enum {
        RED = 41,
        GREEN,
        YELLOW,
        BLUE,
        PURPLE,
        CYAN,
        WHITE,
        BLACK = 0,
    }Color;
    ```
3. 枚舉 - 每一種形狀
    ```c=
    typedef enum {
        EMPTY = -1,
        I,
        J,
        L,
        O,
        S,
        T,
        Z
    }ShapeId;
    ```
4. 輸出形狀
    ![image](https://hackmd.io/_uploads/Sy-9MLefA.png)

可參考:
[枚舉相關資料](https://opensourcedoc.com/c-programming/enumeration/)
[C語言的顏色](https://blog.csdn.net/mr_wangning/article/details/77685371)
## 課堂Code 
```c=
#include <stdio.h>

typedef enum 
{
    EMPTY = -1,
    I,
    J,
    L,
    O,
    S,
    T,
    Z
}ShapeId;

typedef enum 
{
    RED = 41,
    GREEN,
    YELLOW,
    BLUE,
    PURPLE,
    CYAN,
    WHITE,
    BLACK = 0,
}Color;

typedef struct 
{
    ShapeId shape;
    Color color;
    int size;
    char rotates[4][4][4];
}Shape;

Shape shapes[7] = {
    {
        .shape = I,
        .color = CYAN,
        .size = 4,
        .rotates =
        {
            {
                {0, 0, 0, 0},
                {1, 1, 1, 1},
                {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, 0, 0},
                {0, 0, 0, 0},
                {1, 1, 1, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 1, 0, 0},
                {0, 1, 0, 0},
                {0, 1, 0, 0},
                {0, 1, 0, 0}
            }
        }
    },
    {
        .shape = J,
        .color = BLUE,
        .size = 3,
        .rotates =
        {
            {
                {1, 0, 0},
                {1, 1, 1},
                {0, 0, 0}
            },
            {
                {0, 1, 1},
                {0, 1, 0},
                {0, 1, 0}
            },
            {
                {0, 0, 0},
                {1, 1, 1},
                {0, 0, 1}
            },
            {
                {0, 1, 0},
                {0, 1, 0},
                {1, 1, 0}
            }
        }
    },
    {
        .shape = L,
        .color = YELLOW,
        .size = 3,
        .rotates =
        {
            {
                {0, 0, 1},
                {1, 1, 1},
                {0, 0, 0}
            },
            {
                {0, 1, 0},
                {0, 1, 0},
                {0, 1, 1}
            },
            {
                {0, 0, 0},
                {1, 1, 1},
                {1, 0, 0}
            },
            {
                {1, 1, 0},
                {0, 1, 0},
                {0, 1, 0}
            }
        }
    },
    {
        .shape = O,
        .color = WHITE,
        .size = 2,
        .rotates =
        {
            {
                {1, 1},
                {1, 1}
            },
            {
                {1, 1},
                {1, 1}
            },
            {
                {1, 1},
                {1, 1}
            },
            {
                {1, 1},
                {1, 1}
            }
        }
    },
    {
        .shape = S,
        .color = GREEN,
        .size = 3,
        .rotates =
        {
            {
                {0, 1, 1},
                {1, 1, 0},
                {0, 0, 0}
            },
            {
                {0, 1, 0},
                {0, 1, 1},
                {0, 0, 1}
            },
            {
                {0, 0, 0},
                {0, 1, 1},
                {1, 1, 0}
            },
            {
                {1, 0, 0},
                {1, 1, 0},
                {0, 1, 0}
            }
        }
    },
    {
        .shape = T,
        .color = PURPLE,
        .size = 3,
        .rotates =
        {
            {
                {0, 1, 0},
                {1, 1, 1},
                {0, 0, 0}
            },

                {{0, 1, 0},
                {0, 1, 1},
                {0, 1, 0}
            },
            {
                {0, 0, 0},
                {1, 1, 1},
                {0, 1, 0}
            },
            {
                {0, 1, 0},
                {1, 1, 0},
                {0, 1, 0}
            }
        }
    },
    {
        .shape = Z,
        .color = RED,
        .size = 3,
        .rotates =
        {
            {
                {1, 1, 0},
                {0, 1, 1},
                {0, 0, 0}
            },
            {
                {0, 0, 1},
                {0, 1, 1},
                {0, 1, 0}
            },
            {
                {0, 0, 0},
                {1, 1, 0},
                {0, 1, 1}
            },
            {
                {0, 1, 0},
                {1, 1, 0},
                {1, 0, 0}
            }
        }
    },
};

int main() 
{
    Color cur;
    // 幾種方塊 (目前只有 I)
    for (int i = 0; i < 7; i++) {
        //印出方塊
        //第幾個樣式
        for (int r = 0; r < 4; r++) {
            // 二維陣列的對應輸出
            for (int s = 0; s < shapes[i].size; s++) {
                for (int t = 0; t < shapes[i].size; t++) {
                    //如果是 0 就輸出白色
                    if (shapes[i].rotates[r][s][t] == 0) {
                        cur = WHITE;
                    }
                    else {
                        cur = shapes[i].color;
                    }
                    printf("\033[%dm  \033[0m", cur);
                }
                printf("\n");
            }
            printf("\n");
        }
    }
    return 0;
}
```