Skip to content

Commit

Permalink
load file and put mowers in env
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymax25 committed Dec 5, 2021
1 parent 101eac8 commit 4812534
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 27 deletions.
2 changes: 1 addition & 1 deletion projet/funprog-al/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ val compilerOptions = Seq(
"-Wextra-implicit",
"-Wnumeric-widen",
"-Wvalue-discard",
"-Wunused:imports,patvars,privates,locals,explicits,implicits,params,linted",
// "-Wunused:imports,patvars,privates,locals,explicits,implicits,params,linted",
// Linting
//
"-Xlint:adapted-args",
Expand Down
30 changes: 9 additions & 21 deletions projet/funprog-al/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
appplication {
application {
name = "FunProg"
name = ${?APP_NAME} # Pour surcharger la valeur par un variable d'environnement
# name = ${?APP_NAME} # Pour surcharger la valeur par un variable d'environnement

input-file = "/tmp/input.txt"
input-file = ${?INPUT_FILE}
input-file = "tmp/input.txt"
# input-file = ${?INPUT_FILE}

output-json-file = "/tmp/output.json"
output-json-file = ${?OUTPUT_JSON_FILE}
output-json-file = "tmp/output.json"
# output-json-file = ${?OUTPUT_JSON_FILE}

output-csv-file = "/tmp/output.csv"
output-csv-file = ${?OUTPUT_CSV_FILE}
}

# A supprimer
example {
message = "hi buddy"
message = ${?EXAMPLE_MESSAGE}

one = 1
one = ${?EXAMPLE_ONE}

yes = "true"
yes = ${?EXAMPLE_YES}
}
output-csv-file = "tmp/output.csv"
# output-csv-file = ${?OUTPUT_CSV_FILE}
}
131 changes: 126 additions & 5 deletions projet/funprog-al/src/main/scala/progfun/Main.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,131 @@
package fr.esgi.al.funprog

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

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

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
object Main extends App {
println("Ici le programme principal")
// Le code suivant ne compilera pas.
// var tmp = null;
// var tmp2 = if (tmp == 1) "yes" else 1

// println(s"tmp: $tmp, tmp2: $tmp2")
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!"
)
}

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))
)
})

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

val env = new Environnement(sizes(0), sizes(1), 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"
}
5 changes: 5 additions & 0 deletions projet/funprog-al/tmp/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
9 5
1 2 N
GAGAGAGAA
3 3 E
AADAADADDA

0 comments on commit 4812534

Please sign in to comment.