Skip to content

Commit

Permalink
create inputLoader and organize into files
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymax25 committed Dec 7, 2021
1 parent 4812534 commit 89f31db
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 472 deletions.
17 changes: 17 additions & 0 deletions projet/funprog-al/src/main/scala/progfun/Environnement.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package fr.esgi.al.funprog

class Environnement(height: Int, width: Int, mowers: List[Mower]) {

def display(): Unit = {
println("Display environnement:")
for (y <- height to 0 by -1) {
for (x <- 0 to width) {
print(mowers.find(m => m.x == x && m.y == y) match {
case Some(m) => " " + m.direction.toString + " "
case None => " - "
})
}
println()
}
}
}
66 changes: 66 additions & 0 deletions projet/funprog-al/src/main/scala/progfun/InputLoader.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fr.esgi.al.funprog

import better.files._
import fr.esgi.al.funprog._

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
class InputLoader(filePath: String) {

def getFirstLine(lines: List[String]): String = lines match {
case Nil => throw DonneesIncorectesException("No lines in the file!")
case first :: Nil => first
case first :: rest => first
}

def getMowersLines(lines: List[String]): List[String] = lines match {
case Nil => throw DonneesIncorectesException("No mower lines in the file!")
case first :: Nil =>
throw DonneesIncorectesException("No mower lines in the file!")
case first :: rest => rest
}

def parseInput(): (Int, Int, List[Mower]) = {
val f: File = File(this.filePath)
val lines: List[String] = f.lines.toList
val sizeLine: String = getFirstLine(lines)
val mowerLines: List[String] = getMowersLines(lines)

if (mowerLines.size % 2 != 0) {
throw DonneesIncorectesException(
"Even number of lines, each mower must have 1 line for its initial postion and 1 line fir his directions list!"
)
}

val sizes: List[Int] = parseSizeLine(sizeLine)
if (sizes.size != 2) {
throw DonneesIncorectesException(
"You must have two numbers separated by a space in the first line of the input, height and width of the area!"
)
}

val groupedMowerLines = mowerLines.grouped(2).toList
val mowers: List[Mower] = parseMowerLines(groupedMowerLines)

(sizes(0), sizes(1), mowers)
}

def parseSizeLine(sizeLine: String): List[Int] =
sizeLine.split(" ").toList.map(s => s.toInt)

def parseMowerLines(mowerLines: List[List[String]]): List[Mower] =
mowerLines.map(l => {
val coords = l(0).split(" ").toList
if (coords.size != 3) {
throw DonneesIncorectesException(
"wrong line for mower init position!"
)
}
val actions = l(1).split("").toList
new Mower(
coords(0).toInt,
coords(1).toInt,
Direction.getFromString(coords(2))
)
})

}
121 changes: 8 additions & 113 deletions projet/funprog-al/src/main/scala/progfun/Main.scala
Original file line number Diff line number Diff line change
@@ -1,131 +1,26 @@
package fr.esgi.al.funprog

import better.files._
import com.typesafe.config.{Config, ConfigFactory}

import fr.esgi.al.funprog._

final case class DonneesIncorectesException(
val message: String
) extends Exception(message)

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
object Main extends App {

val conf: Config = ConfigFactory.load()
val inputFilePath: String = conf.getString("application.input-file")

val f: File = File(inputFilePath)

def getFirstLine(lines: List[String]) = lines match {
case Nil => throw DonneesIncorectesException("No lines in the file!")
case first :: Nil => first
case first :: rest => first
}
def getMowersLines(lines: List[String]) = lines match {
case Nil => throw DonneesIncorectesException("No mower lines in the file!")
case first :: Nil =>
throw DonneesIncorectesException("No mower lines in the file!")
case first :: rest => rest
}

val sizeLine = getFirstLine(f.lines.toList)
val mowerLines = getMowersLines(f.lines.toList)

val sizes: List[Int] = sizeLine.split(" ").toList.map(s => s.toInt)
if (sizes.size != 2) {
throw DonneesIncorectesException(
"You must have two numbers separated by a space in the first line of the input, height and width of the area!"
)
}
val loader = new InputLoader(inputFilePath)

if (mowerLines.size % 2 != 0) {
throw DonneesIncorectesException(
"even number of lines, each mower must have 1 line for its initial postion and 1 line fir his directions list!"
)
}
val mowersLinesGrouped = mowerLines.grouped(2).toList
val mowers: List[Mower] = mowersLinesGrouped.map(l => {
val coords = l(0).split(" ").toList
if (coords.size != 3) {
throw DonneesIncorectesException(
"wrong line for mower init position!"
)
}
val actions = l(1).split("").toList
new Mower(
coords(0).toInt,
coords(1).toInt,
Direction.getFromString(coords(2))
)
})
val inputs = loader.parseInput()
val x: Int = inputs._1
val y: Int = inputs._2
val mowers: List[Mower] = inputs._3

mowers.foreach(m => println(m))

val env = new Environnement(sizes(0), sizes(1), mowers)
val env = new Environnement(x, y, mowers)

env.display()

}

class Environnement(height: Int, width: Int, mowers: List[Mower]) {

def display(): Unit = {
println("Display environnement:")
for (y <- height to 0 by -1) {
for (x <- 0 to width) {

val toPrint = mowers.find(m => m.x == x && m.y == y) match {
case Some(m) => " " + m.direction.toString + " "
case None => " - "
}
print(toPrint)
}
println()
}
}
}

case class Mower(x: Int, y: Int, direction: Direction) {}

class Action {}

case class Left() extends Action {
val str = "G"
}

case class Right() extends Action {
val str = "D"
}

case class Advance() extends Action {
val str = "A"
}

abstract class Direction {
def toString(): String
}

object Direction {
def getFromString(direction: String): Direction = direction match {
case "N" => new North()
case "S" => new South()
case "W" => new West()
case "E" => new East()
}

}

case class North() extends Direction {
override def toString: String = "N"
}

case class South() extends Direction {
override def toString: String = "S"
}

case class East() extends Direction {
override def toString: String = "E"
}

case class West() extends Direction {
override def toString: String = "W"
}
15 changes: 15 additions & 0 deletions projet/funprog-al/src/main/scala/progfun/mower/Action.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.esgi.al.action

abstract class Action {
def getChar(action: Action): String = action match {
case Left() => "G"
case Right() => "D"
case Advance() => "A"
}
}

case class Left() extends Action {}

case class Right() extends Action {}

case class Advance() extends Action {}
30 changes: 30 additions & 0 deletions projet/funprog-al/src/main/scala/progfun/mower/Direction.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.esgi.al.funprog

abstract class Direction {
def toString(): String
}

object Direction {
def getFromString(direction: String): Direction = direction match {
case "N" => new North()
case "S" => new South()
case "W" => new West()
case "E" => new East()
}
}

case class North() extends Direction {
override def toString: String = "N"
}

case class South() extends Direction {
override def toString: String = "S"
}

case class East() extends Direction {
override def toString: String = "E"
}

case class West() extends Direction {
override def toString: String = "W"
}
3 changes: 3 additions & 0 deletions projet/funprog-al/src/main/scala/progfun/mower/Mower.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package fr.esgi.al.funprog

case class Mower(x: Int, y: Int, direction: Direction) {}
Loading

0 comments on commit 89f31db

Please sign in to comment.