MissEshock2002

SexScripts Card Game Example

Aug 18th, 2020
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 7.84 KB | None | 0 0
  1. // Card Game Example
  2. // by MissEshock2002
  3.  
  4. // This script, from https://ss.deviatenow.com, is protected by this licence:
  5. // CC0
  6.  
  7. setInfos(9, "Card Game Example",
  8.         "a simple card game comparing cards and more examples",
  9.         "MissEshock2002",
  10.         "v1.0", 0x222222, "en", ["example", "tutorial"]);
  11.  
  12.  
  13. // list to map the cards to the card images for "images/cards/deck0" or
  14. // the cards from the bacara packs
  15. // the index is the image name. example: IMAGE_MAP.indexOf("AC") -> "0.png"
  16. List<String> IMAGE_MAP = [
  17.         "AC", "AD", "AH", "AS", "2C", "2D", "2H", "2S", "3C", "3D", "3H", "3S", "4C", "4D", "4H", "4S",
  18.         "5C", "5D", "5H", "5S", "6C", "6D", "6H", "6S", "7C", "7D", "7H", "7S", "8C", "8D", "8H", "8S",
  19.         "9C", "9D", "9H", "9S", "TC", "TD", "TH", "TS", "JC", "JD", "JH", "JS", "QC", "QD", "QH", "QS",
  20.         "KC", "KD", "KH", "KS"
  21. ]
  22.  
  23. // a deck of cards is made of 13 ranks times 4 suits = 52 cards. 'T' is for 10
  24. Map<Character, String> RANKS = ['A': "Ace", '2': "Two", '3': "Three", '4': "Four", '5': "Five", '6': "Six",
  25.                                 '7': "Seven", '8': "Eight", '9': "Nine", 'T': "Ten",
  26.                                 'J': "Jack", 'Q': "Queen", 'K': "King"]
  27. Map<Character, String> SUITS = ['C': "Clubs", 'D': "Diamonds", 'H': "Hearts", 'S': "Spades"]
  28.  
  29. // shorter as lists, if cards as text not needed:
  30. // Remove ".keySet()" in getDeck!
  31. //List<Character> RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
  32. //List<Character> SUITS = ['C', 'D', 'H', 'S']
  33.  
  34.  
  35. // in this game the cards have given values
  36. Map<Character, Byte> CARD_POINT_VALUES = ['A': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
  37.                                           'T': 10, 'J': 11, 'Q': 12, 'K': 13]
  38.  
  39. // card stacks
  40. List<String> deck = []
  41. List<String> player1Hand = []
  42. List<String> player2Hand = []
  43.  
  44. // game vars
  45. int player1Points = 0
  46. int player2Points = 0
  47.  
  48.  
  49. def showCards = { List<String> _deck ->
  50.     // TODO graphics example
  51.     _deck.eachWithIndex { String card, int i ->
  52.         // TODO draw card
  53.     }
  54. }
  55.  
  56.  
  57. def getDeck = {
  58.     /** return a shuffled deck of 52 cards */
  59.     List<String> tempDeck = []
  60.  
  61.     // put 13 ranks and 4 suits to 52 cards: "AC", "AD", "AH", ...
  62.     RANKS.keySet().each { rank ->
  63.         SUITS.keySet().each { suit ->
  64.             tempDeck.add(rank + suit)
  65.         }
  66.     }
  67.     // shuffle the deck befor returning
  68.     Collections.shuffle(tempDeck)
  69.     return tempDeck
  70. }
  71.  
  72. def getPointValue = { List<String> cardDeck ->
  73.     /** retun the point sum of all cards */
  74.  
  75.     int pointSum = 0
  76.     cardDeck.each { card ->
  77.         // card[0] is the first character and the rank of the card string
  78.         pointSum += CARD_POINT_VALUES[card[0]]
  79.     }
  80.  
  81.     return pointSum
  82. }
  83.  
  84. def getCardAsText = { String card ->
  85.     /** retrun the card as a string, example "QH" -> "Queen of Hearts" */
  86.  
  87.     // card[0] is the first character and the rank
  88.     // card[1] is the second character and the suit
  89.     return RANKS[card[0]] + " of " + SUITS[card[1]]
  90. }
  91.  
  92. def showHands = {
  93.     /** show the cards players have on there hands and the points */
  94.  
  95.     String player1LatestCard = (player1Hand.isEmpty()) ? "-" : getCardAsText(player1Hand[-1])
  96.     String player2LatestCard = (player2Hand.isEmpty()) ? "-" : getCardAsText(player2Hand[-1])
  97.  
  98.     show("Player 1 Hand: " + player1Hand +
  99.             "\nPlayer 2 Hand: " + player2Hand +
  100.             "\nPlayer 1 latest card: " + player1LatestCard +
  101.             "\nPlayer 2 latest card: " + player2LatestCard +
  102.             "\n\nPlayer 1 Points: " + player1Points +
  103.             "\nPlayer 2 Points: " + player2Points +
  104.             "\n\nRemaining cards: " + deck.size() +
  105.             "\n\nDEBUG: deck = " + deck
  106.     )
  107. }
  108.  
  109. def getPointsOverview = {
  110.     String pointsOverview = ""
  111.     RANKS.each { rank, rankName ->
  112.         pointsOverview += "${rankName} = ${CARD_POINT_VALUES[rank]}\n"
  113.     }
  114.     return pointsOverview
  115. }
  116.  
  117. def simpleCardGame = {
  118.     show("A simple card game.\n\nPlayer 1 and Player 2 draw 1 card per round till a deck of 52 cards is empty." +
  119.             "\nThe points players have on hand will be counted." +
  120.             "\nThe player with more points wins." +
  121.             "\n\nPoints Overview:\n" + getPointsOverview())
  122.     showButton("Start Game")
  123.  
  124.     // set or reset the game vars:
  125.     deck = getDeck() // make a deck of 52 cards
  126.     player1Hand.clear() // clear() makes a list empty
  127.     player2Hand.clear()
  128.     player1Points = 0
  129.     player2Points = 0
  130.  
  131.     // draw cards till the deck is empty
  132.     while (!deck.isEmpty()) {
  133.         showHands()
  134.  
  135.         // player 1 draws
  136.         showButton("Player 1 draw card")
  137.         String topCard = deck.pop() // pop() removes and gives back the first item in a list
  138.         player1Hand.add(topCard) // add() puts an item at the end of a list
  139.         player1Points = getPointValue(player1Hand)
  140.  
  141.         showHands()
  142.  
  143.         // player 2 draws
  144.         showButton("Player 2 draw card")
  145.         player2Hand.add(deck.pop()) // pop() and add() in one line
  146.         player2Points = getPointValue(player2Hand)
  147.     }
  148.  
  149.     showHands()
  150.     showButton("->")
  151.  
  152.     if (player1Points > player2Points) {
  153.         show("Player 1 wins with ${player1Points} to ${player2Points} points.")
  154.     } else if (player1Points < player2Points) {
  155.         show("Player 2 wins with ${player2Points} to ${player1Points} points.")
  156.     } else {
  157.         show("A draw with ${player1Points} points.")
  158.     }
  159.     showButton("Game Over")
  160. }
  161.  
  162.  
  163. def showMoreExamples = {
  164.     List deckA = []
  165.     List deckB = []
  166.  
  167.     // check if a card is in a deck:
  168.     deckA = ["AC", "KD"]
  169.     deckB = ["QH", "JS"]
  170.     boolean queenInDeckA = "QH" in deckA
  171.     show("is Queen of Hearts in deck ${deckA}: ${queenInDeckA}" +
  172.             "\n\nis Queen of Hearts in deck ${deckB}: ${"QH" in deckB}")
  173.     showButton("->")
  174.  
  175.  
  176.     // check if ranks or suits are in a deck:
  177.     show("is one or more Ace in deck ${deckA}: ${deckA.any { card -> card[0] == "A" }}" +
  178.             "\n\nis one or more Ace in deck ${deckB}: ${deckB.any { card -> card[0] == "A" }}" +
  179.             "\n\nis one or more Hearts in deck ${deckA}: ${deckA.any { card -> card[1] == "H" }}" +
  180.             "\n\nis one or more Hearts in deck ${deckB}: ${deckB.any { card -> card[1] == "H" }}")
  181.     showButton("->")
  182.  
  183.  
  184.     // copy a deck:
  185.     deck = ["AC", "AD"]
  186.     List deckCopy = deck.collect()
  187.     deck.add("2S")
  188.     show("Copy a deck: changing one does not change the other." +
  189.             "\n\nadd \"2S\" to deck: deck = ${deck}\n\ndeckCopy = ${deckCopy}")
  190.     showButton("->")
  191.  
  192.  
  193.     // add deck to deck:
  194.     deck = ["AC", "AD"]
  195.     deckCopy = deck.collect()
  196.     List deckAddToBottom = ["KC", "KD"]
  197.     List deckAddToTop = ["JC", "JD"]
  198.     deck.addAll(deckAddToBottom) // add a deck to the bottom of a deck
  199.     deck.addAll(0, deckAddToTop) // add a deck to the top of a deck
  200.     show("deck = ${deckCopy}" +
  201.             "\n\ndeckAddToBottom = ${deckAddToBottom} put at bottom of deck" +
  202.             "\ndeckAddToTop = ${deckAddToTop} put on top of deck" +
  203.             "\n\ndeck = ${deck}")
  204.     showButton("->")
  205.  
  206.  
  207.     // split deck in half:
  208.     deck = ["AC", "AD", "QH", "QS"]
  209.     List deck1 = []
  210.     List deck2 = []
  211.     if (!deck.isEmpty()) {
  212.         // if odd size: deck1 gets one more card with Math.ceil()
  213.         // with Math.floor() deck2 gets one more card
  214.         int halfDeckSize = Math.ceil(deck.size() / 2)
  215.         deck1 = deck[0..(halfDeckSize - 1)]
  216.         deck2 = deck[halfDeckSize..(deck.size() - 1)]
  217.     }
  218.     show("split deck in half:\n\ndeck = ${deck}\n\ndeck1 = ${deck1}\ndeck2 = ${deck2}")
  219.     showButton("->")
  220. }
  221.  
  222.  
  223. boolean endLoop = false
  224. while (!endLoop) {
  225.     if (getBoolean("Simple card game or more examples?", "simple card game", "more examples")) {
  226.         simpleCardGame()
  227.     } else {
  228.         showMoreExamples()
  229.     }
  230. }
Add Comment
Please, Sign In to add comment