Skip to content

Commit

Permalink
fix mutabilité
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymax25 committed Jan 4, 2022
1 parent 761b7cd commit c9b046c
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 103 deletions.
2 changes: 1 addition & 1 deletion projet/funprog-al/src/main/scala/example/Hello.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait Greeting {

case class MyException(msg: String) extends Exception

@SuppressWarnings(Array("org.wartremover.warts.Throw"))
@SuppressWarnings("org.wartremover.warts.Throw")
def dangerous(): Unit = {
throw new MyException("boooom")
}
Expand Down
96 changes: 39 additions & 57 deletions projet/funprog-al/src/main/scala/progfun/Environnement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package fr.esgi.al.funprog

import play.api.libs.json._

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

def display(): Unit = {
println("Display environnement:")
for (y <- limit_y to 0 by -1) {
for (x <- 0 to limit_x) {
print(mowers.find(m => m.position.x == x && m.position.y == y) match {
case Some(m) => " " + m.position.direction.toString + " "
print(mowers.find(m => m.point.x == x && m.point.y == y) match {
case Some(m) => " " + m.direction.toString + " "
case None => " - "
})
}
Expand All @@ -26,66 +29,45 @@ class Environnement(limit_x: Int, limit_y: Int, var mowers: List[Mower]) {
"tondeuse" -> mowers.map(m => m.toJson)
)

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

def applyAction(mower: Mower): Unit = mower.playedActions match {
case Nil => println("yo")
def applyAction(mower: Mower): Mower = mower.playedActions match {
case Nil => mower
case first :: rest => {
runAction(mower, first)
mower.playedActions = 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")
val updated = runAction(mower, first)
new Mower(
updated.startPoint,
updated.startDirection,
updated.point,
updated.direction,
rest,
updated.originalActions
)
}
}

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

def turnLeft(mower: Mower) = mower.position.direction match {
case North() => mower.position.direction = new West()
case South() => mower.position.direction = new East()
case East() => mower.position.direction = new North()
case West() => mower.position.direction = new South()
def runAction(mower: Mower, action: Action): Mower = action match {
case Left(_) => mower.turnLeft
case Right(_) => mower.turnRight
case Advance(_) => mower.advance(new Point(limit_x, limit_y), this.mowers)
case _ => mower
}
}

def advance(mower: Mower) = mower.position.direction match {
case North() =>
if (isOkMove(mower.position.x, mower.position.y + 1)) {
mower.position.y = mower.position.y + 1
object Environnement {
def execute(env: Environnement): Environnement =
env.mowers.filter(m => m.playedActions.length > 0) match {
case first :: rest => {
print("\u001b[2J")
env.display()
Thread.sleep(250)
execute(new Environnement(env.limit_x, env.limit_y, env.play))
}
case South() =>
if (isOkMove(mower.position.x, mower.position.y - 1)) {
mower.position.y = mower.position.y - 1
}
case East() =>
if (isOkMove(mower.position.x + 1, mower.position.y)) {
mower.position.x = mower.position.x + 1
}
case West() =>
if (isOkMove(mower.position.x - 1, mower.position.y)) {
mower.position.x = mower.position.x - 1
}
}

def isOkMove(x: Int, y: Int): Boolean =
x < 0 || x > limit_x || y < 0 || y > limit_y || mowers
.filter(m => m.position.x == x && m.position.y == y)
.length == 0

case Nil => env
}
}
// use Writes
18 changes: 7 additions & 11 deletions projet/funprog-al/src/main/scala/progfun/InputLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,18 @@ class InputLoader(filePath: String) {
}
val actions: List[Action] =
l(1).split("").toList.map(s => Action.getActionFromString(s))
val position = new Position(
coords(0).toInt,
coords(1).toInt,
Direction.getFromString(coords(2))
)

new Mower(
new Position(
new Point(
coords(0).toInt,
coords(1).toInt,
Direction.getFromString(coords(2))
coords(1).toInt
),
new Position(
Direction.getFromString(coords(2)),
new Point(
coords(0).toInt,
coords(1).toInt,
Direction.getFromString(coords(2))
coords(1).toInt
),
Direction.getFromString(coords(2)),
actions,
actions
)
Expand Down
13 changes: 3 additions & 10 deletions projet/funprog-al/src/main/scala/progfun/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@ object Main extends App {

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

while (env.mowers.filter(m => m.playedActions.length > 0).length > 0) {
env.play()
print("\u001b[2J")
Thread.sleep(250)
env.display()
}
val executed = Environnement.execute(env)

println(Json.prettyPrint(executed.toJson))

// println(env.mowers)

println(Json.prettyPrint(env.toJson))


}
11 changes: 11 additions & 0 deletions projet/funprog-al/src/main/scala/progfun/mower/Direction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package fr.esgi.al.funprog

abstract class Direction {
def toString(): String
def turnRight: Direction
def turnLeft: Direction
}

object Direction {
Expand All @@ -15,16 +17,25 @@ object Direction {

case class North() extends Direction {
override def toString: String = "N"
def turnRight: Direction = new East
def turnLeft: Direction = new West
}

case class South() extends Direction {
override def toString: String = "S"
def turnRight: Direction = new West
def turnLeft: Direction = new East

}

case class East() extends Direction {
override def toString: String = "E"
def turnRight: Direction = new South
def turnLeft: Direction = new North
}

case class West() extends Direction {
override def toString: String = "W"
def turnRight: Direction = new North
def turnLeft: Direction = new South
}
92 changes: 85 additions & 7 deletions projet/funprog-al/src/main/scala/progfun/mower/Mower.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,94 @@ package fr.esgi.al.funprog
import fr.esgi.al.funprog._
import play.api.libs.json._

@SuppressWarnings(Array("org.wartremover.warts.Var"))
case class Mower(
var startPosition: Position,
var position: Position,
var playedActions: List[Action],
class Mower(
val startPoint: Point,
val startDirection: Direction,
val point: Point,
val direction: Direction,
val playedActions: List[Action],
val originalActions: List[Action]
) {

def toJson: JsValue = Json.obj(
"debut" -> startPosition.toJson,
"debut" -> Json.obj(
"point" -> startPoint.toJson,
"direction" -> startDirection.toString
),
"instructions" -> originalActions.map(a => a.toJson()),
"fin" -> position.toJson
"fin" -> Json.obj(
"point" -> point.toJson,
"direction" -> direction.toString
)
)

def turnRight: Mower = new Mower(
startPoint,
startDirection,
point,
direction.turnRight,
playedActions,
originalActions
)

def turnLeft: Mower = new Mower(
startPoint,
startDirection,
point,
direction.turnLeft,
playedActions,
originalActions
)

def advance(limit: Point, mowers: List[Mower]): Mower = {
val newPoint: Point = this.direction match {
case North() =>
if (isOkMove(this.point.x, this.point.y + 1, point, mowers))
new Point(
this.point.x,
this.point.y + 1
)
else
this.point
case South() =>
if (isOkMove(this.point.x, this.point.y - 1, point, mowers))
new Point(
this.point.x,
this.point.y - 1
)
else
this.point

case East() =>
if (isOkMove(this.point.x + 1, this.point.y, point, mowers))
new Point(
this.point.x + 1,
this.point.y
)
else
this.point
case West() =>
if (isOkMove(this.point.x - 1, this.point.y, point, mowers))
new Point(
this.point.x - 1,
this.point.y
)
else
this.point
}

new Mower(
startPoint,
startDirection,
newPoint,
direction,
playedActions,
originalActions
)
}

def isOkMove(x: Int, y: Int, point: Point, mowers: List[Mower]): Boolean =
x < 0 || x > point.x || y < 0 || y > point.y || mowers
.filter(m => m.point.x == x && m.point.y == y)
.length == 0
}
12 changes: 12 additions & 0 deletions projet/funprog-al/src/main/scala/progfun/mower/Point.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.esgi.al.funprog
import play.api.libs.json._

class Point(
val x: Int,
val y: Int
) {
val toJson: JsValue = Json.obj(
"x" -> x,
"y" -> y
)
}
17 changes: 0 additions & 17 deletions projet/funprog-al/src/main/scala/progfun/mower/Position.scala

This file was deleted.

0 comments on commit c9b046c

Please sign in to comment.