-
Notifications
You must be signed in to change notification settings - Fork 12
Enum Ranking ergänzt (contains Problem!) #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,26 +7,150 @@ | |
| */ | ||
| /* | ||
| EURE ANTWORT HIER | ||
| Wir nutzen für Card ein Struct, da es sich um eine statische Sache handelt, die sich nicht ändert. (und nicht als Referenz übergeben werden soll, wenn wir eine Hand erzeugen.) | ||
| Für Suit und Rank kommen am besten Enums in Frage, weil | ||
| */ | ||
| import Foundation | ||
|
|
||
| enum Suit: Int, CustomStringConvertible { | ||
| case diamonds, hearts, clubbs,spades | ||
|
|
||
| var description: String { | ||
| switch self { | ||
| case .diamonds: return "♦" | ||
| case .clubbs: return "♣" | ||
| case .hearts: return "♥" | ||
| case .spades: return "♣" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| enum Rank: Int { | ||
| case two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace | ||
|
|
||
| var description: String{ | ||
| switch self { | ||
| case .two: return "2" | ||
| case .three: return "3" | ||
| case .four: return "4" | ||
| case .five: return "5" | ||
| case .six: return "6" | ||
| case .seven: return "7" | ||
| case .eight: return "8" | ||
| case .nine: return "9" | ||
| case .ten: return "10" | ||
| case .jack: return "J" | ||
| case .queen: return "Q" | ||
| case .king: return "K" | ||
| case .ace: return "A" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| struct Card: CustomStringConvertible { | ||
| let suit: Suit | ||
| let rank: Rank | ||
|
|
||
| var description: String { | ||
| return "\(rank) \(suit)" | ||
|
|
||
| } | ||
| } | ||
|
|
||
| extension Card: Equatable {} | ||
|
|
||
| //: ## Testing | ||
| /* | ||
| var rankingCounts = [Ranking : Int]() | ||
| let samples = 100 | ||
| for i in 0...samples { | ||
| let ranking = PokerHand().ranking | ||
| if rankingCounts[ranking] == nil { | ||
| rankingCounts[ranking] = 1 | ||
| } else { | ||
| rankingCounts[ranking]! += 1 | ||
|
|
||
|
|
||
| func ==(lhs: Card, rhs: Card) -> Bool { | ||
| return lhs.suit == rhs.suit && lhs.rank == rhs.rank | ||
| } | ||
|
|
||
| enum Ranking: Int, CustomStringConvertible { | ||
| case Flush, HighCard | ||
|
|
||
| var description: String { | ||
| switch self { | ||
| case .Flush: return "Flush" | ||
| case .HighCard: return "High Card" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (ranking, count) in rankingCounts { | ||
| print("The probability of being dealt a \(ranking.description) is \(Double(count) / Double(samples) * 100)%") | ||
| struct PokerHand: CustomStringConvertible{ | ||
| let cards: [Card?] = [nil,nil,nil,nil,nil] | ||
| var description: String { | ||
| return PrintHand() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tipp: return cards.map({ $0.description }).joinWithSeparator(" ") |
||
| } | ||
| var ranking: Ranking { | ||
| for card in self.cards { | ||
| if (card!.suit != self.cards[0]!.suit){ | ||
| let ranking = Ranking.HighCard | ||
| return ranking | ||
| } | ||
| } | ||
| let ranking = Ranking.Flush | ||
| return ranking | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| // Initializer | ||
| init() { | ||
| let rndSuit = Suit(rawValue: Int(arc4random_uniform(4)))! | ||
| let rndRank = Rank(rawValue: Int(arc4random_uniform(13)))! | ||
| let rndCard = Card(suit: rndSuit, rank: rndRank) | ||
| for i in 0...5 { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Du kannst hier lieber ein variables Array |
||
| while (contains(self.cards,rndCard)) { | ||
| let rndSuit = Suit(rawValue: Int(arc4random_uniform(4)))! | ||
| let rndRank = Rank(rawValue: Int(arc4random_uniform(13)))! | ||
| let rndCard = Card(suit: rndSuit, rank: rndRank) | ||
| } | ||
| self.cards[i]=rndCard | ||
| } | ||
| } | ||
|
|
||
|
|
||
| func NewRandomCard () -> Card { | ||
| let rndSuit = Suit(rawValue: Int(arc4random_uniform(4)))! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Funktionen klein schreiben, nur Datentypen groß ;) |
||
| let rndRank = Rank(rawValue: Int(arc4random_uniform(13)))! | ||
| let rndCard = Card(suit: rndSuit, rank: rndRank) | ||
| return rndCard | ||
| } | ||
|
|
||
| func PrintHand() -> String { | ||
| var variableString: String = "" | ||
| for card in self.cards { | ||
| variableString+=card!.description | ||
| } | ||
| return variableString | ||
| } | ||
| } | ||
| */ | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| ////: ## Testing | ||
| //let test = PokerHand() | ||
| //print(test) | ||
| let Card1 = Card(suit: .diamonds, rank: .eight) | ||
| let Card2 = Card(suit: .diamonds, rank: .nine) | ||
| print(Card1) | ||
| //var rankingCounts = [Ranking : Int]() | ||
| //let samples = 100 | ||
| //for i in 0...samples { | ||
| // let ranking = PokerHand().ranking | ||
| // if rankingCounts[ranking] == nil { | ||
| // rankingCounts[ranking] = 1 | ||
| // } else { | ||
| // rankingCounts[ranking]! += 1 | ||
| // } | ||
| //} | ||
|
|
||
| //for (ranking, count) in rankingCounts { | ||
| // print("The probability of being dealt a \(ranking.description) is \(Double(count) / Double(samples) * 100)%") | ||
| //} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Schreib das Attribut lieber ohne Anfangswert. In
initdann ein neues Array erstellen undself.cardszuweisen