From 1cd17ebff09ecd4e217bb71e2ebee057c8cc8cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20LAVERGNE?= Date: Sat, 13 Apr 2024 00:00:09 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=B4=20Red=C3=A9finition=20des=20foncti?= =?UTF-8?q?ons=20de=20comparaisons=20(Equals=20&=20GetHashCode)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Trek-12/Models/Cell.cs | 49 ++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/source/Trek-12/Models/Cell.cs b/source/Trek-12/Models/Cell.cs index 8369dc8..66e5f4f 100644 --- a/source/Trek-12/Models/Cell.cs +++ b/source/Trek-12/Models/Cell.cs @@ -2,15 +2,50 @@ public class Cell { - private Position pos; - private int value; - private string type; - private string background; - + public Position Pos { get; private set; } + + private int? value; + public int? Value { + get + { + return value; + } + set + { + if (value < 0) + { + throw new Exception("La valeur doit être supérieure à 0"); + } + this.value = value; + } + } + + private string Type { get; set; } + + private string? Background { get; set; } + public Cell(int x, int y) { - pos = new Position(x, y); + Pos = new Position(x, y); } - public string GetCellType() => type; + public string GetCellType() => Type; + + + // Redefinition de l'opérateur == pour comparer deux cellules + public override bool Equals(object? obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; + } + Cell c = (Cell)obj; + return (Pos.X == c.Pos.X) && (Pos.Y == c.Pos.Y); + } + + // Redefinition de la méthode de Hash pour l'opérateur == + public override int GetHashCode() + { + return Pos.X * Pos.Y; + } } \ No newline at end of file