Skip to content
Snippets Groups Projects
Commit 9063cffb authored by reblochor's avatar reblochor
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
*.iml
target/
.idea
\ No newline at end of file
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>consoleApp</artifactId>
<groupId>org.reblochor</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>consoleApp</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.compiler.jvmTarget>11</kotlin.compiler.jvmTarget>
<mockito.version>2.27.0</mockito.version>
</properties>
<repositories>
<repository>
<id>mavenCentral</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>1.4.21</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>MainKt</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>1.4.21</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.nhaarman.mockitokotlin2</groupId>
<artifactId>mockito-kotlin</artifactId>
<version>2.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
import model.CardValue
import model.Deck
import model.Game
fun main(args: Array<String>) {
while (true) {
println(Game)
val command = readLine()
command?: continue
val splitCommand = command.split(" ")
when(splitCommand.size) {
1 -> when(splitCommand[0]) { // Draw
"d" -> Game.draw()
"s" -> Game.store()
}
0 -> continue
2 -> {
val (keyWord, param) = splitCommand
when (keyWord) {
"m" -> Game.place(param.toInt())
"s" -> Game.store(param.toInt())
}
}
3 -> {
val (keyWord, param1, param2) = splitCommand
when (keyWord) {
"m" -> Game.place(param1.toInt(), param2.toInt())
}
}
4 -> {
val (keyword, origin, card, target) = splitCommand
when (keyword) {
"m" -> Game.move(origin.toInt(), CardValue.fromString(card), target.toInt())
}
}
}
}
}
\ No newline at end of file
package model
import kotlin.math.PI
const val ANSI_RESET = "\u001B[0m"
const val ANSI_BLACK = "\u001B[30m"
const val ANSI_RED = "\u001B[31m"
const val ANSI_GREEN = "\u001B[32m"
const val ANSI_YELLOW = "\u001B[33m"
const val ANSI_BLUE = "\u001B[34m"
const val ANSI_PURPLE = "\u001B[35m"
const val ANSI_CYAN = "\u001B[36m"
const val ANSI_WHITE = "\u001B[37m"
enum class CardValue(val value: String) {
ACE("A") {
override fun next() = TWO
override fun previous(): CardValue? = null
},
TWO("2") {
override fun next() = THREE
override fun previous() = ACE
},
THREE("3") {
override fun next() = FOUR
override fun previous() = TWO
},
FOUR("4") {
override fun next() = FIVE
override fun previous() = THREE
},
FIVE("5") {
override fun next() = SIX
override fun previous() = FOUR
},
SIX("6") {
override fun next() = SEVEN
override fun previous() = FIVE
},
SEVEN("7") {
override fun next() = EIGHT
override fun previous() = SIX
},
EIGHT("8") {
override fun next() = NINE
override fun previous() = SEVEN
},
NINE("9") {
override fun next() = TEN
override fun previous() = EIGHT
},
TEN("10") {
override fun next() = JACK
override fun previous() = NINE
},
JACK("J") {
override fun next() = QUEEN
override fun previous() = TEN
},
QUEEN("Q") {
override fun next() = KING
override fun previous() = JACK
},
KING("K") {
override fun next(): CardValue? = null
override fun previous() = QUEEN
};
companion object {
fun valueOf(value: Int) = fromString(value.toString())
fun fromString(value: String) = when(value) {
"2" -> TWO
"3" -> THREE
"4" -> FOUR
"5" -> FIVE
"6" -> SIX
"7" -> SEVEN
"8" -> EIGHT
"9" -> NINE
"10" -> TEN
"J" -> JACK
"Q" -> QUEEN
"K" -> KING
"A" -> ACE
else -> CardValue.valueOf(value)
}
}
override fun toString() = value
abstract fun next(): CardValue?
abstract fun previous(): CardValue?
}
abstract class Card {
abstract var isFaceUp: Boolean
abstract val stringRepresentation: String
abstract var cardValue: CardValue
abstract val colorConsole: String
abstract fun flip()
override fun toString() = stringRepresentation
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Card) return false
if (other.javaClass != this.javaClass)
return false
if (isFaceUp != other.isFaceUp) return false
if (cardValue != other.cardValue) return false
return true
}
override fun hashCode(): Int {
var result = isFaceUp.hashCode()
result = 31 * result + cardValue.hashCode()
return result
}
}
class HeartCard(override var isFaceUp: Boolean, override var cardValue: CardValue) : Card() {
constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value))
constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value))
override val colorConsole: String
get() = ANSI_RED
override val stringRepresentation: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}♥$ANSI_RESET" else " **"
override fun flip() {
isFaceUp = !isFaceUp
}
}
class DiamondCard(override var isFaceUp: Boolean, override var cardValue: CardValue) : Card() {
constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value))
constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value))
override val colorConsole: String
get() = ANSI_RED
override val stringRepresentation: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}♦$ANSI_RESET" else " **"
override fun flip() {
isFaceUp = !isFaceUp
}
}
class SpadeCard(override var isFaceUp: Boolean, override var cardValue: CardValue) : Card() {
constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value))
constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value))
override val colorConsole: String
get() = ANSI_BLUE
override val stringRepresentation: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}♠$ANSI_RESET" else " **"
override fun flip() {
isFaceUp = !isFaceUp
}
}
class ClubCard(override var isFaceUp: Boolean, override var cardValue: CardValue) : Card() {
override val colorConsole: String
get() = ANSI_BLUE
constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value))
constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value))
override val stringRepresentation: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}♣$ANSI_RESET" else " **"
override fun flip() {
isFaceUp = !isFaceUp
}
}
fun test(x: Number) {
when (x) {
PI -> print("OMG it's PI")
15,14,12 -> print("Equals 15, 14 or 12")
in 0 .. 5 -> print("between 0,5")
is Int -> print("int")
is Double -> print("Double")
else -> print("None of that")
}
}
\ No newline at end of file
package model
import java.util.*
import kotlin.Exception
import kotlin.math.min
/**
* Extension method
* Removes the n last elements of the list (or all the elements if there are less than n
*/
fun <T> MutableList<T>.removeNLasts(n: Int): MutableList<T> {
val res = mutableListOf<T>()
for(i in min(n, size) downTo 1) {
res.add(0, get(size - 1))
removeLast()
}
return res
}
open class Deck(val cards: MutableList<Card> = mutableListOf()) {
val top: Card?
get() = if (cards.isNotEmpty()) cards.last() else null
companion object {
@JvmOverloads
fun deckOf52(faceUp: Boolean = false) = Deck(
CardValue.values().flatMap { value ->
setOf(HeartCard::class.java, DiamondCard::class.java, ClubCard::class.java, SpadeCard::class.java).map { cardClass ->
cardClass.getConstructor(Boolean::class.java, CardValue::class.java).newInstance(faceUp, value)
}
}.shuffled().toMutableList()
)
}
fun addBelow(card: Card) {
cards.add(0, card)
}
open fun draw() = top.also {
if (cards.isNotEmpty())
cards.removeLast()
}
// Operator to add a card to the pile
operator fun plusAssign(card: Card) {
if(checkCardCanGoOnPile(card))
cards+=card
}
operator fun plusAssign(cards: List<Card>) {
cards.forEach {
if(checkCardCanGoOnPile(it))
this.cards+=it
else
throw IllegalArgumentException()
}
}
// Operator to add a card to the pile
operator fun plus(card: Card): Deck? {
if(checkCardCanGoOnPile(card))
return Deck(cards.apply { add(card) })
return null
}
operator fun plus(cards: List<Card>): Deck? {
val newDeck = this.javaClass.getConstructor(List::class.java).newInstance(this.cards)
try {
newDeck += cards
return newDeck
} catch (e: Exception) {
return null
}
}
fun shuffle() {
this.cards.shuffle()
}
override fun toString() : String {
val string = top?.toString()
when {
string?.length == 11 -> return " $string"
string == null -> return " __"
else -> return string
}
}
open fun remove(number: Int) = cards.removeNLasts(number)
fun removeAll() = remove(cards.size)
/**
* Method to check that the given card can go on the pile
* @param card
*/
open fun checkCardCanGoOnPile(card: Card) = true
fun flipCards(count: Int) {
for(i in cards.size-count until cards.size )
cards[i].flip()
}
fun flipDeck() {
cards.reverse()
cards.forEach { it.flip() }
}
fun flipCards() {
flipCards(cards.size)
}
}
class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) {
override fun checkCardCanGoOnPile(card: Card): Boolean {
if(cards.isEmpty())
return card.cardValue == CardValue.KING
val validColor = when (cards.last()) {
is HeartCard, is DiamondCard -> card is SpadeCard || card is ClubCard // Red card on top of pile, new card must be black
else -> card is HeartCard || card is DiamondCard // Black card on top of pile, new card must be red
}
if(!validColor)
return false
return card.cardValue.next()?.equals(cards.last().cardValue) ?: false
}
override fun draw() = top.also {
if (cards.isNotEmpty())
cards.removeLast()
else
return@also
if (top?.isFaceUp == false)
top!!.flip()
}
override fun remove(number: Int): MutableList<Card> {
return super.remove(number).also {
if(top?.isFaceUp == false)
top!!.flip()
}
}
}
class StoreDeck: Deck() {
override fun checkCardCanGoOnPile(card: Card): Boolean {
if(cards.isEmpty())
return card.cardValue == CardValue.ACE
val validColor = when (cards.last()) {
is HeartCard, is DiamondCard -> card is HeartCard || card is DiamondCard
else -> card is SpadeCard || card is ClubCard
}
if(!validColor)
return false
return card.cardValue.previous()?.equals(cards.last().cardValue) ?: false
}
}
\ No newline at end of file
package model
object Game {
val stack = Deck.deckOf52(false)
val visibleDeck: Deck = Deck(stack.remove(1)).apply { top!!.flip() }
val heartPile = StoreDeck()
val diamondPile = StoreDeck()
val clubPile = StoreDeck()
val spadePile = StoreDeck()
val gamePiles = buildPiles(7)
override fun toString() =
"""=========================================================================
$stack ${visibleDeck} $heartPile $diamondPile $clubPile $spadePile
${(0 until 7).joinToString(" ")}
${printPiles()}
=======================================================================
""".trimIndent()
private fun printPiles() = StringBuilder().apply {
val maxIterations = gamePiles.map { pile -> pile.cards.size }.maxOrNull()?:0
for(i in 0 until maxIterations) {
for(pile in gamePiles) {
append(" ")
append(pile.cards.map { it.toString() }.getOrElse(i) {" "})
}
append(System.lineSeparator())
}
toString()
}
private fun buildPiles(number: Int): List<Deck> {
val list = mutableListOf<Deck>()
for(i in 1 .. number)
list.add(GamePile(stack.remove(i)).apply { flipCards(1) })
return list
}
fun place(pile: Int) {
visibleDeck.top?:return
try {
gamePiles[pile] += visibleDeck.draw()!!
}
catch (e: Exception) { }
}
fun place(origin: Int, end:Int) {
try {
gamePiles[end] += gamePiles[origin].draw()?: return
} catch (e: Exception) { }
}
fun move(originPile: Int, rootCardValue: CardValue, targetPileIndex: Int) {
val cards = gamePiles[originPile].cards
val targetPile = gamePiles[targetPileIndex]
val rootCardIndex = cards.indexOfFirst { it.cardValue == rootCardValue }
if (rootCardIndex == -1)
return
if(targetPile.checkCardCanGoOnPile(cards[rootCardIndex])) {
targetPile += gamePiles[originPile].remove(cards.size - rootCardIndex)
}
}
fun draw() {
if(stack.top != null)
visibleDeck.plusAssign(stack.draw()?.apply { flip() }?:return)
else
stack += visibleDeck.run {
flipDeck();
removeAll()
}
}
fun store(pile: Int) {
try {
val card = gamePiles[pile].top?:return
when (card) {
is HeartCard -> if (heartPile.checkCardCanGoOnPile(card)) heartPile += gamePiles[pile].draw()!!
is DiamondCard -> if (diamondPile.checkCardCanGoOnPile(card)) diamondPile += gamePiles[pile].draw()!!
is ClubCard -> if (clubPile.checkCardCanGoOnPile(card)) clubPile += gamePiles[pile].draw()!!
is SpadeCard -> if (spadePile.checkCardCanGoOnPile(card)) spadePile += gamePiles[pile].draw()!!
}
} catch (e: Exception) { }
}
fun store() {
try {
when (val card = visibleDeck.top?:return) {
is HeartCard -> if (heartPile.checkCardCanGoOnPile(card)) heartPile += visibleDeck.draw()!!
is DiamondCard -> if (diamondPile.checkCardCanGoOnPile(card)) diamondPile += visibleDeck.draw()!!
is ClubCard -> if (clubPile.checkCardCanGoOnPile(card)) clubPile += visibleDeck.draw()!!
is SpadeCard -> if (spadePile.checkCardCanGoOnPile(card)) spadePile += visibleDeck.draw()!!
}
} catch (e: Exception) { }
}
}
\ No newline at end of file
package model
package ui
import model.Card
import model.DiamondCard
import model.HeartCard
import java.awt.*
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection
import java.awt.datatransfer.Transferable
import java.lang.Exception
import javax.swing.*
import javax.swing.border.Border
import javax.swing.BorderFactory
import javax.swing.JLabel
import javax.swing.JFrame
class CardUi(val card: Card?): JLabel() {
init {
// setBounds(0,0, 40,20)
preferredSize = Dimension(40,20)
setColor()
setContent()
horizontalAlignment = LEFT
background = Color.GREEN
}
override fun getPreferredSize() = Dimension(40,20)
override fun getMaximumSize() = Dimension(40,20)
override fun getMinimumSize() = Dimension(40,20)
private fun setColor() {
foreground = when {
card?.isFaceUp != true -> Color.BLUE
card is HeartCard || card is DiamondCard -> Color.RED
else -> Color.BLACK
}
}
fun setContent() {
text = card?.toString()
}
fun flip() {
card?.flip()
setColor()
setContent()
}
}
class UIMain: JFrame("Solitaire") {
init {
layout = BoxLayout(this, BoxLayout.Y_AXIS)
add(CardUi(HeartCard(true, 2)))
setSize(300, 100)
isVisible = true
}
}
fun main() {
val frame = JFrame()
val card = CardUi(HeartCard(true, 2))
card.background = Color.GREEN
card.border = BorderFactory.createLineBorder(Color.BLACK, 5)
frame.add(card)
frame.setSize(200, 100)
frame.isVisible = true
}
\ No newline at end of file
package model
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
internal class DeckTest {
@Test
fun addBelow() {
val deck = Deck()
val card1: Card = HeartCard(false, 2)
deck.addBelow(card1)
assertThat(deck.cards).hasSize(1).contains(card1)
val card2 = DiamondCard(false, 2)
deck.addBelow(card2)
assertThat(deck.cards).hasSize(2).first().isEqualTo(card2)
assertThat(deck.top).isNotNull.isEqualTo(card1)
}
@Test
fun draw() {
val deck = Deck()
val card = deck.draw()
assertThat(card).isNull()
assertThat(deck.cards).isEmpty()
val card2 = SpadeCard(true, "2")
deck += card2
assertThat(deck.draw()).isNotNull.isEqualTo(card2)
assertThat(deck.cards).hasSize(0)
deck += card2
val card3 = ClubCard(true, 2)
deck.addBelow(card3)
assertThat(deck.draw()).isNotNull.isEqualTo(card2)
assertThat(deck.cards).hasSize(1).contains(card3)
}
@Test
fun testDrawPile() {
val deck = Deck()
val card2 = SpadeCard(false, "K")
deck += card2
val card3 = HeartCard(true, "Q")
deck += card3
val drawn = deck.draw()
assertThat(drawn).isNotNull.isEqualTo(card3)
assertThat(deck.top).isNotNull.isEqualTo(card2)
}
@Test
fun plusAssign() {
val deck = Deck()
val card1: Card = HeartCard(false, 2)
deck += card1
assertThat(deck.cards).hasSize(1).contains(card1)
val card2 = DiamondCard(false, 2)
deck += card2
assertThat(deck.cards).hasSize(2).first().isEqualTo(card1)
assertThat(deck.top).isNotNull.isEqualTo(card2)
}
@Test
fun flipDeck() {
val deck = Deck(mutableListOf(HeartCard(true, 5), HeartCard(true, 6)))
deck.flipDeck()
assertThat(deck.cards).isEqualTo(mutableListOf(HeartCard(false, 6), HeartCard(false, 5)))
}
@Test
fun testPlusAssign() {
val deck = Deck()
val card1: Card = HeartCard(false, 2)
deck += mutableListOf(card1)
assertThat(deck.cards).hasSize(1).contains(card1)
val card2 = DiamondCard(false, 2)
deck += mutableListOf(card2)
assertThat(deck.cards).hasSize(2).first().isEqualTo(card1)
assertThat(deck.top).isNotNull.isEqualTo(card2)
}
@Test
fun plus() {
val deck = Deck()
val card1: Card = HeartCard(false, 2)
val deck1 = deck + mutableListOf(card1)
assertThat(deck1?.cards).isNotNull.hasSize(1).contains(card1)
val card2 = DiamondCard(false, 2)
var deck2 = deck1!! + mutableListOf(card2)
assertThat(deck2?.cards).isNotNull.hasSize(2).first().isEqualTo(card1)
assertThat(deck2!!.top).isNotNull.isEqualTo(card2)
deck2 = deck2 + mutableListOf()
assertThat(deck2?.cards).isNotNull.hasSize(2).first().isEqualTo(card1)
assertThat(deck2!!.top).isNotNull.isEqualTo(card2)
}
@Test
fun testPlus() {
val deck = Deck()
val card1: Card = HeartCard(false, 2)
val deck1 = deck + card1
assertThat(deck1?.cards).isNotNull.hasSize(1).contains(card1)
val card2 = DiamondCard(false, 2)
val deck2 = deck1!! + card2
assertThat(deck2?.cards).isNotNull.hasSize(2).first().isEqualTo(card1)
assertThat(deck2!!.top).isNotNull.isEqualTo(card2)
}
@Test
fun testPlusKO() {
val deck = GamePile(mutableListOf(HeartCard(false, 2)))
val card2 = DiamondCard(false, 2)
val deck2 = deck + card2
assertThat(deck2).isNull()
}
@Test
fun plusKO() {
val card1: Card = HeartCard(false, 2)
val deck = GamePile(mutableListOf(card1))
val card2 = DiamondCard(false, 2)
val deck2 = deck + mutableListOf(card2)
assertThat(deck2).isNull()
val deck3 = deck + mutableListOf()
assertThat(deck3?.cards).isNotNull.hasSize(1).contains(card1)
}
@Test
fun remove() {
val deck = Deck()
var cards = deck.remove(1)
assertThat(cards).isEmpty()
deck += HeartCard(true, "A")
cards = deck.remove(2)
assertThat(cards).hasSize(1)
assertThat(deck.cards).hasSize(0)
}
@Test
fun testCheckCardStore() {
val deck = StoreDeck()
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(ClubCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, "A"))).isTrue
deck += SpadeCard(false, "A")
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, 2))).isTrue
}
@Test
fun testCheckCardPile() {
val deck = GamePile(mutableListOf(HeartCard(false, 2)))
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(ClubCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, "A"))).isTrue
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, "3"))).isFalse
deck.cards.clear()
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, "K"))).isTrue
}
}
\ No newline at end of file
package model
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.notNull
import com.nhaarman.mockitokotlin2.whenever
import org.assertj.core.api.Assertions.anyOf
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Condition
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.BeforeEach
import org.mockito.Mockito
internal class GameTest {
var thirdDeck = Mockito.spy(Deck::class.java)
var secondDeck = GamePile(mutableListOf(HeartCard(false, 5), ClubCard(true, 6)))
@BeforeEach
fun setUp() {
doReturn(true).whenever(thirdDeck).checkCardCanGoOnPile(any())
}
@Test
fun place() {
(Game.gamePiles as MutableList)[2] = thirdDeck
(Game.gamePiles as MutableList)[1] = secondDeck
Game.place(1, 2)
assertThat(Game.gamePiles[1])
.has(Condition({ deck -> deck.top == HeartCard(true, 5)}, "Top value"))
.has(Condition({deck -> deck.cards.size == 1}, "One value"))
assertThat(Game.gamePiles[2].top).isEqualTo(ClubCard(true, 6))
assertThat(Game.gamePiles[2].cards).hasSize(1)
Game.place(1,2)
assertThat(Game.gamePiles[1])
.has(Condition({ deck -> deck.top == null}, "Top value null"))
.has(Condition({deck -> deck.cards.isEmpty()}, "Empty deck"))
assertThat(Game.gamePiles[2].top).isEqualTo(HeartCard(true, 5))
assertThat(Game.gamePiles[2].cards).hasSize(2)
}
@Test
fun draw() {
Game.visibleDeck.removeAll()
Game.visibleDeck += HeartCard(true, 5)
Game.visibleDeck += HeartCard(true, 6)
Game.stack.removeAll()
Game.draw()
assertThat(Game.visibleDeck.cards).isEmpty()
assertThat(Game.stack.cards).isEqualTo(mutableListOf(HeartCard(false, 6), HeartCard(false, 5)))
}
@Test
fun store() {
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment