Results 1 to 3 of 3

Thread: Four In A Row

  1. #1
    Join Date
    Mar 2019
    Posts
    13
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Unhappy Four In A Row

    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

    Qt Code:
    1. #include "game.h"
    2. #include "polje.h"
    3. #include "player.h"
    4. #include <QApplication>
    5. #include <QGraphicsRectItem>
    6. #include <QDebug>
    7.  
    8. Game::Game(QWidget *parent){
    9.  
    10. scene = new QGraphicsScene();
    11. scene->setSceneRect(0,0,1310,700);
    12.  
    13. setScene(scene); // == view->setScene(scene)
    14. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f); //(znebimo se sliderjov na desni strani) == view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f)
    15. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ; //(znebimo se sliderjov na spodnji strani) == view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff)
    16. setFixedSize(1310,650); // == view->setFixedSize(800,600);
    17. QApplication::setOverrideCursor(Qt::BlankCursor);
    18.  
    19. show();
    20. }
    21.  
    22. void Game::createPlatform(){
    23.  
    24. for(int j=0;j<11;j++){
    25. stevilo_zapolnenih_v_stolpcu[j] = 0;
    26. }
    27.  
    28. int i,k;
    29. for(i=0;i<5;i++){
    30. for(k=0;k<11;k++){
    31. p[i][k] = new Polje();
    32. p[i][k]->setPos(Polje::x,Polje::y);
    33. p[i][k]->setX(Polje::x + 120);
    34. //p = &polje[i][k];
    35. scene->addItem(p[i][k]);
    36.  
    37. }
    38. p[i][k]->setX(0);
    39. p[i][k]->setY(Polje::y + 110);
    40. }
    41.  
    42. player = new Player;
    43. player->setPos(655,110);
    44. scene->addItem(player);
    45.  
    46. player->setFlag(QGraphicsItem::ItemIsFocusable);
    47. player->setFocus();
    48.  
    49. }
    50.  
    51. void Game::victoryScreen(){
    52. scene->clear();
    53.  
    54.  
    55. restart->setRect(0,0,600,150);
    56. exit->setRect(0,0,600,150);
    57.  
    58. restart->setPos(width()/2-300,200);
    59. exit->setPos(width()/2-300,400);
    60.  
    61. scene->addItem(restart);
    62. scene->addItem(exit);
    63.  
    64. //connect(restart,SIGNAL(clicked()),this,SLOT(close( )));
    65.  
    66. }
    67.  
    68. int Game::getSteviloZapolneninhVStolpcu(int collumn){
    69. return stevilo_zapolnenih_v_stolpcu[collumn];
    70. }
    71.  
    72. void Game::setSteviloZapolneninhVStolpcu(int c, int stevilo_vrstice){
    73. stevilo_zapolnenih_v_stolpcu[c] = stevilo_vrstice;
    74. }
    To copy to clipboard, switch view to plain text mode 

    This is the Polje's cpp file ( Tile )

    Qt Code:
    1. #include "polje.h"
    2. #include "game.h"
    3.  
    4. #include <QDebug>
    5. #include <QGraphicsRectItem>
    6. #include <QGraphicsItem>
    7. #include <QObject>
    8.  
    9. extern Game *game;
    10.  
    11. int Polje::x = 0;
    12. int Polje::y = 125;
    13.  
    14. Polje::Polje(QGraphicsItem *parent): QGraphicsRectItem(parent){
    15.  
    16. setRect(0,0,120,110);
    17. setBrush(QBrush("blue"));
    18. setPen(Qt::NoPen);
    19.  
    20. circle = new QGraphicsEllipseItem(this);
    21. circle->setRect(5,5,100,100);
    22. circle->setBrush(QBrush("white"));
    23. //circle->setZValue(1);
    24.  
    25. circle_color = "";
    26. //setZValue(2);
    27. }
    28.  
    29. void Polje::setX(int a){
    30. x = a;
    31. }
    32.  
    33. void Polje::setY(int a){
    34. y = a;
    35. }
    36.  
    37. void Polje::setCircleColor(QString a){
    38. circle_color = a;
    39. }
    40.  
    41. QString Polje::getCircleColor(){
    42. return circle_color;
    43. }
    44.  
    45. void Polje::checkWinner(int vrst, int stol){
    46. //qDebug() << game->p[vrst][stol]->getCircleColor();
    47. }
    To copy to clipboard, switch view to plain text mode 

    This is Player's cpp file:

    Qt Code:
    1. #include "player.h"
    2. #include "game.h"
    3. #include "zeton.h"
    4.  
    5. #include <QDebug>
    6. #include <QGraphicsPolygonItem>
    7. #include <QGraphicsItem>
    8. #include <QObject>
    9. #include <QKeyEvent>
    10.  
    11. extern Game *game;
    12.  
    13. Player::Player(QGraphicsItem *parent): QGraphicsPolygonItem (parent){
    14.  
    15. QPolygonF triangle;
    16. triangle << QPointF(0,0);
    17. triangle << QPointF(-40,-50);
    18. triangle << QPointF(40,-50);
    19.  
    20. setPolygon(triangle);
    21.  
    22. color = "red";
    23. setBrush(QBrush("red"));
    24.  
    25. collumn = 5;
    26. }
    27.  
    28. void Player::keyPressEvent(QKeyEvent *event){
    29.  
    30. if(event->key()==Qt::Key_Left || event->key()==Qt::Key_A){
    31. if(x()-100 > 0){
    32. setPos(x()-120,y());
    33. collumn--;
    34. }
    35. }else if(event->key()==Qt::Key_Right || event->key()==Qt::Key_D){
    36. if(x()+100 < 1310){
    37. setPos(x()+120,y());
    38. collumn++;
    39. }
    40. }else if(event->key()==Qt::Key_Space){
    41. if(game->getSteviloZapolneninhVStolpcu(collumn) != 5 && Zeton::ZetonStoppedMoving == true){
    42. Zeton *zeton = new Zeton(color,collumn);
    43. zeton->setPos(x()-50,y()+20);
    44. game->scene->addItem(zeton);
    45. if(color == "red"){
    46. setBrush(QBrush("yellow"));
    47. color = "yellow";
    48. //qDebug() << "im in red";
    49. }else if(color == "yellow"){
    50. setBrush(QBrush("red"));
    51. color = "red";
    52. //qDebug() << "im in yellow";
    53. }
    54. }
    55. }else if(event->key()==Qt::Key_L){
    56. }
    57. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. #include "zeton.h"
    2. #include "game.h"
    3.  
    4. #include <QBrush>
    5. #include <QObject>
    6. #include <QGraphicsRectItem>
    7. #include <QGraphicsItem>
    8. #include <QTimer>
    9. #include <QDebug>
    10.  
    11. extern Game *game;
    12.  
    13. bool Zeton::ZetonStoppedMoving=true;
    14.  
    15. Zeton::Zeton(QString col, int c, QGraphicsItem *parent): QGraphicsEllipseItem(parent){
    16.  
    17. qDebug() << col;
    18. collumn = c;
    19. setRect(0,0,100,100);
    20. color = col;
    21. qDebug() << color;
    22. if(color == "red")
    23. setBrush(QBrush("red"));
    24. else if(color == "yellow")
    25. setBrush(QBrush("yellow"));
    26.  
    27. //setZValue(3);
    28. stevilo_vrstice_now = 5; // stevilo vrstice v kateri se zeton trenutno nahaja;
    29. stevilo_vrstic = 5; // stevilo vseh vrstic
    30. ZetonStoppedMoving = false;
    31.  
    32. cas = new QTimer;
    33. connect(cas,SIGNAL(timeout()),this,SLOT(premikZeto na()));
    34. cas->start(500);
    35.  
    36.  
    37. /*
    38.   switch(collumn){
    39.   case 0:{Zeton *zeton = new Zeton(collumn);}break;
    40.   case 1:{Zeton *zeton = new Zeton(collumn);}break;
    41.   case 2:{Zeton *zeton = new Zeton(collumn);}break;
    42.   case 3:{}break;
    43.   case 4:{}break;
    44.   case 5:{}break;
    45.   case 6:{}break;
    46.   case 7:{}break;
    47.   case 8:{}break;
    48.   case 9:{}break;
    49.   case 10:{}break;
    50.  
    51.   }
    52.   */
    53. }
    54.  
    55. void Zeton::premikZetona(){
    56.  
    57. if(game->getSteviloZapolneninhVStolpcu(collumn)<stevilo_vrstice_now-1){
    58. setPos(x(),y()+110);
    59. stevilo_vrstice_now--;
    60. //qDebug() << stevilo_vrstice_now;
    61. }else{
    62.  
    63. game->setSteviloZapolneninhVStolpcu(collumn,stevilo_vrst ice_now);
    64. ZetonStoppedMoving = true;
    65. //qDebug() << color;
    66.  
    67. if(color == "red"){
    68. game->p[stevilo_vrstic-stevilo_vrstice_now][collumn]->setCircleColor("red"); // if i comment
    69. } // out this section
    70. // circles spawn normally ( red first,then yellow and so on)
    71. else if(color == "yellow"){ // dunno why
    72. game->p[stevilo_vrstic-stevilo_vrstice_now][collumn]->setCircleColor("yellow");
    73. }
    74.  
    75. cas->stop();
    76. delete cas;
    77.  
    78. //qDebug() << color;
    79. //qDebug() << game->p[stevilo_vrstic-stevilo_vrstice_now][collumn-1]->getCircleColor();
    80. //qDebug() << stevilo_vrstic-stevilo_vrstice_now;
    81. //qDebug() << collumn-1;
    82.  
    83. //Polje::checkWinner(stevilo_vrstic-stevilo_vrstice_now,collumn-1);
    84. //game->victoryScreen();
    85. }
    86.  
    87. }
    To copy to clipboard, switch view to plain text mode 


    Any help is appreciated !!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,249
    Thanks
    36
    Thanked 1,508 Times in 1,459 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Four In A Row

    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,
    _

  3. #3
    Join Date
    Mar 2019
    Posts
    13
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Four In A Row

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.