Text Practice Mode
Sudoku c++
created May 21st 2021, 12:35 by Simone Fantin
6
985 words
7 completed
0
Rating visible after 3 or more votes
saving score / loading statistics ...
00:00
#include <iostream>
#include <ctime>
#include <fstream>
using namespace std;
#define DIM 9
void caricaSudoku(int[][DIM], string, char);
void stampaSudoku(int[][DIM]);
void getPosition(int[][DIM], int&, int&);
bool controlloRiga(int[][DIM], int, int, int);
bool controlloColonna(int[][DIM], int, int, int);
bool controlloCella(int[][DIM], int, int, int);
bool getStop(int[][DIM]);
char n;
string st;
int main()
{
srand(time(NULL));
system("chcp 65001");
system("cls");
int sudoku[DIM][DIM];
int rig = 0, col = 0, num = 0;
bool stop = false;
int scelta = 0;
int sceltaDiff;
bool vinto = false;
do {
cout << "1. Beginner" << endl;
cout << "2. Easy" << endl;
cout << "3. Medium" << endl;
cout << "4. Hard" << endl;
cout << "5. Extreme" << endl;
cout << "Inserisci il numero: ";
cin >> sceltaDiff;
} while (sceltaDiff < 1 || sceltaDiff > 5);
if (sceltaDiff == 1)
st = "beginner";
else if (sceltaDiff == 2)
st = "easy";
else if (sceltaDiff == 3)
st = "medium";
else if (sceltaDiff == 4)
st = "hard";
else if (sceltaDiff == 5)
st = "extreme";
cout << "Difficoltà: " << st << endl;
caricaSudoku(sudoku, st, 'z');
cout << "Sudoku caricato" << endl;
while (stop == false && vinto == false)
{
stampaSudoku(sudoku);
do
{
getPosition(sudoku, rig, col);
} while (sudoku[rig - 1][col - 1] != 0);
stampaSudoku(sudoku);
cout << endl << "Hai scelto la posizione: " << rig << " - " << col << endl;
do
{
do
{
cout << "Inserisci il numero che vuoi mettere: ";
cin >> num;
} while (num < 1 || num > DIM);
} while (controlloRiga(sudoku, rig, col, num) != true || controlloColonna(sudoku, rig, col, num) != true || controlloCella(sudoku, rig, col, num) != true);
sudoku[rig-1][col-1] = num;
stampaSudoku(sudoku);
vinto = getStop(sudoku);
if (vinto == true)
{
stampaSudoku(sudoku);
cout << endl << "Hai finito il sudoku!" << endl;
}
else
{
do
{
cout << endl << "1. Per continuare" << endl;
cout << "2. Per uscire" << endl;
cout << "3. Per ricominciare" << endl;
cout << "Inserisci il numero: ";
cin >> scelta;
} while (scelta < 1 || scelta > 3);
if (scelta == 2)
stop = true;
else if (scelta == 3)
{
caricaSudoku(sudoku, st, n);
}
}
if (stop == true)
{
stampaSudoku(sudoku);
cout << endl << "Hai interrotto il gioco" << endl;
}
}
}
bool getStop(int s[][DIM])
{
int conta = 0;
for (int i = 0; i < DIM; i++)
{
for (int j = 0; j < DIM; j++)
{
if (s[i][j] == 0)
conta++;
}
}
if (conta == 0)
return true;
else
return false;
}
bool controlloRiga(int m[][DIM], int r, int c, int n)
{
for (int i = 0; i < DIM; i++)
{
if (m[r-1][i] == n)
{
cout << n << " è già presente nella casella " << r << " - " << c << endl;
return false;
}
}
return true;
}
bool controlloColonna(int m[][DIM], int r, int c, int n)
{
for (int i = 0; i < DIM; i++)
{
if (m[i][c-1] == n)
{
cout << n << " è già presente nella casella " << r << " - " << c << endl;
return false;
}
}
return true;
}
bool controlloCella(int m[][DIM], int r, int c, int n)
{
for (int i = ((r-1)/3)*3; i < (((r-1)/3)*3 + 3); i++)
{
for (int j = ((c-1)/3)*3; j < (((c-1)/3)*3 + 3); j++)
{
if (m[i][j] == n)
{
cout << n << " è già presente nella casella " << r << " - " << c << endl;
return false;
}
}
}
return true;
}
char getCar()
{
int num = rand() % 5;
if (num == 0)
n = 'a';
else if (num == 1)
n = 'b';
else if (num == 2)
n = 'c';
else if (num == 3)
n = 'd';
else if (num == 4)
n = 'e';
return n;
}
void caricaSudoku(int s[][DIM], string t, char r)
{
string f;
int var;
char car;
char l;
ifstream file("tables.txt");
cout << "Cercando stringa" << endl;
do {
file >> f;
} while (f != t);
if (r == 'z')
car = getCar();
else
car = n;
cout << "Cercando carattere(" << car << ")" << endl;
do {
file >> l;
} while (l != car);
for (int i = 0; i < DIM; i++)
{
for (int j = 0; j < DIM; j++)
{
file >> s[i][j];
}
}
file.close();
}
void stampaSudoku(int s[][DIM])
{
system("cls");
cout << " Il Sudoku ~ Fantini" << endl;
if (st == "beginner")
cout << " Difficoltà " << st << endl;
else if (st == "easy")
cout << " Difficoltà easy" << endl;
else if (st == "medium")
cout << " Difficoltà medium" << endl;
else if (st == "hard")
cout << " Difficoltà hard" << endl;
else if (st == "extreme")
cout << " Difficoltà extreme" << endl;
cout << "┏━━━┯━━━┯━━━┳━━━┯━━━┯━━━┳━━━┯━━━┯━━━┓" << endl;
for (int i = 0; i < DIM; i++)
{
cout << "┃";
for (int j = 0; j < DIM; j++)
{
cout << " ";
if (s[i][j] == 0)
cout << " ";
else
cout << s[i][j];
if (j == 2 || j == 5 || j == 8)
cout << " ┃";
else
cout << " │";
}
cout << endl;
if (i == 2 || i == 5)
cout << "┣━━━┿━━━┿━━━╋━━━┿━━━┿━━━╋━━━┿━━━┿━━━┫" << endl;
else if (i != 8)
cout << "┠───┼───┼───╂───┼───┼───╂───┼───┼───┨" << endl;
}
cout << "┗━━━┷━━━┷━━━┻━━━┷━━━┷━━━┻━━━┷━━━┷━━━┛";
}
void getPosition(int m[][DIM], int &rig, int &col)
{
stampaSudoku(m);
cout << endl;
do
{
cout << "Inserisci il numero della riga: ";
cin >> rig;
} while (rig < 1 || rig > DIM);
stampaSudoku(m);
cout << endl << "Hai scelto la riga numero: " << rig << endl;
do
{
cout <<"Inserisci il numero della colonna: ";
cin >> col;
} while (col < 1 || col > DIM);
}
#include <ctime>
#include <fstream>
using namespace std;
#define DIM 9
void caricaSudoku(int[][DIM], string, char);
void stampaSudoku(int[][DIM]);
void getPosition(int[][DIM], int&, int&);
bool controlloRiga(int[][DIM], int, int, int);
bool controlloColonna(int[][DIM], int, int, int);
bool controlloCella(int[][DIM], int, int, int);
bool getStop(int[][DIM]);
char n;
string st;
int main()
{
srand(time(NULL));
system("chcp 65001");
system("cls");
int sudoku[DIM][DIM];
int rig = 0, col = 0, num = 0;
bool stop = false;
int scelta = 0;
int sceltaDiff;
bool vinto = false;
do {
cout << "1. Beginner" << endl;
cout << "2. Easy" << endl;
cout << "3. Medium" << endl;
cout << "4. Hard" << endl;
cout << "5. Extreme" << endl;
cout << "Inserisci il numero: ";
cin >> sceltaDiff;
} while (sceltaDiff < 1 || sceltaDiff > 5);
if (sceltaDiff == 1)
st = "beginner";
else if (sceltaDiff == 2)
st = "easy";
else if (sceltaDiff == 3)
st = "medium";
else if (sceltaDiff == 4)
st = "hard";
else if (sceltaDiff == 5)
st = "extreme";
cout << "Difficoltà: " << st << endl;
caricaSudoku(sudoku, st, 'z');
cout << "Sudoku caricato" << endl;
while (stop == false && vinto == false)
{
stampaSudoku(sudoku);
do
{
getPosition(sudoku, rig, col);
} while (sudoku[rig - 1][col - 1] != 0);
stampaSudoku(sudoku);
cout << endl << "Hai scelto la posizione: " << rig << " - " << col << endl;
do
{
do
{
cout << "Inserisci il numero che vuoi mettere: ";
cin >> num;
} while (num < 1 || num > DIM);
} while (controlloRiga(sudoku, rig, col, num) != true || controlloColonna(sudoku, rig, col, num) != true || controlloCella(sudoku, rig, col, num) != true);
sudoku[rig-1][col-1] = num;
stampaSudoku(sudoku);
vinto = getStop(sudoku);
if (vinto == true)
{
stampaSudoku(sudoku);
cout << endl << "Hai finito il sudoku!" << endl;
}
else
{
do
{
cout << endl << "1. Per continuare" << endl;
cout << "2. Per uscire" << endl;
cout << "3. Per ricominciare" << endl;
cout << "Inserisci il numero: ";
cin >> scelta;
} while (scelta < 1 || scelta > 3);
if (scelta == 2)
stop = true;
else if (scelta == 3)
{
caricaSudoku(sudoku, st, n);
}
}
if (stop == true)
{
stampaSudoku(sudoku);
cout << endl << "Hai interrotto il gioco" << endl;
}
}
}
bool getStop(int s[][DIM])
{
int conta = 0;
for (int i = 0; i < DIM; i++)
{
for (int j = 0; j < DIM; j++)
{
if (s[i][j] == 0)
conta++;
}
}
if (conta == 0)
return true;
else
return false;
}
bool controlloRiga(int m[][DIM], int r, int c, int n)
{
for (int i = 0; i < DIM; i++)
{
if (m[r-1][i] == n)
{
cout << n << " è già presente nella casella " << r << " - " << c << endl;
return false;
}
}
return true;
}
bool controlloColonna(int m[][DIM], int r, int c, int n)
{
for (int i = 0; i < DIM; i++)
{
if (m[i][c-1] == n)
{
cout << n << " è già presente nella casella " << r << " - " << c << endl;
return false;
}
}
return true;
}
bool controlloCella(int m[][DIM], int r, int c, int n)
{
for (int i = ((r-1)/3)*3; i < (((r-1)/3)*3 + 3); i++)
{
for (int j = ((c-1)/3)*3; j < (((c-1)/3)*3 + 3); j++)
{
if (m[i][j] == n)
{
cout << n << " è già presente nella casella " << r << " - " << c << endl;
return false;
}
}
}
return true;
}
char getCar()
{
int num = rand() % 5;
if (num == 0)
n = 'a';
else if (num == 1)
n = 'b';
else if (num == 2)
n = 'c';
else if (num == 3)
n = 'd';
else if (num == 4)
n = 'e';
return n;
}
void caricaSudoku(int s[][DIM], string t, char r)
{
string f;
int var;
char car;
char l;
ifstream file("tables.txt");
cout << "Cercando stringa" << endl;
do {
file >> f;
} while (f != t);
if (r == 'z')
car = getCar();
else
car = n;
cout << "Cercando carattere(" << car << ")" << endl;
do {
file >> l;
} while (l != car);
for (int i = 0; i < DIM; i++)
{
for (int j = 0; j < DIM; j++)
{
file >> s[i][j];
}
}
file.close();
}
void stampaSudoku(int s[][DIM])
{
system("cls");
cout << " Il Sudoku ~ Fantini" << endl;
if (st == "beginner")
cout << " Difficoltà " << st << endl;
else if (st == "easy")
cout << " Difficoltà easy" << endl;
else if (st == "medium")
cout << " Difficoltà medium" << endl;
else if (st == "hard")
cout << " Difficoltà hard" << endl;
else if (st == "extreme")
cout << " Difficoltà extreme" << endl;
cout << "┏━━━┯━━━┯━━━┳━━━┯━━━┯━━━┳━━━┯━━━┯━━━┓" << endl;
for (int i = 0; i < DIM; i++)
{
cout << "┃";
for (int j = 0; j < DIM; j++)
{
cout << " ";
if (s[i][j] == 0)
cout << " ";
else
cout << s[i][j];
if (j == 2 || j == 5 || j == 8)
cout << " ┃";
else
cout << " │";
}
cout << endl;
if (i == 2 || i == 5)
cout << "┣━━━┿━━━┿━━━╋━━━┿━━━┿━━━╋━━━┿━━━┿━━━┫" << endl;
else if (i != 8)
cout << "┠───┼───┼───╂───┼───┼───╂───┼───┼───┨" << endl;
}
cout << "┗━━━┷━━━┷━━━┻━━━┷━━━┷━━━┻━━━┷━━━┷━━━┛";
}
void getPosition(int m[][DIM], int &rig, int &col)
{
stampaSudoku(m);
cout << endl;
do
{
cout << "Inserisci il numero della riga: ";
cin >> rig;
} while (rig < 1 || rig > DIM);
stampaSudoku(m);
cout << endl << "Hai scelto la riga numero: " << rig << endl;
do
{
cout <<"Inserisci il numero della colonna: ";
cin >> col;
} while (col < 1 || col > DIM);
}
