Help me to solve this problem because in output it is directly showing game over
What I have tried:
#ifndef BLOCK_H
#define BLOCK_H
#include "Coordinates.h"
class Block{
private:
Coordinates coordinates;
int block[4][4];
int blockShape;
int arr[22][13];
int board[22][13];
int x=4;
int y=0;
bool gameover=false;
public:
Block(){}
int blocks();
bool makeBlock();
void moveBlock(int x2, int y2);
bool rotateBlock();
void destructBlock();
void collidable();
bool isCollide(int x2, int y2);
};
#endif
#include <iostream>
#include <vector>
#include "Block.h"
#include "Board.h"
using namespace std;
int shapeOfBlocks[4][4][4] =
{
{
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 }
},
{
{ 0, 0, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 0, 0 },
{ 0, 1, 0, 0 }
},
{
{ 0, 0, 1, 0 },
{ 0, 1, 1, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 0, 0 }
},
{
{ 0, 1, 0, 0 },
{ 0, 1, 1, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 0 }
}
};
bool Block::makeBlock()
{
x=4;
y=0;
int blockShape=0;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
block[i][j]=0;
block[i][j]=shapeOfBlocks[blockShape][i][j];
}
}
for(int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
board[i][j+4]=arr[i][j+4]+block[i][j];
if (board[i][j+4]>1)
{
gameover=true;
return true;
}
}
}
return false;
}
void Block::moveBlock(int x2, int y2)
{
int x=4, y=0;
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
board[y+i][x+j]-=block[i][j];
}
}
x=x2;
y=y2;
for(int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
board[y+i][x+j]+= block[i][j];
}
}
Board obj;
obj.display();
}
bool Block::rotateBlock()
{
vector<vector<int>> temp(4, vector<int>(4, 0));
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
temp[i][j]=block[i][j];
}
}
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
block[i][j]=temp[3-j][i];
}
}
if (isCollide(x, y))
{
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
block[i][j]=temp[i][j];
}
}
return true;
}
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
board[y+i][x+j]-=temp[i][j];
board[y+i][x+j]+=block[i][j];
}
}
Board obj;
obj.display();
return false;
}
void Block::collidable()
{
for (int i=0; i<21; i++)
{
for (int j=0; j<12; j++)
{
arr[i][j]=board[i][j];
}
}
}
bool Block::isCollide(int x2, int y2)
{
for (int i=0; i<4; i++)
{
for (int j=0; j<4; j++)
{
if ((block[i][j] && arr[y2 + i][x2 + j])!=0)
{
return true;
}
}
}
return false;
}
void Block::destructBlock()
{
if (!isCollide(x, y+1))
{
moveBlock(x, y+1);
}
else
{
Board obj;
collidable();
makeBlock();
obj.display();
}
}
#ifndef BOARD_H
#define BOARD_H
#include "Block.h"
class Board{
private:
Block block;
int board[22][13];
int arr[22][13];
int GAMESPEED=20000;
int x=4;
int y=0;
public:
Board(){}
int gameOver();
void gameLoop();
void display();
void initGame();
void userInput();
};
#endif
#include <iostream>
#include <vector>
#include <conio.h>
#include "Board.h"
using namespace std;
int Board::gameOver()
{
char a;
cout << " ##### # # # ####### ####### # # ####### ######\n" ;
cout << "# # # # ## ## # # # # # # # #\n";
cout << "# # # # # # # # # # # # # # #\n";
cout << "# #### # # # # # ##### # # # # ##### ######\n";
cout << "# # ####### # # # # # # # # # #\n";
cout << "# # # # # # # # # # # # # #\n";
cout << " ##### # # # # ####### ####### # ####### # #\n";
cout << "\n\nPress any key and enter\n";
cin >> a;
return 0;
}
void Board::gameLoop()
{
int time = 0;
initGame();
while (!gameover)
{
if (kbhit())
{
userInput();
}
if (time < GAMESPEED)
{
time++;
}
else
{
block.destructBlock();
time = 0;
}
}
}
void Board::display()
{
system("cls");
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 12; j++)
{
switch (board[i][j])
{
case 0:
std::cout << " " ;
break;
case 9:
std::cout << "@";
break;
default:
std::cout << "#" ;
break;
}
}
std::cout << std::endl;
}
if (gameover)
{
system("cls");
gameOver();
}
}
void Board::initGame()
{
for (int i = 0; i <= 20; i++)
{
for (int j = 0; j <= 11; j++)
{
if ((j == 0) || (j == 11) || (i == 20))
{
board[i][j] = arr[i][j] = 9;
}
else
{
board[i][j] = arr[i][j] = 0;
}
}
}
block.makeBlock();
display();
}
void Board::userInput()
{
char key;
key = getch();
switch (key)
{
case 'd':
if (!block.isCollide(x+1, y))
{
block.moveBlock(x+1, y);
}
break;
case 'a':
if (!block.isCollide(x-1, y))
{
block.moveBlock(x-1, y);
}
break;
case 's':
if (!block.isCollide(x, y+1))
{
block.moveBlock(x, y+1);
}
break;
case ' ':
block.rotateBlock();
}
}
#ifndef COORDINATES_H
#define COORDINATES_H
class Coordinates{
private:
int x;
int y;
public:
Coordinates(){}
Coordinates(int a,int b);
void setX(int x);
void setY(int y);
int getX();
int getY();
};
#endif
#include <iostream>
#include "Coordinates.h"
using namespace std;
Coordinates::Coordinates(int x, int y):x(x),y(y){}
void Coordinates::setX(int x)
{
x=4;
}
void Coordinates::setY(int y)
{
y=0;
}
int Coordinates::getX()
{
return x;
}
int Coordinates::getY()
{
return y;
}