#include "widget.h"
#include "ui_widget.h"
#include <QGridLayout>
#include <QFrame>
#include <QLineEdit>
#include <QIntValidator>
#include <QMessageBox>
#include <QPushButton>
#include <QRandomGenerator>
#include <QDateTime>
#include <QSet>
#include <iostream>
#include <QSignalMapper>
SudokuWidget
::SudokuWidget(QWidget * parent
)
for (int y = 0;y < 9; y++){
for (int x =0; x < 9; x++){
game_data[y].push_back(0);
}
}
pValidator->setRange( 1, 9 );
int square_counter = 0;
// random_position(20, random_pos_value); // QVector <QPair <int, int>
mainLayout->setSpacing(0);
mainLayout->setAlignment(Qt::AlignCenter);
for (int mr = 0; mr < 3; mr++) {
for(int mc = 0; mc < 3; mc++) {
widget
->setFrameStyle
(QFrame::Plain);
widget
->setFrameShape
(QFrame::Box);
/
gridLayout->setSpacing(0);
gridLayout->setMargin(0);
for(int r = 0; r < 3; r++) {
for (int c = 0; c < 3; c++) {
int rowColId = (square_counter / 9) + (square_counter % 9) * 10;
mapper->setMapping( tile, rowColId );
connect(tile, &QLineEdit::editingFinished, mapper, &QSignalMapper::map ); // error - no matching member function for call to connect
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->setValidator(pValidator);
tiles.push_back( tile );
square_counter++;
gridLayout->addWidget(tile, r, c, 1, 1, Qt::AlignCenter);
}
}
mainLayout->addWidget(widget, mr, mc, 1, 1, Qt::AlignCenter);
}
}
connect( mapper, SIGNAL( mapped( int ) ), this, SLOT( onMapped( int ) ) );
// for (int i = 0;i < 20; i++){
// tiles[random_pos_value[i].first]->setText(QString::number(random_pos_value[i].second));
// QMessageBox::about(this,"????????", "(" + QString::number(random_pos_value[i].first / 9) + ", " + QString::number(random_pos_value[i].first % 9) + ")" + " = " + QString::number(random_pos_value[i].second));
// tiles[random_pos_value[i].first]->setReadOnly(true);
// }
//setLayout(mainLayout);
}
void SudokuWidget::onMapped( int rowColId )
{
int row = rowColId / 10;
int col = rowColId % 10;
int square_value = tile->text().split(" ")[0].toInt(); // Retrieve the QString text from the QLineEdit, convert it to an int,
// store it in your game data structure, check to see if it is correct,
game_data[col][row] = square_value;
if (is_legal_box(square_value, col, row, game_data) && is_legal_row(square_value, col, game_data) && is_legal_colum(square_value, row, game_data) ){
QMessageBox::about(this,
"?",
"Its working");
// whatever your game decides to do. }
}
bool SudokuWidget
::is_legal_row(int value,
int y, QVector<
QVector <
int >>
&tiles1
) {
int count = 0;
for (int i = 0;i < 9; i++){
if (value == tiles1[y][i]){
count++;
}
}
if (count == 1){
return true;
}
return false;
}
bool SudokuWidget::is_legal_colum(int value, int x, QVector<QVector< int > > &tiles1)
{
int count = 0;
for (int i = 0;i < 9; i++){
if (value == tiles1[i][x]){
count++;
}
}
if (count == 1){
return true;
}
return false;
}
//void SudokuWidget::random_position(int digits, QVector <QPair <int, int> > &random_pos_value)
//{
// QVector < QVector < QLineEdit* > > random_boards(81);
// for (int y1 = 0;y1 < 9; y1++){
// for (int x1 = 0;x1 < 9; x1++){
// QLineEdit *line_edit = new QLineEdit("0");
// random_boards[y1].push_back(line_edit);
// }
// }
// qsrand(QDateTime::currentMSecsSinceEpoch() / 1000);
// int count = 0;
// int value;
// while (true){
// int position = (qrand() % 80) + 0;
// int y = position / 9;
// int x = position % 9;
// if (random_boards[y][x]->text() == "0"){
// while (true){
// value = (qrand() % 9) + 1;
// random_boards[y][x]->setText(QString::number(value));
// if (is_legal_box(value, y, x, random_boards) && is_legal_colum(value, x, random_boards) && is_legal_row(value, y, random_boards)){
// break;
// }
// random_boards[y][x]->setText("0");
// }
// QPair <int, int> values = {position,value};
// random_pos_value.push_back(values);
// count++;
// if (count == digits){
// break;
// }
// }
// }
//}
int SudokuWidget::box_position(int y, int x)
{
return 3*(y/3) + (x/3);
}
bool SudokuWidget::is_legal_box(int value, int y1, int x1, QVector<QVector< int > > &tiles1)
{
//box
int count = 0;
int square_position = box_position(y1, x1);
for (int y = (square_position / 3) * 3; y < ((square_position / 3) * 3) + 3;y++){
for (int x = (square_position % 3) * 3; x < ((square_position % 3) * 3) + 3;x++){
if (tiles1[y][x] == value){
count++;
}
}
}
if (count == 1){
return true;
}
return false;
}
#include "widget.h"
#include "ui_widget.h"
#include <QGridLayout>
#include <QFrame>
#include <QLineEdit>
#include <QIntValidator>
#include <QMessageBox>
#include <QPushButton>
#include <QRandomGenerator>
#include <QDateTime>
#include <QSet>
#include <iostream>
#include <QSignalMapper>
SudokuWidget::SudokuWidget(QWidget * parent)
: QWidget(parent) {
mapper = new QSignalMapper( this );
for (int y = 0;y < 9; y++){
for (int x =0; x < 9; x++){
game_data[y].push_back(0);
}
}
QIntValidator *pValidator = new QIntValidator( this );
pValidator->setRange( 1, 9 );
int square_counter = 0;
// random_position(20, random_pos_value); // QVector <QPair <int, int>
QGridLayout *mainLayout = new QGridLayout(this);
mainLayout->setSpacing(0);
mainLayout->setAlignment(Qt::AlignCenter);
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(this);
int rowColId = (square_counter / 9) + (square_counter % 9) * 10;
mapper->setMapping( tile, rowColId );
connect(tile, &QLineEdit::editingFinished, mapper, &QSignalMapper::map ); // error - no matching member function for call to connect
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);
tile->setValidator(pValidator);
tiles.push_back( tile );
square_counter++;
gridLayout->addWidget(tile, r, c, 1, 1, Qt::AlignCenter);
}
}
mainLayout->addWidget(widget, mr, mc, 1, 1, Qt::AlignCenter);
}
}
connect( mapper, SIGNAL( mapped( int ) ), this, SLOT( onMapped( int ) ) );
// for (int i = 0;i < 20; i++){
// tiles[random_pos_value[i].first]->setText(QString::number(random_pos_value[i].second));
// QMessageBox::about(this,"????????", "(" + QString::number(random_pos_value[i].first / 9) + ", " + QString::number(random_pos_value[i].first % 9) + ")" + " = " + QString::number(random_pos_value[i].second));
// tiles[random_pos_value[i].first]->setReadOnly(true);
// }
//setLayout(mainLayout);
}
void SudokuWidget::onMapped( int rowColId )
{
int row = rowColId / 10;
int col = rowColId % 10;
QLineEdit * tile = tiles[ row * 9 + col ];
int square_value = tile->text().split(" ")[0].toInt(); // Retrieve the QString text from the QLineEdit, convert it to an int,
// store it in your game data structure, check to see if it is correct,
game_data[col][row] = square_value;
if (is_legal_box(square_value, col, row, game_data) && is_legal_row(square_value, col, game_data) && is_legal_colum(square_value, row, game_data) ){
QMessageBox::about(this, "?", "Its working"); // whatever your game decides to do.
}
}
bool SudokuWidget::is_legal_row(int value, int y, QVector< QVector < int >> &tiles1)
{
int count = 0;
for (int i = 0;i < 9; i++){
if (value == tiles1[y][i]){
count++;
}
}
if (count == 1){
return true;
}
return false;
}
bool SudokuWidget::is_legal_colum(int value, int x, QVector<QVector< int > > &tiles1)
{
int count = 0;
for (int i = 0;i < 9; i++){
if (value == tiles1[i][x]){
count++;
}
}
if (count == 1){
return true;
}
return false;
}
//void SudokuWidget::random_position(int digits, QVector <QPair <int, int> > &random_pos_value)
//{
// QVector < QVector < QLineEdit* > > random_boards(81);
// for (int y1 = 0;y1 < 9; y1++){
// for (int x1 = 0;x1 < 9; x1++){
// QLineEdit *line_edit = new QLineEdit("0");
// random_boards[y1].push_back(line_edit);
// }
// }
// qsrand(QDateTime::currentMSecsSinceEpoch() / 1000);
// int count = 0;
// int value;
// while (true){
// int position = (qrand() % 80) + 0;
// int y = position / 9;
// int x = position % 9;
// if (random_boards[y][x]->text() == "0"){
// while (true){
// value = (qrand() % 9) + 1;
// random_boards[y][x]->setText(QString::number(value));
// if (is_legal_box(value, y, x, random_boards) && is_legal_colum(value, x, random_boards) && is_legal_row(value, y, random_boards)){
// break;
// }
// random_boards[y][x]->setText("0");
// }
// QPair <int, int> values = {position,value};
// random_pos_value.push_back(values);
// count++;
// if (count == digits){
// break;
// }
// }
// }
//}
int SudokuWidget::box_position(int y, int x)
{
return 3*(y/3) + (x/3);
}
bool SudokuWidget::is_legal_box(int value, int y1, int x1, QVector<QVector< int > > &tiles1)
{
//box
int count = 0;
int square_position = box_position(y1, x1);
QVector <QString> values;
for (int y = (square_position / 3) * 3; y < ((square_position / 3) * 3) + 3;y++){
for (int x = (square_position % 3) * 3; x < ((square_position % 3) * 3) + 3;x++){
if (tiles1[y][x] == value){
count++;
}
}
}
if (count == 1){
return true;
}
return false;
}
To copy to clipboard, switch view to plain text mode
Bookmarks