Go to the documentation of this file.00001
00007 #include <stdlib.h>
00008 #include <vector>
00009 #include <string>
00010 #include <sstream>
00011 #include <iostream>
00012 #include <iomanip>
00013 #include <fstream>
00014
00015 #include "kakuro.h"
00016
00025 unsigned int string2uint(std::string str)
00026 {
00027 std::istringstream buffer(str);
00028 unsigned int number;
00029 buffer >> number;
00030 return number;
00031 }
00032
00033
00034 void Kakuro::parseCell(std::string cell,
00035 bool * type,
00036 unsigned int * horizontal,
00037 unsigned int * vertical )
00038 {
00039 *type = false;
00040 if (cell.compare("-")==0)
00041 {
00042 *type = true;
00043 *horizontal = 0;
00044 *vertical = 0;
00045 }
00046 else if (cell.compare("-\\-")==0)
00047 {
00048 *horizontal = 0;
00049 *vertical = 0;
00050 }
00051 else
00052 {
00053 std::string part1="",part2="";
00054 int current=1;
00055 for(unsigned int i=0; i<cell.size(); i++)
00056 if (cell[i]=='\\')
00057 current=2;
00058 else if (current==1)
00059 part1 = part1 + cell[i];
00060 else if (current==2)
00061 part2 = part2 + cell[i];
00062 if ( (part1.compare("-")!=0) && (part2.compare("-")!=0) )
00063 {
00064 *vertical = string2uint(part1);
00065 *horizontal = string2uint(part2);
00066 }
00067 else if (part1.compare("-")!=0)
00068 {
00069 *vertical = string2uint(part1);
00070 *horizontal = 0;
00071 }
00072 else
00073 {
00074 *vertical = 0;
00075 *horizontal = string2uint(part2);
00076 }
00077
00078 }
00079 }
00080
00081
00082 void Kakuro::printGrids()
00083 {
00084 std::cout<<"Structure:"<<std::endl;
00085 for (unsigned int i=0; i<gridStruct.size(); i++)
00086 {
00087 std::cout<<std::endl;
00088 for (unsigned int j=0; j<gridStruct[i].size(); j++)
00089 if (gridStruct[i][j])
00090 std::cout<<" 0";
00091 else
00092 std::cout<<" 1";
00093 }
00094 std::cout<<std::endl<<"Vertical hints:"<<std::endl;
00095 for (unsigned int i=0; i<verticalSum.size(); i++)
00096 {
00097 std::cout<<std::endl;
00098 for (unsigned int j=0; j<verticalSum[i].size(); j++)
00099 std::cout<<std::setw(4)<<verticalSum[i][j];
00100 }
00101 std::cout<<std::endl<<"Horizontal hints:"<<std::endl;
00102 for (unsigned int i=0; i<horizontalSum.size(); i++)
00103 {
00104 std::cout<<std::endl;
00105 for (unsigned int j=0; j<horizontalSum[i].size(); j++)
00106 std::cout<<std::setw(4)<<horizontalSum[i][j];
00107 }
00108 std::cout<<std::endl<<std::endl;
00109 }
00110
00111
00112 void Kakuro::parseGrid(std::istream * input)
00113 {
00114 std::string inputLine;
00115 char x;
00116 (*input)>>nSize;
00117 (*input)>>x;
00118 (*input)>>mSize;
00119 std::vector<unsigned int> strLine;
00120 std::vector<unsigned int> horLine;
00121 std::vector<unsigned int> vertLine;
00122 unsigned int j=0;
00123 while ((*input).good())
00124 {
00125 (*input)>>x;
00126 (*input).putback(x);
00127 (*input)>>inputLine;
00128 bool type;
00129 unsigned int hor, vert;
00130 parseCell(inputLine,&type,&hor,&vert);
00131 if (type)
00132 strLine.push_back((unsigned int)1);
00133 else
00134 strLine.push_back((unsigned int)0);
00135 horLine.push_back(hor);
00136 vertLine.push_back(vert);
00137 j++;
00138 if (j==mSize)
00139 {
00140 j=0;
00141 gridStruct.push_back(strLine);
00142 horizontalSum.push_back(horLine);
00143 verticalSum.push_back(vertLine);
00144 strLine.clear();
00145 horLine.clear();
00146 vertLine.clear();
00147 }
00148 }
00149 }
00150
00151
00152 void Kakuro::printToStream(std::ostream &out, bool isEmpty)
00153 {
00154 out<<nSize<<" x "<<mSize;
00155 for (unsigned int i=0; i<nSize; i++)
00156 {
00157 out<<std::endl;
00158 for (unsigned int j=0; j<mSize; j++)
00159 {
00160 if ( gridStruct[i][j]==0 )
00161 {
00162 if ( (horizontalSum[i][j] != 0)
00163 &&(verticalSum[i][j] != 0) )
00164 out<<std::setw(2)<<std::right<<verticalSum[i][j]
00165 <<"\\"<<std::setw(2)<<std::left<<horizontalSum[i][j];
00166 else if ( horizontalSum[i][j] != 0)
00167 out<<" -\\"<<std::setw(2)<<std::left<<horizontalSum[i][j];
00168 else if ( verticalSum[i][j] != 0)
00169 out<<std::setw(2)<<std::right<<verticalSum[i][j]<<"\\- ";
00170 else
00171 out<<" -\\- ";
00172 }
00173 else
00174 {
00175 if (isEmpty)
00176 out<<" - ";
00177 else
00178 out<<" "<<gridStruct[i][j]<<" ";
00179 }
00180 out<<" ";
00181 }
00182 }
00183 out<<std::endl<<std::endl;
00184 }
00185
00186
00187 void Kakuro::grids2sums()
00188 {
00189
00190 for (unsigned int i=0; i<nSize; i++)
00191 for (unsigned int j=0; j<mSize; j++)
00192 if (horizontalSum[i][j] != 0)
00193 {
00194 std::vector<unsigned int> x;
00195 std::vector<unsigned int> y;
00196
00197 for (unsigned int k=j+1; k<mSize; k++)
00198 if (!gridStruct[i][k])
00199 break;
00200 else
00201 {
00202 x.push_back(i);
00203 y.push_back(k);
00204
00205 }
00206
00207 sumsValue.push_back(horizontalSum[i][j]);
00208 sumsAbscissa.push_back(x);
00209 sumsOrdinate.push_back(y);
00210 }
00211
00212 for (unsigned int i=0; i<nSize; i++)
00213 for (unsigned int j=0; j<mSize; j++)
00214 if (verticalSum[i][j] != 0)
00215 {
00216 std::vector<unsigned int> x;
00217 std::vector<unsigned int> y;
00218
00219 for (unsigned int k=i+1; k<nSize; k++)
00220 if (!gridStruct[k][j])
00221 break;
00222 else
00223 {
00224 x.push_back(k);
00225 y.push_back(j);
00226
00227 }
00228
00229 sumsValue.push_back(verticalSum[i][j]);
00230 sumsAbscissa.push_back(x);
00231 sumsOrdinate.push_back(y);
00232 }
00233 }
00234
00235
00236 void Kakuro::assignValue(unsigned int i, unsigned int j, unsigned int value)
00237 {
00238 if (i>nSize)
00239 {
00240 std::cout<<"ERROR: assigning value out of grid ("
00241 <<i<<">"<<nSize<<")"<<std::endl;
00242 exit(100);
00243 }
00244 else if(j>mSize)
00245 {
00246 std::cout<<"ERROR: assigning value out of grid ("
00247 <<j<<">"<<mSize<<")"<<std::endl;
00248 exit(100);
00249 }
00250 else
00251 gridStruct[i][j] = value;
00252 }
00253
00254
00255
00256
00257 unsigned int Kakuro::getNSize()
00258 { return nSize; }
00259
00260 unsigned int Kakuro::getMSize()
00261 { return mSize; }
00262
00263 unsigned int Kakuro::getNHints()
00264 { return sumsValue.size(); }
00265
00266 std::vector< std::vector<unsigned int> > Kakuro::getGridStruc()
00267 { return gridStruct;}
00268
00269 std::vector< std::vector<unsigned int> > Kakuro::getHorizontalSum()
00270 { return horizontalSum; }
00271
00272 std::vector< std::vector<unsigned int> > Kakuro::getVerticalSum()
00273 { return verticalSum; }
00274
00275 std::vector<unsigned int> Kakuro::getSumsValue()
00276 { return sumsValue; }
00277
00278 std::vector< std::vector<unsigned int> > Kakuro::getSumsOrdinate()
00279 { return sumsOrdinate; }
00280
00281 std::vector< std::vector<unsigned int> > Kakuro::getSumsAbscissa()
00282 { return sumsAbscissa; }
00283
00284
00285
00286
00287
00288
00289
00290