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

Final version

parent 9063cffb
No related branches found
No related tags found
No related merge requests found
// TODO: Extension de la classe String: createPseudo
fun String.createPseudo(): String {
return String(this.toCharArray().apply { shuffle() })
}
\ No newline at end of file
import model.CardValue import model.CardValue
import model.Deck
import model.Game import model.Game
import model.Score
import java.util.*
fun main(args: Array<String>) { fun main(args: Array<String>) {
// Get user identity
var identity: String?
do {
println("Bonjour, qui es-tu?")
identity = readLine()
} while (identity == null)
// Create pseudo from identity
var pseudo = ""
/* TODO: Uncomment for question extension */
pseudo = identity.createPseudo()
println("Bonjour à toi $pseudo, jouons au solitaire")
// TODO: Uncomment for java -> kotlin
val score = Score(pseudo, 0, Calendar.getInstance().time)
// Game loop
while (true) { while (true) {
// Display game
println(Game) println(Game)
// Get user command
val command = readLine() val command = readLine()
// If command is null, loop
command?: continue command?: continue
// Split command with keyword and arguments
val splitCommand = command.split(" ") val splitCommand = command.split(" ")
// Case of all the different command lengths
when(splitCommand.size) { when(splitCommand.size) {
1 -> when(splitCommand[0]) { // Draw 0 -> continue // Empty command, loop
"d" -> Game.draw() 1 -> when(splitCommand[0]) {
"s" -> Game.store() "d" -> Game.draw() // Draw new card
"s" -> Game.store() // Store card on top
} }
0 -> continue
2 -> { 2 -> {
// Get keyword and differentiate cases
val (keyWord, param) = splitCommand val (keyWord, param) = splitCommand
when (keyWord) { when (keyWord) {
"m" -> Game.place(param.toInt()) "m" -> Game.place(param.toInt()) // Place visible pile top card to a game pile
"s" -> Game.store(param.toInt()) "s" -> Game.store(param.toInt()) // Place game pile top card on store
} }
} }
3 -> { 3 -> {
...@@ -34,7 +64,8 @@ fun main(args: Array<String>) { ...@@ -34,7 +64,8 @@ fun main(args: Array<String>) {
} }
} }
} }
// TODO uncomment java -> kotlin traduction
score.commandsUsed += 1
println("Player: ${score.playerName} Turns:${score.commandsUsed} StartTime:${score.startTime}")
} }
} }
\ No newline at end of file
package model package model
import kotlin.math.PI import javax.swing.plaf.ColorUIResource
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"
const val COLOR_RESET = "\u001B[0m"
const val COLOR_BLACK = "\u001B[30m"
const val COLOR_RED = "\u001B[31m"
const val COLOR_GREEN = "\u001B[32m"
const val COLOR_YELLOW = "\u001B[33m"
const val COLOR_BLUE = "\u001B[34m"
const val COLOR_PURPLE = "\u001B[35m"
const val COLOR_CYAN = "\u001B[36m"
const val COLOR_WHITE = "\u001B[37m"
const val heartSymbol = "♥"
// TODO: Heritage
class HeartCard(isFaceUp: Boolean, cardValue: CardValue) : Card(isFaceUp, cardValue) {
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() = COLOR_RED
override val symbol: String
get() = heartSymbol
}
/**
* Abstract class representing a Card, a card has a value, and a state (faceUp or faceDown).
* The type of the card (giving the colorConsole and symbol) properties are implemented by subclasses (like `HeartCard`)
*/
abstract class Card(var isFaceUp: Boolean, var cardValue: CardValue) {
/**
* Get the string representation of the card.
* Example: 10♥ if the card is face up or ** if the card is face down
* The complicated expression is to add color and some spacing to align 10♥ with 2♥ (make them the same length)
*/
private val stringRepresentation: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}$symbol$COLOR_RESET" else " **"
/**
* Abstract property representing the color the card will be printed in the console (constants defined in top of screen)
*/
abstract val colorConsole: String
/**
* Abstract property string, the symbol of the card (heart, diamond, club, spade)
*/
abstract val symbol: String
/**
* Flips the card
*/
fun flip() {
isFaceUp = !isFaceUp
}
/**
* Those are just to string, equals and hashCode method, it isn't really worth documenting
*/
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
}
}
/**
* Enumeration corresponding to card values from to two Ace
*/
enum class CardValue(val value: String) { enum class CardValue(val value: String) {
ACE("A") { ACE("A") {
override fun next() = TWO override fun next() = TWO
...@@ -82,7 +154,7 @@ enum class CardValue(val value: String) { ...@@ -82,7 +154,7 @@ enum class CardValue(val value: String) {
"Q" -> QUEEN "Q" -> QUEEN
"K" -> KING "K" -> KING
"A" -> ACE "A" -> ACE
else -> CardValue.valueOf(value) else -> valueOf(value)
} }
} }
...@@ -92,107 +164,38 @@ enum class CardValue(val value: String) { ...@@ -92,107 +164,38 @@ enum class CardValue(val value: String) {
} }
abstract class Card { class DiamondCard(isFaceUp: Boolean, cardValue: CardValue) : Card(isFaceUp, cardValue) {
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: Int) : this(isFaceUp, CardValue.valueOf(value))
constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value)) constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value))
override val colorConsole: String override val colorConsole: String
get() = ANSI_RED get() = COLOR_RED
override val stringRepresentation: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}♦$ANSI_RESET" else " **"
override val symbol: String
override fun flip() { get() = "♦"
isFaceUp = !isFaceUp
}
} }
class SpadeCard(override var isFaceUp: Boolean, override var cardValue: CardValue) : Card() { class SpadeCard(isFaceUp: Boolean, cardValue: CardValue) : Card(isFaceUp, cardValue) {
constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value)) constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value))
constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value)) constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value))
override val colorConsole: String override val colorConsole: String
get() = ANSI_BLUE get() = COLOR_BLUE
override val stringRepresentation: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}♠$ANSI_RESET" else " **"
override fun flip() { override val symbol: String
isFaceUp = !isFaceUp get() = "♠"
}
} }
class ClubCard(override var isFaceUp: Boolean, override var cardValue: CardValue) : Card() { class ClubCard(isFaceUp: Boolean, cardValue: CardValue) : Card(isFaceUp, cardValue) {
override val colorConsole: String override val colorConsole: String
get() = ANSI_BLUE get() = COLOR_BLUE
constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value)) constructor(isFaceUp: Boolean, value: Int) : this(isFaceUp, CardValue.valueOf(value))
constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value)) constructor(isFaceUp: Boolean, value: String): this(isFaceUp, CardValue.fromString(value))
override val stringRepresentation: String override val symbol: String
get() = if (isFaceUp) "$colorConsole${if(cardValue != CardValue.TEN) " " else ""}${cardValue}♣$ANSI_RESET" else " **" get() = "♣"
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 package model
import java.util.* import java.util.*
import kotlin.Exception
import kotlin.math.min import kotlin.math.min
/** /**
* Extension method * Extension method
* Removes the n last elements of the list (or all the elements if there are less than n * 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> { fun <T> MutableList<T>.removeNLasts(n: Int): MutableList<T> {
val res = mutableListOf<T>() val res = mutableListOf<T>()
...@@ -17,12 +17,19 @@ fun <T> MutableList<T>.removeNLasts(n: Int): MutableList<T> { ...@@ -17,12 +17,19 @@ fun <T> MutableList<T>.removeNLasts(n: Int): MutableList<T> {
return res return res
} }
/**
* Class representing a deck, it contains only a list of cards and several methods to manipulate that list
*/
open class Deck(val cards: MutableList<Card> = mutableListOf()) { open class Deck(val cards: MutableList<Card> = mutableListOf()) {
val top: Card? val top: Card?
get() = if (cards.isNotEmpty()) cards.last() else null get() = if (cards.isNotEmpty()) cards.last() else null
companion object { companion object {
/**
* Static method used to create a shuffled Deck of 52 cards.
* @param faceUp Optional param used to determine if the cards in the deck are visible (faceUp) or hidden (!faceUp)
*/
@JvmOverloads @JvmOverloads
fun deckOf52(faceUp: Boolean = false) = Deck( fun deckOf52(faceUp: Boolean = false) = Deck(
CardValue.values().flatMap { value -> CardValue.values().flatMap { value ->
...@@ -33,88 +40,142 @@ open class Deck(val cards: MutableList<Card> = mutableListOf()) { ...@@ -33,88 +40,142 @@ open class Deck(val cards: MutableList<Card> = mutableListOf()) {
) )
} }
/**
* Adds a card under the deck
*/
fun addBelow(card: Card) { fun addBelow(card: Card) {
cards.add(0, card) cards.add(0, card)
} }
/**
* Draws the top card from the deck.
* This method returns the top card of the deck and also removes it from the deck
*/
open fun draw() = top.also { open fun draw() = top.also {
if (cards.isNotEmpty()) if (cards.isNotEmpty())
cards.removeLast() cards.removeLast()
} }
// Operator to add a card to the pile /**
* Operator +=
* Adds a card on top of the deck if the card can go on it
*/
operator fun plusAssign(card: Card) { operator fun plusAssign(card: Card) {
if(checkCardCanGoOnPile(card)) if(pileCompatibilityCheck(card))
cards+=card cards+=card
} }
/**
* Operator +=
* Adds a list of cards on top of the deck.
* Iterates through all of the cards of the list and adds them one by one, stops if a card is misplaced
*/
operator fun plusAssign(cards: List<Card>) { operator fun plusAssign(cards: List<Card>) {
cards.forEach { cards.forEach {
if(checkCardCanGoOnPile(it)) if(pileCompatibilityCheck(it))
this.cards+=it this.cards+=it
else else
throw IllegalArgumentException() return
} }
} }
// Operator to add a card to the pile /**
operator fun plus(card: Card): Deck? { * Operator +
if(checkCardCanGoOnPile(card)) * Convenience function doing the same as `addSingleCardAndReturnNewDeck`
return Deck(cards.apply { add(card) }) * TODO: Operator overload
return null */
operator fun plus(card: Card) = addSingleCardAndReturnNewDeck(card)
/**
* Creates a new Deck of cards being equal to this deck with the given card on top
* TODO: If expression
*/
fun addSingleCardAndReturnNewDeck(card: Card) = if(pileCompatibilityCheck(card)) {
Deck(cards.apply { add(card) })
}
else {
null
} }
/**
* Operator +
* Creates a new Deck of cards being equal to this deck with the given cards on top
*/
operator fun plus(cards: List<Card>): Deck? { operator fun plus(cards: List<Card>): Deck? {
val newDeck = this.javaClass.getConstructor(List::class.java).newInstance(this.cards) val newDeck = this.javaClass.getConstructor(List::class.java).newInstance(this.cards)
try { newDeck += cards
newDeck += cards return newDeck
return newDeck
} catch (e: Exception) {
return null
}
} }
/**
* Shuffles the deck
*/
fun shuffle() { fun shuffle() {
this.cards.shuffle() this.cards.shuffle()
} }
/**
* Returns the deck as a string, which is:
* The top card if there is one or __ if no card is visible
*/
override fun toString() : String { override fun toString() : String {
val string = top?.toString() val string = top?.toString()
when { return when {
string?.length == 11 -> return " $string" string == null -> " __"
string == null -> return " __" else -> string
else -> return string
} }
} }
/**
* Removes the number last cards of the deck and returns them
*/
open fun remove(number: Int) = cards.removeNLasts(number) open fun remove(number: Int) = cards.removeNLasts(number)
/**
* Removes all the cards from the deck
*/
fun removeAll() = remove(cards.size) fun removeAll() = remove(cards.size)
/** /**
* Method to check that the given card can go on the pile * Method to check that the given card can go on the deck
* @param card * @param card
*/ */
open fun checkCardCanGoOnPile(card: Card) = true open fun pileCompatibilityCheck(card: Card) = true
/**
* Flips all the N last cards (preserves cards order)
*/
fun flipCards(count: Int) { fun flipCards(count: Int) {
for(i in cards.size-count until cards.size ) for(i in cards.size-count until cards.size )
cards[i].flip() cards[i].flip()
} }
/**
* Flips the whole deck, flipping each cards and reversing the cards order
*/
fun flipDeck() { fun flipDeck() {
cards.reverse() cards.reverse()
cards.forEach { it.flip() } cards.forEach { it.flip() }
} }
/**
* Flips all the cards in the deck (preserves cards order)
*/
fun flipCards() { fun flipCards() {
flipCards(cards.size) flipCards(cards.size)
} }
} }
/**
* SubClass of `Deck` corresponding to the piles of the Tableau (the seven fanned piles where the player can put incoming cards)
*/
class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) { class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) {
override fun checkCardCanGoOnPile(card: Card): Boolean { /**
* Overrides the default card check (always set to true)
* A card can go on the pile if it is of a different color than the top one and is exactly one value down
*/
override fun pileCompatibilityCheck(card: Card): Boolean {
// If the pile is empty, then only a king can be put on it
if(cards.isEmpty()) if(cards.isEmpty())
return card.cardValue == CardValue.KING return card.cardValue == CardValue.KING
...@@ -126,9 +187,13 @@ class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) { ...@@ -126,9 +187,13 @@ class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) {
if(!validColor) if(!validColor)
return false return false
return card.cardValue.next()?.equals(cards.last().cardValue) ?: false // The next value of the incoming card is the value of the top card
return card.cardValue.next()?.equals(top?.cardValue) ?: false
} }
/**
* Drawing a card from the pile returns it, removes it from the pile and flips the top card if it is face down
*/
override fun draw() = top.also { override fun draw() = top.also {
if (cards.isNotEmpty()) if (cards.isNotEmpty())
cards.removeLast() cards.removeLast()
...@@ -138,6 +203,9 @@ class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) { ...@@ -138,6 +203,9 @@ class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) {
top!!.flip() top!!.flip()
} }
/**
* Removes the top N cards from the pile and flips the new top card if it is
*/
override fun remove(number: Int): MutableList<Card> { override fun remove(number: Int): MutableList<Card> {
return super.remove(number).also { return super.remove(number).also {
if(top?.isFaceUp == false) if(top?.isFaceUp == false)
...@@ -147,8 +215,12 @@ class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) { ...@@ -147,8 +215,12 @@ class GamePile(cards: MutableList<Card> = mutableListOf()): Deck(cards) {
} }
class StoreDeck: Deck() { /**
override fun checkCardCanGoOnPile(card: Card): Boolean { * SubClass of Deck corresponding to a store deck (the four decks on the top right of the solitaire game)
*/
class StoreDeck<T: Card>: Deck() {
override fun pileCompatibilityCheck(card: Card): Boolean {
if(cards.isEmpty()) if(cards.isEmpty())
return card.cardValue == CardValue.ACE return card.cardValue == CardValue.ACE
...@@ -162,4 +234,6 @@ class StoreDeck: Deck() { ...@@ -162,4 +234,6 @@ class StoreDeck: Deck() {
return card.cardValue.previous()?.equals(cards.last().cardValue) ?: false return card.cardValue.previous()?.equals(cards.last().cardValue) ?: false
} }
fun checkCardCanGoOnPile(card: T): Boolean = pileCompatibilityCheck(card)
} }
\ No newline at end of file
...@@ -4,11 +4,10 @@ object Game { ...@@ -4,11 +4,10 @@ object Game {
val stack = Deck.deckOf52(false) val stack = Deck.deckOf52(false)
val visibleDeck: Deck = Deck(stack.remove(1)).apply { top!!.flip() } val visibleDeck: Deck = Deck(stack.remove(1)).apply { top!!.flip() }
val heartPile = StoreDeck() val heartPile = StoreDeck<HeartCard>()
val diamondPile = StoreDeck() val diamondPile = StoreDeck<DiamondCard>()
val clubPile = StoreDeck() val clubPile = StoreDeck<ClubCard>()
val spadePile = StoreDeck() val spadePile = StoreDeck<SpadeCard>()
val gamePiles = buildPiles(7) val gamePiles = buildPiles(7)
override fun toString() = override fun toString() =
...@@ -41,26 +40,24 @@ ${printPiles()} ...@@ -41,26 +40,24 @@ ${printPiles()}
} }
fun place(pile: Int) { fun place(pile: Int) {
visibleDeck.top?:return val card = visibleDeck.top?:return
try { if(gamePiles[pile].pileCompatibilityCheck(card))
gamePiles[pile] += visibleDeck.draw()!! gamePiles[pile] += visibleDeck.draw()?: return
}
catch (e: Exception) { }
} }
fun place(origin: Int, end:Int) { fun place(origin: Int, end:Int) {
try { val card = gamePiles[origin].top?: return
if(gamePiles[end].pileCompatibilityCheck(card))
gamePiles[end] += gamePiles[origin].draw()?: return gamePiles[end] += gamePiles[origin].draw()?: return
} catch (e: Exception) { }
} }
fun move(originPile: Int, rootCardValue: CardValue, targetPileIndex: Int) { fun move(originPile: Int, rootCardValue: CardValue, targetPileIndex: Int) {
val cards = gamePiles[originPile].cards val cards = gamePiles[originPile].cards
val targetPile = gamePiles[targetPileIndex] val targetPile = gamePiles[targetPileIndex]
val rootCardIndex = cards.indexOfFirst { it.cardValue == rootCardValue } val rootCardIndex = cards.filter { it.isFaceUp }.indexOfFirst { it.cardValue == rootCardValue }
if (rootCardIndex == -1) if (rootCardIndex == -1)
return return
if(targetPile.checkCardCanGoOnPile(cards[rootCardIndex])) { if(targetPile.pileCompatibilityCheck(cards[rootCardIndex])) {
targetPile += gamePiles[originPile].remove(cards.size - rootCardIndex) targetPile += gamePiles[originPile].remove(cards.size - rootCardIndex)
} }
} }
...@@ -76,25 +73,27 @@ ${printPiles()} ...@@ -76,25 +73,27 @@ ${printPiles()}
} }
fun store(pile: Int) { fun store(pile: Int) {
try { val card = gamePiles[pile].top?:return
val card = gamePiles[pile].top?:return when (card) {
when (card) { is HeartCard -> if (heartPile.checkCardCanGoOnPile(card)) heartPile += gamePiles[pile].draw()!!
is HeartCard -> if (heartPile.checkCardCanGoOnPile(card)) heartPile += gamePiles[pile].draw()!! is DiamondCard -> if (diamondPile.checkCardCanGoOnPile(card)) diamondPile += gamePiles[pile].draw()!!
is DiamondCard -> if (diamondPile.checkCardCanGoOnPile(card)) diamondPile += gamePiles[pile].draw()!! is ClubCard -> if (clubPile.checkCardCanGoOnPile(card)) clubPile += gamePiles[pile].draw()!!
is ClubCard -> if (clubPile.checkCardCanGoOnPile(card)) clubPile += gamePiles[pile].draw()!! is SpadeCard -> if (spadePile.checkCardCanGoOnPile(card)) spadePile += gamePiles[pile].draw()!!
is SpadeCard -> if (spadePile.checkCardCanGoOnPile(card)) spadePile += gamePiles[pile].draw()!! }
}
} catch (e: Exception) { }
} }
/**
* Puts the visible deck card on one of the store piles
* TODO: NullSafety
* TODO: SmartCast => To show that smartcast works, make them specify explicitely the type of card (card: Card) and try to do `is DiamondCard -> heartPile.checkCardCanGoOnPile(card) and see that it does not work
*/
fun store() { fun store() {
try { val card = visibleDeck.top?:return
when (val card = visibleDeck.top?:return) { when (card) {
is HeartCard -> if (heartPile.checkCardCanGoOnPile(card)) heartPile += visibleDeck.draw()!! is HeartCard -> if (heartPile.checkCardCanGoOnPile(card)) heartPile += visibleDeck.draw()!!
is DiamondCard -> if (diamondPile.checkCardCanGoOnPile(card)) diamondPile += visibleDeck.draw()!! is DiamondCard -> if (diamondPile.checkCardCanGoOnPile(card)) diamondPile += visibleDeck.draw()!!
is ClubCard -> if (clubPile.checkCardCanGoOnPile(card)) clubPile += visibleDeck.draw()!! is ClubCard -> if (clubPile.checkCardCanGoOnPile(card)) clubPile += visibleDeck.draw()!!
is SpadeCard -> if (spadePile.checkCardCanGoOnPile(card)) spadePile += visibleDeck.draw()!! is SpadeCard -> if (spadePile.checkCardCanGoOnPile(card)) spadePile += visibleDeck.draw()!!
} }
} catch (e: Exception) { }
} }
} }
\ No newline at end of file
package model
import java.util.*
// TODO: Java traduction
class Score(val playerName: String, var commandsUsed: Int, val startTime: Date)
\ 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
...@@ -151,7 +151,7 @@ internal class DeckTest { ...@@ -151,7 +151,7 @@ internal class DeckTest {
@Test @Test
fun testCheckCardStore() { fun testCheckCardStore() {
val deck = StoreDeck() val deck = StoreDeck<Card>()
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(ClubCard(false, 2))).isFalse assertThat(deck.checkCardCanGoOnPile(ClubCard(false, 2))).isFalse
...@@ -167,14 +167,14 @@ internal class DeckTest { ...@@ -167,14 +167,14 @@ internal class DeckTest {
fun testCheckCardPile() { fun testCheckCardPile() {
val deck = GamePile(mutableListOf(HeartCard(false, 2))) val deck = GamePile(mutableListOf(HeartCard(false, 2)))
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse assertThat(deck.pileCompatibilityCheck(DiamondCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(ClubCard(false, 2))).isFalse assertThat(deck.pileCompatibilityCheck(ClubCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, 2))).isFalse assertThat(deck.pileCompatibilityCheck(SpadeCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, "A"))).isTrue assertThat(deck.pileCompatibilityCheck(SpadeCard(false, "A"))).isTrue
assertThat(deck.checkCardCanGoOnPile(SpadeCard(false, "3"))).isFalse assertThat(deck.pileCompatibilityCheck(SpadeCard(false, "3"))).isFalse
deck.cards.clear() deck.cards.clear()
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, 2))).isFalse assertThat(deck.pileCompatibilityCheck(DiamondCard(false, 2))).isFalse
assertThat(deck.checkCardCanGoOnPile(DiamondCard(false, "K"))).isTrue assertThat(deck.pileCompatibilityCheck(DiamondCard(false, "K"))).isTrue
} }
} }
\ No newline at end of file
...@@ -2,9 +2,7 @@ package model ...@@ -2,9 +2,7 @@ package model
import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.doReturn import com.nhaarman.mockitokotlin2.doReturn
import com.nhaarman.mockitokotlin2.notNull
import com.nhaarman.mockitokotlin2.whenever import com.nhaarman.mockitokotlin2.whenever
import org.assertj.core.api.Assertions.anyOf
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Condition import org.assertj.core.api.Condition
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
...@@ -18,7 +16,7 @@ internal class GameTest { ...@@ -18,7 +16,7 @@ internal class GameTest {
@BeforeEach @BeforeEach
fun setUp() { fun setUp() {
doReturn(true).whenever(thirdDeck).checkCardCanGoOnPile(any()) doReturn(true).whenever(thirdDeck).pileCompatibilityCheck(any())
} }
@Test @Test
......
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