Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
81
Task/Playing-cards/Ceylon/playing-cards.ceylon
Normal file
81
Task/Playing-cards/Ceylon/playing-cards.ceylon
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import com.vasileff.ceylon.random.api { ... }
|
||||
|
||||
"""Run the example code for Rosetta Code ["Playing cards" task] (http://rosettacode.org/wiki/Playing_cards)."""
|
||||
shared void run() {
|
||||
variable value deck = Deck();
|
||||
print("New deck (``deck.size`` cards): ``deck``
|
||||
");
|
||||
|
||||
deck = deck.shuffle();
|
||||
print("Shuffeled deck (``deck.size`` cards): ``deck``
|
||||
");
|
||||
|
||||
print("Deal three hands: ");
|
||||
for (i in 1..3) {
|
||||
value [hand, _deck] = deck.deal();
|
||||
print("- Dealt ``hand.size`` cards to hand ``i`` : ``join(hand)``");
|
||||
deck = _deck;
|
||||
}
|
||||
|
||||
print("
|
||||
Deck (``deck.size`` cards) after dealing three hands: ``deck``");
|
||||
|
||||
}
|
||||
|
||||
abstract class Suit() of clubs | hearts | spades | diamonds {}
|
||||
|
||||
object clubs extends Suit() { string = "♣"; }
|
||||
object hearts extends Suit() { string = "♥"; }
|
||||
object spades extends Suit() { string = "♠"; }
|
||||
object diamonds extends Suit() { string = "♦"; }
|
||||
|
||||
abstract class Pip() of two | three | four | five | six | seven | eight | nine | ten | jack | queen | king | ace {}
|
||||
object two extends Pip() { string = "2"; }
|
||||
object three extends Pip() { string = "3"; }
|
||||
object four extends Pip() { string = "4"; }
|
||||
object five extends Pip() { string = "5"; }
|
||||
object six extends Pip() { string = "6"; }
|
||||
object seven extends Pip() { string = "7"; }
|
||||
object eight extends Pip() { string = "8"; }
|
||||
object nine extends Pip() { string = "9"; }
|
||||
object ten extends Pip() { string = "10"; }
|
||||
object jack extends Pip() { string = "J"; }
|
||||
object queen extends Pip() { string = "Q"; }
|
||||
object king extends Pip() { string = "K"; }
|
||||
object ace extends Pip() { string = "A"; }
|
||||
|
||||
class Card(shared Pip pip, shared Suit suit) {
|
||||
string = "``pip`` of ``suit``";
|
||||
}
|
||||
|
||||
|
||||
String join(Card[] cards) => ", ".join { *cards };
|
||||
|
||||
class Deck (cards = [ for (suit in `Suit`.caseValues) for (pip in `Pip`.caseValues) Card(pip, suit) ]) {
|
||||
shared Card[] cards;
|
||||
|
||||
shared Deck shuffle(Random rnd = platformRandom())
|
||||
=> if (nonempty cards)
|
||||
then Deck( [*randomize(cards, rnd)] )
|
||||
else this;
|
||||
|
||||
shared Integer size => cards.size;
|
||||
|
||||
shared Boolean empty => cards.empty;
|
||||
|
||||
string => if (size > 13)
|
||||
then "\n " + "\n ". join { *cards.partition(13).map((cards) => join(cards)) }
|
||||
else join(cards);
|
||||
|
||||
shared [Card[], Deck] deal(Integer handSize = 5) {
|
||||
if (handSize >= cards.size) {
|
||||
return [cards, Deck([])];
|
||||
}
|
||||
else {
|
||||
return [
|
||||
cards.initial(handSize),
|
||||
Deck(cards.skip(handSize).sequence())
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Task/Playing-cards/Nim/playing-cards.nim
Normal file
43
Task/Playing-cards/Nim/playing-cards.nim
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import math
|
||||
randomize()
|
||||
|
||||
proc shuffle[T](x: var seq[T]) =
|
||||
for i in countdown(x.high, 0):
|
||||
let j = random(i + 1)
|
||||
swap(x[i], x[j])
|
||||
|
||||
type
|
||||
Suit = enum ♥, ♦, ♣, ♠
|
||||
|
||||
Pip = enum c02, c03, c04, c05, c06, c07, c08, c09, c10, cQu, cKi, cAs
|
||||
|
||||
Card = object
|
||||
pip: Pip
|
||||
suit: Suit
|
||||
|
||||
Deck = object
|
||||
cards: seq[Card]
|
||||
|
||||
proc `$`(c: Card): string = $c.pip & $c.suit
|
||||
|
||||
proc initDeck(): Deck =
|
||||
result = Deck(cards: @[])
|
||||
for suit in Suit:
|
||||
for pip in Pip:
|
||||
result.cards.add Card(pip: pip, suit: suit)
|
||||
|
||||
proc `$`(d: Deck): string = $d.cards
|
||||
|
||||
proc shuffle(d: var Deck) = shuffle(d.cards)
|
||||
|
||||
proc deal(d: var Deck): Card =
|
||||
d.shuffle()
|
||||
d.cards.pop()
|
||||
|
||||
var d = initDeck()
|
||||
echo "40 cards from a deck:"
|
||||
for i in 0..4:
|
||||
for j in 0..7:
|
||||
stdout.write($d.deal(), " ")
|
||||
echo ""
|
||||
echo "The remaining cards are: ", $d
|
||||
245
Task/Playing-cards/Ring/playing-cards.ring
Normal file
245
Task/Playing-cards/Ring/playing-cards.ring
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
Load "guilib.ring"
|
||||
|
||||
nScale = 1
|
||||
|
||||
app1 = new qApp
|
||||
|
||||
mypic = new QPixmap("cards.jpg")
|
||||
|
||||
mypic2 = mypic.copy(0,(124*4)+1,79,124)
|
||||
Player1EatPic = mypic.copy(80,(124*4)+1,79,124)
|
||||
Player2EatPic= mypic.copy(160,(124*4)+1,79,124)
|
||||
|
||||
aMyCards = []
|
||||
aMyValues = []
|
||||
for x1 = 0 to 3
|
||||
for y1 = 0 to 12
|
||||
temppic = mypic.copy((79*y1)+1,(124*x1)+1,79,124)
|
||||
aMyCards + temppic
|
||||
aMyValues + (y1+1)
|
||||
next
|
||||
next
|
||||
|
||||
nPlayer1Score = 0 nPlayer2Score=0
|
||||
|
||||
do
|
||||
Page1 = new Game
|
||||
Page1.Start()
|
||||
again Page1.lnewgame
|
||||
|
||||
mypic.delete()
|
||||
mypic2.delete()
|
||||
Player1EatPic.delete()
|
||||
Player2EatPic.delete()
|
||||
|
||||
for t in aMyCards
|
||||
t.delete()
|
||||
next
|
||||
|
||||
func gui_setbtnpixmap pBtn,pPixmap
|
||||
pBtn {
|
||||
setIcon(new qicon(pPixmap.scaled(width(),height(),0,0)))
|
||||
setIconSize(new QSize(width(),height()))
|
||||
}
|
||||
|
||||
Class Game
|
||||
|
||||
nCardsCount = 10
|
||||
win1 layout1 label1 label2 layout2 layout3 aBtns aBtns2
|
||||
aCards nRole=1 aStatus = list(nCardsCount) aStatus2 = aStatus
|
||||
aValues aStatusValues = aStatus aStatusValues2 = aStatus
|
||||
Player1EatPic Player2EatPic
|
||||
lnewgame = false
|
||||
nDelayEat = 0.5
|
||||
nDelayNewGame = 1
|
||||
|
||||
func start
|
||||
|
||||
win1 = new qWidget() {
|
||||
setwindowtitle("Five")
|
||||
setstylesheet("background-color: White")
|
||||
showfullscreen()
|
||||
}
|
||||
|
||||
layout1 = new qvboxlayout()
|
||||
|
||||
label1 = new qlabel(win1) {
|
||||
settext("Player (1) - Score : " + nPlayer1Score)
|
||||
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
|
||||
setstylesheet("color: White; background-color: Purple;
|
||||
font-size:20pt")
|
||||
setfixedheight(200)
|
||||
}
|
||||
|
||||
closebtn = new qpushbutton(win1) {
|
||||
settext("Close Application")
|
||||
setstylesheet("font-size: 18px ; color : white ;
|
||||
background-color: black ;")
|
||||
setclickevent("Page1.win1.close()")
|
||||
}
|
||||
|
||||
aCards = aMyCards
|
||||
aValues = aMyValues
|
||||
|
||||
layout2 = new qhboxlayout()
|
||||
|
||||
aBtns = []
|
||||
|
||||
for x = 1 to nCardsCount
|
||||
aBtns + new qpushbutton(win1)
|
||||
aBtns[x].setfixedwidth(79*nScale)
|
||||
aBtns[x].setfixedheight(124*nScale)
|
||||
gui_setbtnpixmap(aBtns[x],mypic2)
|
||||
layout2.addwidget(aBtns[x])
|
||||
aBtns[x].setclickevent("Page1.Player1click("+x+")")
|
||||
next
|
||||
|
||||
layout1.addwidget(label1)
|
||||
layout1.addlayout(layout2)
|
||||
|
||||
label2 = new qlabel(win1) {
|
||||
settext("Player (2) - Score : " + nPlayer2Score)
|
||||
setalignment(Qt_AlignHCenter | Qt_AlignVCenter)
|
||||
setstylesheet("color: white; background-color: red;
|
||||
font-size:20pt")
|
||||
setfixedheight(200)
|
||||
}
|
||||
|
||||
layout3 = new qhboxlayout()
|
||||
|
||||
aBtns2 = []
|
||||
for x = 1 to nCardsCount
|
||||
aBtns2 + new qpushbutton(win1)
|
||||
aBtns2[x].setfixedwidth(79*nScale)
|
||||
aBtns2[x].setfixedheight(124*nScale)
|
||||
gui_setbtnpixmap(aBtns2[x],mypic2)
|
||||
layout3.addwidget(aBtns2[x])
|
||||
aBtns2[x].setclickevent("Page1.Player2click("+x+")")
|
||||
next
|
||||
|
||||
layout1.addwidget(label2)
|
||||
layout1.addlayout(layout3)
|
||||
layout1.addwidget(closebtn)
|
||||
|
||||
win1.setlayout(layout1)
|
||||
|
||||
app1.exec()
|
||||
|
||||
Func Player1Click x
|
||||
if nRole = 1 and aStatus[x] = 0
|
||||
nPos = ((random(100)+clock())%(len(aCards)-1)) + 1
|
||||
gui_setbtnpixmap(aBtns[x],aCards[nPos])
|
||||
del(aCards,nPos)
|
||||
nRole = 2
|
||||
aStatus[x] = 1
|
||||
aStatusValues[x] = aValues[nPos]
|
||||
del(aValues,nPos)
|
||||
Player1Eat(x,aStatusValues[x])
|
||||
checknewgame()
|
||||
ok
|
||||
|
||||
Func Player2Click x
|
||||
if nRole = 2 and aStatus2[x] = 0
|
||||
nPos = ((random(100)+clock())%(len(aCards)-1)) + 1
|
||||
gui_setbtnpixmap(aBtns2[x],aCards[nPos])
|
||||
del(aCards,nPos)
|
||||
nRole = 1
|
||||
aStatus2[x] = 1
|
||||
aStatusValues2[x] = aValues[nPos]
|
||||
del(aValues,nPos)
|
||||
Player2Eat(x,aStatusValues2[x])
|
||||
checknewgame()
|
||||
ok
|
||||
|
||||
Func Player1Eat nPos,nValue
|
||||
|
||||
app1.processEvents()
|
||||
|
||||
delay(nDelayEat)
|
||||
lEat = false
|
||||
for x = 1 to nCardsCount
|
||||
if aStatus2[x] = 1 and (aStatusValues2[x] = nValue or nValue=5)
|
||||
aStatus2[x] = 2
|
||||
gui_setbtnpixmap(aBtns2[x],Player1EatPic)
|
||||
lEat = True
|
||||
nPlayer1Score++
|
||||
ok
|
||||
if (x != nPos) and (aStatus[x] = 1) and
|
||||
(aStatusValues[x] = nValue or nValue=5)
|
||||
aStatus[x] = 2
|
||||
gui_setbtnpixmap(aBtns[x],Player1EatPic)
|
||||
lEat = True
|
||||
nPlayer1Score++
|
||||
ok
|
||||
next
|
||||
if lEat
|
||||
nPlayer1Score++
|
||||
gui_setbtnpixmap(aBtns[nPos],Player1EatPic)
|
||||
aStatus[nPos] = 2
|
||||
label1.settext("Player (1) - Score : " + nPlayer1Score)
|
||||
ok
|
||||
|
||||
Func Player2Eat nPos,nValue
|
||||
|
||||
app1.processEvents()
|
||||
|
||||
delay(nDelayEat)
|
||||
lEat = false
|
||||
for x = 1 to nCardsCount
|
||||
if aStatus[x] = 1 and (aStatusValues[x] = nValue or nValue = 5)
|
||||
aStatus[x] = 2
|
||||
gui_setbtnpixmap(aBtns[x],Player2EatPic)
|
||||
lEat = True
|
||||
nPlayer2Score++
|
||||
ok
|
||||
|
||||
if (x != nPos) and (aStatus2[x] = 1) and
|
||||
(aStatusValues2[x] = nValue or nValue=5 )
|
||||
aStatus2[x] = 2
|
||||
gui_setbtnpixmap(aBtns2[x],Player2EatPic)
|
||||
lEat = True
|
||||
nPlayer2Score++
|
||||
ok
|
||||
next
|
||||
if lEat
|
||||
nPlayer2Score++
|
||||
gui_setbtnpixmap(aBtns2[nPos],Player2EatPic)
|
||||
aStatus2[nPos] = 2
|
||||
label2.settext("Player (2) - Score : " + nPlayer2Score)
|
||||
ok
|
||||
|
||||
Func checknewgame
|
||||
if isnewgame()
|
||||
lnewgame = true
|
||||
|
||||
if nPlayer1Score > nPlayer2Score
|
||||
label1.settext("Player (1) Wins!!!")
|
||||
ok
|
||||
if nPlayer2Score > nPlayer1Score
|
||||
label2.settext("Player (2) Wins!!!")
|
||||
ok
|
||||
|
||||
app1.processEvents()
|
||||
delay(nDelayNewGame)
|
||||
|
||||
win1.delete()
|
||||
app1.quit()
|
||||
ok
|
||||
|
||||
Func isnewgame
|
||||
for t in aStatus
|
||||
if t = 0
|
||||
return false
|
||||
ok
|
||||
next
|
||||
for t in aStatus2
|
||||
if t = 0
|
||||
return false
|
||||
ok
|
||||
next
|
||||
return true
|
||||
|
||||
Func delay x
|
||||
nTime = x * 1000
|
||||
oTest = new qTest
|
||||
oTest.qsleep(nTime)
|
||||
31
Task/Playing-cards/Sidef/playing-cards.sidef
Normal file
31
Task/Playing-cards/Sidef/playing-cards.sidef
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
define Pip = <A 2 3 4 5 6 7 8 9 10 J Q K>;
|
||||
define Suit = <♦ ♣ ♥ ♠>;
|
||||
|
||||
class Card(pip, suit) {
|
||||
method to_s { pip + suit }
|
||||
}
|
||||
|
||||
class Deck(cards=[]) {
|
||||
|
||||
method init {
|
||||
cards = gather {
|
||||
Pip.each { |p| Suit.each { |s| take(Card(p, s)) } }
|
||||
}
|
||||
}
|
||||
|
||||
method shuffle {
|
||||
cards.shuffle!;
|
||||
}
|
||||
|
||||
method deal { cards.shift };
|
||||
method to_s { cards.join(" ") };
|
||||
}
|
||||
|
||||
var d = Deck();
|
||||
say "Deck: #{d}";
|
||||
|
||||
var top = d.deal;
|
||||
say "Top card: #{top}";
|
||||
|
||||
d.shuffle;
|
||||
say "Deck, shuffled: #{d}";
|
||||
182
Task/Playing-cards/Swift/playing-cards.swift
Normal file
182
Task/Playing-cards/Swift/playing-cards.swift
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
import Foundation
|
||||
|
||||
// extend any Indexed collection to be able to shuffle (see http://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift)
|
||||
extension CollectionType where Index == Int {
|
||||
/// Return a copy of `self` with its elements shuffled
|
||||
func shuffle() -> [Generator.Element] {
|
||||
var list = Array(self)
|
||||
list.shuffleInPlace()
|
||||
return list
|
||||
}
|
||||
}
|
||||
|
||||
extension MutableCollectionType where Index == Int {
|
||||
/// Shuffle the elements of `self` in-place.
|
||||
mutating func shuffleInPlace() {
|
||||
// empty and single-element collections don't shuffle
|
||||
if count < 2 { return }
|
||||
|
||||
for i in 0..<count - 1 {
|
||||
let j = Int(arc4random_uniform(UInt32(count - i))) + i
|
||||
guard i != j else { continue }
|
||||
swap(&self[i], &self[j])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// now the model structs
|
||||
enum CardColor : Int {
|
||||
case Red
|
||||
case Black
|
||||
}
|
||||
extension CardColor : CustomStringConvertible {
|
||||
var description : String {
|
||||
switch self {
|
||||
case .Red:
|
||||
return "Red"
|
||||
case .Black:
|
||||
return "Black"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Suit : Int {
|
||||
case Hearts = 1
|
||||
case Diamonds
|
||||
case Spades
|
||||
case Clubs
|
||||
|
||||
var color : CardColor {
|
||||
switch self {
|
||||
case .Hearts, .Diamonds:
|
||||
return .Red
|
||||
case .Spades, .Clubs:
|
||||
return .Black
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Pip : Int {
|
||||
case Ace = 1
|
||||
case Two = 2
|
||||
case Three = 3
|
||||
case Four = 4
|
||||
case Five = 5
|
||||
case Six = 6
|
||||
case Seven = 7
|
||||
case Eight = 8
|
||||
case Nine = 9
|
||||
case Ten = 10
|
||||
case Jack = 11
|
||||
case Queen = 12
|
||||
case King = 13
|
||||
}
|
||||
|
||||
struct Card {
|
||||
let pip : Pip
|
||||
let suit : Suit
|
||||
|
||||
var isFaceCard : Bool {
|
||||
return pip.rawValue > 10
|
||||
}
|
||||
|
||||
var color : CardColor {
|
||||
return suit.color
|
||||
}
|
||||
}
|
||||
extension Card : Equatable {}
|
||||
func == (l:Card, r:Card) -> Bool {
|
||||
return l.pip == r.pip &&
|
||||
l.suit == r.suit
|
||||
}
|
||||
extension Card : CustomStringConvertible {
|
||||
var description : String {
|
||||
return "\(pip) of \(suit)"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Deck {
|
||||
var cards : [Card]
|
||||
|
||||
var count : Int {
|
||||
return cards.count
|
||||
}
|
||||
|
||||
init(shuffling:Bool=true) {
|
||||
var startcards = [Card]()
|
||||
for suit in (Suit.Hearts.rawValue...Suit.Clubs.rawValue) {
|
||||
for pip in (Pip.Ace.rawValue...Pip.King.rawValue) {
|
||||
startcards.append(Card(pip: Pip(rawValue: pip)!, suit: Suit(rawValue: suit)!))
|
||||
}
|
||||
}
|
||||
cards = startcards
|
||||
|
||||
if shuffling {
|
||||
shuffle()
|
||||
}
|
||||
}
|
||||
|
||||
mutating func shuffle() {
|
||||
cards.shuffleInPlace()
|
||||
}
|
||||
|
||||
mutating func deal() -> Card {
|
||||
let out = cards.removeFirst()
|
||||
return out
|
||||
}
|
||||
|
||||
}
|
||||
extension Deck : CustomStringConvertible {
|
||||
var description : String {
|
||||
return "\(count) cards: \(cards.description)"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// test some cards
|
||||
let kh = Card(pip: .King, suit: .Hearts)
|
||||
let ad = Card(pip: .Ace, suit: .Diamonds)
|
||||
let tc = Card(pip: .Two, suit: .Clubs)
|
||||
let fc = Card(pip: Pip(rawValue:4)!, suit: .Spades)
|
||||
|
||||
|
||||
// create an unshuffled deck
|
||||
var efg = Deck(shuffling: false)
|
||||
|
||||
|
||||
// create a shuffled deck and print its contents
|
||||
var d = Deck()
|
||||
print(d)
|
||||
|
||||
// deal three cards
|
||||
d.deal()
|
||||
d.deal()
|
||||
d.deal()
|
||||
d
|
||||
|
||||
// deal a couple more cards and check their color
|
||||
let c = d.deal()
|
||||
c.color
|
||||
|
||||
let cc = d.deal()
|
||||
cc.color
|
||||
|
||||
// deal out the rest of the deck, leaving just one card
|
||||
while d.count > 1 {
|
||||
d.deal()
|
||||
}
|
||||
d
|
||||
|
||||
// test equality of a couple cards
|
||||
if kh == Card(pip: Pip.King, suit: Suit.Clubs) {
|
||||
let a = true
|
||||
}
|
||||
else {
|
||||
let a = false
|
||||
}
|
||||
|
||||
kh != Card(pip: Pip.King, suit: Suit.Clubs)
|
||||
kh.isFaceCard
|
||||
fc.isFaceCard
|
||||
Loading…
Add table
Add a link
Reference in a new issue