Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <sstream>
- using namespace std;
- const int TAB_FILAS = 9;
- const int TAB_COLUMNAS = 9;
- string TABLERO[TAB_FILAS][TAB_COLUMNAS] { //tablero inicial con pistas
- {"5"," "," ", " "," ","7"," "," "," "},
- {"2"," "," ","1"," ","5"," "," "," "},
- {"4"," ","8"," "," "," "," ","6"," "},
- {" "," "," "," ","6"," "," "," ","3"},
- {" "," "," ","8"," ","3"," "," "," "},
- {"7"," "," "," "," "," "," "," "," "},
- {" "," "," "," "," "," ","2","8"," "},
- {" "," "," ","4"," ","9"," "," ","5"},
- {" "," "," "," ","8"," "," "," "," "}
- };
- void dibujar_cabezero_de_tablero(){
- for(int i = 0; i < TAB_FILAS; ++i)
- cout<< setw(3) <<i;
- cout <<"\n";
- }
- string pintar_tabla(){
- string tabla = "";
- int renglon = 0;
- for(int f = 0; f < TAB_FILAS + 1; ++f)
- tabla.append("___");
- tabla.append("\n");
- for(int f = 0; f < TAB_FILAS; ++f){
- ++renglon;
- ostringstream s;
- s << f;
- s <<" ";
- tabla.append(s.str() +" ");
- for(int c = 0; c < TAB_COLUMNAS; ++c){
- tabla.append(TABLERO[f][c] +" " +"|");
- }
- tabla.append("\n");
- if(renglon % 1 == 0)
- for(int i = 0; i < 10; ++i)
- tabla.append("___");
- tabla.append("\n");
- }
- return tabla;
- }
- bool num_esta_en_columna(string &num, int fila, int colum){
- for(int c = 0; c < TAB_COLUMNAS; ++c)
- if(TABLERO[fila][c].compare(num) ==0)
- return true;
- return false;
- }
- bool num_esta_en_fila(string &num, int fila, int colum){
- for(int f = 0; f < TAB_FILAS; ++f)
- if(TABLERO[f][colum].compare(num) == 0)
- return true;
- return false;
- }
- bool num_esta_en_casilla(string &num,int fila,int colum){
- return TABLERO[fila][colum].compare(num) == 0;
- }
- bool num_esta_repetido(string &num, int fila, int colum){
- //si no hay espacio esta ocupado
- if(TABLERO[fila][colum].compare(" ") != 0)
- return true;
- //casilla no tiene espacio vamos a ver si el nro esta repetido
- if(num_esta_en_casilla(num,fila,colum))
- return true;
- else if(num_esta_en_casilla(num,fila,colum))
- return true;
- else if(num_esta_en_columna(num,fila,colum))
- return true;
- return false;
- }
- void agregar_num_a_tablero(string &num, int fila, int colum){
- TABLERO[fila][colum].assign(num);
- }
- // si no hay espacios ganaste el juego
- bool gano_juego(){
- for(int f = 0; f < TAB_FILAS; ++f ){
- for(int c = 0; c < TAB_COLUMNAS; ++c){
- if(TABLERO[f][c].compare(" ") == 0)
- return false;
- }
- }
- return true;
- }
- int main(){
- string num = "",tabla = pintar_tabla();
- int fila,columna;
- bool finalizar = false;
- string menu =
- "1 para ingresar nro\n"
- "2 para finalizar \n";
- int seleccion;
- while(!finalizar && !gano_juego()){
- cout << menu << endl;
- cin >> seleccion;
- cin.get();
- if(seleccion == 2)
- finalizar = true;
- else{
- cout <<"\tTABLERO SUDOKU\n";
- dibujar_cabezero_de_tablero();
- cout << tabla << endl;
- cout <<"\ningrese fila y columna"<<":";
- cin >> fila >> columna;
- cin.get();
- cout <<"\ningrese numero";
- cin >> num;
- cout <<"\n\n";
- while(num_esta_repetido(num,fila,columna)){
- cout <<"\nnro esta repetido";
- cout <<"\ningrese fila y columna"<<":";
- cin >> fila >> columna;
- cin.get();
- cout <<"\ningrese numero";
- cin >> num;
- }
- agregar_num_a_tablero(num,fila,columna);
- tabla = pintar_tabla();
- }
- }
- cout <<"\n\n\tRESULTADO FINAL\n";
- cout <<"\n=====================================\n";
- cout <<"\n\tTABLERO SUDOKU\n";
- dibujar_cabezero_de_tablero();
- cout << tabla;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement