#include <iostream>#include <vector>#include <cstdlib>#include <ctime>#include <conio.h>#include <windows.h>
using namespace std;
const int WIDTH = 15;const int HEIGHT = 20;
vector<vector<char>> grid(HEIGHT, vector<char>(WIDTH, ' '));
struct Point { int x, y;};
vector<vector<Point>> shapes = { { {0, 0}, {1, 0}, {0, 1}, {1, 1} }, // O { {0, 0}, {1, 0}, {2, 0}, {3, 0} }, // I { {0, 0}, {1, 0}, {2, 0}, {2, 1} }, // L { {0, 1}, {1, 1}, {2, 1}, {2, 0} }, // J { {0, 1}, {1, 1}, {1, 0}, {2, 0} }, // S { {0, 0}, {1, 0}, {1, 1}, {2, 1} }, // Z { {0, 1}, {1, 0}, {1, 1}, {2, 1} } // T};
Point currentPiece[4];Point nextPiece[4];
int currentShape;int nextShape;
void drawGrid() { system("cls"); vector<vector<char>> tempGrid = grid; for (int i = 1; i <= WIDTH + 1; i++) { cout<<"O"; } for (int i = 0; i < 4; i++) { int x = currentPiece[i].x; int y = currentPiece[i].y; if (x >= 0 && x < HEIGHT && y >= 0 && y < WIDTH) { tempGrid[x][y] = '#'; } }
for (int i = 0; i < HEIGHT; i++) { cout << "O"; for (int j = 0; j < WIDTH; j++) { cout << tempGrid[i][j]; } cout << "O"; cout << endl; } for (int i = 1; i <= WIDTH + 2; i++) { cout<<"O"; }}
void spawnPiece() { currentShape = nextShape; nextShape = rand() % shapes.size(); for (int i = 0; i < 4; i++) { currentPiece[i].x = shapes[currentShape][i].x; currentPiece[i].y = shapes[currentShape][i].y + WIDTH / 2 - 1; }}
bool isValidMove(int dx, int dy) { for (int i = 0; i < 4; i++) { int newX = currentPiece[i].x + dx; int newY = currentPiece[i].y + dy; if (newX >= HEIGHT || newY < 0 || newY >= WIDTH || (newX >= 0 && grid[newX][newY] != ' ')) { return false; } } return true;}
void gameOver(); void placePiece() { for (int i = 0; i < 4; i++) { if (currentPiece[i].x < 0) { gameOver(); return; } grid[currentPiece[i].x][currentPiece[i].y] = '#'; }}
void clearLines() { for (int i = HEIGHT - 1; i >= 0; i--) { bool full = true; for (int j = 0; j < WIDTH; j++) { if (grid[i][j] == ' ') { full = false; break; } } if (full) { for (int j = 0; j < WIDTH; j++) { grid[i][j] = ' '; } for (int k = i; k > 0; k--) { for (int j = 0; j < WIDTH; j++) { grid[k][j] = grid[k - 1][j]; } } i++; } }}
void gameOver() { system("cls"); cout << "Game Over!" << endl; exit(0);}
int main() { srand(time(0)); nextShape = rand() % shapes.size(); spawnPiece();
while (true) { if (_kbhit()) { char key = _getch(); if (key == 'a' && isValidMove(0, -1)) { for (int i = 0; i < 4; i++) { currentPiece[i].y--; } } if (key == 'd' && isValidMove(0, 1)) { for (int i = 0; i < 4; i++) { currentPiece[i].y++; } } if (key == 's' && isValidMove(1, 0)) { for (int i = 0; i < 4; i++) { currentPiece[i].x++; } } if (key == 'w') { Point original[4]; for (int i = 0; i < 4; i++) { original[i] = currentPiece[i]; } Point pivot = original[1]; for (int i = 0; i < 4; i++) { int dx = original[i].y - pivot.y; int dy = original[i].x - pivot.x; currentPiece[i].x = pivot.x - dx; currentPiece[i].y = pivot.y + dy; } if (!isValidMove(0, 0)) { for (int i = 0; i < 4; i++) { currentPiece[i] = original[i]; } } } }
if (isValidMove(1, 0)) { for (int i = 0; i < 4; i++) { currentPiece[i].x++; } } else { placePiece(); clearLines(); spawnPiece(); if (!isValidMove(0, 0)) { gameOver(); } }
drawGrid(); Sleep(500); }
return 0;}
// you may paste the entre forum body into Dev-C++
// Use WASD to move the Tetris pieces