Go to the documentation of this file.00001
00008 #include <stdlib.h>
00009 #include <string>
00010
00011 #include "slitherlink.h"
00012
00013
00014 Slitherlink::Slitherlink()
00015 {
00016 width = 0;
00017 height = 0;
00018 }
00019
00020
00021 void Slitherlink::parseInput(std::istream *input)
00022 {
00023 std::string line;
00024 char c;
00025 (*input) >> height;
00026 (*input) >> c;
00027 (*input) >> width;
00028 for (unsigned int i=0; i<height; i++)
00029 {
00030 std::vector<bool> horizontal(width,false);
00031 std::vector<bool> vertical(width+1,false);
00032 std::vector<int> hintLine(width);
00033 for (unsigned int j=0; j<width; j++)
00034 {
00035 (*input) >> c;
00036 if (c == '-')
00037 hintLine[j] = -1;
00038 else
00039 hintLine[j] = c - '0';
00040 }
00041 hints.push_back(hintLine);
00042 hVertices.push_back(horizontal);
00043 vVertices.push_back(vertical);
00044 }
00045
00046 std::vector<bool> lastLine(width,false);
00047 hVertices.push_back(lastLine);
00048 }
00049
00050
00051 void Slitherlink::setVertex(bool horizontal, unsigned int i,
00052 unsigned int j, bool value)
00053 {
00054 if (
00055 (horizontal && (j >= width)) ||
00056 (!horizontal && (i >= height))
00057 )
00058 {
00059 std::cout<<"ERROR in Slitherlink.setVertex() index out"
00060 " of range (i="<<i<<",j="<<j<<") while (width="
00061 <<width<<", height="<<height<<")"<<std::endl;
00062 exit(1);
00063 }
00064 else if (horizontal)
00065 hVertices[i][j] = value;
00066 else
00067 vVertices[i][j] = value;
00068 }
00069
00070
00071
00072 int Slitherlink:: getHint(unsigned int i, unsigned int j)
00073 {
00074 if ((i >= height)||(j >= width))
00075 {
00076 std::cout<<"ERROR: in Slitherlink.setNode() index out "
00077 "of range: (i="<<i<<",j="<<j<<") while (width="
00078 <<width<<", height="<<height<<")"<<std::endl;
00079 exit(1);
00080 }
00081 else
00082 return hints[i][j];
00083 }
00084
00085
00086 unsigned int Slitherlink::getWidth()
00087 {
00088 return width;
00089 }
00090
00091
00092 unsigned int Slitherlink::getHeight()
00093 {
00094 return height;
00095 }
00096
00097
00098
00099
00100
00101 void Slitherlink::printNonHintLine(unsigned int i)
00102 {
00103 for (unsigned int j=0; j<width; j++)
00104 {
00105 if (i>0 && i<height)
00106 {
00107 if (vVertices[i-1][j] && vVertices[i][j])
00108 std::cout<<"|";
00109 else if (j>0)
00110 {
00111 if (hVertices[i][j-1] && hVertices[i][j])
00112 std::cout<<"_";
00113 else
00114 std::cout<<" ";
00115 }
00116 else
00117 std::cout<<" ";
00118 }
00119 else
00120 {
00121 if (j>0)
00122 {
00123 if (hVertices[i][j-1] && hVertices[i][j])
00124 std::cout<<"_";
00125 else
00126 std::cout<<" ";
00127 }
00128 else
00129 std::cout<<" ";
00130 }
00131 if (hVertices[i][j])
00132 std::cout<<"_";
00133 else
00134 std::cout<<" ";
00135 }
00136
00137 if (i>0 && i<height)
00138 if (vVertices[i-1][width] && vVertices[i][width])
00139 std::cout<<"|";
00140 std::cout<<std::endl;
00141 }
00142
00143
00144 void Slitherlink::printSolution()
00145 {
00146 for (unsigned int i=0; i<height; i++)
00147 {
00148
00149 printNonHintLine(i);
00150
00151 for (unsigned int j=0; j<width; j++)
00152 {
00153 if (vVertices[i][j])
00154 std::cout<<"|";
00155 else
00156 std::cout<<" ";
00157 if (hints[i][j] != -1)
00158 std::cout<<hints[i][j];
00159 else
00160 std::cout<<" ";
00161 }
00162
00163 if (vVertices[i][width])
00164 std::cout<<"|"<<std::endl;
00165 else
00166 std::cout<<" "<<std::endl;
00167 }
00168 printNonHintLine(height);
00169 }