Question: In Swift(Xcode), Can you add one more player to the Word Guessing Game in this code. Here are some hints to write code. - The
In Swift(Xcode), Can you add one more player to the Word Guessing Game in this code. Here are some hints to write code.
- The player takes turn to play the game
- We start with player 1, when one round is finished (either player 1 guessed the word correct or incorrect), we start new round and player 2 will guess the word
- Each player has equal number of words to guess
- When the words are running out, use Alert to indicate the winner
- For the UI (Main.storyboard), you can just add a label to indicate which player is currently playing. Or you like to add more view/control to enhance the UI.
- The logic for single player is working. For the 2nd player, you can just add another set of variables and call them in your function. Or if you have new ideas, you can implement them, as long as they are working.
import UIKit
class ViewController: UIViewController {
@IBOutlet var buttonLetters: [UIButton]!
@IBOutlet weak var labelCorrectWord: UILabel!
@IBOutlet weak var labelScore: UILabel!
@IBOutlet weak var imageViewTree: UIImageView!
@IBAction func buttonTapped(_ sender: UIButton) {
sender.isEnabled = false
let letterString = sender.title(for: .normal)!
let letterGuessed = Character(letterString.lowercased())
currentGame.playerGuessed(letter: letterGuessed)
//updateUI()
updateGameState()
}
func updateGameState(){
if currentGame.remainingIncorrectMoves == 0 {
totalLosses += 1
} else if currentGame.word == currentGame.formattedWord {
totalWins += 1
} else {
updateUI()
}
}
//variables
var wordList: [String] = ["swift","ruby","python",]
let incorrectMovesAllowed = 7
var totalWins = 0 {
didSet {
newRound()
}
}
var totalLosses = 0 {
didSet {
newRound()
}
}
var currentGame: Game!
func newRound(){
if !wordList.isEmpty{
let newWord = wordList.removeFirst()
currentGame = Game(word: newWord, remainingIncorrectMoves: incorrectMovesAllowed, guessedLetters: [])
updateUI()
enableLetterButtons(true)
} else {
showAlert(msg: "Game is over!", title: "Wins: \(totalWins) Losses:\(totalLosses)")
labelCorrectWord.text = ""
labelScore.text = ""
imageViewTree.image = UIImage(named: "Tree 0")
enableLetterButtons(false)
}
}
func showAlert(msg: String, title: String)
{
let alert = UIAlertController(title: msg, message: title, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)
}
func enableLetterButtons (_ enable: Bool){
for button in buttonLetters {
button.isEnabled = enable
}
}
func updateUI(){
labelCorrectWord.text = currentGame.formattedWord
labelScore.text = "Wins: \(totalWins) Losses:\(totalLosses)"
imageViewTree.image = UIImage(named: "Tree \(currentGame.remainingIncorrectMoves)")
}
override func viewDidLoad() {
super.viewDidLoad()
newRound()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
