Results 1 to 3 of 3

Thread: Help with my sudoku game

  1. #1
    Join Date
    Apr 2020
    Location
    Lithuania
    Posts
    24
    Thanks
    17
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Help with my sudoku game

    Hello again...

    I am stuck and I don't have any ideas how to solve it.

    My main.cpp code:
    #########################################
    #include "widget.h"

    #include <QApplication>

    #include <QtCore>
    #include <QtWidgets>

    int main(int argc, char * argv[])
    {
    QApplication app(argc,argv);
    QWidget mainWidget;
    mainWidget.setWindowTitle("Sudoku");

    QGridLayout *mainLayout = new QGridLayout(&mainWidget);
    mainLayout->setSpacing(0);

    for (int mr = 0; mr < 3; mr++) {
    for(int mc = 0; mc < 3; mc++) {
    QFrame *widget = new QFrame;
    widget->setFrameStyle(QFrame::Plain);
    widget->setFrameShape(QFrame::Box);

    QGridLayout *gridLayout = new QGridLayout(widget);
    gridLayout->setSpacing(0);
    gridLayout->setMargin(0);

    for(int r = 0; r < 3; r++) {
    for (int c = 0; c < 3; c++) {
    QLineEdit *tile = new QLineEdit("X");
    tile->setMaxLength(1);
    tile->setFixedSize(30,30);
    tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-style: solid; border-color: black black black black; }");
    tile->setAlignment(Qt::AlignCenter);
    tile->setFrame(QFrame::Box);
    gridLayout->addWidget(tile, r, c, 1, 1, Qt::AlignCenter);
    }
    }

    mainLayout->addWidget(widget, mr, mc, 1, 1, Qt::AlignCenter);
    }
    }
    mainWidget.show();
    return app.exec();
    }
    ################################################## ###################

    So, how can I know when my tile is selected by user?And how to get input from that specific tile(what he typed in the tile) , and how to get edited tile position ?;

    Thank you

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,484
    Thanks
    252
    Thanked 713 Times in 703 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Help with my sudoku game

    So, how can I know when my tile is selected by user?And how to get input from that specific tile(what he typed in the tile) , and how to get edited tile position ?;
    The most convenient way to do this is to create a custom widget to represent your sudoku board, and use that as your main widget instead of just plain QWidget. Call it something like "SudokuWidget". Move all of the widget and layout creation code you now have in main() into the constructor for this new widget.

    To monitor the user actions on the line edits, you need to connect the QLineEdit::editingFinished() signals to a slot in your SudokuWidget class. In that slot, you will determine which line edit was responsible (using QObject::sender() or QSignalMapper maybe), convert the line edit's text to an integer, and store that in the data structure you are using to keep track of the player's moves. By the way, you will also have to set some of the line edits to read-only if they contain the predetermined (fixed) numbers. The user can't be allowed to edit those.

    You should also consider using a QIntValidator for your line edits to prevent the user from typing anything they want.

    To make it easy to determine which line edit was changed, you could send all of the editingFinished() signals through a QSignalMapper. You can use the QSignalMapper::setMapping() "int" version to easily assign the row and column to each QLineEdit. Simply assign the ones digit to the row and the tens digit to the column, so as you go across the first row, the mapping values would be 00, 01, 02, 03, ..., 08, 09. The next row would be 10, 11, ..., 18, 19 and so forth. In your SudokuWidget slot that you connect to the QSignalMapper::mapped() signal, you can decode the row and column as row = i / 10 and column = i % 10, where "i" is the integer passed in the mapping() call.

    Qt Code:
    1. // SudokuWidget.h
    2.  
    3. class SudokuWidget : public QWidget
    4. {
    5. Q_OBJECT;
    6.  
    7. public:
    8. SudokuWidget( QWidget * parent );
    9. ~SudokuWidget();
    10.  
    11. private slots:
    12. void onMapped( int rowColId );
    13.  
    14. private:
    15. QSignalMapper * mapper;
    16. QVector< QLineEdit * > tiles;
    17. };
    18.  
    19. // SudokuWidget.cpp
    20.  
    21. SudokuWidget::Sudokuwidget( QWidget * parent )
    22. : QWidget( parent )
    23. {
    24. mapper = new QSignalMapper( this );
    25.  
    26. QIntValidator *pValidator = new QIntValidator( this );
    27. pValidator->setRange( 1, 9 );
    28.  
    29. // Create grid layout, create 9 frames, create 9 cells in each frame
    30.  
    31. // ... in the innermost loop where you create the tiles
    32.  
    33. for ( int row = 0; row < 9; row++ )
    34. {
    35. for ( int col = 0; col < 9; col++ )
    36. {
    37. QLineEdit * tile = new QLineEdit( this );
    38. int rowColId = col + row * 10;
    39. mapper.setMapping( tile, rowColId );
    40. connect( tile, &QLineEdit::editingFinished, mapper, &QSignalMapper::map );
    41.  
    42. tiles.push_back( tile ); // save the line edit so you can get to it later
    43. // ...
    44. }
    45. }
    46.  
    47. connect( mapper, SIGNAL( mapped( int ) ), this, SLOT( onMapped( int ) ) );
    48. // ...
    49. }
    50.  
    51. void SudokuWidget::onMapped( int rowColId )
    52. {
    53. int row = rowColId / 10;
    54. int col = rowColId % 10;
    55.  
    56. QLineEdit * tile = tiles[ row * 9 + col ];
    57.  
    58. // Retrieve the QString text from the QLineEdit, convert it to an int,
    59. // store it in your game data structure, check to see if it is correct,
    60. // whatever your game decides to do.
    61. }
    To copy to clipboard, switch view to plain text mode 

    Google will help you find examples of other Sudoku games written in Qt / C++.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    laurynas2003 (7th April 2020)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,484
    Thanks
    252
    Thanked 713 Times in 703 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Help with my sudoku game

    I forgot to set the validator on the line edit. So add this into the loop that creates each tile:

    Qt Code:
    1. tile->setValidator( pValidator );
    To copy to clipboard, switch view to plain text mode 

    Now the line edit will refuse to accept anything the user types except a single digit between 1 - 9. Some sudoku games allow the user to put multiple numbers in the same square (in other words, all the possibilities for that square), then erase them one by one. If you implement that, then the validator will not work for that purpose.

    You could derive a different validator in that case, and it could be smarter. For example, if it allows the user to type multiple numbers, then it should not allow typing the same number more than once.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    laurynas2003 (7th April 2020)

Similar Threads

  1. Sudoku grid
    By laurynas2003 in forum Newbie
    Replies: 2
    Last Post: 6th April 2020, 07:27
  2. Replies: 6
    Last Post: 21st April 2019, 00:28
  3. Sudoku front end
    By tom989 in forum Newbie
    Replies: 1
    Last Post: 20th August 2013, 22:49
  4. Replies: 1
    Last Post: 22nd May 2010, 07:38
  5. IQ Game
    By qtgears in forum Qt-based Software
    Replies: 0
    Last Post: 6th October 2009, 08:24

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.