Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Help me to solve this problem because in output it is directly showing game over

What I have tried:

C++
#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
C++
#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();
    }
}
C++
#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

C++
#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();
    }
}

C++
#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;
}
Posted
Updated 8-Dec-17 11:18am
v3
Comments
Rick York 8-Dec-17 13:25pm    
You didn't include the part where the Board is instantiated and the gameLoop is called. As it is, there no code posted here where gameover is set so we can't help you at the moment.

Please note how your question has been revised. A description of your problem goes first and your code goes into the "what I have tried" section.
Member 13565402 8-Dec-17 13:27pm    
gameover is set for both classes
Member 13565402 8-Dec-17 13:29pm    
sorry for that in private section their is also a varial of bool named game over of class board

1 solution

You show some code that is not complete, we can't run it to reproduce the problem.
The debugger is the only tool that can help you.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900