import scala.collection.mutable.ListBuffer object error { var error: Boolean = false } object Main { def main(args: Array[String]) { val main: Main = new Main() println("Enter your values, line by line, in the form X X X X X X X X X below.") var multiArray = Array.ofDim[Int](9, 9) multiArray = main.getInput main.duplicateLine(multiArray) main.duplicateColumn(multiArray) for (i <- 0.until(3); j <- 0.until(3)) { main.threeByThree(multiArray, i, j) } if (error.error == false) { println("This is a valid sudoku input!") } } } class Main { def getInput = { val array: Array[Int] = Array.ofDim(9) val multiArray: Array[Array[Int]] = Array.ofDim[Int](9, 9) var s: String = null for (i <- 0.until(9)) { s = scala.io.StdIn.readLine() println(s + " <-- row entered, continue below:") var k: Int = 0 for (j <- 0.until(9)) { multiArray(i)(j) = s.charAt(k).asDigit k = k + 2 } } println("\n") multiArray.foreach(row => println(row.mkString(" "))) multiArray } def duplicateLine(array: Array[Array[Int]]): Unit = { for (i <- 0.until(9)) { var duplicate = new ListBuffer[Integer] for (j <- 0.until(9)) { val valueToCheck: Int = array(i)(j) if ((duplicate.contains(valueToCheck))) { error.error = true println("Rule violated in row " + (i + 1)) } else { duplicate += (valueToCheck) } } } } def duplicateColumn(array: Array[Array[Int]]): Unit = { for (j <- 0.until(9)) { var duplicatec = new ListBuffer[Integer] for (i <- 0.until(9)) { val valueToCheck: Int = array(i)(j) if (duplicatec.contains(valueToCheck)) { error.error = true println("Rule violated in column " + (j + 1)) } else { duplicatec += (valueToCheck) } } } } def threeByThree(array: Array[Array[Int]], row: Int, col: Int): Unit = { var duplicatet = new ListBuffer[Integer] for (i <- 0.until(3); j <- 0.until(3)) { val valueToCheck: Int = array(i + (row * 3))(j + (col * 3)) if (duplicatet.contains(valueToCheck)) { error.error = true row match { case 0 => if (col == 0) { println("Rule violated in 3x3 grid upper left") } else if (col == 1) { println("Rule violated in 3x3 grid upper middle") } else { println("Rule violated in 3x3 grid upper right") } case 1 => if (col == 0) { println("Rule violated in 3x3 grid middle left") } else if (col == 1) { println("Rule violated in 3x3 grid middle") } else { println("Rule violated in 3x3 grid middle right") } case 2 => if (col == 0) { println("Rule violated in 3x3 grid bottom left") } else if (col == 1) { println("Rule violated in 3x3 grid bottom middle") } else { println("Rule violated in 3x3 grid bottom right") } } } else { duplicatet += (valueToCheck) } } } }
Write, Run & Share Scala code online using OneCompiler's Scala online compiler for free. It's one of the robust, feature-rich online compilers for Scala language, running on the latest version 2.13.8. Getting started with the OneCompiler's Scala compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Scala
and start coding.
OneCompiler's Scala online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Scala program which takes name as input and prints hello message with your name.
object Hello {
def main(args: Array[String]): Unit = {
val name = scala.io.StdIn.readLine() // Read input from STDIN
println("Hello " + name )
}
}
Scala is both object-oriented and functional programming language by Martin Odersky in the year 2003.
Variable is a name given to the storage area in order to identify them in our programs.
var or val Variable-name [: Data-Type] = [Initial Value];
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression) {
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} else if (condition-expression2) {
//code if above condition is true
}
else if(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the above conditions are false
}
For loop is used to iterate a set of statements based on a criteria.
for(index <- range){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition)
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
def functionname(parameters : parameters-type) : returntype = { //code
}
You can either use =
or not in the function definition. If =
is not present, function will not return any value.