// Nonogram solver by fedimser // Date: 29.05.2017 function zero1D(size) { var arr = []; while (size--) arr.push(0); return arr; } function zero2D(rows, cols) { var array = [], row = []; while (cols--) row.push(0); while (rows--) array.push(row.slice()); return array; } class Line { constructor(groups) { this.groups = groups; this.gn = groups.length; if(this.gn>0) { this.restLength = zero1D(this.gn); this.restLength[this.gn-1] = groups[this.gn-1]; for(var i=this.gn-2;i>=0;i--) { this.restLength[i] = groups[i]+1+this.restLength[i+1]; } } } setCells(cells) { this.length = cells.length; this.cells = cells; this.sure = zero1D(this.length); for (var i=0; i0) return; if (pos + this.restLength[g]>this.length) return; var ok = true; for (var i = pos; i= this.maxGroup) {type="groupVert"; x=i; y=j-this.maxGroup; } else if (i >= this.maxGroup && j < this.maxGroup) {type="groupHor"; x=i-this.maxGroup; y=j; } else {type="cell"; x=i-this.maxGroup; y=j-this.maxGroup;} var cell = document.createElement("td"); cell.style.width="20px"; cell.style.height="20px"; cell.style.padding="0px"; if (type=="invisible"){ cell.style.border="0px solid black"; } else if (type=="cell"){ //cell.style.border="1px solid black"; if(x%5==0) cell.style["border-top"]="2px solid black"; if(y%5==0) cell.style["border-left"]="2px solid black"; if(x==0) cell.style["border-top"]="4px solid black"; if(y==0) cell.style["border-left"]="4px solid black"; if(this.cells[x][y]==1) cell.style.backgroundColor="black"; } else if (type=="groupVert"){ //cell.style.border="1px solid black"; if(y%5==0) cell.style["border-left"]="2px solid black"; if(y==0) cell.style["border-left"]="4px solid black"; } else if (type=="groupHor") { if(x%5==0) cell.style["border-top"]="2px solid black"; if(x==0) cell.style["border-top"]="4px solid black"; } if (type[0]=="g") { var inp = document.createElement("input"); //inp.type = "number"; //inp.minimum = 0; //inp.maximum = Math.max(this.width, this.height); inp.style.width = this.cellSize; inp.x = x; inp.y = y; inp.caller = this; inp.onclick= function(){this.select();} if (type=="groupHor"){ inp.value = this.groupsHor[x][y]; inp.onchange = function() { this.caller.groupsHor[this.x][this.y] = parseInt(this.value); } } else { inp.value = this.groupsVert[y][x]; inp.onchange = function() { this.caller.groupsVert[this.y][this.x] = parseInt(this.value); } } cell.appendChild(inp); } //cell.style.margin="0.1px"; //var cellText = document.createTextNode("cell is row "+j+", column "+i); //cell.appendChild(cellText); row.appendChild(cell); } tblBody.appendChild(row); console.log(i, this.height); } tbl.appendChild(tblBody); this.tableDiv.appendChild(tbl); tbl.setAttribute("border", "2"); } prepareGroups(groups) { var ret = []; for (var x=0;x0) row.push(groups[x][y]); ret.push(row); } return ret; } solve() { var nonogram = new Nonogram(this.prepareGroups(this.groupsHor), this.prepareGroups(this.groupsVert)); this.ansText.innerHTML = nonogram.solveAndCheck(); this.cells = nonogram.matrix; this.displayTable(); } }