diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7446506 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.metals/ +.bloop/ +target/ +project/metals.sbt diff --git a/scala/.scalafmt.conf b/scala/.scalafmt.conf new file mode 100644 index 0000000..78155c2 --- /dev/null +++ b/scala/.scalafmt.conf @@ -0,0 +1 @@ +version = "2.0.1" diff --git a/scala/build.sbt b/scala/build.sbt new file mode 100644 index 0000000..70223cb --- /dev/null +++ b/scala/build.sbt @@ -0,0 +1,8 @@ +name := "scala" + +version := "0.1" + +scalaVersion := "2.13.1" + +libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.8" +libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test" diff --git a/scala/project/build.properties b/scala/project/build.properties new file mode 100644 index 0000000..010613d --- /dev/null +++ b/scala/project/build.properties @@ -0,0 +1 @@ +sbt.version = 1.3.3 \ No newline at end of file diff --git a/scala/src/main/scala/day1/TicTacToe.scala b/scala/src/main/scala/day1/TicTacToe.scala new file mode 100644 index 0000000..1c98459 --- /dev/null +++ b/scala/src/main/scala/day1/TicTacToe.scala @@ -0,0 +1,78 @@ +package day1; + +object Move extends Enumeration { + type Move = Value + val X, O, ? = Value +} + +object TicTacToe extends App{ + + println(winner(List( + Move.?, Move.?, Move.?, + Move.?, Move.?, Move.?, + Move.?, Move.?, Move.?, + ))) + + println(winner(List( + Move.X, Move.O, Move.O, + Move.O, Move.X, Move.O, + Move.?, Move.?, Move.O, + ))) + + println(winner(List( + Move.X, Move.X, Move.X, + Move.O, Move.X, Move.O, + Move.?, Move.?, Move.O, + ))) + + println(winner(List( + Move.?, Move.X, Move.X, + Move.O, Move.X, Move.O, + Move.?, Move.?, Move.O + ))) + + +def winner(board: List[Move.Move]): Move.Move = { + val a1 :: a2 :: a3 :: b1 :: b2 :: b3 :: c1 :: c2 :: c3 :: _ = board + + List( + (a1, a2, a3), + (b1, b2, b3), + (c1, c2, c3), + (a1, b1, c1), + (a2, b2, c2), + (a3, b3, c3), + (a1, b2, c3), + (a3, b2, c1), + ).find((rule) => { + rule._1 == rule._2 && rule._1 == rule._3 + }).map(_._1) + .getOrElse(Move.?) + +} + +winner(List( + Move.?, Move.?, Move.?, + Move.?, Move.?, Move.?, + Move.?, Move.?, Move.?, +)) + +winner(List( + Move.X, Move.O, Move.O, + Move.O, Move.X, Move.O, + Move.?, Move.?, Move.O, +)) + +winner(List( + Move.X, Move.X, Move.X, + Move.O, Move.X, Move.O, + Move.?, Move.?, Move.O, +)) + +winner(List( + Move.?, Move.X, Move.X, + Move.O, Move.X, Move.O, + Move.?, Move.?, Move.O +)) + +}