diff --git a/Pendu/GameEngine.swift b/Pendu/GameEngine.swift index 2c08f530e2c3fe4600fce5e034d4e35175439ca4..5f3beb0af9ef121f4eed7bd80233d9f60a077ba1 100644 --- a/Pendu/GameEngine.swift +++ b/Pendu/GameEngine.swift @@ -11,15 +11,15 @@ import CoreData class GameEngine: NSObject { private var listThemeAnimalsEasy : [String] = ["Chien", "Chat", "Lion", "Girafe", "elephant", "Serpent", "Oiseau", "Tigre", "Papillon", "Singe"]; - private var listThemeSportsEasy : [String] = ["Football", "Basket-ball", "Tennis", "Natation", "Rugby", "Golf", "Volley-ball", "Baseball", "Athletisme", "Boxe"]; + private var listThemeSportsEasy : [String] = ["Football", "Tennis", "Natation", "Rugby", "Golf", "Baseball", "Athletisme", "Boxe"]; private var listThemeFoodEasy : [String] = ["Pomme", "Banane", "Carotte", "Poisson", "Riz", "Fromage", "Pain", "Chocolat", "Fraise", "Poulet"]; private var listThemeCitiesEasy : [String] = ["Paris", "Londres", "New York", "Rome", "Tokyo", "Sydney", "Barcelone", "Berlin", "Istanbul", "Mumbai"]; private var listThemeColorsEasy : [String] = ["Rouge", "Bleu", "Vert", "Jaune", "Rose", "Violet", "Orange", "Gris", "Marron", "Noir"]; - private var listThemeAnimalsHard : [String] = ["Chimpanze", "Lynx", "Hippopotame", "Pelican", "Kangourou", "Anaconda", "Chimere", "Panda roux", "Calmar geant", "Ouistiti"]; - private var listThemeSportsHard : [String] = ["Escrime", "Squash", "Halterophilie", "Badminton", "Ski de fond", "Pentathlon moderne", "Canoë-kayak", "Aviron", "Patinage artistique", "Triathlon"]; - private var listThemeFoodHard : [String] = ["Artichaut", "Quinoa", "Courge", "Wasabi", "Chou-fleur", "Sarrasin", "Salsifis", "Morue", "Canneberge", "Pistache"]; - private var listThemeCitiesHard : [String] = ["Copenhague", "Budapest", "Dubrovnik", "Seoul", "Marrakech", "Reykjavik", "La Nouvelle-Orleans", "Bratislava", "Wellington", "Accra"]; + private var listThemeAnimalsHard : [String] = ["Chimpanze", "Lynx", "Hippopotame", "Pelican", "Kangourou", "Anaconda", "Chimere", "Panda roux", "Ouistiti"]; + private var listThemeSportsHard : [String] = ["Escrime", "Squash", "Halterophilie", "Badminton", "Aviron", "Triathlon"]; + private var listThemeFoodHard : [String] = ["Artichaut", "Quinoa", "Courge", "Wasabi", "Sarrasin", "Salsifis", "Morue", "Canneberge", "Pistache"]; + private var listThemeCitiesHard : [String] = ["Copenhague", "Budapest", "Dubrovnik", "Seoul", "Marrakech", "Reykjavik", "Bratislava", "Wellington", "Accra"]; private var listThemeColorsHard : [String] = ["ecarlate", "Azur", "emeraude", "Safran", "Pourpre", "Indigo", "Sienne", "Cyan", "Vermillon", "Lavande"]; private var wordToGuess : String = ""; @@ -166,6 +166,18 @@ class GameEngine: NSObject { return self.score; } + public func getPlayerName() -> String { + return self.playerName; + } + + public func getDifficulty() -> String { + return self.difficulty; + } + + public func getCategoryName() -> String { + return self.theme; + } + public func saveScore() -> Void { // Enregistrement du score dans la base Core Data diff --git a/Pendu/GameViewController.swift b/Pendu/GameViewController.swift index b2263ce4be1e91478c9595cab168a2b7658751c7..7e35ddd54890513a3eefbee158aab133c8e0158b 100644 --- a/Pendu/GameViewController.swift +++ b/Pendu/GameViewController.swift @@ -183,16 +183,21 @@ class GameViewController: UIViewController { } func initializeWordState() { - let words = wordToGuess.components(separatedBy: " ") + let words = wordToGuess.components(separatedBy: " ") print(words) - for word in words { - var wordState = "" - for _ in word { - wordState += "_" - } - currentWordState.append(wordState) - } - } + + for word in words { + var wordState = "" + for char in word { + if char == " " { + wordState += " " + } else { + wordState += "_" + } + } + currentWordState.append(wordState) + } + } func updateWordLabel() { let wordString = currentWordState.joined(separator: " ") @@ -201,6 +206,26 @@ class GameViewController: UIViewController { func checkIfWordFound() { if currentWordState.joined() == wordToGuess { + let url = URL(string: "https://miscs.sl-projects.com/IUT-ORSAY/LP-PRISM/TP-iOS/") + + let parametre = ["score": gameEngine.getScore(), "categoryName": gameEngine.getCategoryName(), "difficulty": gameEngine.getDifficulty(), "playerName": gameEngine.getPlayerName()] as [String : Any] + + let jsonData = try? JSONSerialization.data(withJSONObject: parametre) + + // Créer une requête URLRequest + var request = URLRequest(url: (url)!) + request.httpMethod = "POST" + request.httpBody = jsonData + + // Créer une session URLSession + let session = URLSession.shared + + // Créer une tâche de données pour envoyer la requête + let task = session.dataTask(with: request) + + // Démarrer la tâche de données + task.resume() + isWordFound = true wordLabel.textColor = UIColor.green A_Button.isHidden = true @@ -260,38 +285,37 @@ class GameViewController: UIViewController { } } - func guessLetter(_ letter:String) { - let guessedLetter = letter.uppercased() - - var updateWordState = currentWordState + func guessLetter(_ letter: String) { + let guessedLetter = letter.uppercased() - let guessedCorrectly = gameEngine.guessLetter(letter: Character(letter)) - score.text = "Score : " + String(gameEngine.getScore()) - - for (wordIndex, word) in wordToGuess.components(separatedBy: " ").enumerated() { - var updateWord = "" - let wordCharacters = Array(currentWordState[wordIndex]) - for (charIndex, char) in word.enumerated() { - let currentChar = String(char) - - if currentChar.uppercased() == guessedLetter { - updateWord += currentChar -// guessedCorrectly = true - } else { - let currentWordStateChar = String(wordCharacters[charIndex]) - updateWord += currentWordStateChar - } + var updateWordState = currentWordState + + let guessedCorrectly = gameEngine.guessLetter(letter: Character(letter)) + score.text = "Score : " + String(gameEngine.getScore()) + + for (wordIndex, word) in wordToGuess.components(separatedBy: .whitespacesAndNewlines).enumerated() { + var updateWord = "" + let wordCharacters = Array(currentWordState[wordIndex]) + for (charIndex, char) in word.enumerated() { + let currentChar = String(char) + + if currentChar.uppercased() == guessedLetter { + updateWord += currentChar + } else { + let currentWordStateChar = String(wordCharacters[charIndex]) + updateWord += currentWordStateChar } - updateWordState[wordIndex] = updateWord - } - currentWordState = updateWordState - updateWordLabel() - checkIfWordFound() - - if !guessedCorrectly { - updateImage() } + updateWordState[wordIndex] = updateWord } + currentWordState = updateWordState + updateWordLabel() + checkIfWordFound() + + if !guessedCorrectly { + updateImage() + } + } func updateImage() {