Advertisement
Guest User

pringao

a guest
Mar 21st, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include "sudoku.h"
  2.  
  3. int main()
  4. {
  5. int opcio = 0, columna, fila, numero;
  6. Sudoku s;
  7. string fitxer = "sudoku.txt";
  8.  
  9. s.llegeix(fitxer);
  10. s.mostra();
  11.  
  12. while ((!s.estaComplet()) && (opcio != 3))
  13. {
  14. cout << endl << "Escolleix una opcio (1, 2, 3): ";
  15. cin >> opcio;
  16.  
  17. switch (opcio)
  18. {
  19. case 1:
  20. cout << "Introdueix la fila: ";
  21. cin >> fila;
  22. cout << "Introdueix la columna: ";
  23. cin >> columna;
  24. cout << "Introdueix el numero: ";
  25. cin >> numero;
  26. cout << endl;
  27.  
  28. if (!s.estaComplet())
  29. {
  30. s.afegeixNumero(fila, columna, numero);
  31. }
  32.  
  33. s.mostra();
  34. break;
  35.  
  36. case 2:
  37. cout << "Introdueix la fila: ";
  38. cin >> fila;
  39. cout << "Introdueix la columna: ";
  40. cin >> columna;
  41. cout << endl;
  42.  
  43. s.treuNumero(fila, columna);
  44. s.mostra();
  45. break;
  46.  
  47. case 3:
  48. cout << endl << "S'ha acabat la partida";
  49. break;
  50.  
  51. default:
  52. cout << "Opcio no disponible.";
  53. break;
  54. }
  55. }
  56. return 0;
  57. }
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. #include "sudoku.h"
  66.  
  67. void Sudoku::llegeix(string nomFitxer)
  68. {
  69. ifstream fitxer;
  70. fitxer.open(nomFitxer);
  71.  
  72. if (fitxer.is_open())
  73. {
  74. for (int i = 0; i < N_FILES; i++)
  75. {
  76. for (int j = 0; j < N_COLUMNES; j++)
  77. {
  78. int valor;
  79. fitxer >> valor;
  80.  
  81. if (!fitxer.eof())
  82. {
  83. m_tauler[i][j].posaNumero(valor);
  84. m_tauler[i][j].posaOriginal(true);
  85. }
  86. }
  87. }
  88. fitxer.close();
  89. }
  90. }
  91.  
  92. void Sudoku::mostra () const
  93. {
  94. for (int i = 0; i < N_FILES; i++)
  95. {
  96. for (int j = 0; j < N_COLUMNES; j++)
  97. {
  98. cout << m_tauler[i][j].getNumero();
  99. }
  100. cout << endl;
  101. }
  102. }
  103.  
  104. int Sudoku::afegeixNumero(int fila, int columna, int numero)
  105. {
  106. if (m_tauler[fila][columna].estaLliure() == true)
  107. {
  108. m_tauler[fila][columna].posaNumero(numero);
  109. return numero;
  110. }
  111. else
  112. {
  113. cout << "Error al afegir" << endl;
  114. return 0;
  115. }
  116. }
  117.  
  118. int Sudoku::treuNumero(int fila, int columna)
  119. {
  120. int r = 0;
  121. if (m_tauler[fila][columna].esOriginal() == false)
  122. {
  123. m_tauler[fila][columna].posaNumero(0);
  124. r = 1;
  125. }
  126. return r;
  127. }
  128.  
  129. bool Sudoku::estaComplet() const
  130. {
  131. bool ple = true;
  132.  
  133. for (int i = 0; i < N_FILES; i++)
  134. {
  135. for (int j = 0; j < N_COLUMNES; j++)
  136. {
  137. if (m_tauler[i][j].getNumero() == 0)
  138. ple = false;
  139. }
  140. }
  141.  
  142. return ple;
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. #include "casella.h"
  153.  
  154. #define N_FILES 9
  155. #define N_COLUMNES 9
  156. #define CASELLA_BUIDA 0
  157.  
  158. class Sudoku
  159. {
  160. public:
  161. Sudoku() {};
  162. ~Sudoku() {};
  163. void llegeix(string nomFitxer);
  164. void mostra() const;
  165. int afegeixNumero(int fila, int columna, int numero);
  166. int treuNumero(int fila, int columna);
  167. bool estaComplet() const;
  168.  
  169. private:
  170. Casella m_tauler[N_FILES][N_COLUMNES];
  171. int m_nLliures;
  172. };
  173.  
  174.  
  175.  
  176.  
  177.  
  178. #pragma once
  179. #include <iostream>
  180. #include <fstream>
  181. #include <string>
  182.  
  183. using namespace std;
  184.  
  185. #define CASELLA_BUIDA 0
  186.  
  187. class Casella
  188. {
  189. public:
  190. Casella() { m_numero = 1; m_original = false; m_lliure = true; };
  191. ~Casella() {};
  192.  
  193. //Setters
  194. void posaOriginal(bool original) { m_original = original; }
  195. int posaNumero(int numero) { m_numero = numero; return m_numero; }
  196. int eliminaNumero() { m_numero = CASELLA_BUIDA; m_lliure = true; };
  197.  
  198. //Getters
  199. int getNumero() const { return m_numero; };
  200. bool esOriginal() const { return m_original; };
  201. bool estaLliure() const { return m_lliure; };
  202.  
  203. private:
  204. int m_numero;
  205. bool m_original;
  206. bool m_lliure;
  207. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement