-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardController.swift
73 lines (66 loc) · 2.62 KB
/
BoardController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// BoardController.swift
// Wordle
//
// Created by Mari Batilando on 2/20/23.
//
import Foundation
import UIKit
class BoardController: NSObject,
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout {
// MARK: - Properties
let numItemsPerRow = 5
let numRows = 6
let collectionView: UICollectionView
let goalWord: [String]
var numGuesses = 0
var currRow: Int {
return numGuesses / numItemsPerRow
}
init(collectionView: UICollectionView) {
self.collectionView = collectionView
self.goalWord = WordGenerator.generateRandomWord()!.map { String($0) }
super.init()
collectionView.delegate = self
collectionView.dataSource = self
}
// MARK: - Public Methods
func enter(_ string: String) {
guard numGuesses < numItemsPerRow * numRows else { return }
let cell = collectionView.cellForItem(at: IndexPath(item: numGuesses, section: 0)) as! LetterCell
cell.set(letter: string)
UIView.animate(withDuration: 0.1,
delay: 0.0,
options: [.autoreverse],
animations: {
// Exercise 7: Change the scale of the cell by 1.05
// Tip: Use the transform property of the cell. Use transform.scaledBy to modify the scale. This should feel familiar to lab 1.
// Checkpoint: After finishing this exercise, you should now be able to see that the board animates whenever you enter a new letter! If it's not animating, check your work on this exercise.
// START YOUR CODE HERE
// ...
cell.transform.scaledBy(x: 1.05, y: 1.05)
// END YOUR CODE HERE
}, completion: { finished in
cell.transform = CGAffineTransformIdentity
})
if isFinalGuessInRow() {
markLettersInRow()
}
numGuesses += 1
}
func deleteLastCharacter() {
guard numGuesses > 0 && numGuesses % numItemsPerRow != 0 else { return }
let cell = collectionView.cellForItem(at: IndexPath(item: numGuesses - 1, section: 0)) as! LetterCell
numGuesses -= 1
// Exercise 6: Look at the available LetterCell's methods to clear the letter and set the style to `initial`
// Tip: Checkout the public methods on LetterCell.swift
// Checkpoint: After finishing this exercise, you should now be able to tap on the delete keyboard cell and have the last letter deleted on the board! If it's not working, check your work on this exercise and make sure deleteLastCharacter() is called properly in exercise 3.
// START YOUR CODE HERE
cell.clearLetter()
cell.set(style: .initial)
// ...
// END YOUR CODE HERE
}
}