├── .gitattributes ├── allPieces.h ├── board.h ├── main.cpp └── pieces.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /allPieces.h: -------------------------------------------------------------------------------- 1 | #ifndef pieceS_H 2 | #define pieceS_H 3 | #include"pieces.h" 4 | 5 | 6 | class pieceS :public pieces { // skiew type piece 7 | public: 8 | pieceS() { 9 | pie = new char*[4]; 10 | for (int i = 0; i < 4; i++) { 11 | pie[i] = new char[4]; 12 | } 13 | 14 | pie[0][0] = ' ', pie[0][1] = ' ', pie[0][2] = ' ', pie[0][3] = ' '; 15 | pie[1][0] = ' ', pie[1][1] = ' ', pie[1][2] = 'o', pie[1][3] = 'o'; 16 | pie[2][0] = ' ', pie[2][1] = ' ', pie[2][2] = 'o', pie[2][3] = ' '; 17 | pie[3][0] = ' ', pie[3][1] = 'o', pie[3][2] = 'o', pie[3][3] = ' '; 18 | 19 | } 20 | 21 | char **getPiece() { 22 | return pie; 23 | } 24 | 25 | ~pieceS() { 26 | if (pie != NULL) { 27 | for (int i = 0; i < 4; i++) { 28 | delete[] pie[i]; 29 | } 30 | delete[] pie; 31 | } 32 | } 33 | 34 | }; 35 | #endif // !pieceS_H 36 | 37 | ///////////////////////////////////////////////////////// 38 | #ifndef pieceL_H 39 | #define pieceL_H 40 | #include "pieces.h" 41 | 42 | class pieceL :public pieces { //L type piece 43 | public: 44 | pieceL() { 45 | pie = new char*[4]; 46 | for (int i = 0; i < 4; i++) { 47 | pie[i] = new char[4]; 48 | } 49 | 50 | pie[0][0] = ' ', pie[0][1] = ' ', pie[0][2] = ' ', pie[0][3] = ' '; 51 | pie[1][0] = ' ', pie[1][1] = ' ', pie[1][2] = 'o', pie[1][3] = ' '; 52 | pie[2][0] = ' ', pie[2][1] = ' ', pie[2][2] = 'o', pie[2][3] = ' '; 53 | pie[3][0] = ' ', pie[3][1] = ' ', pie[3][2] = 'o', pie[3][3] = 'o'; 54 | 55 | } 56 | char **getPiece() { 57 | return pie; 58 | } 59 | ~pieceL() { 60 | if (pie != NULL) { 61 | for (int i = 0; i < 4; i++) { 62 | delete[] pie[i]; 63 | } 64 | delete[] pie; 65 | } 66 | } 67 | }; 68 | #endif // !pieceL 69 | 70 | ///////////////////////////////////////////// 71 | 72 | #ifndef pieceI_H 73 | #define pieceI_H 74 | #include"pieces.h" 75 | 76 | class pieceI :public pieces { // straight line piece 77 | public: 78 | pieceI() { 79 | pie = new char*[4]; 80 | for (int i = 0; i < 4; i++) { 81 | pie[i] = new char[4]; 82 | } 83 | 84 | pie[0][0] = ' ', pie[0][1] = ' ', pie[0][2] = ' ', pie[0][3] = 'o'; 85 | pie[1][0] = ' ', pie[1][1] = ' ', pie[1][2] = ' ', pie[1][3] = 'o'; 86 | pie[2][0] = ' ', pie[2][1] = ' ', pie[2][2] = ' ', pie[2][3] = 'o'; 87 | pie[3][0] = ' ', pie[3][1] = ' ', pie[3][2] = ' ', pie[3][3] = 'o'; 88 | } 89 | char **getPiece() { 90 | return pie; 91 | } 92 | ~pieceI() { 93 | if (pie != NULL) { 94 | for (int i = 0; i < 4; i++) { 95 | delete[] pie[i]; 96 | } 97 | delete[] pie; 98 | } 99 | } 100 | }; 101 | 102 | #endif // !pieceI 103 | 104 | #ifndef pieceSq_H 105 | #define pieceSq_H 106 | #include"pieces.h" 107 | 108 | class pieceSq :public pieces {//square type piece 109 | public: 110 | pieceSq() { 111 | pie = new char*[4]; 112 | for (int i = 0; i < 4; i++) { 113 | pie[i] = new char[4]; 114 | } 115 | 116 | pie[0][0] = ' ', pie[0][1] = ' ', pie[0][2] = ' ', pie[0][3] = ' '; 117 | pie[1][0] = ' ', pie[1][1] = 'o', pie[1][2] = 'o', pie[1][3] = 'o'; 118 | pie[2][0] = ' ', pie[2][1] = 'o', pie[2][2] = 'o', pie[2][3] = 'o'; 119 | pie[3][0] = ' ', pie[3][1] = 'o', pie[3][2] = 'o', pie[3][3] = 'o'; 120 | } 121 | char **getPiece() { 122 | return pie; 123 | } 124 | ~pieceSq() { 125 | if (pie != NULL) { 126 | for (int i = 0; i < 4; i++) { 127 | delete[] pie[i]; 128 | } 129 | delete[] pie; 130 | } 131 | } 132 | }; 133 | 134 | 135 | #endif // !pieceSq 136 | 137 | #ifndef pieceT_H 138 | #define pieceT_H 139 | #include"pieces.h" 140 | 141 | class pieceT :public pieces { // T type piece 142 | public: 143 | pieceT() { 144 | pie = new char*[4]; 145 | for (int i = 0; i < 4; i++) { 146 | pie[i] = new char[4]; 147 | } 148 | 149 | pie[0][0] = ' ', pie[0][1] = ' ', pie[0][2] = ' ', pie[0][3] = ' '; 150 | pie[1][0] = ' ', pie[1][1] = 'o', pie[1][2] = 'o', pie[1][3] = 'o'; 151 | pie[2][0] = ' ', pie[2][1] = ' ', pie[2][2] = 'o', pie[2][3] = ' '; 152 | pie[3][0] = ' ', pie[3][1] = ' ', pie[3][2] = 'o', pie[3][3] = ' '; 153 | } 154 | char **getPiece() { 155 | return pie; 156 | } 157 | ~pieceT() { 158 | if (pie != NULL) { 159 | for (int i = 0; i < 4; i++) { 160 | delete[] pie[i]; 161 | } 162 | delete[] pie; 163 | } 164 | } 165 | }; 166 | 167 | #endif // !pieceT 168 | -------------------------------------------------------------------------------- /board.h: -------------------------------------------------------------------------------- 1 | #ifndef board_h 2 | #define board_hs 3 | #include"pieces.h" 4 | #include 5 | using namespace std; 6 | 7 | class board : public pieces{ 8 | int boardRows; 9 | int boardColumn; 10 | public: 11 | board() { 12 | boardRows = 19; // board size + piece array size 13 | boardColumn = 22; 14 | 15 | //allocate memory 16 | pie = new char*[boardColumn]; // row coloum of board 17 | for (int i = 0; i < boardColumn; i++) { 18 | pie[i] = new char[boardRows]; 19 | } 20 | 21 | for (int i = 0; i < boardColumn; i++) { 22 | for (int j = 0; j < boardRows; j++) { 23 | pie[i][j] = ' '; 24 | } 25 | } 26 | 27 | for (int i = 4; i < boardRows; i++) { // show upper line 28 | pie[0][i] = 'o'; 29 | } 30 | 31 | for (int i = 4; i < boardRows; i++) { // show line at bottom 32 | pie[boardColumn - 1][i] = 'o'; 33 | } 34 | 35 | for (int i = 1; i < boardColumn; i++) { // left line store in array 36 | pie[i][4] = 'o'; 37 | } 38 | 39 | for (int i = 0; i < boardColumn; i++) { // right line stroe in array 40 | pie[i][boardRows - 1] = 'o'; 41 | } 42 | } 43 | 44 | int getColumn() { 45 | return boardColumn; 46 | } 47 | int getRows() { 48 | return boardRows; 49 | } 50 | 51 | void printBoard() { 52 | for (int i = 0; i < boardColumn; i++) { 53 | for (int j = 0; j < boardRows; j++) { 54 | cout << pie[i][j]; 55 | } 56 | cout << "\n"; 57 | } 58 | } 59 | 60 | void clearScreen() { 61 | for (int i = 0; i < boardColumn; i++) { 62 | for (int j = 0; j < boardRows; j++) { 63 | cout << " "; 64 | } 65 | cout << " \n"; 66 | } 67 | } 68 | 69 | void remove(char ** arr, int x, int y) { 70 | for (int i = 0; i < 4; i++) { 71 | for (int j = 0; j < 4; j++) { 72 | if (arr[i][j] == 'o') { 73 | pie[y + i][x + j] = ' '; 74 | } 75 | } 76 | } 77 | } 78 | 79 | void setArray(char ** arr, int x, int y) { 80 | for (int i = 0; i < 4; i++) { 81 | for (int j = 0; j < 4; j++) { 82 | if (arr[i][j] == 'o') { 83 | pie[y + i][x + j] = arr[i][j]; 84 | } 85 | } 86 | } 87 | } 88 | }; 89 | 90 | 91 | #endif // !board_h 92 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include"board.h" 3 | #include"pieces.h" 4 | #include"allPieces.h" 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | class tetris{ 11 | board *Board; 12 | pieceS SType; 13 | pieceL LType; 14 | pieceSq SqType; 15 | pieceI IType; 16 | pieceT TType; 17 | bool BottomLine; // check is bottom line present 18 | char **shape; // hold randome shape 19 | public: 20 | tetris() { 21 | BottomLine = false; 22 | Board = new board; 23 | shape = NULL; 24 | } 25 | char ** randomeShape() { 26 | srand(time(0)); 27 | int index = rand() % 5; 28 | if (index == 0) { 29 | return SType.getPiece(); 30 | } 31 | else if (index == 1) { 32 | return LType.getPiece(); 33 | } 34 | else if (index == 2) { 35 | return SqType.getPiece(); 36 | } 37 | else if (index == 3) { 38 | return IType.getPiece(); 39 | } 40 | else if (index == 4) { 41 | return TType.getPiece(); 42 | } 43 | } 44 | bool isBootomLine(char **ptr, int y, int x) { 45 | 46 | //row 3rd 47 | if (ptr[3][3] == 'o' && Board->pie[x + 4][y + 3] == 'o') { 48 | return true; 49 | } 50 | else if (ptr[3][2] == 'o' && Board->pie[x + 4][y + 2] == 'o') { 51 | return true; 52 | } 53 | else if (ptr[3][1] == 'o' && Board->pie[x + 4][y + 1] == 'o') { 54 | return true; 55 | } 56 | else if (ptr[3][0] == 'o' && Board->pie[x + 4][y + 0] == 'o') { 57 | return true; 58 | } 59 | 60 | //row 2nd 61 | else if (ptr[2][3] == 'o' && Board->pie[x + 3][y + 3] == 'o') { 62 | return true; 63 | } 64 | else if (ptr[2][2] == 'o' && Board->pie[x + 3][y + 2] == 'o') { 65 | return true; 66 | } 67 | else if (ptr[2][1] == 'o' && Board->pie[x + 3][y + 1] == 'o') { 68 | return true; 69 | } 70 | else if (ptr[2][0] == 'o' && Board->pie[x + 3][y + 0] == 'o') { 71 | return true; 72 | } 73 | 74 | //row 1st 75 | else if (ptr[1][3] == 'o' && Board->pie[x + 2][y + 3] == 'o') { 76 | return true; 77 | } 78 | else if (ptr[1][2] == 'o' && Board->pie[x + 2][y + 2] == 'o') { 79 | return true; 80 | } 81 | else if (ptr[1][1] == 'o' && Board->pie[x + 2][y + 1] == 'o') { 82 | return true; 83 | } 84 | else if (ptr[1][0] == 'o' && Board->pie[x + 2][y + 0] == 'o') { 85 | return true; 86 | } 87 | 88 | //row 0th 89 | else if (ptr[0][3] == 'o' && Board->pie[x + 1][y + 3] == 'o') { 90 | return true; 91 | } 92 | else if (ptr[0][2] == 'o' && Board->pie[x + 1][y + 2] == 'o') { 93 | return true; 94 | } 95 | else if (ptr[0][1] == 'o' && Board->pie[x + 1][y + 1] == 'o') { 96 | return true; 97 | } 98 | else if (ptr[0][0] == 'o' && Board->pie[x + 1][y + 0] == 'o') { 99 | return true; 100 | } 101 | else { 102 | return false; 103 | } 104 | } 105 | bool isLeftFree(char **ptr, int y, int x) { 106 | if (ptr[0][0] == 'o' && Board->pie[x-1][y] == 'o') { 107 | return true; 108 | } 109 | else if (ptr[0][1] == 'o' && Board->pie[x-1][y+1] == 'o') { 110 | return true; 111 | } 112 | else if (ptr[0][2] == 'o' && Board->pie[x-1][y+2] == 'o') { 113 | return true; 114 | } 115 | else if (ptr[0][3] == 'o' && Board->pie[x-1][y+3] == 'o') { 116 | return true; 117 | } 118 | else { 119 | return false; 120 | } 121 | } 122 | 123 | 124 | void play() { 125 | 126 | do { 127 | int x = 9; 128 | int y = 1; 129 | shape = randomeShape(); 130 | 131 | BottomLine = false; 132 | int oldX = x; 133 | int oldY = y; 134 | while (!BottomLine) { // make a function that detect bottom line and return 1 if you detect 135 | Board->setArray(shape,x,y); 136 | Board->printBoard(); 137 | 138 | oldX = x; 139 | oldY = y; 140 | //keyboard input 141 | if (GetAsyncKeyState(VK_DOWN) != 0) { 142 | Sleep(250); 143 | } 144 | else { 145 | Sleep(500); 146 | } 147 | if (GetAsyncKeyState(VK_UP) != 0) {// change shape function here 148 | 149 | } 150 | 151 | if (GetAsyncKeyState(VK_LEFT) != 0 && !isLeftFree(shape,x,y)) { //your work 152 | if (x > 2) { // left line detection function here 153 | x--; 154 | } 155 | } 156 | 157 | if (GetAsyncKeyState(VK_RIGHT) != 0) {// your work 158 | if (x < 17) { // place rigth line detection function here 159 | x++; 160 | } 161 | } 162 | system("CLS"); 163 | Board->remove(shape, oldX, oldY); 164 | if (isBootomLine(shape, oldX, oldY)) { 165 | BottomLine = true; 166 | Board->setArray(shape, x, y); 167 | } 168 | if (BottomLine == false) { 169 | y++; 170 | } 171 | //now generate emptSpace That will remove the effect; 172 | } 173 | } while (1); 174 | } 175 | 176 | }; 177 | 178 | 179 | 180 | int main() { 181 | 182 | tetris game; 183 | game.play(); 184 | 185 | return 0; 186 | } -------------------------------------------------------------------------------- /pieces.h: -------------------------------------------------------------------------------- 1 | #ifndef piece_H 2 | #define piece_H 3 | #include 4 | using namespace std; 5 | 6 | class pieces { 7 | public: 8 | char **pie; 9 | int x; 10 | int y; 11 | pieces() { 12 | pie = NULL; 13 | x = 0; 14 | y = 0; 15 | } 16 | 17 | void setX(int x) { 18 | this->x = x; 19 | } 20 | 21 | void setY(int y) { 22 | this->y = y; 23 | } 24 | 25 | int getX() { 26 | return x; 27 | } 28 | 29 | int getY() { 30 | return y; 31 | } 32 | 33 | virtual void printBoard() { // print board 34 | for (int i = 0; i < 5; i++) { 35 | for (int j = 0; j < 5; j++) { 36 | cout << pie[i][j]; 37 | } 38 | cout << " "; 39 | } 40 | } 41 | 42 | virtual char** getPiece() { 43 | return pie; 44 | } 45 | 46 | ~pieces() { 47 | 48 | } 49 | }; 50 | #endif 51 | --------------------------------------------------------------------------------