Giter Club home page Giter Club logo

swiftui-cs193p-spring2020's Introduction

SwiftUI-cs193p-Spring2020

Here I will upload solutions for assignments of cs193p 2020

Assignment 1

Assignment 1

2

We can shuffle cards in our init of our Model MemoryGame.

    init(numberOfPairsOfCards: Int, cardContentFactory: (Int) -> CardContent) {
        cards = Array<Card>()
        for pairIndex in 0..<numberOfPairsOfCards {
            let content: CardContent = cardContentFactory(pairIndex)
            cards.append(Card(content: content, id: pairIndex*2))
            cards.append(Card(content: content, id: pairIndex*2+1))
        }
        cards.shuffle() //Here we can shuffle the cards
    }

In swift we have a built-in method called shuffle(), and he is shuffling elements.

3

Changing ratio surely is a UI thing, so we need to change something in our View. Luckily, we have a built-in method called aspectRatio(), so we need to use it

HStack {
            return ForEach(viewModel.cards){ card in
                CardView(card: card).onTapGesture {
                    self.viewModel.choose(card: card)
                    
                }
                .aspectRatio(2/3, contentMode: .fit) // Here the aspectRatio() Usage
            }
        }

4

We are returning numberOfPairsOfCards in the EmojiMemoryGame in the function called createMemoryGame(), so all what we need is generate random number from 2 to 5 and set it as numberOfPairsOfCards.

return MemoryGame<String>(numberOfPairsOfCards: .random(in: 2...5)) { pairIndex in emojis[pairIndex]} 
//Take note at .random(in: 2...5) here we're generating numberOfPairsOfCards 

5

Changing fonts is UI thing,so we need to change something in our View. I decided to solve this problem in one line, and used ternary operator for that.

struct ContentView: View {
    
    var viewModel: EmojiMemoryGame
    
    var body: some View {
        HStack {
            return ForEach(viewModel.cards){ card in
                CardView(card: card).onTapGesture {
                    self.viewModel.choose(card: card)
                    
                }
                .aspectRatio(2/3, contentMode: .fit)
            }
        }
        .font(viewModel.cards.count ==  5 ? .largeTitle : .body) // Ternary operator here.
        .padding()
        .foregroundColor(Color.orange)
        
    }
}

Extra credit

I think that one of the ways to solve this is to change the emojis array in the EmojiMemoryGame. My solution is deleting some emojis.

while emojis.count > 5 {
            emojis.remove(at: emojis.firstIndex(of: emojis.randomElement()!)!)
        }

Thanks, if you found any mistake here, please report it

swiftui-cs193p-spring2020's People

Contributors

camotsuc avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

swiftui-cs193p-spring2020's Issues

Assignments

Hey there,
it is nice to compare solutions.
Currently I am stuck in assignment 2, I consider the assignment a bit too unspecific and confusing.
Having trouble to interpret what exactly is desired.
How about you? Any chance we could discuss the assignment?

Best regards...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.