Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Card Game Example
- // by MissEshock2002
- // This script, from https://ss.deviatenow.com, is protected by this licence:
- // CC0
- setInfos(9, "Card Game Example",
- "a simple card game comparing cards and more examples",
- "MissEshock2002",
- "v1.0", 0x222222, "en", ["example", "tutorial"]);
- // list to map the cards to the card images for "images/cards/deck0" or
- // the cards from the bacara packs
- // the index is the image name. example: IMAGE_MAP.indexOf("AC") -> "0.png"
- List<String> IMAGE_MAP = [
- "AC", "AD", "AH", "AS", "2C", "2D", "2H", "2S", "3C", "3D", "3H", "3S", "4C", "4D", "4H", "4S",
- "5C", "5D", "5H", "5S", "6C", "6D", "6H", "6S", "7C", "7D", "7H", "7S", "8C", "8D", "8H", "8S",
- "9C", "9D", "9H", "9S", "TC", "TD", "TH", "TS", "JC", "JD", "JH", "JS", "QC", "QD", "QH", "QS",
- "KC", "KD", "KH", "KS"
- ]
- // a deck of cards is made of 13 ranks times 4 suits = 52 cards. 'T' is for 10
- Map<Character, String> RANKS = ['A': "Ace", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six",
- '7': "Seven", '8': "Eight", '9': "Nine", 'T': "Ten",
- 'J': "Jack", 'Q': "Queen", 'K': "King"]
- Map<Character, String> SUITS = ['C': "Clubs", 'D': "Diamonds", 'H': "Hearts", 'S': "Spades"]
- // shorter as lists, if cards as text not needed:
- // Remove ".keySet()" in getDeck!
- //List<Character> RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
- //List<Character> SUITS = ['C', 'D', 'H', 'S']
- // in this game the cards have given values
- Map<Character, Byte> CARD_POINT_VALUES = ['A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
- 'T': 10, 'J': 11, 'Q': 12, 'K': 13]
- // card stacks
- List<String> deck = []
- List<String> player1Hand = []
- List<String> player2Hand = []
- // game vars
- int player1Points = 0
- int player2Points = 0
- def showCards = { List<String> _deck ->
- // TODO graphics example
- _deck.eachWithIndex { String card, int i ->
- // TODO draw card
- }
- }
- def getDeck = {
- /** return a shuffled deck of 52 cards */
- List<String> tempDeck = []
- // put 13 ranks and 4 suits to 52 cards: "AC", "AD", "AH", ...
- RANKS.keySet().each { rank ->
- SUITS.keySet().each { suit ->
- tempDeck.add(rank + suit)
- }
- }
- // shuffle the deck befor returning
- Collections.shuffle(tempDeck)
- return tempDeck
- }
- def getPointValue = { List<String> cardDeck ->
- /** retun the point sum of all cards */
- int pointSum = 0
- cardDeck.each { card ->
- // card[0] is the first character and the rank of the card string
- pointSum += CARD_POINT_VALUES[card[0]]
- }
- return pointSum
- }
- def getCardAsText = { String card ->
- /** retrun the card as a string, example "QH" -> "Queen of Hearts" */
- // card[0] is the first character and the rank
- // card[1] is the second character and the suit
- return RANKS[card[0]] + " of " + SUITS[card[1]]
- }
- def showHands = {
- /** show the cards players have on there hands and the points */
- String player1LatestCard = (player1Hand.isEmpty()) ? "-" : getCardAsText(player1Hand[-1])
- String player2LatestCard = (player2Hand.isEmpty()) ? "-" : getCardAsText(player2Hand[-1])
- show("Player 1 Hand: " + player1Hand +
- "\nPlayer 2 Hand: " + player2Hand +
- "\nPlayer 1 latest card: " + player1LatestCard +
- "\nPlayer 2 latest card: " + player2LatestCard +
- "\n\nPlayer 1 Points: " + player1Points +
- "\nPlayer 2 Points: " + player2Points +
- "\n\nRemaining cards: " + deck.size() +
- "\n\nDEBUG: deck = " + deck
- )
- }
- def getPointsOverview = {
- String pointsOverview = ""
- RANKS.each { rank, rankName ->
- pointsOverview += "${rankName} = ${CARD_POINT_VALUES[rank]}\n"
- }
- return pointsOverview
- }
- def simpleCardGame = {
- show("A simple card game.\n\nPlayer 1 and Player 2 draw 1 card per round till a deck of 52 cards is empty." +
- "\nThe points players have on hand will be counted." +
- "\nThe player with more points wins." +
- "\n\nPoints Overview:\n" + getPointsOverview())
- showButton("Start Game")
- // set or reset the game vars:
- deck = getDeck() // make a deck of 52 cards
- player1Hand.clear() // clear() makes a list empty
- player2Hand.clear()
- player1Points = 0
- player2Points = 0
- // draw cards till the deck is empty
- while (!deck.isEmpty()) {
- showHands()
- // player 1 draws
- showButton("Player 1 draw card")
- String topCard = deck.pop() // pop() removes and gives back the first item in a list
- player1Hand.add(topCard) // add() puts an item at the end of a list
- player1Points = getPointValue(player1Hand)
- showHands()
- // player 2 draws
- showButton("Player 2 draw card")
- player2Hand.add(deck.pop()) // pop() and add() in one line
- player2Points = getPointValue(player2Hand)
- }
- showHands()
- showButton("->")
- if (player1Points > player2Points) {
- show("Player 1 wins with ${player1Points} to ${player2Points} points.")
- } else if (player1Points < player2Points) {
- show("Player 2 wins with ${player2Points} to ${player1Points} points.")
- } else {
- show("A draw with ${player1Points} points.")
- }
- showButton("Game Over")
- }
- def showMoreExamples = {
- List deckA = []
- List deckB = []
- // check if a card is in a deck:
- deckA = ["AC", "KD"]
- deckB = ["QH", "JS"]
- boolean queenInDeckA = "QH" in deckA
- show("is Queen of Hearts in deck ${deckA}: ${queenInDeckA}" +
- "\n\nis Queen of Hearts in deck ${deckB}: ${"QH" in deckB}")
- showButton("->")
- // check if ranks or suits are in a deck:
- show("is one or more Ace in deck ${deckA}: ${deckA.any { card -> card[0] == "A" }}" +
- "\n\nis one or more Ace in deck ${deckB}: ${deckB.any { card -> card[0] == "A" }}" +
- "\n\nis one or more Hearts in deck ${deckA}: ${deckA.any { card -> card[1] == "H" }}" +
- "\n\nis one or more Hearts in deck ${deckB}: ${deckB.any { card -> card[1] == "H" }}")
- showButton("->")
- // copy a deck:
- deck = ["AC", "AD"]
- List deckCopy = deck.collect()
- deck.add("2S")
- show("Copy a deck: changing one does not change the other." +
- "\n\nadd \"2S\" to deck: deck = ${deck}\n\ndeckCopy = ${deckCopy}")
- showButton("->")
- // add deck to deck:
- deck = ["AC", "AD"]
- deckCopy = deck.collect()
- List deckAddToBottom = ["KC", "KD"]
- List deckAddToTop = ["JC", "JD"]
- deck.addAll(deckAddToBottom) // add a deck to the bottom of a deck
- deck.addAll(0, deckAddToTop) // add a deck to the top of a deck
- show("deck = ${deckCopy}" +
- "\n\ndeckAddToBottom = ${deckAddToBottom} put at bottom of deck" +
- "\ndeckAddToTop = ${deckAddToTop} put on top of deck" +
- "\n\ndeck = ${deck}")
- showButton("->")
- // split deck in half:
- deck = ["AC", "AD", "QH", "QS"]
- List deck1 = []
- List deck2 = []
- if (!deck.isEmpty()) {
- // if odd size: deck1 gets one more card with Math.ceil()
- // with Math.floor() deck2 gets one more card
- int halfDeckSize = Math.ceil(deck.size() / 2)
- deck1 = deck[0..(halfDeckSize - 1)]
- deck2 = deck[halfDeckSize..(deck.size() - 1)]
- }
- show("split deck in half:\n\ndeck = ${deck}\n\ndeck1 = ${deck1}\ndeck2 = ${deck2}")
- showButton("->")
- }
- boolean endLoop = false
- while (!endLoop) {
- if (getBoolean("Simple card game or more examples?", "simple card game", "more examples")) {
- simpleCardGame()
- } else {
- showMoreExamples()
- }
- }
Add Comment
Please, Sign In to add comment