Skip to content

Commit

Permalink
refactored everything to be neatly organized into functions. The logi…
Browse files Browse the repository at this point in the history
…c is not affected
  • Loading branch information
DeltriDev committed Jul 20, 2024
1 parent 2826204 commit 552b0af
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import PackageDescription

let package = Package(
name: "mastermind!",
name: "mastermind",
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.executableTarget(
name: "mastermind!"),
name: "mastermind"),
]
)
85 changes: 48 additions & 37 deletions Sources/main.swift
Original file line number Diff line number Diff line change
@@ -1,39 +1,55 @@
//intro to the game
print("Welcome to the code-breaking game MasterMind, written in Swift!")
print("Try to guess the number sequence of numbers from 1-8!")
print("Every number can be used once")
print("You have 12 tries")
//necessary variables
var tries = 0
let possibleCharacters:[String] = ["1","2","3","4","5","6","7","8"]
let possibleCharactersString = "12345678"
var correctAnswer:String = ""
//generate the correct answer
repeat
let possibleCharacters:Set<String.Element> = ["1","2","3","4","5","6","7","8"]
var correctAnswer:String = GenerateCorrectAnswer()

//run the game
GameLoop()

func GameLoop()
{
let charToAdd = possibleCharacters[Int.random(in: 0...7)]
if !correctAnswer.contains(charToAdd)
GameIntro()
repeat
{
correctAnswer += charToAdd
}
}while correctAnswer.count < 4
//game loop
repeat
print("Try \(tries+1)")
if AwaitAnswer()
{
print("You broke the code! Congratulations!") //add how many tries it took
readLine()
break
}
else if tries == 12
{
print("You lost...")
readLine()
break
}
}while tries<12
}

//explain the rules to the player
func GameIntro()
{
print("Try \(tries+1)")
if AwaitAnswer()
{
print("You broke the code! Congratulations!") //add how many tries it took
readLine()
break
}
else if tries == 12
print("Welcome to the code-breaking game MasterMind, written in Swift!")
print("Try to guess the number sequence of numbers from 1-8!")
print("Every number can be used once")
print("You have 12 tries")
}

//returns a 4 letter string made with elements from possibleCharacters
func GenerateCorrectAnswer() -> String
{
var correctAnswer = ""
repeat
{
print("You lost...")
readLine()
break
}
}while tries<12
let charToAdd = possibleCharacters.randomElement()!
if !correctAnswer.contains(charToAdd)
{
correctAnswer += String(charToAdd)
}
}while correctAnswer.count < 4
return correctAnswer
}

//asks for an answer and returns true if its correct
func AwaitAnswer() -> Bool
Expand All @@ -42,13 +58,8 @@ func AwaitAnswer() -> Bool
let answer = readLine()
if let unwrappedAnswer = answer
{
//FIXME: if you insert a 4-letter sequence of letters it doesnt stop, wooooops
//FIXME: don't let the player type a character more than once
let arrayAnswer = Array(unwrappedAnswer)
let arrayPossibleCharacters = Array(possibleCharactersString)
let setAnswer = Set(arrayAnswer)
let setPossibleCharacters = Set(arrayPossibleCharacters)
let commonCharacters = setAnswer.intersection(setPossibleCharacters).count
let setAnswer = Set(unwrappedAnswer)
let commonCharacters = setAnswer.intersection(possibleCharacters).count
if unwrappedAnswer.count == 4 && commonCharacters == 4
{
tries+=1
Expand Down

0 comments on commit 552b0af

Please sign in to comment.