diff --git a/.idea/libraries/sbt__org_scala_lang_scala_library_2_13_15_jar.xml b/.idea/libraries/sbt__org_scala_lang_scala_library_2_13_15_jar.xml deleted file mode 100644 index a3b6460..0000000 --- a/.idea/libraries/sbt__org_scala_lang_scala_library_2_13_15_jar.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/sbt__scala_sdk_2_13_15.xml b/.idea/libraries/sbt__scala_sdk_2_13_15.xml deleted file mode 100644 index 507e2df..0000000 --- a/.idea/libraries/sbt__scala_sdk_2_13_15.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - file://$USER_HOME$/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala2-sbt-bridge/2.13.15/scala2-sbt-bridge-2.13.15.jar - - - - - - \ No newline at end of file diff --git a/src/main/scala/Worksheet.sc b/src/main/scala/Worksheet.sc index 4f3da0a..68abf58 100644 --- a/src/main/scala/Worksheet.sc +++ b/src/main/scala/Worksheet.sc @@ -1,3 +1,5 @@ +import de.knockoutwhist.cards.CardManager + val eol = sys.props("line.separator") // String = def bar(cellwidth: Int = 3, cellNum: Int = 3) = (("+" + "-" * cellwidth)*cellNum) + "+" + eol def cells(cellwidth: Int = 3, cellNum: Int = 3) = ("|" + " " * cellwidth) *cellNum + "|" + eol @@ -5,4 +7,6 @@ def mesh(cellwidth: Int = 3, cellNum: Int = 3) = (bar(cellwidth, cellNum) + cell bar(8,6) cells() -mesh() \ No newline at end of file +mesh() + +CardManager.cardContainer \ No newline at end of file diff --git a/src/main/scala/KnockOutWhist.scala b/src/main/scala/de/knockoutwhist/KnockOutWhist.scala similarity index 68% rename from src/main/scala/KnockOutWhist.scala rename to src/main/scala/de/knockoutwhist/KnockOutWhist.scala index cc0cf37..24e2391 100644 --- a/src/main/scala/KnockOutWhist.scala +++ b/src/main/scala/de/knockoutwhist/KnockOutWhist.scala @@ -1,30 +1,34 @@ +package de.knockoutwhist -object Main { +import de.knockoutwhist.cards.CardManager + + +object KnockOutWhist { class KnockOutWhist { } - val eol = sys.props("line.separator") + val eol: String = sys.props("line.separator") - def bar(cellWidth: Int = 9, cellNum: Int = 1) = + def bar(cellWidth: Int = 9, cellNum: Int = 1): String = ("-" * cellWidth) * cellNum - def cells(cellWidth: Int = 7, cellNum: Int = 1) = + def cells(cellWidth: Int = 7, cellNum: Int = 1): String = ("|" + " " * cellWidth) * cellNum + "|" - def meshk = + def meshk: String = bar() + cells() + eol + "|Karten | " + eol + (cells()+ eol) * 2 + bar() + eol - def mesha = + def mesha: String = bar() + cells() + eol + "|Ablage | " + eol + (cells()+ eol) * 2 + bar() + eol - def mesht1 = + private def mesht1 = bar() + eol + cells() + eol + "|Trumpfe| " + eol + "|s1 |\n" + cells() + eol + bar() + eol - def mesht2 = + private def mesht2 = bar() + eol + cells() + eol + "|Trumpfe|" + eol + "|s2 |\n" + cells() + eol + bar() + eol - def meshTable = { + private def meshTable = { bar() + "\t" + bar() + eol + cells() + "\t" + cells() + eol + "|Karten |" + "\t" + "|Ablage |" + eol + (cells() + "\t" + cells() + eol) * 2 + bar() + "\t" + bar() + eol @@ -36,5 +40,7 @@ object Main { print(meshTable) print(mesht1) print(mesht2) + + } } \ No newline at end of file diff --git a/src/main/scala/de/knockoutwhist/cards/Card.scala b/src/main/scala/de/knockoutwhist/cards/Card.scala index 362f601..64de6e1 100644 --- a/src/main/scala/de/knockoutwhist/cards/Card.scala +++ b/src/main/scala/de/knockoutwhist/cards/Card.scala @@ -1,31 +1,29 @@ package de.knockoutwhist.cards -enum Suit: - case Spades - case Hearts - case Diamonds - case Clubs - +enum Suit(identifier: String): + case Spades extends Suit("♠") + case Hearts extends Suit("♥") + case Diamonds extends Suit("♦") + case Clubs extends Suit("♣") end Suit -enum CardValue: - case Two - case Three - case Four - case Five - case Six - case Seven - case Eight - case Nine - case Ten - case Jack - case Queen - case King - case Ace - +enum CardValue(identifier: String): + case Two extends CardValue("2") + case Three extends CardValue("3") + case Four extends CardValue("4") + case Five extends CardValue("5") + case Six extends CardValue("6") + case Seven extends CardValue("7") + case Eight extends CardValue("8") + case Nine extends CardValue("9") + case Ten extends CardValue("10") + case Jack extends CardValue("J") + case Queen extends CardValue("Q") + case King extends CardValue("K") + case Ace extends CardValue("A") end CardValue case class Card(cardValue: CardValue, suit: Suit) { - override def toString: String = s"$cardValue of $suit" + } diff --git a/src/main/scala/de/knockoutwhist/cards/CardManager.scala b/src/main/scala/de/knockoutwhist/cards/CardManager.scala new file mode 100644 index 0000000..c1b719b --- /dev/null +++ b/src/main/scala/de/knockoutwhist/cards/CardManager.scala @@ -0,0 +1,34 @@ +package de.knockoutwhist.cards + +import scala.util.Random + +object CardManager { + + + val cardContainer: Array[Card] = { + val cc = new Array[Card](52) + var i = 0 + for (suit <- Suit.values) { + for (cardValue <- CardValue.values) { + cc(i) = Card(cardValue, suit) + i+=1 + } + } + cc + } + private var currentIdx = 0 + + def shuffleAndReset(): Unit = { + Random.shuffle(cardContainer) + currentIdx = 0 + } + + def nextCard(): Card = { + val card = cardContainer(currentIdx) + currentIdx += 1 + card + } + + + +} diff --git a/src/test/scala/de/knockoutwhist/cards/CardTests.scala b/src/test/scala/de/knockoutwhist/cards/CardTests.scala new file mode 100644 index 0000000..ca6ea20 --- /dev/null +++ b/src/test/scala/de/knockoutwhist/cards/CardTests.scala @@ -0,0 +1,77 @@ +package de.knockoutwhist.cards + +import org.scalatest.matchers.must.Matchers +import org.scalatest.wordspec.AnyWordSpec + +class CardTests extends AnyWordSpec with Matchers{ + + "Knock Out Whist" should { + "The card container can't be empty" in { + CardManager.cardContainer must not be empty + } + "A deck should have 52 cards" in { + CardManager.cardContainer must have size 52 + } + "A deck should have 13 cards of each suit" in { + "13 cards of Spades" in { + CardManager.cardContainer.count(_.suit == Suit.Spades) mustBe 13 + } + "13 cards of Hearts" in { + CardManager.cardContainer.count(_.suit == Suit.Hearts) mustBe 13 + } + "13 cards of Diamonds" in { + CardManager.cardContainer.count(_.suit == Suit.Diamonds) mustBe 13 + } + "13 cards of Clubs" in { + CardManager.cardContainer.count(_.suit == Suit.Clubs) mustBe 13 + } + } + "A deck should have 4 cards of each value" in { + "4 cards of Two" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Two) mustBe 4 + } + "4 cards of Three" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Three) mustBe 4 + } + "4 cards of Four" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Four) mustBe 4 + } + "4 cards of Five" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Five) mustBe 4 + } + "4 cards of Six" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Six) mustBe 4 + } + "4 cards of Seven" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Seven) mustBe 4 + } + "4 cards of Eight" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Eight) mustBe 4 + } + "4 cards of Nine" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Nine) mustBe 4 + } + "4 cards of Ten" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Ten) mustBe 4 + } + "4 cards of Jack" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Jack) mustBe 4 + } + "4 cards of Queen" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Queen) mustBe 4 + } + "4 cards of King" in { + CardManager.cardContainer.count(_.cardValue == CardValue.King) mustBe 4 + } + "4 cards of Ace" in { + CardManager.cardContainer.count(_.cardValue == CardValue.Ace) mustBe 4 + } + } + "Check if shuffleAndReset shuffles the deck" in { + val originalDeck = CardManager.cardContainer.clone() + CardManager.shuffleAndReset() + CardManager.cardContainer must not equal originalDeck + } + } + +}