Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6. const int TAB_FILAS = 9;
  7. const int TAB_COLUMNAS = 9;
  8. string TABLERO[TAB_FILAS][TAB_COLUMNAS] { //tablero inicial con pistas
  9. {"5"," "," ", " "," ","7"," "," "," "},
  10. {"2"," "," ","1"," ","5"," "," "," "},
  11. {"4"," ","8"," "," "," "," ","6"," "},
  12. {" "," "," "," ","6"," "," "," ","3"},
  13. {" "," "," ","8"," ","3"," "," "," "},
  14. {"7"," "," "," "," "," "," "," "," "},
  15. {" "," "," "," "," "," ","2","8"," "},
  16. {" "," "," ","4"," ","9"," "," ","5"},
  17. {" "," "," "," ","8"," "," "," "," "}
  18. };
  19.  
  20. void dibujar_cabezero_de_tablero(){
  21. for(int i = 0; i < TAB_FILAS; ++i)
  22. cout<< setw(3) <<i;
  23. cout <<"\n";
  24. }
  25.  
  26. string pintar_tabla(){
  27. string tabla = "";
  28. int renglon = 0;
  29. for(int f = 0; f < TAB_FILAS + 1; ++f)
  30. tabla.append("___");
  31. tabla.append("\n");
  32. for(int f = 0; f < TAB_FILAS; ++f){
  33. ++renglon;
  34. ostringstream s;
  35. s << f;
  36. s <<" ";
  37. tabla.append(s.str() +" ");
  38. for(int c = 0; c < TAB_COLUMNAS; ++c){
  39. tabla.append(TABLERO[f][c] +" " +"|");
  40. }
  41. tabla.append("\n");
  42. if(renglon % 1 == 0)
  43. for(int i = 0; i < 10; ++i)
  44. tabla.append("___");
  45. tabla.append("\n");
  46. }
  47. return tabla;
  48. }
  49.  
  50. bool num_esta_en_columna(string &num, int fila, int colum){
  51. for(int c = 0; c < TAB_COLUMNAS; ++c)
  52. if(TABLERO[fila][c].compare(num) ==0)
  53. return true;
  54. return false;
  55. }
  56.  
  57. bool num_esta_en_fila(string &num, int fila, int colum){
  58. for(int f = 0; f < TAB_FILAS; ++f)
  59. if(TABLERO[f][colum].compare(num) == 0)
  60. return true;
  61. return false;
  62. }
  63. bool num_esta_en_casilla(string &num,int fila,int colum){
  64. return TABLERO[fila][colum].compare(num) == 0;
  65. }
  66.  
  67. bool num_esta_repetido(string &num, int fila, int colum){
  68.  
  69. //si no hay espacio esta ocupado
  70. if(TABLERO[fila][colum].compare(" ") != 0)
  71. return true;
  72. //casilla no tiene espacio vamos a ver si el nro esta repetido
  73. if(num_esta_en_casilla(num,fila,colum))
  74. return true;
  75. else if(num_esta_en_casilla(num,fila,colum))
  76. return true;
  77. else if(num_esta_en_columna(num,fila,colum))
  78. return true;
  79. return false;
  80. }
  81.  
  82.  
  83. void agregar_num_a_tablero(string &num, int fila, int colum){
  84. TABLERO[fila][colum].assign(num);
  85. }
  86.  
  87. // si no hay espacios ganaste el juego
  88. bool gano_juego(){
  89. for(int f = 0; f < TAB_FILAS; ++f ){
  90. for(int c = 0; c < TAB_COLUMNAS; ++c){
  91. if(TABLERO[f][c].compare(" ") == 0)
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97.  
  98.  
  99. int main(){
  100. string num = "",tabla = pintar_tabla();
  101. int fila,columna;
  102. bool finalizar = false;
  103. string menu =
  104. "1 para ingresar nro\n"
  105. "2 para finalizar \n";
  106. int seleccion;
  107.  
  108. while(!finalizar && !gano_juego()){
  109. cout << menu << endl;
  110. cin >> seleccion;
  111. cin.get();
  112. if(seleccion == 2)
  113. finalizar = true;
  114. else{
  115. cout <<"\tTABLERO SUDOKU\n";
  116. dibujar_cabezero_de_tablero();
  117. cout << tabla << endl;
  118. cout <<"\ningrese fila y columna"<<":";
  119. cin >> fila >> columna;
  120. cin.get();
  121. cout <<"\ningrese numero";
  122. cin >> num;
  123. cout <<"\n\n";
  124. while(num_esta_repetido(num,fila,columna)){
  125. cout <<"\nnro esta repetido";
  126. cout <<"\ningrese fila y columna"<<":";
  127. cin >> fila >> columna;
  128. cin.get();
  129. cout <<"\ningrese numero";
  130. cin >> num;
  131. }
  132. agregar_num_a_tablero(num,fila,columna);
  133. tabla = pintar_tabla();
  134. }
  135. }
  136.  
  137. cout <<"\n\n\tRESULTADO FINAL\n";
  138. cout <<"\n=====================================\n";
  139. cout <<"\n\tTABLERO SUDOKU\n";
  140. dibujar_cabezero_de_tablero();
  141. cout << tabla;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement