Skip to content
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

Score updates #7

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Yamb/TopBottomDiceSelectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TopBottomDiceSelectionViewController: UIViewController, UICollectionViewDa
lazy var clearButton: ClearDoneButton = {
var button = ClearDoneButton()
button.setTitle("Clear", for: .normal)
button.addTarget(self, action: #selector(onCancel), for: .touchUpInside)
button.addTarget(self, action: #selector(onClear), for: .touchUpInside)
return button
}()

Expand Down Expand Up @@ -135,7 +135,7 @@ class TopBottomDiceSelectionViewController: UIViewController, UICollectionViewDa
field?.hasStar = sender.isSelected
}

@objc func onCancel(_ sender: Any) {
@objc func onClear(_ sender: Any) {
delegate?.didClear(indexPath: field?.indexPath)
dismiss(animated: true) {
self.delegate?.didDismiss()
Expand Down
28 changes: 26 additions & 2 deletions Yamb/YambDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ class YambDataSource {
}()

var totalScore: Int {
get {
return getTotalScore()
}
}

private func getTotalScore() -> Int {
var sumTop = 0
var sumMiddle = 0
var sumBottom = 0

fieldsDict[0]?.filter { $0.type == .Result }.forEach {
sumTop += $0.score ?? 0
}
Expand All @@ -103,8 +109,9 @@ class YambDataSource {
fieldsDict[2]?.filter { $0.type == .Yamb && $0.hasStar }.forEach {_ in
sumTop += 50
}

return sumTop + sumMiddle + sumBottom

}

var isGameEnded: Bool {
Expand Down Expand Up @@ -149,6 +156,7 @@ class YambDataSource {
let fields = fields.filter { $0.column == column && $0.type != .Result }
for field in fields {
guard let score = field.score else { return 0 }
if score == 0 {return 0}
sum = field.row == .min ? (sum - score) : (sum + score)
}

Expand Down Expand Up @@ -178,6 +186,20 @@ class YambDataSource {
updateEnabledFields()
}

func clearedScore(indexPath: IndexPath) -> Int {
var result = 0
if let field = fieldsDict[indexPath.section]?[indexPath.item] {
if let score = field.score {
result = score
if (field.hasStar) {
result += 50
}
}

}
return result
}

func loadScores() {
if let fields = userDefaults.value(forKey: kScoreDictKey) as? Data,
let decodedFields = try? PropertyListDecoder().decode([Int: [Field]].self, from: fields) {
Expand All @@ -199,6 +221,7 @@ class YambDataSource {

func clear(indexPath: IndexPath) {
if let field = fieldsDict[indexPath.section]?[indexPath.item] {
setScore(diceRolls: [], indexPath: indexPath, hasStar: false)
field.score = nil
field.hasStar = false
lastPlayedField = field
Expand Down Expand Up @@ -321,4 +344,5 @@ class Field: Equatable, Codable {
self.indexPath = indexPath
self.isEnabled = true
}

}
29 changes: 17 additions & 12 deletions Yamb/YambViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class YambViewController: UIViewController, UICollectionViewDataSource, UICollec

lazy var totalScoreLabel: UILabel = {
var label = UILabel()
label.text = "Total: "
label.text = "Total: \(dataSource.totalScore)"
return label
}()

Expand Down Expand Up @@ -51,15 +51,17 @@ class YambViewController: UIViewController, UICollectionViewDataSource, UICollec
}()

lazy var topStack: UIStackView = {
var stackView = UIStackView(arrangedSubviews: [button, UIView(), totalScoreLabel])
var stackView = UIStackView(arrangedSubviews: [newGameButton, UIView(), totalScoreLabel])
stackView.axis = .horizontal
stackView.spacing = 10
stackView.spacing = 20
stackView.distribution = .fill
stackView.alignment = .fill
stackView.isLayoutMarginsRelativeArrangement = true
stackView.layoutMargins = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 20)
return stackView
}()

lazy var button: UIButton = {
lazy var newGameButton: UIButton = {
var b = UIButton()
b.setTitle("New Game", for: .normal)
b.backgroundColor = .systemBlue
Expand Down Expand Up @@ -110,16 +112,14 @@ class YambViewController: UIViewController, UICollectionViewDataSource, UICollec

view.backgroundColor = .white
view.addSubview(contentStack)

contentStack.snp.makeConstraints { make in
make.edges.equalTo(view.safeAreaLayoutGuide)
}

yambCollectionView.snp.makeConstraints { make in
make.right.equalTo(view.safeAreaLayoutGuide).inset(yambPadding)
}

//self.navigationController?.navigationBar.isHidden = true

}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Expand Down Expand Up @@ -180,23 +180,28 @@ class YambViewController: UIViewController, UICollectionViewDataSource, UICollec

func didSelect(_ diceRolls: [DiceRoll], indexPath: IndexPath?, hasStar: Bool) {
guard let indexPath = indexPath else { return }



dataSource.setScore(diceRolls: diceRolls, indexPath: indexPath, hasStar: hasStar)
yambCollectionView.reloadData()
totalScoreLabel.text = "Total: \(dataSource.totalScore)"
totalScoreLabel.text = "Total: \(self.dataSource.totalScore)"
}



func didClear(indexPath: IndexPath?) {
guard let indexPath = indexPath else { return }
dataSource.clear(indexPath: indexPath)
yambCollectionView.reloadData()
totalScoreLabel.text = "Total: \(dataSource.totalScore)"
dataSource.clear(indexPath: indexPath)
totalScoreLabel.text = "Total: \(self.dataSource.totalScore)"
}

@objc func onNewGame(_ sender: Any) {
let alert = UIAlertController(title: "Are you sure you want to abandon the current game and start a new one?", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak self] _ in
guard let self = self else { return }

self.dataSource.resetScores()
self.yambCollectionView.reloadData()
self.totalScoreLabel.text = "Total: \(self.dataSource.totalScore)"
Expand All @@ -207,7 +212,7 @@ class YambViewController: UIViewController, UICollectionViewDataSource, UICollec

func didDismiss() {
if dataSource.isGameEnded {
let alert = UIAlertController(title: "GAME OVER", message: "TOTAL SCORE: \(dataSource.totalScore)", preferredStyle: .alert)
let alert = UIAlertController(title: "GAME OVER", message: "TOTAL SCORE: \(self.dataSource.totalScore)", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "New game", style: .default, handler: { _ in
self.dataSource.resetScores()
self.yambCollectionView.reloadData()
Expand Down