Skip to content

Commit

Permalink
mower mouvement loop ok
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymax25 committed Dec 7, 2021
1 parent 1cc6ee1 commit 22d5d9d
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 11 deletions.
2 changes: 1 addition & 1 deletion projet/funprog-al/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ val compilerOptions = Seq(
"-Xlint:stars-align",
"-Xlint:constant",
"-Xlint:nonlocal-return",
"-Xlint:valpattern",
// "-Xlint:valpattern",
"-Xlint:eta-zero,eta-sam",
"-Xlint:deprecation",
"-Xlint:nullary-unit", // Warn when nullary methods return Unit.
Expand Down
65 changes: 64 additions & 1 deletion projet/funprog-al/src/main/scala/progfun/Environnement.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.esgi.al.funprog

class Environnement(height: Int, width: Int, mowers: List[Mower]) {
@SuppressWarnings(Array("org.wartremover.warts.Var"))
class Environnement(height: Int, width: Int, var mowers: List[Mower]) {

def display(): Unit = {
println("Display environnement:")
Expand All @@ -14,4 +15,66 @@ class Environnement(height: Int, width: Int, mowers: List[Mower]) {
println()
}
}

def play(): Unit = mowers.filter(m => m.actions.length > 0) match {
case first :: rest => {
applyAction(first)
}
case Nil => println("no mowers to play")
}

def applyAction(mower: Mower): Unit = mower.actions match {
case Nil => println("yo")
case first :: rest => {
runAction(mower, first)
mower.actions = rest
}
}

def runAction(mower: Mower, action: Action): Unit = {
action match {
case Left(_) => turnLeft(mower)
case Right(_) => turnRight(mower)
case Advance(_) => advance(mower)
case _ => println("no action")
}
}

def turnRight(mower: Mower) = mower.direction match {
case North() => mower.direction = new East()
case South() => mower.direction = new West()
case East() => mower.direction = new South()
case West() => mower.direction = new North()
}

def turnLeft(mower: Mower) = mower.direction match {
case North() => mower.direction = new West()
case South() => mower.direction = new East()
case East() => mower.direction = new North()
case West() => mower.direction = new South()
}

def advance(mower: Mower) = mower.direction match {
case North() =>
if (isOkMove(mower.x, mower.y + 1)) {
mower.y = mower.y + 1
}
case South() =>
if (isOkMove(mower.x, mower.y - 1)) {
mower.y = mower.y - 1
}
case East() =>
if (isOkMove(mower.x + 1, mower.y)) {
mower.x = mower.x + 1
}
case West() =>
if (isOkMove(mower.x - 1, mower.y)) {
mower.x = mower.x - 1
}
}

def isOkMove(x: Int, y: Int): Boolean =
x < 0 || x > width || y < 0 || y > height || mowers
.filter(m => m.x == x && m.y == y)
.length == 0
}
3 changes: 2 additions & 1 deletion projet/funprog-al/src/main/scala/progfun/InputLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class InputLoader(filePath: String) {
"wrong line for mower init position!"
)
}
val actions: List[Action] = l(1).split("").toList.map(s => new Action(s))
val actions: List[Action] =
l(1).split("").toList.map(s => Action.getActionFromString(s))
new Mower(
coords(0).toInt,
coords(1).toInt,
Expand Down
12 changes: 11 additions & 1 deletion projet/funprog-al/src/main/scala/progfun/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,15 @@ object Main extends App {

val env = new Environnement(x, y, mowers)

env.display()
println(env.mowers)

while (env.mowers.filter(m => m.actions.length > 0).length > 0) {
env.play()
env.display()
Thread.sleep(250)
}

println("End of all mower sequences, writing result file...")

println(env.mowers)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Action(str: String) {}

object Action {

def apply(str: String): Action = str match {
def getActionFromString(str: String): Action = str match {
case "G" => new Left("G")
case "D" => new Right("D")
case "A" => new Advance("A")
Expand Down
12 changes: 8 additions & 4 deletions projet/funprog-al/src/main/scala/progfun/mower/Direction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ object Direction {
}

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

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

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

case class West() extends Direction {
override def toString: String = "W"
// override def toString: String = "W"
override def toString: String = "<"
}
8 changes: 7 additions & 1 deletion projet/funprog-al/src/main/scala/progfun/mower/Mower.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ package fr.esgi.al.funprog

import fr.esgi.al.funprog._

case class Mower(x: Int, y: Int, direction: Direction, actions: List[Action]) {}
@SuppressWarnings(Array("org.wartremover.warts.Var"))
case class Mower(
var x: Int,
var y: Int,
var direction: Direction,
var actions: List[Action]
) {}
2 changes: 1 addition & 1 deletion projet/funprog-al/tmp/input.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
9 5
5 5
1 2 N
GAGAGAGAA
3 3 E
Expand Down

0 comments on commit 22d5d9d

Please sign in to comment.