Results 1 to 19 of 19

Thread: Random Sudoku board generator

Threaded View

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

    Default Random Sudoku board generator

    Hello ,

    So i am trying to generate a sudoku board that would be solvable, with 20 random digits in random positions , but when I run the code numbers appear in different places then they should be , for example
    my function generates y = 8 x = 8 value = 5 , but on my board that tile is empty and 5 appears in other position. Could someone tell me how to fix this?

    Thank you

    Qt Code:
    1. // widget.h
    2. #ifndef WIDGET_H
    3. #define WIDGET_H
    4.  
    5. #include <QWidget>
    6.  
    7. QT_BEGIN_NAMESPACE
    8. class QLineEdit;
    9. QT_END_NAMESPACE
    10.  
    11.  
    12.  
    13. class SudokuWidget : public QWidget
    14. {
    15. Q_OBJECT;
    16.  
    17. public:
    18. SudokuWidget( QWidget *parent );
    19.  
    20.  
    21. private slots:
    22. // void onMapped( int rowColId );
    23. // void on_pushButton_clicked();
    24.  
    25. private:
    26. // QSignalMapper *mapper;
    27. QVector < QLineEdit *> tiles;
    28.  
    29. QVector <QPair <int, int>> random_pos_value;
    30. void random_position(int digits, QVector < QPair <int, int> > &pos_value);
    31.  
    32. bool is_legal_row(int value, int y, int x, QVector< QVector < QLineEdit* >> &tiles1);
    33. bool is_legal_colum(int value, int y, int x, QVector< QVector < QLineEdit* >> &tiles1);
    34.  
    35. int box_position(int y, int x);
    36. bool is_legal_box(int value, int y,int x, QVector< QVector < QLineEdit* >> &tiles1);
    37. };
    38.  
    39.  
    40. #endif // WIDGET_H
    41.  
    42. // widget.cpp
    43.  
    44. #include "widget.h"
    45. #include "ui_widget.h"
    46.  
    47. #include <QGridLayout>
    48. #include <QFrame>
    49. #include <QLineEdit>
    50. #include <QIntValidator>
    51. #include <QMessageBox>
    52. #include <QPushButton>
    53. #include <QRandomGenerator>
    54. #include <QDateTime>
    55. #include <QSet>
    56. #include <iostream>
    57.  
    58.  
    59. SudokuWidget::SudokuWidget(QWidget * parent)
    60. : QWidget(parent) {
    61.  
    62. QIntValidator *pValidator = new QIntValidator( this );
    63. pValidator->setRange( 1, 9 );
    64.  
    65. int square_counter = 0;
    66. random_position(20, random_pos_value);
    67.  
    68.  
    69. QGridLayout *mainLayout = new QGridLayout(this);
    70. mainLayout->setSpacing(0);
    71.  
    72. for (int mr = 0; mr < 3; mr++) {
    73. for(int mc = 0; mc < 3; mc++) {
    74.  
    75. QFrame *widget = new QFrame;
    76. widget->setFrameStyle(QFrame::Plain);
    77. widget->setFrameShape(QFrame::Box);
    78.  
    79. QGridLayout *gridLayout = new QGridLayout(widget);
    80. gridLayout->setSpacing(0);
    81. gridLayout->setMargin(0);
    82.  
    83. for(int r = 0; r < 3; r++) {
    84. for (int c = 0; c < 3; c++) {
    85.  
    86. QLineEdit *tile = new QLineEdit("");
    87. square_counter++;
    88.  
    89. tile->setMaxLength(1);
    90. tile->setFixedSize(30,30);
    91. tile->setStyleSheet("QLineEdit{ border-width: 1.5px; border-style: solid; border-color: black black black black; }");
    92. tile->setAlignment(Qt::AlignCenter);
    93. tile->setFrame(QFrame::Box);
    94. tile->setValidator(pValidator);
    95.  
    96. tiles.push_back( tile );
    97.  
    98. gridLayout->addWidget(tile, r, c, 1, 1, Qt::AlignCenter);
    99. }
    100. }
    101. mainLayout->addWidget(widget, mr, mc, 1, 1, Qt::AlignCenter);
    102. }
    103. }
    104. for (int i = 0;i < 80; i++){
    105. for (int j = 0;j < 20; j++){
    106. if (random_pos_value[j].first == i){
    107. tiles[i]->setText(QString::number(random_pos_value[j].second));
    108. tiles[i]->setReadOnly(true);
    109. }
    110. }
    111. }
    112.  
    113. setLayout(mainLayout);
    114. }
    115.  
    116.  
    117. bool SudokuWidget::is_legal_row(int value,int y, int x, QVector< QVector < QLineEdit* >> &tiles1)
    118. {
    119. int count = 0;
    120. for (int i = 0;i < 9; i++){
    121. if (QString::number(value) == tiles1[y][i]->text()){
    122. count++;
    123. }
    124. }
    125. if (count == 1){
    126. return true;
    127. }
    128. return false;
    129. }
    130.  
    131. bool SudokuWidget::is_legal_colum(int value,int y, int x, QVector<QVector<QLineEdit *> > &tiles1)
    132. {
    133. int count = 0;
    134. for (int i = 0;i < 9; i++){
    135. if (QString::number(value) == tiles1[i][x]->text()){
    136. count++;
    137. }
    138. }
    139. if (count == 1){
    140. return true;
    141. }
    142. return false;
    143. }
    144.  
    145. void SudokuWidget::random_position(int digits, QVector <QPair <int, int> > &random_pos_value)
    146. {
    147.  
    148. QVector < QVector < QLineEdit* > > random_boards(81);
    149. for (int y1 = 0;y1 < 9; y1++){
    150. for (int x1 = 0;x1 < 9; x1++){
    151. QLineEdit *line_edit = new QLineEdit("0");
    152. random_boards[y1].push_back(line_edit);
    153. }
    154. }
    155. qsrand(QDateTime::currentMSecsSinceEpoch() / 1000);
    156. int count = 0;
    157. int value;
    158. while (true){
    159. int position = (qrand() % 80) + 0;
    160. int y = position / 9;
    161. int x = position % 9;
    162. if (random_boards[y][x]->text() == "0"){
    163. while (true){
    164. value = (qrand() % 9) + 1;
    165. random_boards[y][x]->setText(QString::number(value));
    166. if (is_legal_box(value, y, x, random_boards) && is_legal_colum(value, y, x, random_boards) ){
    167. break;
    168. }
    169. random_boards[y][x]->setText("0");
    170. }
    171. QPair <int, int> values = {position,value};
    172. random_pos_value.push_back(values);
    173. count++;
    174. if (count == digits){
    175. break;
    176. }
    177. }
    178. }
    179. }
    180.  
    181.  
    182. int SudokuWidget::box_position(int y, int x)
    183. {
    184. return 3*(y/3) + (x/3);
    185. }
    186.  
    187. bool SudokuWidget::is_legal_box(int value, int y1, int x1, QVector<QVector<QLineEdit *> > &tiles1)
    188. {
    189. //box
    190. int count = 0;
    191. int square_position = box_position(y1, x1);
    192. QVector <QString> values;
    193. for (int y = (square_position / 3) * 3; y < ((square_position / 3) * 3) + 3;y++){
    194. for (int x = (square_position % 3) * 3; x < ((square_position % 3) * 3) + 3;x++){
    195. if (tiles1[y][x]->text() == QString::number(value)){
    196. count++;
    197. }
    198. }
    199.  
    200. }
    201. if (count == 1){
    202. return true;
    203. }
    204. return false;
    205. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by laurynas2003; 10th April 2020 at 12:02.

Similar Threads

  1. Replies: 6
    Last Post: 21st April 2019, 01:28
  2. Random Generator for Androidx86 in Qt5
    By alexo in forum Newbie
    Replies: 1
    Last Post: 8th September 2014, 07:30
  3. Replies: 1
    Last Post: 7th April 2010, 17:26
  4. Random No Generator in C/C++
    By ankurjain in forum General Programming
    Replies: 1
    Last Post: 6th July 2006, 12:33

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.