From f41e283af63261b2c463abebb2e46d1704afa956 Mon Sep 17 00:00:00 2001 From: Vincent Lafeychine <vincent.lafeychine@gmail.com> Date: Tue, 1 Feb 2022 19:53:08 +0100 Subject: [PATCH 1/2] Add scalafmt formatter + build --- .scalafmt.conf | 12 ++++++++++++ build.sbt | 5 +++++ project/build.properties | 1 + 3 files changed, 18 insertions(+) create mode 100644 .scalafmt.conf create mode 100644 build.sbt create mode 100644 project/build.properties diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..ccddde2 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,12 @@ +# https://diamantidis.github.io/2020/05/17/ci-with-github-actions-for-scala-project +version = "3.3.0" + +runner.dialect = scala3 +rewrite.scala3.convertToNewSyntax = true + +maxColumn = 120 + +indent.main = 4 +indent.callSite = 4 + +project.git = true diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..3814790 --- /dev/null +++ b/build.sbt @@ -0,0 +1,5 @@ +lazy val root = (project in file(".")) + .settings( + name := "prog-2", + scalaVersion := "3.1.0" + ) diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..9edb75b --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.5.4 -- GitLab From c7be832c6300df4d87ef4945e4227c2f224f00be Mon Sep 17 00:00:00 2001 From: Vincent Lafeychine <vincent.lafeychine@gmail.com> Date: Tue, 1 Feb 2022 19:56:59 +0100 Subject: [PATCH 2/2] Add example + Capacity + Pokemon + Type --- src/main/scala/Main.scala | 11 +++++++++++ src/main/scala/Pokemon/Capacity.scala | 1 + src/main/scala/Pokemon/CapacityFactory.scala | 6 ++++++ src/main/scala/Pokemon/Pokemon.scala | 10 ++++++++++ src/main/scala/Pokemon/PokemonFactory.scala | 10 ++++++++++ src/main/scala/Pokemon/Type.scala | 7 +++++++ 6 files changed, 45 insertions(+) create mode 100644 src/main/scala/Main.scala create mode 100644 src/main/scala/Pokemon/Capacity.scala create mode 100644 src/main/scala/Pokemon/CapacityFactory.scala create mode 100644 src/main/scala/Pokemon/Pokemon.scala create mode 100644 src/main/scala/Pokemon/PokemonFactory.scala create mode 100644 src/main/scala/Pokemon/Type.scala diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala new file mode 100644 index 0000000..1c393e2 --- /dev/null +++ b/src/main/scala/Main.scala @@ -0,0 +1,11 @@ +@main def main() = + val pkmn = PokemonFactory("Bulbasaur") + val pkmn2 = PokemonFactory("Charmander") + + println(pkmn) + println(pkmn2) + + pkmn.attack(CapacityFactory("Vine Whip"), pkmn2) + + println(pkmn) + println(pkmn2) diff --git a/src/main/scala/Pokemon/Capacity.scala b/src/main/scala/Pokemon/Capacity.scala new file mode 100644 index 0000000..ae60435 --- /dev/null +++ b/src/main/scala/Pokemon/Capacity.scala @@ -0,0 +1 @@ +class Capacity(name: String, val ctype: Type) diff --git a/src/main/scala/Pokemon/CapacityFactory.scala b/src/main/scala/Pokemon/CapacityFactory.scala new file mode 100644 index 0000000..08d04df --- /dev/null +++ b/src/main/scala/Pokemon/CapacityFactory.scala @@ -0,0 +1,6 @@ +object CapacityFactory: + + private case class VineWhip() extends Capacity("Vine Whip", Grass) + + def apply(name: String): Capacity = name match + case "Vine Whip" => VineWhip() diff --git a/src/main/scala/Pokemon/Pokemon.scala b/src/main/scala/Pokemon/Pokemon.scala new file mode 100644 index 0000000..b16a05f --- /dev/null +++ b/src/main/scala/Pokemon/Pokemon.scala @@ -0,0 +1,10 @@ +trait Pokemon(var name: String, var life: Int, val ptype: Type): + + def attack(capacity: Capacity, other: Pokemon) = + val multiplicator = capacity.ctype.computeMultiplier(other.ptype) + + other.decreaseLife(Math.round(multiplicator.floatValue)) + + def decreaseLife(x: Int) = this.life = this.life - x + + override def toString(): String = s"[${this.name}] Life: ${this.life}" diff --git a/src/main/scala/Pokemon/PokemonFactory.scala b/src/main/scala/Pokemon/PokemonFactory.scala new file mode 100644 index 0000000..e0f9220 --- /dev/null +++ b/src/main/scala/Pokemon/PokemonFactory.scala @@ -0,0 +1,10 @@ +object PokemonFactory: + + private case class Bulbasaur() extends Pokemon("Bulbasaur", 25, Grass) + private case class Charmander() extends Pokemon("Charmander", 30, Fire) + private case class Squirtle() extends Pokemon("Squirtle", 25, Water) + + def apply(name: String): Pokemon = name match + case "Bulbasaur" => Bulbasaur() + case "Charmander" => Charmander() + case "Squirtle" => Squirtle() diff --git a/src/main/scala/Pokemon/Type.scala b/src/main/scala/Pokemon/Type.scala new file mode 100644 index 0000000..b75a3cd --- /dev/null +++ b/src/main/scala/Pokemon/Type.scala @@ -0,0 +1,7 @@ +sealed trait Type(val strength: List[Type], val weakness: List[Type]): + def computeMultiplier(ptype: Type) = + if strength.contains(ptype) then 2 else if weakness.contains(ptype) then 0.5 else 1 + +case object Grass extends Type(List(Water), List(Fire)) +case object Water extends Type(List(Fire), List(Grass, Water)) +case object Fire extends Type(List(Grass), List(Water)) -- GitLab