Skip to content
Snippets Groups Projects
Commit 0ff8824b authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

Création de la méthode d'enregistrement du score.

parent 1838eaac
No related branches found
No related tags found
No related merge requests found
...@@ -6,24 +6,32 @@ ...@@ -6,24 +6,32 @@
// //
import UIKit import UIKit
import CoreData
class GameEngine: NSObject { class GameEngine: NSObject {
private var listThemeAnimalsEasy : [String] = ["Chien", "Chat", "Lion", "Girafe", "Éléphant", "Serpent", "Oiseau", "Tigre", "Papillon", "Singe"] private var listThemeAnimalsEasy : [String] = ["Chien", "Chat", "Lion", "Girafe", "Éléphant", "Serpent", "Oiseau", "Tigre", "Papillon", "Singe"];
private var listThemeSportsEasy : [String] = ["Football", "Basket-ball", "Tennis", "Natation", "Rugby", "Golf", "Volley-ball", "Baseball", "Athlétisme", "Boxe"] private var listThemeSportsEasy : [String] = ["Football", "Basket-ball", "Tennis", "Natation", "Rugby", "Golf", "Volley-ball", "Baseball", "Athlétisme", "Boxe"];
private var listThemeFoodEasy : [String] = ["Pomme", "Banane", "Carotte", "Poisson", "Riz", "Fromage", "Pain", "Chocolat", "Fraise", "Poulet"] 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 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 listThemeColorsEasy : [String] = ["Rouge", "Bleu", "Vert", "Jaune", "Rose", "Violet", "Orange", "Gris", "Marron", "Noir"];
private var listThemeAnimalsHard : [String] = ["Chimpanzé", "Lynx", "Hippopotame", "Pélican", "Kangourou", "Anaconda", "Chimère", "Panda roux", "Calmar géant", "Ouistiti"] private var listThemeAnimalsHard : [String] = ["Chimpanzé", "Lynx", "Hippopotame", "Pélican", "Kangourou", "Anaconda", "Chimère", "Panda roux", "Calmar géant", "Ouistiti"];
private var listThemeSportsHard : [String] = ["Escrime", "Squash", "Haltérophilie", "Badminton", "Ski de fond", "Pentathlon moderne", "Canoë-kayak", "Aviron", "Patinage artistique", "Triathlon"] private var listThemeSportsHard : [String] = ["Escrime", "Squash", "Haltérophilie", "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 listThemeFoodHard : [String] = ["Artichaut", "Quinoa", "Courge", "Wasabi", "Chou-fleur", "Sarrasin", "Salsifis", "Morue", "Canneberge", "Pistache"];
private var listThemeCitiesHard : [String] = ["Copenhague", "Budapest", "Dubrovnik", "Séoul", "Marrakech", "Reykjavik", "La Nouvelle-Orléans", "Bratislava", "Wellington", "Accra"] private var listThemeCitiesHard : [String] = ["Copenhague", "Budapest", "Dubrovnik", "Séoul", "Marrakech", "Reykjavik", "La Nouvelle-Orléans", "Bratislava", "Wellington", "Accra"];
private var listThemeColorsHard : [String] = ["Écarlate", "Azur", "Émeraude", "Safran", "Pourpre", "Indigo", "Sienne", "Cyan", "Vermillon", "Lavande"] private var listThemeColorsHard : [String] = ["Écarlate", "Azur", "Émeraude", "Safran", "Pourpre", "Indigo", "Sienne", "Cyan", "Vermillon", "Lavande"];
private var wordToGuess : String = "" private var wordToGuess : String = "";
private var attemptsRemaining : Int = 0 private var attemptsRemaining : Int = 0;
private var guessedLetters : [Character] = [] private var guessedLetters : [Character] = [];
private var theme : String = "";
private var difficulty : String = "";
private var playerName : String = "";
private var score : Int = 0;
private var combo : Int = 0;
private var managedObjectContext: NSManagedObjectContext?;
override init() { override init() {
// //
...@@ -31,6 +39,8 @@ class GameEngine: NSObject { ...@@ -31,6 +39,8 @@ class GameEngine: NSObject {
func WordToGuess(theme: String, difficulty: String) -> String{ func WordToGuess(theme: String, difficulty: String) -> String{
var word = "" var word = ""
self.theme = theme;
self.difficulty = difficulty;
if theme == "Animals"{ if theme == "Animals"{
if difficulty == "Easy"{ if difficulty == "Easy"{
let randomInt = Int.random(in: 0..<listThemeAnimalsEasy.count) let randomInt = Int.random(in: 0..<listThemeAnimalsEasy.count)
...@@ -95,6 +105,24 @@ class GameEngine: NSObject { ...@@ -95,6 +105,24 @@ class GameEngine: NSObject {
public func saveScore() -> Void { public func saveScore() -> Void {
// Enregistrement du score dans la base Core Data // Enregistrement du score dans la base Core Data
// Création du gestionnaire d'objets persistants
let context: NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext;
// Création de l'instance de l'entité CoreData
let entity = NSEntityDescription.entity(forEntityName: "Score", in: context)!;
let object = NSManagedObject(entity: entity, insertInto: context);
object.setValue(theme, forKeyPath: "categoryName");
object.setValue(difficulty, forKeyPath: "difficulty");
object.setValue(playerName, forKeyPath: "playerName");
object.setValue(score, forKeyPath: "score");
// On enregistre
do {
try context.save();
} catch let error as NSError {
print("Erreur lors de l'enregistrement des données : \(error), \(error.userInfo)");
}
} }
static func getOnlineScoreboard() -> Void { static func getOnlineScoreboard() -> Void {
......
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="20086" systemVersion="21E258" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> <model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="20086" systemVersion="21E258" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Scoreboard" representedClassName="Scoreboard" syncable="YES" codeGenerationType="class"> <entity name="Score" representedClassName="Score" syncable="YES" codeGenerationType="class">
<attribute name="categoryName" optional="YES" attributeType="String"/> <attribute name="categoryName" optional="YES" attributeType="String"/>
<attribute name="difficulty" optional="YES" attributeType="String"/> <attribute name="difficulty" optional="YES" attributeType="String"/>
<attribute name="playerName" optional="YES" attributeType="String"/> <attribute name="playerName" optional="YES" attributeType="String"/>
<attribute name="score" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/> <attribute name="score" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
</entity> </entity>
<elements> <elements>
<element name="Scoreboard" positionX="-63" positionY="-18" width="128" height="89"/> <element name="Score" positionX="-63" positionY="-18" width="128" height="89"/>
</elements> </elements>
</model> </model>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment