CardDrawer { start { var cards = new List() //creates java.util.ArrayList addNumberedCards(cards) //calling method with 3 arguments (last 2 are default) addCardWithAllColors("Ace",cards) addCardWithAllColors("Queen",cards) addCardWithAllColors("King",cards) addCardWithAllColors("Jack",cards) //Calling with named arguments (and in differnet order) //The last parameter (cardsPerPlayer) is ommited (it's default value is 5) drawCardsForPlayers(playersAmount -> 5,cardsList -> cards) }
addNumberedCards(List cardsList,int first=2, int last=10) { for i from first to last { //loop from first to last (inclusive) var numberString = new java.lang.Integer(i).toString() addCardWithAllColors(numberString,cardsList) } }
drawCardsForPlayers(List cardsList,int playersAmount = 3,int cardsPerPlayer = 5) { if(cardsList.size() < (playersAmount * cardsPerPlayer)) { print "ERROR - Not enough cards" //No exceptions yet :) return } var random = new java.util.Random() for i from 1 to playersAmount { var playernumberString = new java.lang.Integer(i).toString() print "player " + playernumberString + " is drawing:" for j from 1 to cardsPerPlayer { var dawnCardIndex = random.nextInt(cardsList.size() - 1) var drawedCard = cardsList.remove(dawnCardIndex) print " drawed:" + drawedCard } } } }
kuba@kuba-laptop:~/repos/Enkel-JVM-language$ java CardDrawer player 1 is drawing: drawed:{Diamonds,Queen} drawed:{Spades,7} drawed:{Hearts,Jack} drawed:{Spades,4} drawed:{Hearts,2} player 2 is drawing: drawed:{Diamonds,4} drawed:{Hearts,Ace} drawed:{Diamonds,Jack} drawed:{Spades,Queen} drawed:{Spades,King} player 3 is drawing: drawed:{Diamonds,Ace} drawed:{Clubs,2} drawed:{Clubs,3} drawed:{Spades,8} drawed:{Clubs,7} player 4 is drawing: drawed:{Spades,Ace} drawed:{Diamonds,3} drawed:{Clubs,4} drawed:{Clubs,6} drawed:{Diamonds,2} player 5 is drawing: drawed:{Hearts,4} drawed:{Hearts,Queen} drawed:{Hearts,10} drawed:{Clubs,Jack} drawed:{Diamonds,8}