diff --git a/Pendu/Base.lproj/Main.storyboard b/Pendu/Base.lproj/Main.storyboard index 722c042620ae57d22d924f577987ed8744b2b1a5..9d448a3cb797f3bee66175eb9aeab6144b83901c 100644 --- a/Pendu/Base.lproj/Main.storyboard +++ b/Pendu/Base.lproj/Main.storyboard @@ -11,7 +11,6 @@ <!--View Controller--> <scene sceneID="tne-QT-ifu"> <objects> - <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/> <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Pendu" customModuleProvider="target" sceneMemberID="viewController"> <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC"> <rect key="frame" x="0.0" y="0.0" width="414" height="896"/> @@ -441,15 +440,16 @@ <outlet property="R_Button" destination="jFS-B8-RI5" id="cpM-ni-QDg"/> <outlet property="S_Button" destination="7g4-B8-EDq" id="kmf-hs-Yeq"/> <outlet property="T_Button" destination="IHm-sb-sqg" id="x8v-Ge-iH1"/> - <outlet property="TextWord" destination="z7N-Ws-jLx" id="pAe-cg-dJC"/> <outlet property="U_Button" destination="JKG-hu-Aix" id="gtC-ZF-jxy"/> <outlet property="V_Button" destination="hn5-QO-hjX" id="68l-xk-lSc"/> <outlet property="W_Button" destination="Jwa-vY-VHc" id="aL0-nz-8gV"/> <outlet property="X_Button" destination="0Ds-p6-HNv" id="K6T-ma-kCd"/> <outlet property="Y_Button" destination="NdK-Hc-8G9" id="vwa-By-hL8"/> <outlet property="Z_Button" destination="DbP-7q-EIq" id="nX8-cc-NvD"/> + <outlet property="wordLabel" destination="z7N-Ws-jLx" id="Qck-61-KN8"/> </connections> </viewController> + <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/> </objects> <point key="canvasLocation" x="321.73913043478262" y="105.80357142857143"/> </scene> diff --git a/Pendu/ViewController.swift b/Pendu/ViewController.swift index 81f80dc7498e2e5c7b4c0759bbec6f5225fcc2a3..42033e537edc3b0f7cfa24e148c8e31616190b62 100644 --- a/Pendu/ViewController.swift +++ b/Pendu/ViewController.swift @@ -30,7 +30,7 @@ extension UIView { class ViewController: UIViewController { - @IBOutlet weak var TextWord: UILabel! + @IBOutlet weak var wordLabel: UILabel! @IBOutlet weak var A_Button: UIButton! @IBOutlet weak var Z_Button: UIButton! @@ -139,32 +139,65 @@ class ViewController: UIViewController { } func InputButton(inputButton: UIButton) { + guard let letter = inputButton.titleLabel?.text else { + return + } + guessLetter(Character(letter)) inputButton.isEnabled = false } func penduImage(nameFile: String) { let image = UIImage(named: nameFile) let imageView = UIImageView(image: image!) - imageView.frame = CGRect(x: 50, y: 220, width: 250, height: 250) + imageView.frame = CGRect(x: 50, y: 250, width: 250, height: 250) view.addSubview(imageView) } + let wordToGess = "VOITURE BLEU" + var currentWordState = "" override func viewDidLoad() { - TextWord.text = "- - - - -" view.addBackground() super.viewDidLoad() + initializeWordState() + updateWordLabel() + penduImage(nameFile: "humain8") let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40)) - label.center = CGPoint(x: 160, y: 230) + label.center = CGPoint(x: 170, y: 230) label.font = UIFont(name: "whatever it takes", size: 40) label.textAlignment = .center label.textColor = UIColor.white label.text = "Pendu" self.view.addSubview(label) } - - + + func initializeWordState() { + for _ in wordToGess { + currentWordState += "_" + } + } + + func updateWordLabel() { + wordLabel.text = currentWordState + } + + func guessLetter(_ letter:Character) { + let guessedLetter = letter.uppercased() + guard wordToGess.contains(guessedLetter) + else { + return + } + for (index, char) in wordToGess.enumerated() { + if char.uppercased() == guessedLetter { + let startIndex = currentWordState.startIndex + let offsetIndex = currentWordState.index(startIndex, offsetBy: index) + + currentWordState.replaceSubrange(offsetIndex...offsetIndex, with: String(char)) + } + } + updateWordLabel() + } }