Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sudoku.h"
- int main()
- {
- int opcio = 0, columna, fila, numero;
- Sudoku s;
- string fitxer = "sudoku.txt";
- s.llegeix(fitxer);
- s.mostra();
- while ((!s.estaComplet()) && (opcio != 3))
- {
- cout << endl << "Escolleix una opcio (1, 2, 3): ";
- cin >> opcio;
- switch (opcio)
- {
- case 1:
- cout << "Introdueix la fila: ";
- cin >> fila;
- cout << "Introdueix la columna: ";
- cin >> columna;
- cout << "Introdueix el numero: ";
- cin >> numero;
- cout << endl;
- if (!s.estaComplet())
- {
- s.afegeixNumero(fila, columna, numero);
- }
- s.mostra();
- break;
- case 2:
- cout << "Introdueix la fila: ";
- cin >> fila;
- cout << "Introdueix la columna: ";
- cin >> columna;
- cout << endl;
- s.treuNumero(fila, columna);
- s.mostra();
- break;
- case 3:
- cout << endl << "S'ha acabat la partida";
- break;
- default:
- cout << "Opcio no disponible.";
- break;
- }
- }
- return 0;
- }
- #include "sudoku.h"
- void Sudoku::llegeix(string nomFitxer)
- {
- ifstream fitxer;
- fitxer.open(nomFitxer);
- if (fitxer.is_open())
- {
- for (int i = 0; i < N_FILES; i++)
- {
- for (int j = 0; j < N_COLUMNES; j++)
- {
- int valor;
- fitxer >> valor;
- if (!fitxer.eof())
- {
- m_tauler[i][j].posaNumero(valor);
- m_tauler[i][j].posaOriginal(true);
- }
- }
- }
- fitxer.close();
- }
- }
- void Sudoku::mostra () const
- {
- for (int i = 0; i < N_FILES; i++)
- {
- for (int j = 0; j < N_COLUMNES; j++)
- {
- cout << m_tauler[i][j].getNumero();
- }
- cout << endl;
- }
- }
- int Sudoku::afegeixNumero(int fila, int columna, int numero)
- {
- if (m_tauler[fila][columna].estaLliure() == true)
- {
- m_tauler[fila][columna].posaNumero(numero);
- return numero;
- }
- else
- {
- cout << "Error al afegir" << endl;
- return 0;
- }
- }
- int Sudoku::treuNumero(int fila, int columna)
- {
- int r = 0;
- if (m_tauler[fila][columna].esOriginal() == false)
- {
- m_tauler[fila][columna].posaNumero(0);
- r = 1;
- }
- return r;
- }
- bool Sudoku::estaComplet() const
- {
- bool ple = true;
- for (int i = 0; i < N_FILES; i++)
- {
- for (int j = 0; j < N_COLUMNES; j++)
- {
- if (m_tauler[i][j].getNumero() == 0)
- ple = false;
- }
- }
- return ple;
- }
- #include "casella.h"
- #define N_FILES 9
- #define N_COLUMNES 9
- #define CASELLA_BUIDA 0
- class Sudoku
- {
- public:
- Sudoku() {};
- ~Sudoku() {};
- void llegeix(string nomFitxer);
- void mostra() const;
- int afegeixNumero(int fila, int columna, int numero);
- int treuNumero(int fila, int columna);
- bool estaComplet() const;
- private:
- Casella m_tauler[N_FILES][N_COLUMNES];
- int m_nLliures;
- };
- #pragma once
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- #define CASELLA_BUIDA 0
- class Casella
- {
- public:
- Casella() { m_numero = 1; m_original = false; m_lliure = true; };
- ~Casella() {};
- //Setters
- void posaOriginal(bool original) { m_original = original; }
- int posaNumero(int numero) { m_numero = numero; return m_numero; }
- int eliminaNumero() { m_numero = CASELLA_BUIDA; m_lliure = true; };
- //Getters
- int getNumero() const { return m_numero; };
- bool esOriginal() const { return m_original; };
- bool estaLliure() const { return m_lliure; };
- private:
- int m_numero;
- bool m_original;
- bool m_lliure;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement