1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#define DEBUG
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
class cell{
bool m_occu; //occupied is shown '-'
int m_num; // number is shown
public:
cell() : m_occu(false), m_num(0) {}
void setMark(const int num){m_num = num; m_occu = true;}
bool isMarked() const { return m_occu; }
int getNum(){ return m_num;}
friend ostream& operator << (ostream& o, const cell& c){
if (!c.m_occu) return o << setw(2) << '-';
else if (c.m_num == 0) return o << setw(2) << '-';
return o << setw(2) << c.m_num;
}
};
class board {
vector<vector <cell> >m_map;
bool col_row;
public:
board() {
vector<cell> a_row(9);
col_row = false;
for (int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
a_row[j].setMark(j+1);
}
random_shuffle(a_row.begin(), a_row.end());
m_map.push_back(a_row);
}
}
bool setMark(const int num, const int r, const int c){
if (m_map[r][c].isMarked()) return false;
m_map[r][c].setMark(num);
return true;
}
bool isMarked(const int r, const int c) const {return m_map[r][c].isMarked();}
void swap(){
srand(time(0));
int s = rand()% 3 + 1;
if (col_row == false){//swap rows
for (int i = 0; i < s; i++){
int src = rand()%9;
int dst = rand()%9;
while (src == dst){// make sure that src and dst are different
dst = rand()%9;
}
m_map[src].swap(m_map[dst]);
cout<<"Rows " << char('P' + src) << " and " <<char('P' + dst)<< " Switched"<<endl;
}
col_row = true;
}
else // swap columns
{
cell hold;
int src = rand()%9;
int dst = rand()%9;
while (src == dst){// make sure that src and dst are different
dst = rand()%9;
}
for(int j = 0; j < 9; j++){
hold = m_map[j][src];
m_map[j][src] = m_map[j][dst];
m_map[j][dst] = hold;
}
cout<<"columns " << char('A' + src) << " and " << char('A' + dst) << " switched"<<endl;
col_row = false;
}
}
void erase() //To erase elements from any position of the row
{
srand(time(0));
int row = rand()%9; int col = rand()%9;
int num = 0;
m_map[row][col].setMark(num);
}
bool verify(){// check rows
int compareNUM = 0;
for (int row = 0, col = 0; row < 3 && col < 0; row++, col++){
compareNUM = m_map[row][col].getNum();
for (int r = 0, c = 0; r < 3 && c < 3; r++, c++){
if(m_map[r][c].getNum() == compareNUM){cout << "Error" << endl; return false;}
else return true;
}
}
for (int row = 0, col = 3; row < 3 && col < 6; row++, col++){
compareNUM = m_map[row][col].getNum();
for (int r = 0, c = 3; r < 3 && c < 6; r++, c++){
if(m_map[r][c].getNum() == compareNUM){cout << "Error" << endl; return false;}
else return true;
}
}
for (int row = 0, col = 6; row < 3 && col < 9; row++, col++){
compareNUM = m_map[row][col].getNum();
for (int r = 0, c = 6; r < 3 && c < 9; r++, c++){
if(m_map[r][c].getNum() == compareNUM){cout << "Error" << endl; return false;}
else return true;
}
}
for (int row = 3, col = 0; row < 6 && col < 3; row++, col++){
compareNUM = m_map[row][col].getNum();
for (int r = 3, c = 0; r < 6 && c < 3; r++, c++){
if(m_map[r][c].getNum() == compareNUM){cout << "Error" << endl; return false;}
else return true;
}
}
return false;
}
friend ostream& operator << (ostream& o, const board& b){
cout << " ";
for (int col = 0; col < 9; ++col) o << setw(2) << char('A' + col);
o << endl;
for (int row = 0; row < 9; ++row){
o << setw(2) << char('P' + row);
for (int col = 0; col < 9; ++col) o << b.m_map[row][col];
o << endl;
}
return o;
}
};
int main() {
board b;
string command;
cout<<"Welcome to Sudoku Initializer!"<<endl;
while(true)
{
cin>>command;
if (command == "show") cout << b << endl;
else if (command == "swap") b.swap();
else if (command == "verify")b.verify();
else if (command == "erase") b.erase();
else if (command == "quit"){cout << "Bye "<< endl; break;}
}
return 0;
}
|