PDA

View Full Version : Four In A Row



MongKong
4th April 2019, 18:47
I have another problem !!!
So far i made it that when i press space i spawn circles and they slowly go down. But when im in the last column and i press it spawns red circle, and when i press space again it spawns red circle again and it shouldnt !!! at every other column it normally spawns, first red then yellow and so on, but in the last one it doesn't do that...

here is the clip:

https://streamable.com/84yg8

This is Game cpp file



#include "game.h"
#include "polje.h"
#include "player.h"
#include <QApplication>
#include <QGraphicsRectItem>
#include <QDebug>

Game::Game(QWidget *parent){

scene = new QGraphicsScene();
scene->setSceneRect(0,0,1310,700);

setScene(scene); // == view->setScene(scene)
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f); //(znebimo se sliderjov na desni strani) == view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f)
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ; //(znebimo se sliderjov na spodnji strani) == view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff)
setFixedSize(1310,650); // == view->setFixedSize(800,600);
QApplication::setOverrideCursor(Qt::BlankCursor);

show();
}

void Game::createPlatform(){

for(int j=0;j<11;j++){
stevilo_zapolnenih_v_stolpcu[j] = 0;
}

int i,k;
for(i=0;i<5;i++){
for(k=0;k<11;k++){
p[i][k] = new Polje();
p[i][k]->setPos(Polje::x,Polje::y);
p[i][k]->setX(Polje::x + 120);
//p = &polje[i][k];
scene->addItem(p[i][k]);

}
p[i][k]->setX(0);
p[i][k]->setY(Polje::y + 110);
}

player = new Player;
player->setPos(655,110);
scene->addItem(player);

player->setFlag(QGraphicsItem::ItemIsFocusable);
player->setFocus();

}

void Game::victoryScreen(){
scene->clear();

QGraphicsRectItem *restart = new QGraphicsRectItem;
QGraphicsRectItem *exit = new QGraphicsRectItem;

restart->setRect(0,0,600,150);
exit->setRect(0,0,600,150);

restart->setPos(width()/2-300,200);
exit->setPos(width()/2-300,400);

scene->addItem(restart);
scene->addItem(exit);

//connect(restart,SIGNAL(clicked()),this,SLOT(close( )));

}

int Game::getSteviloZapolneninhVStolpcu(int collumn){
return stevilo_zapolnenih_v_stolpcu[collumn];
}

void Game::setSteviloZapolneninhVStolpcu(int c, int stevilo_vrstice){
stevilo_zapolnenih_v_stolpcu[c] = stevilo_vrstice;
}


This is the Polje's cpp file ( Tile )



#include "polje.h"
#include "game.h"

#include <QDebug>
#include <QGraphicsRectItem>
#include <QGraphicsItem>
#include <QObject>

extern Game *game;

int Polje::x = 0;
int Polje::y = 125;

Polje::Polje(QGraphicsItem *parent): QGraphicsRectItem(parent){

setRect(0,0,120,110);
setBrush(QBrush("blue"));
setPen(Qt::NoPen);

circle = new QGraphicsEllipseItem(this);
circle->setRect(5,5,100,100);
circle->setBrush(QBrush("white"));
//circle->setZValue(1);

circle_color = "";
//setZValue(2);
}

void Polje::setX(int a){
x = a;
}

void Polje::setY(int a){
y = a;
}

void Polje::setCircleColor(QString a){
circle_color = a;
}

QString Polje::getCircleColor(){
return circle_color;
}

void Polje::checkWinner(int vrst, int stol){
//qDebug() << game->p[vrst][stol]->getCircleColor();
}


This is Player's cpp file:



#include "player.h"
#include "game.h"
#include "zeton.h"

#include <QDebug>
#include <QGraphicsPolygonItem>
#include <QGraphicsItem>
#include <QObject>
#include <QKeyEvent>

extern Game *game;

Player::Player(QGraphicsItem *parent): QGraphicsPolygonItem (parent){

QPolygonF triangle;
triangle << QPointF(0,0);
triangle << QPointF(-40,-50);
triangle << QPointF(40,-50);

setPolygon(triangle);

color = "red";
setBrush(QBrush("red"));

collumn = 5;
}

void Player::keyPressEvent(QKeyEvent *event){

if(event->key()==Qt::Key_Left || event->key()==Qt::Key_A){
if(x()-100 > 0){
setPos(x()-120,y());
collumn--;
}
}else if(event->key()==Qt::Key_Right || event->key()==Qt::Key_D){
if(x()+100 < 1310){
setPos(x()+120,y());
collumn++;
}
}else if(event->key()==Qt::Key_Space){
if(game->getSteviloZapolneninhVStolpcu(collumn) != 5 && Zeton::ZetonStoppedMoving == true){
Zeton *zeton = new Zeton(color,collumn);
zeton->setPos(x()-50,y()+20);
game->scene->addItem(zeton);
if(color == "red"){
setBrush(QBrush("yellow"));
color = "yellow";
//qDebug() << "im in red";
}else if(color == "yellow"){
setBrush(QBrush("red"));
color = "red";
//qDebug() << "im in yellow";
}
}
}else if(event->key()==Qt::Key_L){
}
}


and this is Zeton's cpp file ( circles (red and yellow)) --- if i comment out the if(color == "red") ... else if(color == "yellow") ... section circles spawn on the last column normally, first red, then yellow and so on, but i dont know why !!! plus i need that function inside the if and else statements to be called.



#include "zeton.h"
#include "game.h"

#include <QBrush>
#include <QObject>
#include <QGraphicsRectItem>
#include <QGraphicsItem>
#include <QTimer>
#include <QDebug>

extern Game *game;

bool Zeton::ZetonStoppedMoving=true;

Zeton::Zeton(QString col, int c, QGraphicsItem *parent): QGraphicsEllipseItem(parent){

qDebug() << col;
collumn = c;
setRect(0,0,100,100);
color = col;
qDebug() << color;
if(color == "red")
setBrush(QBrush("red"));
else if(color == "yellow")
setBrush(QBrush("yellow"));

//setZValue(3);
stevilo_vrstice_now = 5; // stevilo vrstice v kateri se zeton trenutno nahaja;
stevilo_vrstic = 5; // stevilo vseh vrstic
ZetonStoppedMoving = false;

cas = new QTimer;
connect(cas,SIGNAL(timeout()),this,SLOT(premikZeto na()));
cas->start(500);


/*
switch(collumn){
case 0:{Zeton *zeton = new Zeton(collumn);}break;
case 1:{Zeton *zeton = new Zeton(collumn);}break;
case 2:{Zeton *zeton = new Zeton(collumn);}break;
case 3:{}break;
case 4:{}break;
case 5:{}break;
case 6:{}break;
case 7:{}break;
case 8:{}break;
case 9:{}break;
case 10:{}break;

}
*/
}

void Zeton::premikZetona(){

if(game->getSteviloZapolneninhVStolpcu(collumn)<stevilo_vrstice_now-1){
setPos(x(),y()+110);
stevilo_vrstice_now--;
//qDebug() << stevilo_vrstice_now;
}else{

game->setSteviloZapolneninhVStolpcu(collumn,stevilo_vrst ice_now);
ZetonStoppedMoving = true;
//qDebug() << color;

if(color == "red"){
game->p[stevilo_vrstic-stevilo_vrstice_now][collumn]->setCircleColor("red"); // if i comment
} // out this section
// circles spawn normally ( red first,then yellow and so on)
else if(color == "yellow"){ // dunno why
game->p[stevilo_vrstic-stevilo_vrstice_now][collumn]->setCircleColor("yellow");
}

cas->stop();
delete cas;

//qDebug() << color;
//qDebug() << game->p[stevilo_vrstic-stevilo_vrstice_now][collumn-1]->getCircleColor();
//qDebug() << stevilo_vrstic-stevilo_vrstice_now;
//qDebug() << collumn-1;

//Polje::checkWinner(stevilo_vrstic-stevilo_vrstice_now,collumn-1);
//game->victoryScreen();
}

}



Any help is appreciated !!!!

anda_skoa
11th April 2019, 22:04
Does it always happen on the last column even if you remove or add a column?

If I see this correctly your "state", i.e. which color is next, is in Player::color, correct?

Add a private Player::setColor() method and then make sure no other part of the code writes "color" directly.
Then put a log input into that and see if it is being called twice with the same color.

If yes, then either there is a call for the other color missing or there is a wrong second call with the same color.

Cheers,
_

MongKong
12th April 2019, 16:35
anda_skoa i'm sorry for being an idiot and wasting your time.... I made a table for Four in a Row tab[5][11] but then i created another table tab2 which stores how many circles are in each collumn and i declared that table like this: tab2[10] instead of tab2[11] ... was one short....

Thank you tho.