#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <Windows.h>
using namespace std;
// 게임 보드 크기
const int BOARD_WIDTH = 10;
const int BOARD_HEIGHT = 20;
// 테트리미노 모양 (각각 4x4 그리드에서 블록이 어떻게 위치하는지)
vector<vector<vector<int>>> tetrominoes = {
{{1, 1, 1, 1}}, // I
{{1, 1}, {1, 1}}, // O
{{0, 1, 0}, {1, 1, 1}}, // T
{{1, 0, 0}, {1, 1, 1}}, // L
{{0, 0, 1}, {1, 1, 1}}, // J
{{1, 1, 0}, {0, 1, 1}}, // S
{{0, 1, 1}, {1, 1, 0}} // Z
};
// 게임 보드 클래스
class Board {
public:
vector<vector<int>> board;
Board() {
board.resize(BOARD_HEIGHT, vector<int>(BOARD_WIDTH, 0));
}
// 보드 출력
void display() {
system("cls"); // 콘솔 화면 지우기
for (int i = 0; i < BOARD_HEIGHT; ++i) {
for (int j = 0; j < BOARD_WIDTH; ++j) {
if (board[i][j] == 0)
cout << ".";
else
cout << "#";
}
cout << endl;
}
}
// 행 삭제 및 점수 증가
void clearFullLines() {
for (int i = BOARD_HEIGHT - 1; i >= 0; --i) {
bool fullLine = true;
for (int j = 0; j < BOARD_WIDTH; ++j) {
if (board[i][j] == 0) {
fullLine = false;
break;
}
}
if (fullLine) {
// 행을 위로 밀기
for (int k = i; k > 0; --k) {
for (int j = 0; j < BOARD_WIDTH; ++j) {
board[k][j] = board[k - 1][j];
}
}
for (int j = 0; j < BOARD_WIDTH; ++j) {
board[0][j] = 0; // 첫 번째 행은 빈 칸으로 설정
}
}
}
}
};
// 테트리미노 클래스
class Tetromino {
public:
vector<vector<int>> shape;
int x, y;
Tetromino(int type) {
shape = tetrominoes[type];
x = BOARD_WIDTH / 2 - shape[0].size() / 2;
y = 0;
}
// 회전
void rotate() {
int rows = shape.size();
int cols = shape[0].size();
vector<vector<int>> newShape(cols, vector<int>(rows));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
newShape[j][rows - 1 - i] = shape[i][j];
}
}
shape = newShape;
}
// 이동
void move(int dx, int dy) {
x += dx;
y += dy;
}
};
// 게임 클래스
class Game {
public:
Board board;
Tetromino* currentTetromino;
Game() {
srand(time(0));
spawnTetromino();
}
// 새로운 테트리미노 생성
void spawnTetromino() {
int type = rand() % 7;
currentTetromino = new Tetromino(type);
}
// 테트리미노를 보드에 고정
void placeTetromino() {
for (int i = 0; i < currentTetromino->shape.size(); ++i) {
for (int j = 0; j < currentTetromino->shape[i].size(); ++j) {
if (currentTetromino->shape[i][j] != 0) {
board.board[currentTetromino->y + i][currentTetromino->x + j] = 1;
}
}
}
board.clearFullLines(); // 완전한 행을 지움
spawnTetromino(); // 새로운 테트리미노 생성
}
// 게임 진행
void update() {
board.display();
placeTetromino();
}
// 키 입력 처리
void processInput() {
if (_kbhit()) {
char ch = _getch();
if (ch == 'a') { // 왼쪽 이동
currentTetromino->move(-1, 0);
}
if (ch == 'd') { // 오른쪽 이동
currentTetromino->move(1, 0);
}
if (ch == 's') { // 아래로 이동
currentTetromino->move(0, 1);
}
if (ch == 'w') { // 회전
currentTetromino->rotate();
}
}
}
};
int main() {
Game game;
while (true) {
game.processInput(); // 입력 처리
game.update(); // 게임 상태 업데이트
Sleep(500); // 게임 속도 조절 (50ms)
}
return 0;
}
안되네.
댓글 0