首页 > 代码库 > 用swift重写stanford CS193P的纸牌游戏 (1)- Card 和Deck

用swift重写stanford CS193P的纸牌游戏 (1)- Card 和Deck

本系列编号基本对应stanford CS193P的课程编号,可能有一两节课的误差:比如我标(1)就对应Lecture 1,但有时我做得快了就变成(1)对应lecture 1的全部和lecture 2的一部分。

 

前言:

我没学过OC,想学swift,可是网上只有教swift基本语法和写命令行程序的初级教程。

所以我只好拿个OC的教程,一边学swift,一边把OC的教程里的代码改成swift的,我想改完了之后应该就等于看了swift的教程了吧。

 

正文:

本文包括两个类,Card和Deck,以及一个单元测试类。写完之后的感觉:原来的教程教OC语法的部分看得我睡着了,而直接用swift写极简单。

 

具体值得记录的点有:

1.swift没有自动类型转换,只有类型推断,在arc4random % count 的时候无法自动转换类型。所以我加了个强制类型转换。

2.我debug测试类的时候发现xcode里显示不出我往deck.cards里插入的card,显示为invalid,但实际上是正确的。可能和我用的xcode6还是beta1有关。

3.drawRandomCard 的时候一开始我先做了remove,后做return,结果已经被remove掉的元素无法return。后加了个中间变量先暂存了一下要return的card。

4.感觉原来的OC语法比swift复杂很多。

5.感觉MVC部分CS193P的老头讲得挺好的。

 

代码:

首先是测试类:

 1 // 2 //  CardGameTests.swift 3 //  CardGameTests 4 // 5 //  Created by colin.zt on 14-6-6. 6 //  Copyright (c) 2014年 Ting. All rights reserved. 7 // 8  9 import XCTest10 11 class CardGameTests: XCTestCase {12     13     override func setUp() {14         super.setUp()15         // Put setup code here. This method is called before the invocation of each test method in the class.16     }17     18     override func tearDown() {19         // Put teardown code here. This method is called after the invocation of each test method in the class.20         super.tearDown()21     }22     23     func testCard() {24         // This is an example of a functional test case.25         // XCTAssert(true, "Pass")26         var a: Card = Card()27         var b: Card = Card()28         a.contents = "test card a"29         b.contents = "test card b"30         var cards: Card[] = [a,b]31         var result = a.match(cards)32       //  println(a.contents)33         XCTAssert(result == 1, "Pass")34     }35     36     func testDeck() {37      38         var deck_a:Deck = Deck()39        40         var card_a:Card = Card()41         var card_b:Card = Card()42         var card_c:Card = Card()43         44         card_a.contents = "card a"45         card_b.contents = "card b"46         card_c.contents = "card c"47         48         deck_a.addCard(card_a)49         pirnt_all_cards_left(deck_a)50         XCTAssert(deck_a.cards[0].contents == "card a", "Pass")51         52         deck_a.addCard(card_b)53         pirnt_all_cards_left(deck_a)54         XCTAssert(deck_a.cards[0].contents == "card a", "Pass")55         XCTAssert(deck_a.cards[1].contents == "card b", "Pass")56         57         deck_a.addCard(card_c,atTop:true)58         pirnt_all_cards_left(deck_a)59         XCTAssert(deck_a.cards[0].contents == "card c", "Pass")60         XCTAssert(deck_a.cards[1].contents == "card a", "Pass")61         XCTAssert(deck_a.cards[2].contents == "card b", "Pass")62         63         var random_card: Card = deck_a.drawRandomCard()64         println("Now draw this random card:")65         println(random_card.contents)66         pirnt_all_cards_left(deck_a)67         XCTAssert(random_card.match(deck_a.cards) == 0, "Pass")68         XCTAssert(deck_a.cards.count == 2)69         70         var random_card_2: Card = deck_a.drawRandomCard()71         println("Now draw this random card_2:")72         println(random_card_2.contents)73         pirnt_all_cards_left(deck_a)74         XCTAssert(random_card_2.match(deck_a.cards) == 0, "Pass")75         XCTAssert(deck_a.cards.count == 1)76     }77     78     func pirnt_all_cards_left(deck:Deck){79         println("_______________________________________")80         println("Now these cards in deck")81         for card in deck.cards{82             println(card.contents)83         }84         println("_______________________________________")85     }86     87     func testPerformanceExample() {88         // This is an example of a performance test case.89         self.measureBlock() {90             // Put the code you want to measure the time of here.91         }92     }93     94 }
View Code

然后是Card类

 1 // 2 //  Card.swift 3 //  CardGame 4 // 5 //  Created by colin.zt on 14-10-18. 6 //  Copyright (c) 2014年 Ting. All rights reserved. 7 // 8  9 import UIKit10 11 class Card: NSObject {12     13     init(){}  //empty init method14     var contents: String = "" //give it a default value15     var chosen: Bool = false16     var matched: Bool = false17     18     //check if some card mathes some cards19     func match(cards: Card[])->Int{        20         var score = 021         for card in cards{22             if card.contents == self.contents23             {24                 score = 125             }26         }27         return score28     }29     30     31 }

最后是Deck类

 1 // 2 //  Deck.swift 3 //  CardGame 4 // 5 //  Created by colin.zt on 14-10-18. 6 //  Copyright (c) 2014年 Ting. All rights reserved. 7 // 8  9 import UIKit10 11 class Deck: NSObject {12     13     init(){} //init14     var cards: Card[] = [] //a deck has an array of cards, empty by default15     16     //add a card into this deck‘s cards array17     func addCard(card: Card){18         self.addCard(card,atTop:false)19     }20     21     //add a card into this decks‘s cards array‘s top, first element22     func addCard(card:Card,atTop:Bool){23         if atTop {24             cards.insert(card, atIndex: 0)25         }26         else{27             cards.append(card)28         }29     }30     31     //draw a card from the deck randomly, it removes the card32     func drawRandomCard()->Card{33         var index = Int(arc4random()) % self.cards.count34         var random_card:Card = cards[index]35         cards.removeAtIndex(index)36         return random_card37     }38 }

用swift重写stanford CS193P的纸牌游戏 (1)- Card 和Deck