Advertisement
Guest User

Untitled

a guest
Sep 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 21.25 KB | None | 0 0
  1. import java.io._
  2. import scala.io.Source
  3.  
  4. object PuzzleSolver extends App {
  5.  
  6.   class Square(xNumber:Int, yNumber:Int, values:List[String] = List("-","S"), solved:Boolean = false) {
  7.     var possibleValues = values
  8.     val x = xNumber
  9.     val y = yNumber
  10.     var s = solved
  11.  
  12.     def setValues(solution: String) = {
  13.       this.s = true
  14.       this.possibleValues = List(solution)
  15.       // new Square(this.box, this.x, this.y, List(solution), true)
  16.     }
  17.  
  18.     def getValue:String = {
  19.       if(this.s) possibleValues.head
  20.       else "Not solved"
  21.     }
  22.  
  23.     def removeValue(wrongSolution:String):Square = {
  24.       val newList = this.possibleValues.filter(_ != wrongSolution)
  25.       if(newList.length > 1)
  26.         new Square(x, y, newList, false)
  27.       else
  28.         new Square(x,y,newList,true)
  29.     }
  30.  
  31.     // Returns a list with all neighbour squares in clockwise rotation
  32.     def getNeighbours(boardSize:Int):List[String] = {
  33.       var right = ""
  34.       var left = ""
  35.       var over = ""
  36.       var under = ""
  37.  
  38.       if(x == boardSize) right = "-"
  39.       else {
  40.         if(getSquare(x+1, y).possibleValues.length > 1) right = "?"
  41.         else right = getSquare(x+1,y).possibleValues.head
  42.       }
  43.  
  44.       if(x == 1) left = "-"
  45.       else {
  46.         if(getSquare(x-1, y).possibleValues.length > 1) left = "?"
  47.         else left = getSquare(x-1, y).possibleValues.head
  48.       }
  49.  
  50.       if(y == 1) over = "-"
  51.       else {
  52.         if(getSquare(x, y-1).possibleValues.length > 1) over = "?"
  53.         else over = getSquare(x, y-1).possibleValues.head
  54.       }
  55.  
  56.       if(y == boardSize) under = "-"
  57.       else {
  58.         if(getSquare(x, y+1).possibleValues.length > 1) under = "?"
  59.         else under = getSquare(x, y+1).possibleValues.head
  60.       }
  61.  
  62.       val neighbours = List[String](right, under, left, over)
  63.       neighbours
  64.     }
  65.  
  66.   }
  67.  
  68.   var allSquares = List[Square]()
  69.   var unsolvedSquares = List[Square]()
  70.   var boatsList = List[List[Square]]()
  71.   var savedBoard = List[Square]()
  72.  
  73.   def getAllFromX(i:Int):List[Square] = {
  74.     allSquares.filter(_.x == i)
  75.   }
  76.  
  77.   def getAllFromY(i:Int):List[Square] = {
  78.     allSquares.filter(_.y == i)
  79.   }
  80.  
  81.   def getSquare(x:Int, y:Int):Square = {
  82.     allSquares.filter(_.x == x).filter(_.y == y).head
  83.   }
  84.  
  85.   def setValue(x:Int, y:Int, solution:String):Unit = {
  86.     val s = getSquare(x,y)
  87.     allSquares = allSquares.filter(_ != s)
  88.     s.setValues(solution)
  89.     allSquares = allSquares :+ s
  90.   }
  91.  
  92.   def removeValue(x:Int, y:Int, wrongSolution:String):Unit = {
  93.     var s = getSquare(x,y)
  94.     allSquares = allSquares.filter(_ != s)
  95.     s = s.removeValue(wrongSolution)
  96.     allSquares = allSquares :+ s
  97.   }
  98.  
  99.   def getShips(ships:String):List[(Int, Int)] = {
  100.     var boats = List[(Int, Int)]()
  101.     var shipList = List[String]()
  102.  
  103.     // Read in ships from file
  104.     for(i <- 0 until ships.length){
  105.       if(ships.charAt(i) == 'x'){
  106.         shipList = shipList ::: List(s"${ships(i-1)}${ships(i)}${ships(i+1)}")
  107.       }
  108.     }
  109.  
  110.     var amount = 0
  111.     var size = 0
  112.     var xIndex = 0
  113.  
  114.     // Save ships in a list of tuples(amount, size)
  115.     // Example: (3, 1) means 3x1, or 3 boats of size 1
  116.     for(ship <- shipList){
  117.       xIndex = ship.indexOf('x')
  118.       amount = ship.substring(0, xIndex).toInt
  119.       size = ship.substring(xIndex + 1, ship.length).toInt
  120.  
  121.       boats = boats :+ (amount, size)
  122.     }
  123.  
  124.     // Return tuple list:
  125.     boats
  126.   }
  127.  
  128.   // Takes in a size as parameter, and loops through the list of tuples for the correct size
  129.   // When it finds the correct tuple, decrement the amount
  130.   // Return the new list
  131.   def decrementBoats(size:Int, boats:List[(Int, Int)]):List[(Int, Int)] = {
  132.     var boats2 = List[(Int, Int)]()
  133.     for(boat <- boats){
  134.       if(boat._2 == size && boat._1 > 0) boats2 = boats2 :+ (boat._1 - 1, boat._2)
  135.       else boats2 = boats2 :+ (boat._1, boat._2)
  136.     }
  137.     boats2
  138.   }
  139.  
  140.   def incrementBoats(size:Int, boats:List[(Int, Int)]):List[(Int, Int)] = {
  141.     var boats2 = List[(Int, Int)]()
  142.     for(boat <- boats){
  143.       if(boat._2 == size) boats2 = boats2 :+ (boat._1 + 1, boat._2)
  144.       else boats2 = boats2 :+ (boat._1, boat._2)
  145.     }
  146.     boats2
  147.   }
  148.  
  149.   def saveBoard(allSquares:List[Square]):Unit = {
  150.     savedBoard = allSquares
  151.   }
  152.  
  153.   def allowedMove(horizontalHints:List[Int], verticalHints:List[Int]):Boolean = {
  154.     var boardSize = horizontalHints.length
  155.     // Check if board fulfills all rules
  156.     // No boats touch each other
  157.     // Correct amount of boats in each row/col according to hints
  158.     var i = 1
  159.     for(hint <- horizontalHints){
  160.       if(getAllFromX(i).count((s:Square) => s.getValue == "S") > hint) return false
  161.       i += 1
  162.     }
  163.     i = 1
  164.     for(hint <- verticalHints){
  165.       if(getAllFromY(i).count((s:Square) => s.getValue == "S") > hint) return false
  166.       i += 1
  167.     }
  168.  
  169.     for(boat <- boatsList){
  170.       if(!isBoatBuilt(boat, boardSize)) return false
  171.     }
  172.  
  173.     true
  174.   }
  175.  
  176.   def bruteforceParty(boats:List[(Int, Int)], horizontalHints:List[Int], verticalHints:List[Int]):List[(Int, Int)] = {
  177.     var maxShip = 0
  178.     unsolvedSquares = getUnsolved
  179.  
  180.     for(boat <- boats){
  181.       if(boat._2 > maxShip && boat._1 > 0) maxShip = boat._2
  182.     }
  183.  
  184.     saveBoard(allSquares)
  185.  
  186.     var x = unsolvedSquares.head.x
  187.     var y = unsolvedSquares.head.y
  188.     var hint = verticalHints(y-1)
  189.  
  190.     //Get all lines where maxShip can mathematically be placed from hint and where the amount of boats is not yet solved
  191.     //Change the first square in unsolvedSquare, set it to value "boat"
  192.     if(hint >= maxShip && getAllFromY(x).count((s:Square) => s.getValue == "S") < hint){
  193.       val chosen = allSquares.filter((s:Square) => getSquare(s.x, s.y) == unsolvedSquares.head)
  194.       chosen.head.setValues("S")
  195.       println("Value of (" + getSquare(x, y).x + "," + getSquare(x, y).y + ") is changed to S\n")
  196.     }
  197.     var updatedShips = boats
  198.     updatedShips = updateBoatsList(horizontalHints.length, boats)
  199.  
  200.     updatedShips
  201.   }
  202.  
  203.   def getUnsolved:List[Square] = {
  204.     unsolvedSquares = allSquares.filter((u:Square) => !u.s)
  205.     unsolvedSquares
  206.   }
  207.  
  208.  
  209.  
  210.   def updateUnsolved():List[Square] = {
  211.     unsolvedSquares = unsolvedSquares.drop(1)
  212.     unsolvedSquares
  213.   }
  214.  
  215.   //TODO: Update so function checks if an entire boat is surrounded by water instead of only checking endpoints of boat
  216.   def isBoatBuilt(boat:List[Square], boardSize:Int):Boolean = {
  217.     //Check for boat of size 1 (one boat-tile is surrounded by water)
  218.     if(boat.length == 1){
  219.       if(boat.head.getNeighbours(boardSize).forall((n:String) => n == "-")) return true
  220.     }
  221.  
  222.     else {
  223.       var headNeighbours = boat.head.getNeighbours(boardSize)
  224.       var lastNeighbours = boat.last.getNeighbours(boardSize)
  225.  
  226.       //Check for horizontal boat (Head -> left tile = water, right tile = boat)(Last -> left tile = boat, right tile = water)
  227.       if (headNeighbours(2) == "-" && headNeighbours(0) == "S"
  228.         && lastNeighbours(0) == "-" && lastNeighbours(2) == "S") return true
  229.  
  230.       //Check for vertical boat (Head -> upper tile = water, lower tile = boat)(Last -> upper tile = boat, lower tile = water)
  231.       if (headNeighbours(3) == "-" && headNeighbours(1) == "S"
  232.         && lastNeighbours(1) == "-" && lastNeighbours(3) == "S") return true
  233.     }
  234.     false
  235.   }
  236.  
  237.   def isBoatFinished(boat:List[Square], boardSize:Int): Boolean ={
  238.     //Check for boat of size 1 (one boat-tile is surrounded by water)
  239.     if(boat.length == 1){
  240.       if(boat.head.getNeighbours(boardSize).forall((n:String) => n == "-")) return true
  241.     }
  242.  
  243.     else {
  244.       var headNeighbours = boat.head.getNeighbours(boardSize)
  245.       var lastNeighbours = boat.last.getNeighbours(boardSize)
  246.  
  247.       //Check for horizontal boat (Head -> left tile = water, right tile = boat)(Last -> left tile = boat, right tile = water)
  248.       if (headNeighbours(2) == "-" && headNeighbours(0) != "?"
  249.         && lastNeighbours(0) == "-" && lastNeighbours(2) != "?") return true
  250.  
  251.       //Check for vertical boat (Head -> upper tile = water, lower tile = boat)(Last -> upper tile = boat, lower tile = water)
  252.       if (headNeighbours(3) == "-" && headNeighbours(1) != "?"
  253.         && lastNeighbours(1) == "-" && lastNeighbours(3) != "?") return true
  254.     }
  255.     false
  256.   }
  257.  
  258.   def updateBoatsList(boardSize:Int, ships:List[(Int, Int)]):List[(Int, Int)] = {
  259.     var boat = List[Square]()
  260.     var boat2 = List[Square]()
  261.     var updatedShips = ships
  262.  
  263.     //Horizontal boats
  264.     for(row <- 1 to boardSize) {
  265.       val line = getAllFromY(row)
  266.       for (i <- 1 to boardSize) {
  267.         if (getAllFromY(row).filter((s: Square) => s.x == i).head.possibleValues.head == "S") {
  268.           boat2 = boat2 :+ line.filter((s:Square) => s.x == i).head
  269.         }
  270.         else {
  271.           if(boat2.length > boat.length){
  272.             boat = boat2
  273.             if(isBoatBuilt(boat, boardSize)) {
  274.               if(!boatsList.contains(boat)) {
  275.                 boatsList = boatsList :+ boat
  276.                 updatedShips = decrementBoats(boat.length, updatedShips)
  277.               }
  278.             }
  279.             boat = List[Square]()
  280.             boat2 = List[Square]()
  281.           }
  282.         }
  283.       }
  284.     }
  285.  
  286.     boat = List[Square]()
  287.     boat2 = List[Square]()
  288.  
  289.     //Vertical boats
  290.     for(col <- 1 to boardSize) {
  291.       val line = getAllFromX(col)
  292.       for (i <- 1 to boardSize) {
  293.         if (getAllFromX(col).filter((s: Square) => s.y == i).head.possibleValues.head == "S") {
  294.           boat2 = boat2 :+ line.filter((s:Square) => s.y == i).head
  295.         }
  296.         else {
  297.           if(boat2.length > boat.length){
  298.             boat = boat2
  299.             if(isBoatBuilt(boat, boardSize)) {
  300.               if(!boatsList.contains(boat)) {
  301.                 boatsList = boatsList :+ boat
  302.                 updatedShips = decrementBoats(boat.length, updatedShips)
  303.               }
  304.             }
  305.             boat = List[Square]()
  306.             boat2 = List[Square]()
  307.           }
  308.         }
  309.       }
  310.     }
  311.     updatedShips
  312.   }
  313.  
  314.   def getHints(hintLine:String):List[Int] = {
  315.     var hint = List[Int]()
  316.     for(i <- 0 until hintLine.length){
  317.       if(hintLine.charAt(i) == ' '){
  318.         hint = hint ::: List(hintLine.charAt(i+1).toString.toInt)
  319.       }
  320.     }
  321.     hint
  322.   }
  323.  
  324.   def waterZeros(verticalHints:List[Int], horizontalHints:List[Int], size:Int):Unit = {
  325.     horizontalHints.foreach(hint => if(hint == 0) getAllFromX(horizontalHints.indexOf(hint)+1).foreach(u => setValue(u.x, u.y,"-")))
  326.     verticalHints.foreach(hint => if(hint == 0) getAllFromY(verticalHints.indexOf(hint)+1).foreach(u => setValue(u.x, u.y,"-")))
  327.   }
  328.  
  329.   def fillAroundHints():Unit={
  330.     allSquares.filter(_.possibleValues.length==1).foreach(k=> k.possibleValues.head match {
  331.       case "*" => {
  332.         allSquares.filter((b: Square) => (b.y == k.y - 1 && (b.x == k.x - 1 || b.x == k.x || b.x == k.x + 1)) || (b.y == k.y + 1 && (b.x == k.x - 1 || b.x == k.x || b.x == k.x + 1)) || (b.y == k.y && (b.x == k.x - 1 || b.x == k.x + 1))).foreach(z => setValue(z.x, z.y, "-"))
  333.         setValue(k.x, k.y, "S")
  334.       }
  335.       case "<" => {
  336.         allSquares.filter((b: Square) => (b.y == k.y - 1 && (b.x == k.x - 1 || b.x == k.x || b.x == k.x + 1)) || (b.y == k.y + 1 && (b.x == k.x - 1 || b.x == k.x || b.x == k.x + 1)) || (b.y == k.y && b.x == k.x - 1)).foreach(z => setValue(z.x, z.y, "-"))
  337.         if(getSquare(k.x+1,k.y).possibleValues.length!=1)setValue(k.x + 1, k.y, "S")
  338.         setValue(k.x, k.y, "S")
  339.       }
  340.       case ">" => {
  341.         allSquares.filter((b: Square) => (b.y == k.y - 1 && (b.x == k.x - 1 || b.x == k.x || b.x == k.x + 1)) || (b.y == k.y + 1 && (b.x == k.x - 1 || b.x == k.x || b.x == k.x + 1)) || (b.y == k.y && b.x == k.x + 1)).foreach(z => setValue(z.x, z.y, "-"))
  342.         if(getSquare(k.x-1,k.y).possibleValues.length!=1)setValue(k.x - 1, k.y, "S")
  343.         setValue(k.x, k.y, "S")
  344.       }
  345.       case "A" => {
  346.         allSquares.filter((b:Square)=> (b.y == k.y-1 && (b.x ==k.x-1||b.x ==k.x || b.x ==k.x+1)) || (b.y ==k.y+1 && (b.x ==k.x-1|| b.x==k.x+1)) || (b.y==k.y && (b.x==k.x-1 || b.x==k.x+1))).foreach(z=>setValue(z.x,z.y,"-"))
  347.         if(getSquare(k.x,k.y+1).possibleValues.length!=1)setValue(k.x, k.y+1, "S")
  348.         setValue(k.x, k.y, "S")
  349.       }
  350.       case "V" => {
  351.         allSquares.filter((b:Square)=> (b.y == k.y-1 && (b.x ==k.x-1|| b.x ==k.x+1)) || (b.y ==k.y+1 && (b.x ==k.x-1||b.x==k.x || b.x==k.x+1)) || (b.y==k.y && (b.x==k.x-1 || b.x==k.x+1))).foreach(z=>setValue(z.x,z.y,"-"))
  352.         if(getSquare(k.x,k.y-1).possibleValues.length!=1)setValue(k.x, k.y-1, "S")
  353.         setValue(k.x, k.y, "S")
  354.       }
  355.       case "+" => allSquares.filter((b:Square)=> (b.y == k.y-1 && (b.x ==k.x-1|| b.x ==k.x+1)) || (b.y ==k.y+1 && (b.x ==k.x-1|| b.x==k.x+1))).foreach(z=>setValue(z.x,z.y,"-"))
  356.       /*{
  357.               allSquares.filter((b:Square)=> (b.y == k.y-1 && (b.x ==k.x-1|| b.x ==k.x+1)) || (b.y ==k.y+1 && (b.x ==k.x-1|| b.x==k.x+1))).foreach(z=>setValue(z.x,z.y,"-"))
  358.               setValue(k.x, k.y, "S")
  359.             }*/
  360.       case "S" => allSquares.filter((b:Square)=> (b.y == k.y-1 && (b.x ==k.x-1|| b.x ==k.x+1)) || (b.y ==k.y+1 && (b.x ==k.x-1|| b.x==k.x+1))).foreach(z=>setValue(z.x,z.y,"-"))
  361.       case "-" =>
  362.     })
  363.   }
  364.  
  365.   def checkPlus(k:Square)={
  366.     if(getSquare(k.x,k.y-1).possibleValues== List("-") || getSquare(k.x,k.y+1).possibleValues== List("-")){
  367.       setValue(k.x+1,k.y,"S")
  368.       setValue(k.x-1,k.y,"S")
  369.       setValue(k.x,k.y,"S")
  370.     }
  371.     else if (getSquare(k.x+1,k.y).possibleValues== "-" || getSquare(k.x-1,k.y).possibleValues== "-"){
  372.       setValue(k.x,k.y+1,"S")
  373.       setValue(k.x,k.y-1,"S")
  374.       setValue(k.x,k.y,"S")
  375.     }
  376.   }
  377.  
  378.  
  379.   // Fills water in the corner squares of each boat
  380.   def waterCorners():Unit = {
  381.     allSquares.filter(_.possibleValues.head == "S").foreach(k => k.possibleValues.head match {
  382.       case "S" => allSquares.filter((b:Square) => (b.y == k.y-1 && (b.x ==k.x-1|| b.x ==k.x+1)) || (b.y ==k.y+1 && (b.x ==k.x-1|| b.x==k.x+1))).foreach(z=>setValue(z.x,z.y,"-"))
  383.       case _ =>
  384.     })
  385.   }
  386.  
  387.   def checkBoatsXY(horizontal:List[Int], vertical:List[Int], size:Int):Unit = {
  388.     for (i <- 0 until size) {
  389.       if (getAllFromX(i + 1).filter(_.possibleValues.length == 1).count(_.possibleValues.head != "-") == horizontal(i)) {
  390.         getAllFromX(i + 1).filter(_.possibleValues.length != 1).foreach(u => setValue(u.x, u.y, "-"))
  391.       } else if (horizontal(i) - getAllFromX(i + 1).count(_.possibleValues.head == "S") == getAllFromX(i + 1).count(_.possibleValues.length != 1)){
  392.         getAllFromX(i + 1).filter(_.possibleValues.length != 1).foreach(u => setValue(u.x, u.y, "S"))
  393.       }
  394.  
  395.       if (getAllFromY(i + 1).filter(_.possibleValues.length == 1).count(_.possibleValues.head != "-") == vertical(i)) {
  396.         getAllFromY(i + 1).filter(_.possibleValues.length != 1).foreach(u => setValue(u.x, u.y, "-"))
  397.       } else if (vertical(i) - getAllFromY(i + 1).count(_.possibleValues.head == "S") == getAllFromY(i + 1).count(_.possibleValues.length != 1)) {
  398.         getAllFromY(i + 1).filter(_.possibleValues.length != 1).foreach(u => setValue(u.x, u.y, "S"))
  399.       }
  400.     }
  401.   }
  402.  
  403.   def checkIfSolved():Boolean = {
  404.     allSquares.forall(_.s)
  405.   }
  406.  
  407.   def printToFile(solved:String):Unit = {
  408.     val out = new BufferedWriter(new FileWriter(new File("puzzle_solved.txt")))
  409.  
  410.     out.write(solved)
  411.     out.close()
  412.   }
  413.  
  414.   def fillOnebyOneSquares(size:Int): Unit ={
  415.     allSquares.filter(_.possibleValues.length==2).filter(_.getNeighbours(size)==List("-","-","-","-")).foreach(i=>setValue(i.x,i.y,"-"))
  416.   }
  417.  
  418.   def fillBydecreasingsize(boardSize:Int,currentround:Int,boats:List[Tuple2[Int,Int]],currentsize:Tuple2[Int,Int]): Unit ={
  419.     var boat = List[Square]()
  420.     var boat2 = List[Square]()
  421.  
  422.  
  423.     for(row <- 1 to boardSize) {
  424.       val line = getAllFromY(row)
  425.       for (i <- 1 to boardSize) {
  426.         if (getAllFromY(row).filter((s: Square) => s.x == i).head.possibleValues.head == "S") {
  427.           boat2 = boat2 :+ line.filter((s:Square) => s.x == i).head
  428.         }
  429.         else {
  430.           if(boat2.length > boat.length){
  431.             boat = boat2
  432.             if(isBoatFinished(boat, boardSize)) {
  433.               if(boat.head.x-1!=0) {
  434.                 setValue(boat.head.x - 1,boat.head.y, "-")
  435.               }
  436.               if(boat.last.x+1<=boardSize){
  437.                 setValue(boat.last.x + 1,boat.last.y, "-")
  438.  
  439.               }
  440.               }
  441.             boat = List[Square]()
  442.             boat2 = List[Square]()
  443.           }
  444.         }
  445.       }
  446.     }
  447.  
  448.     boat = List[Square]()
  449.     boat2 = List[Square]()
  450.  
  451.     //Vertical boats
  452.     for(col <- 1 to boardSize) {
  453.       val line = getAllFromX(col)
  454.       for (i <- 1 to boardSize) {
  455.         if (getAllFromX(col).filter((s: Square) => s.y == i).head.possibleValues.head == "S") {
  456.           boat2 = boat2 :+ line.filter((s:Square) => s.y == i).head
  457.         }
  458.         else {
  459.           if(boat2.length > boat.length){
  460.             boat = boat2
  461.             if(isBoatFinished(boat, boardSize)) {
  462.               if(boat.head.y-1>0) {
  463.                 setValue(boat.head.x, boat.head.y-1, "-")
  464.               }
  465.               if(boat.last.y+1<=boardSize){
  466.                 setValue(boat.head.x,boat.last.y + 1, "-")
  467.               }
  468.             }
  469.             boat = List[Square]()
  470.             boat2 = List[Square]()
  471.           }
  472.         }
  473.       }
  474.     }
  475.       //fill currentsize-1's
  476.     if(currentsize._2==2){
  477.       return
  478.     }
  479.     fillBydecreasingsize(boardSize,currentround+1,boats,boats(boats.length-currentround-1))
  480.     return
  481.  
  482.   }
  483.  
  484.   def mainSolver(path:String):Unit = {
  485.     val lines = Source.fromFile(path).getLines.toList
  486.     var smolLines = lines
  487.     val puzzleAmount = lines(0).charAt(8).toString.toInt
  488.     var solved = "Puzzles " + puzzleAmount +"\n"
  489.     var currentBoard = List[Square]()
  490.     var lastBoard = List[Square]()
  491.     var updatedShips = List[(Int, Int)]()
  492.     var bruteforceFlag = true
  493.  
  494.     smolLines = smolLines.drop(1)
  495.  
  496.     for(i <- 0 until puzzleAmount){ //one loop is one puzzle solving sequence
  497.  
  498.       smolLines = smolLines.drop(1)
  499.       val ships = getShips(smolLines.head)
  500.       smolLines = smolLines.drop(1)
  501.       val horizontalHints = getHints(smolLines.head)
  502.       smolLines = smolLines.drop(1)
  503.       val verticalHints = getHints(smolLines.head)
  504.       val size = verticalHints.length
  505.       smolLines = smolLines.drop(1)
  506.       smolLines = smolLines.drop(1)
  507.  
  508.       for (y <- 0 until size) {
  509.         var boatline = smolLines.head.filter(_ != ' ')
  510.  
  511.         for (x <- 0 until size) {
  512.           if (boatline.charAt(x) != '?') {
  513.             allSquares = allSquares ::: List(new Square(x + 1, y + 1, List(boatline.charAt(x).toString), true))
  514.           } else {
  515.             allSquares = allSquares ::: List(new Square(x + 1, y + 1))
  516.           }
  517.         }
  518.         smolLines = smolLines.drop(1)
  519.       }
  520.  
  521.       currentBoard = allSquares
  522.       updatedShips = ships
  523.  
  524.       /*****************************************************************************************************************/
  525.  
  526.       def printTempSolution():Unit = {
  527.         print("   ")
  528.         horizontalHints.foreach((h:Int) => print(h + " "))
  529.         println()
  530.         for(y <- 1 to size){
  531.           print(verticalHints(y-1) + "  ")
  532.           for(x <- 1 to size){
  533.             if(getSquare(x,y).possibleValues.length > 1) print("? ")
  534.             else print(getSquare(x,y).possibleValues.head + " ")
  535.           }
  536.           println()
  537.         }
  538.         println()
  539.       }
  540.  
  541.       printTempSolution() // Original puzzle
  542.       fillAroundHints()
  543.       waterZeros(verticalHints, horizontalHints, size)
  544.       printTempSolution()
  545.  
  546.       while (!checkIfSolved()) {
  547.         lastBoard = currentBoard
  548.         checkBoatsXY(horizontalHints, verticalHints, size)
  549.         waterCorners()
  550.         printTempSolution()
  551.         currentBoard = allSquares
  552.  
  553.  
  554.  
  555.         for(x <- 1 to size){
  556.           for(y <- 1 to size){
  557.             if(getSquare(x,y).possibleValues==List("+")) {
  558.               checkPlus(getSquare(x, y))
  559.             }
  560.           }
  561.         }
  562.  
  563.         updatedShips = updateBoatsList(size, updatedShips)
  564.  
  565.  
  566.         if(updatedShips.head._2==1 && updatedShips.head._1==0){
  567.           fillOnebyOneSquares(size)
  568.         }
  569.         if(updatedShips.last._1==0){
  570.           fillBydecreasingsize(size,0,updatedShips,updatedShips.last)
  571.         }
  572.  
  573.  
  574.         if(currentBoard == lastBoard) {
  575.  
  576.           println("I AM STUCK PLEASE HELP")
  577.           if(bruteforceFlag) updatedShips = bruteforceParty(updatedShips, horizontalHints, verticalHints)
  578.           unsolvedSquares = updateUnsolved()
  579.         }
  580.       }
  581.  
  582.       /****************************************************************************************************************/
  583.  
  584.       updatedShips = updateBoatsList(size, updatedShips)
  585.       println("Ships: " + ships)
  586.       println("Updated: " + updatedShips)
  587.       println("Vertical: " + verticalHints)
  588.       println("Horizontal: " + horizontalHints)
  589.  
  590.       solved += "Size " + size + "x" + size + "\n"
  591.  
  592.       for(y <- 1 to size) {
  593.         for(x <- 1 to size){
  594.           solved += getSquare(x,y).possibleValues.head + " "
  595.           print(getSquare(x,y).possibleValues.head + " ")
  596.         }
  597.         solved += "\n"
  598.         println()
  599.       }
  600.  
  601.       //println(size)
  602.       allSquares = List[Square]()
  603.       boatsList = List[List[Square]]()
  604.       println("\n")
  605.     }
  606.  
  607.     printToFile(solved)
  608.   }
  609.  
  610.   mainSolver("puzzle_unsolved.txt")
  611.  
  612. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement