Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I recently posted my Hangman gameHangman game, and got some amazing feedback! The main thing suggested was to create a word generator that could read JSON files so that my hangman brain class didn't have to generate the word used on top of handling gameplay. To do this, I created the HangmanWordGenerator class that parses JSON and generates a word for the HangmanBrain class. Before, the HangmanBrain used a set "database" in the form of three arrays (one for each difficulty) and picked a word itself. I am wondering how I did in implementing these improvements.

I recently posted my Hangman game, and got some amazing feedback! The main thing suggested was to create a word generator that could read JSON files so that my hangman brain class didn't have to generate the word used on top of handling gameplay. To do this, I created the HangmanWordGenerator class that parses JSON and generates a word for the HangmanBrain class. Before, the HangmanBrain used a set "database" in the form of three arrays (one for each difficulty) and picked a word itself. I am wondering how I did in implementing these improvements.

I recently posted my Hangman game, and got some amazing feedback! The main thing suggested was to create a word generator that could read JSON files so that my hangman brain class didn't have to generate the word used on top of handling gameplay. To do this, I created the HangmanWordGenerator class that parses JSON and generates a word for the HangmanBrain class. Before, the HangmanBrain used a set "database" in the form of three arrays (one for each difficulty) and picked a word itself. I am wondering how I did in implementing these improvements.

deleted 510 characters in body
Source Link
Jared
  • 711
  • 1
  • 7
  • 20
import Foundation
import Darwin

//MARK: - Global Enum

enum DifficultyLevel: String {
case Easy = "Easy"
case Medium = "Medium"
case Hard = "Hard"
}

class HangmanBrain {

init() {
    //not sure what to do here? without an empty init xcode says I have to use the one below
}

init(word: String) {
    self.word = word //when the user inputs a word
}

//MARK: - Database of Words

private struct Words {
    static let Easy: [String] = ["fireplace","apple","january","tooth","cookies","mysterious","essential","magenta","darling","pterodactyl"]
    static let Medium: [String] = ["palace","thumb","eleven","monkey","hunter","wounds","wright","egypt","slaves","zipper"]
    static let Hard: [String] = ["jazz","puff","jiff","sphinx","vex","pox","hajj","jinx","vine","mom"]
    static let numberOfWordsPerLevel = 10
}

//MARK: - Properties

var level: DifficultyLevel? {
    didSet {
        wordGenerator = HangmanWordGenerator(level: level!)
        word = wordGenerator!.generateWord()
        setNumberOfGuesses(level: level!)
    }
}

var wordGenerator : HangmanWordGenerator? = nil

var guesses: Int? = nil

var word: String? = nil {
    didSet {
        makeNewGameWord()
    }
}

var gameWord = ""

private let wordsByLevel : [String: [String]] = ["Easy": Words.Easy, "Medium": Words.Medium, "Hard": Words.Hard]

//MARK: - Gameplay Functions

private func makeNewGameWord() {
    gameWord = ""
    for _ in word!.characters {
        createGameWord(character: "_")
    }
}

private func setNumberOfGuesses(level level: DifficultyLevel) {
    switch level.rawValue {
    case "Easy": guesses = 10
    case "Medium": guesses = 8
    case "Hard": guesses = 6
    default: break
    }
}

func checkGuessAndUpdateGameWordAndGuesses(character character: String) {
    var guessIsCorrect = false
    let answer = word!
    let currentWord = gameWord as String
    gameWord = ""
    let currentWordTrimmed = currentWord.stringByReplacingOccurrencesOfString(" ", withString: "")
    let numberOfLetters = answer.characters.count as Int
    for i in 0...numberOfLetters-1 {
        let start = advance(currentWordTrimmed.startIndex, i)
        let end = advance(currentWordTrimmed.startIndex, i+1)
        let subCurrentWord = currentWordTrimmed.substringWithRange(Range<String.Index>(start: start, end: end))
        if subCurrentWord != "_" {
            createGameWord(character: subCurrentWord)
        } else {
            let subAnswer = answer.substringWithRange(Range<String.Index>(start: start, end: end))
            if subAnswer == character.lowercaseString {
                guessIsCorrect = true
                createGameWord(character: subAnswer)
            } else {
                createGameWord(character: "_")
            }
        }
    }
    if(!guessIsCorrect) {
        guesses = guesses! - 1
    }
}

func buildCorrectWord() {
    gameWord = ""
    for c in word!.characters {
        createGameWord(character: "\(c)")
    }
}

func createGameWord(character character: String) {
    gameWord += "\(character) "
}

func theUserWon() -> Bool {
    for ch in gameWord.characters {
        if "\(ch)" == "_" {
            return false
        }
    }
    return true
}

func theUserLost() -> Bool {
    return guesses == 0
}
}
import Foundation
import Darwin

//MARK: - Global Enum

enum DifficultyLevel: String {
case Easy = "Easy"
case Medium = "Medium"
case Hard = "Hard"
}

class HangmanBrain {

init() {
    //not sure what to do here? without an empty init xcode says I have to use the one below
}

init(word: String) {
    self.word = word //when the user inputs a word
}

//MARK: - Database of Words

private struct Words {
    static let Easy: [String] = ["fireplace","apple","january","tooth","cookies","mysterious","essential","magenta","darling","pterodactyl"]
    static let Medium: [String] = ["palace","thumb","eleven","monkey","hunter","wounds","wright","egypt","slaves","zipper"]
    static let Hard: [String] = ["jazz","puff","jiff","sphinx","vex","pox","hajj","jinx","vine","mom"]
    static let numberOfWordsPerLevel = 10
}

//MARK: - Properties

var level: DifficultyLevel? {
    didSet {
        wordGenerator = HangmanWordGenerator(level: level!)
        word = wordGenerator!.generateWord()
        setNumberOfGuesses(level: level!)
    }
}

var wordGenerator : HangmanWordGenerator? = nil

var guesses: Int? = nil

var word: String? = nil {
    didSet {
        makeNewGameWord()
    }
}

var gameWord = ""

private let wordsByLevel : [String: [String]] = ["Easy": Words.Easy, "Medium": Words.Medium, "Hard": Words.Hard]

//MARK: - Gameplay Functions

private func makeNewGameWord() {
    gameWord = ""
    for _ in word!.characters {
        createGameWord(character: "_")
    }
}

private func setNumberOfGuesses(level level: DifficultyLevel) {
    switch level.rawValue {
    case "Easy": guesses = 10
    case "Medium": guesses = 8
    case "Hard": guesses = 6
    default: break
    }
}

func checkGuessAndUpdateGameWordAndGuesses(character character: String) {
    var guessIsCorrect = false
    let answer = word!
    let currentWord = gameWord as String
    gameWord = ""
    let currentWordTrimmed = currentWord.stringByReplacingOccurrencesOfString(" ", withString: "")
    let numberOfLetters = answer.characters.count as Int
    for i in 0...numberOfLetters-1 {
        let start = advance(currentWordTrimmed.startIndex, i)
        let end = advance(currentWordTrimmed.startIndex, i+1)
        let subCurrentWord = currentWordTrimmed.substringWithRange(Range<String.Index>(start: start, end: end))
        if subCurrentWord != "_" {
            createGameWord(character: subCurrentWord)
        } else {
            let subAnswer = answer.substringWithRange(Range<String.Index>(start: start, end: end))
            if subAnswer == character.lowercaseString {
                guessIsCorrect = true
                createGameWord(character: subAnswer)
            } else {
                createGameWord(character: "_")
            }
        }
    }
    if(!guessIsCorrect) {
        guesses = guesses! - 1
    }
}

func buildCorrectWord() {
    gameWord = ""
    for c in word!.characters {
        createGameWord(character: "\(c)")
    }
}

func createGameWord(character character: String) {
    gameWord += "\(character) "
}

func theUserWon() -> Bool {
    for ch in gameWord.characters {
        if "\(ch)" == "_" {
            return false
        }
    }
    return true
}

func theUserLost() -> Bool {
    return guesses == 0
}
}
import Foundation
import Darwin

//MARK: - Global Enum

enum DifficultyLevel: String {
case Easy = "Easy"
case Medium = "Medium"
case Hard = "Hard"
}

class HangmanBrain {

init() {
    //not sure what to do here? without an empty init xcode says I have to use the one below
}

init(word: String) {
    self.word = word //when the user inputs a word
}

//MARK: - Properties

var level: DifficultyLevel? {
    didSet {
        wordGenerator = HangmanWordGenerator(level: level!)
        word = wordGenerator!.generateWord()
        setNumberOfGuesses(level: level!)
    }
}

var wordGenerator : HangmanWordGenerator? = nil

var guesses: Int? = nil

var word: String? = nil {
    didSet {
        makeNewGameWord()
    }
}

var gameWord = ""

//MARK: - Gameplay Functions

private func makeNewGameWord() {
    gameWord = ""
    for _ in word!.characters {
        createGameWord(character: "_")
    }
}

private func setNumberOfGuesses(level level: DifficultyLevel) {
    switch level.rawValue {
    case "Easy": guesses = 10
    case "Medium": guesses = 8
    case "Hard": guesses = 6
    default: break
    }
}

func checkGuessAndUpdateGameWordAndGuesses(character character: String) {
    var guessIsCorrect = false
    let answer = word!
    let currentWord = gameWord as String
    gameWord = ""
    let currentWordTrimmed = currentWord.stringByReplacingOccurrencesOfString(" ", withString: "")
    let numberOfLetters = answer.characters.count as Int
    for i in 0...numberOfLetters-1 {
        let start = advance(currentWordTrimmed.startIndex, i)
        let end = advance(currentWordTrimmed.startIndex, i+1)
        let subCurrentWord = currentWordTrimmed.substringWithRange(Range<String.Index>(start: start, end: end))
        if subCurrentWord != "_" {
            createGameWord(character: subCurrentWord)
        } else {
            let subAnswer = answer.substringWithRange(Range<String.Index>(start: start, end: end))
            if subAnswer == character.lowercaseString {
                guessIsCorrect = true
                createGameWord(character: subAnswer)
            } else {
                createGameWord(character: "_")
            }
        }
    }
    if(!guessIsCorrect) {
        guesses = guesses! - 1
    }
}

func buildCorrectWord() {
    gameWord = ""
    for c in word!.characters {
        createGameWord(character: "\(c)")
    }
}

func createGameWord(character character: String) {
    gameWord += "\(character) "
}

func theUserWon() -> Bool {
    for ch in gameWord.characters {
        if "\(ch)" == "_" {
            return false
        }
    }
    return true
}

func theUserLost() -> Bool {
    return guesses == 0
}
}
deleted 32 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Second attempt at hangmanHangman in swiftSwift

I recently posted my hangman game: Hangman in Swiftgame, and got some amazing feedback! The main thing suggested was to create a word generator that could read JSON files so that my hangman brain class didn't have to generate the word used on top of handling gameplay. To do this, I created the HangmanWordGeneratorHangmanWordGenerator class that parses JSON and generates a word for the HangmanBrainHangmanBrain class. Before, the HangmanBrainHangmanBrain used a set "database" in the form of three arrays (one for each difficulty) and picked a word itself. I am wondering how I did in implementing these improvements.

Here is relevant code:

Thanks for any help!

Second attempt at hangman in swift

I recently posted my hangman game: Hangman in Swift, and got some amazing feedback! The main thing suggested was to create a word generator that could read JSON files so that my hangman brain class didn't have to generate the word used on top of handling gameplay. To do this, I created the HangmanWordGenerator class that parses JSON and generates a word for the HangmanBrain class. Before, the HangmanBrain used a set "database" in the form of three arrays (one for each difficulty) and picked a word itself. I am wondering how I did in implementing these improvements.

Here is relevant code:

Thanks for any help!

Second attempt at Hangman in Swift

I recently posted my Hangman game, and got some amazing feedback! The main thing suggested was to create a word generator that could read JSON files so that my hangman brain class didn't have to generate the word used on top of handling gameplay. To do this, I created the HangmanWordGenerator class that parses JSON and generates a word for the HangmanBrain class. Before, the HangmanBrain used a set "database" in the form of three arrays (one for each difficulty) and picked a word itself. I am wondering how I did in implementing these improvements.

clarity
Source Link
Jared
  • 711
  • 1
  • 7
  • 20
Loading
Source Link
Jared
  • 711
  • 1
  • 7
  • 20
Loading