From 9157d0d1c7f7ab8f403f0ab94c1711a4bb6bb5d7 Mon Sep 17 00:00:00 2001 From: Geoffrey Liu Date: Wed, 9 Mar 2022 21:22:28 -0800 Subject: [PATCH 1/4] Infinite mode to respect the max guesses allowed. #15 Also fixes keyboard state again, hopefully for real this time. #18. --- .../ClueGuessViewController.swift | 6 +-- .../Models/GameGuessesModel.swift | 2 +- .../WordleKeyboardInputView.swift | 48 +++++++++---------- 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/WordleWithFriends/ClueGuessViewController.swift b/WordleWithFriends/ClueGuessViewController.swift index 0275d96..6e6addb 100644 --- a/WordleWithFriends/ClueGuessViewController.swift +++ b/WordleWithFriends/ClueGuessViewController.swift @@ -244,11 +244,7 @@ extension ClueGuessViewController: UITableViewDelegate, UITableViewDataSource { 1 } - func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - if gameGuessesModel.gamemode == .infinite { - return gameGuessesModel.numberOfGuesses + 1 - } - + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return GameSettings.maxGuesses.readIntValue() } diff --git a/WordleWithFriends/Models/GameGuessesModel.swift b/WordleWithFriends/Models/GameGuessesModel.swift index 18d33f1..ab15cfb 100644 --- a/WordleWithFriends/Models/GameGuessesModel.swift +++ b/WordleWithFriends/Models/GameGuessesModel.swift @@ -50,7 +50,7 @@ struct GameGuessesModel { if didGuessCorrectly { isGameOver = true return .win - } else if gamemode != .infinite && letterGuesses.count > GameSettings.maxGuesses.readIntValue() { + } else if letterGuesses.count > GameSettings.maxGuesses.readIntValue() { isGameOver = true return .lose } else { diff --git a/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift b/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift index ad23f41..d75e586 100644 --- a/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift +++ b/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift @@ -24,25 +24,23 @@ final class WordleKeyboardInputView: UIInputView { private var keyReferences: [WeakRef] = [] private weak var forfeitKey: WordleKeyboardKey? - private let keyboardLayout: [[WordleKeyboardKey]] = { - let characterRows = ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"] - var keyRows = characterRows.map { row in - row.map { - WordleKeyboardKey(keyType: .char($0)) - } - } - keyRows[keyRows.count-1].prepend(WordleKeyboardKey(keyType: .submit)) - keyRows[keyRows.count-1].append(WordleKeyboardKey(keyType: .del)) - - return keyRows - }() - var delegate: KeyTapDelegate? { didSet { setupKeyboard() } } + private lazy var mainStackView: UIStackView = { + let stackView = UIStackView() + stackView.translatesAutoresizingMaskIntoConstraints = false + stackView.alignment = .center + stackView.axis = .vertical + stackView.distribution = .fill + stackView.spacing = Layout.rowSpacing + + return stackView + }() + private let gamemode: GameMode init(gamemode: GameMode) { @@ -54,20 +52,22 @@ final class WordleKeyboardInputView: UIInputView { fatalError("init(coder:) has not been implemented") } - private lazy var mainStackView: UIStackView = { - let stackView = UIStackView() - stackView.translatesAutoresizingMaskIntoConstraints = false - stackView.alignment = .center - stackView.axis = .vertical - stackView.distribution = .fill - stackView.spacing = Layout.rowSpacing + private func getKeyboardLayout() -> [[WordleKeyboardKey]] { + let characterRows = ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"] + var keyRows = characterRows.map { row in + row.map { + WordleKeyboardKey(keyType: .char($0)) + } + } + keyRows[keyRows.count-1].prepend(WordleKeyboardKey(keyType: .submit)) + keyRows[keyRows.count-1].append(WordleKeyboardKey(keyType: .del)) - return stackView - }() + return keyRows + } func getPortraitModeKeyWidth() -> CGFloat { let keyboardWidth = UIScreen.main.bounds.width - let keyboardRowKeyWidths = keyboardLayout.enumerated().map { index, row -> CGFloat in + let keyboardRowKeyWidths = getKeyboardLayout().enumerated().map { index, row -> CGFloat in let totalSpace = row.reduce(0) { res, key in switch key.keyType { case .char(_), .forfeit(_), .mainMenu: @@ -112,7 +112,7 @@ final class WordleKeyboardInputView: UIInputView { let keyWidth = getPortraitModeKeyWidth() - keyboardLayout.enumerated().forEach { index, row in + getKeyboardLayout().enumerated().forEach { index, row in let keyboardRow = KeyboardRow() keyboardRow.delegate = delegate let keyRowRefs = keyboardRow.configure(keys: row, keyWidth: keyWidth) From a83a63e837f14765434456dba3a6d3fe0fcccf32 Mon Sep 17 00:00:00 2001 From: Geoffrey Liu Date: Wed, 9 Mar 2022 21:28:22 -0800 Subject: [PATCH 2/4] Main menu button to be enabled for all gamemodes. #20 --- .../Views/CustomKeyboard/WordleKeyboardInputView.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift b/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift index d75e586..b00aba8 100644 --- a/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift +++ b/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift @@ -134,11 +134,7 @@ final class WordleKeyboardInputView: UIInputView { let operationKeysRow = KeyboardRow() operationKeysRow.delegate = delegate - if gamemode == .infinite { - operationKeysRow.configure(keys: [forfeitKey, mainMenuKey], keyWidth: keyWidth) - } else { - operationKeysRow.configure(keys: [forfeitKey], keyWidth: keyWidth) - } + operationKeysRow.configure(keys: [forfeitKey, mainMenuKey], keyWidth: keyWidth) mainStackView.addArrangedSubview(operationKeysRow) From b5c164b7974938131427b5f703008485cc1bc79a Mon Sep 17 00:00:00 2001 From: Geoffrey Liu Date: Wed, 9 Mar 2022 21:37:35 -0800 Subject: [PATCH 3/4] Main menu confirmation dlg --- .../GameMessagingViewController.swift | 5 +++-- WordleWithFriends/ClueGuessViewController.swift | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/WordleWithFriends/Child ViewControllers/GameMessagingViewController.swift b/WordleWithFriends/Child ViewControllers/GameMessagingViewController.swift index 4de0c3c..e790d69 100644 --- a/WordleWithFriends/Child ViewControllers/GameMessagingViewController.swift +++ b/WordleWithFriends/Child ViewControllers/GameMessagingViewController.swift @@ -46,11 +46,12 @@ final class GameMessagingViewController: UIViewController { alertController.addAction(shareButton) alertController.addAction(mainMenuButton) + mainMenuButton.setValue("Main menu", forKeyPath: "title") + switch gamemode { case .human: - mainMenuButton.setValue("Main menu", forKeyPath: "title") + break case .computer: - mainMenuButton.setValue("Main menu", forKeyPath: "title") alertController.addAction(newClueButton) case .infinite: break // Infinite game mode never touches this VC diff --git a/WordleWithFriends/ClueGuessViewController.swift b/WordleWithFriends/ClueGuessViewController.swift index 6e6addb..c07c44a 100644 --- a/WordleWithFriends/ClueGuessViewController.swift +++ b/WordleWithFriends/ClueGuessViewController.swift @@ -244,7 +244,7 @@ extension ClueGuessViewController: UITableViewDelegate, UITableViewDataSource { 1 } - func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return GameSettings.maxGuesses.readIntValue() } @@ -380,6 +380,12 @@ extension ClueGuessViewController: KeyTapDelegate { } func didTapMainMenu() { - navigationController?.popViewController(animated: true) + let alertController = DismissableAlertController(title: nil, message: "Return to main menu?", preferredStyle: .alert) + alertController.addAction(.init(title: "Yes", style: .default, handler: { [weak self] _ in + self?.navigationController?.popViewController(animated: true) + })) + alertController.addAction(.init(title: "Cancel", style: .cancel, handler: nil)) + + present(alertController, animated: true) } } From a552d350cde2b7f192f4f7f5f43d487d9a28286b Mon Sep 17 00:00:00 2001 From: Geoffrey Liu Date: Wed, 9 Mar 2022 21:41:02 -0800 Subject: [PATCH 4/4] Make keyboard layout a computed var --- .../WordleKeyboardInputView.swift | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift b/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift index b00aba8..e663dd2 100644 --- a/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift +++ b/WordleWithFriends/Views/CustomKeyboard/WordleKeyboardInputView.swift @@ -24,6 +24,19 @@ final class WordleKeyboardInputView: UIInputView { private var keyReferences: [WeakRef] = [] private weak var forfeitKey: WordleKeyboardKey? + private var keyboardLayout: [[WordleKeyboardKey]] { + let characterRows = ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"] + var keyRows = characterRows.map { row in + row.map { + WordleKeyboardKey(keyType: .char($0)) + } + } + keyRows[keyRows.count-1].prepend(WordleKeyboardKey(keyType: .submit)) + keyRows[keyRows.count-1].append(WordleKeyboardKey(keyType: .del)) + + return keyRows + } + var delegate: KeyTapDelegate? { didSet { setupKeyboard() @@ -52,22 +65,9 @@ final class WordleKeyboardInputView: UIInputView { fatalError("init(coder:) has not been implemented") } - private func getKeyboardLayout() -> [[WordleKeyboardKey]] { - let characterRows = ["QWERTYUIOP", "ASDFGHJKL", "ZXCVBNM"] - var keyRows = characterRows.map { row in - row.map { - WordleKeyboardKey(keyType: .char($0)) - } - } - keyRows[keyRows.count-1].prepend(WordleKeyboardKey(keyType: .submit)) - keyRows[keyRows.count-1].append(WordleKeyboardKey(keyType: .del)) - - return keyRows - } - func getPortraitModeKeyWidth() -> CGFloat { let keyboardWidth = UIScreen.main.bounds.width - let keyboardRowKeyWidths = getKeyboardLayout().enumerated().map { index, row -> CGFloat in + let keyboardRowKeyWidths = keyboardLayout.enumerated().map { index, row -> CGFloat in let totalSpace = row.reduce(0) { res, key in switch key.keyType { case .char(_), .forfeit(_), .mainMenu: @@ -112,7 +112,7 @@ final class WordleKeyboardInputView: UIInputView { let keyWidth = getPortraitModeKeyWidth() - getKeyboardLayout().enumerated().forEach { index, row in + keyboardLayout.enumerated().forEach { index, row in let keyboardRow = KeyboardRow() keyboardRow.delegate = delegate let keyRowRefs = keyboardRow.configure(keys: row, keyWidth: keyWidth)