From 5d0be44a68322aeb6f06b51aa96bb05f2f06ae0d Mon Sep 17 00:00:00 2001 From: cldupland Date: Wed, 27 Nov 2019 12:00:22 +0100 Subject: [PATCH] Ajout des fractions en cours et traitement des chevauchements de cartes --- TheGameExtreme/model/Card/Card.cs | 15 ++--- .../model/Card/cardType/FractionCard.cs | 30 +++++++++ TheGameExtreme/model/Fraction.cs | 61 +++++++++++++++++++ TheGameExtreme/model/deck/FractionDeck.cs | 37 +++++++++++ TheGameExtreme/model/piles/FractionPiles.cs | 23 +++++++ .../view/GamePreparationPage.xaml.cs | 2 +- TheGameExtreme/view/MainPage.xaml.cs | 10 +-- .../view/TouchManipulationBitmap.cs | 33 +++++----- TheGameExtreme/viewmodel/CardVM.cs | 6 +- TheGameExtreme/viewmodel/Main.cs | 6 +- 10 files changed, 186 insertions(+), 37 deletions(-) create mode 100644 TheGameExtreme/model/Card/cardType/FractionCard.cs create mode 100644 TheGameExtreme/model/Fraction.cs create mode 100644 TheGameExtreme/model/deck/FractionDeck.cs create mode 100644 TheGameExtreme/model/piles/FractionPiles.cs diff --git a/TheGameExtreme/model/Card/Card.cs b/TheGameExtreme/model/Card/Card.cs index f4e4f3f..59537ac 100644 --- a/TheGameExtreme/model/Card/Card.cs +++ b/TheGameExtreme/model/Card/Card.cs @@ -3,10 +3,8 @@ using System.ComponentModel; namespace TheGameExtreme.model.card { - public abstract class Card // : INotifyPropertyChanged + public abstract class Card { - - //public event PropertyChangedEventHandler PropertyChanged; private decimal value; public decimal Value { @@ -14,20 +12,19 @@ namespace TheGameExtreme.model.card set { this.value = value; - //OnPropertyChange("Value"); } } - //private void OnPropertyChange(string v) - //{ - // PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(v)); - //} - public Card(decimal value) { Value = value; } + public override string ToString() + { + return Value.ToString(); + } + public abstract String getName(); public abstract bool rapidEffect(); diff --git a/TheGameExtreme/model/Card/cardType/FractionCard.cs b/TheGameExtreme/model/Card/cardType/FractionCard.cs new file mode 100644 index 0000000..45f2fc0 --- /dev/null +++ b/TheGameExtreme/model/Card/cardType/FractionCard.cs @@ -0,0 +1,30 @@ +using System; +namespace TheGameExtreme.model.card.cardType +{ + public class FractionCard : Card + { + + public static readonly String CARD_FRACTION = "FractionCard"; + private Fraction fraction; + + public FractionCard(Fraction value) : base(value.Result()) + { + fraction = value; + } + + public override bool rapidEffect() + { + return false; + } + + override public String getName() + { + return CARD_FRACTION; + } + + public override string ToString() + { + return fraction.ToString(); + } + } +} diff --git a/TheGameExtreme/model/Fraction.cs b/TheGameExtreme/model/Fraction.cs new file mode 100644 index 0000000..6683fe4 --- /dev/null +++ b/TheGameExtreme/model/Fraction.cs @@ -0,0 +1,61 @@ + +using System; +namespace TheGameExtreme.model +{ + public class Fraction + { + public int Numerateur; + public int Denominateur; + + public Fraction(int numerateur, int denominateur) + { + Numerateur = numerateur; + Denominateur = denominateur; + } + + //public int CompareTo(Fraction fraction) + //{ + // decimal d = (Numerateur / Denominateur) - (fraction.Numerateur / fraction.Denominateur); + // if (d > 0) + // { + // return 1; + // } + // else if (d < 0) + // { + // return -1; + // } + // else + // { + // return 0; + // } + //} + + public bool testDiviseurCommun(Fraction fraction) + { + if (PGCD(Numerateur, fraction.Numerateur) == Numerateur || PGCD(Denominateur, fraction.Denominateur) == Denominateur) + { + return true; + } + return false; + } + + private int PGCD(int a, int b) + { + int temp = a % b; + if (temp == 0) + return b; + return PGCD(b, temp); + } + + public decimal Result() + { + decimal result = (decimal)Numerateur / (decimal)Denominateur; + return result; + } + + override public string ToString() + { + return Numerateur.ToString() + "/" + Denominateur.ToString(); + } + } +} diff --git a/TheGameExtreme/model/deck/FractionDeck.cs b/TheGameExtreme/model/deck/FractionDeck.cs new file mode 100644 index 0000000..eb142f8 --- /dev/null +++ b/TheGameExtreme/model/deck/FractionDeck.cs @@ -0,0 +1,37 @@ +using System; +using TheGameExtreme.model.card.cardType; + +namespace TheGameExtreme.model.deck +{ + public class FractionDeck : Deck + { + public FractionDeck() + { + Random random = new Random(); + for (int i = 1; i < 100; i ++) + { + int entier = random.Next(0, 100); + int deci = random.Next(0,100); + decimal d = (decimal)entier + (decimal)deci * 0.001m; + int numerateur = (int)(d * 1000m); + int denominateur = 1000; + int pgcd = PGCD(numerateur, denominateur); + while (pgcd != 1) + { + numerateur = numerateur / pgcd; + denominateur = denominateur / pgcd; + pgcd = PGCD(numerateur, denominateur); + } + deck.Add(new FractionCard(new Fraction(numerateur, denominateur))); + } + } + + private int PGCD(int a, int b) + { + int temp = a % b; + if (temp == 0) + return b; + return PGCD(b, temp); + } + } +} diff --git a/TheGameExtreme/model/piles/FractionPiles.cs b/TheGameExtreme/model/piles/FractionPiles.cs new file mode 100644 index 0000000..d09c4f3 --- /dev/null +++ b/TheGameExtreme/model/piles/FractionPiles.cs @@ -0,0 +1,23 @@ +using System; +using TheGameExtreme.model.card.cardType; + +namespace TheGameExtreme.model.piles +{ + public class FractionPiles : Piles + { + public FractionPiles(int nbPile) : base(nbPile) + { + for (int i = 0; i < nbPile; i++) + { + if (i < (nbPile * 0.5)) + { + ListOrderedStacks[i].Push(new ClassicCard(0)); + } + else + { + ListOrderedStacks[i].Push(new ClassicCard(100)); + } + } + } + } +} diff --git a/TheGameExtreme/view/GamePreparationPage.xaml.cs b/TheGameExtreme/view/GamePreparationPage.xaml.cs index 579af70..831f015 100644 --- a/TheGameExtreme/view/GamePreparationPage.xaml.cs +++ b/TheGameExtreme/view/GamePreparationPage.xaml.cs @@ -8,7 +8,7 @@ namespace TheGameExtreme.view public partial class GamePreparationPage : ContentPage { public List listNbPlayer = new List { 1, 2, 3, 4, 5 }; - public List listGameMode = new List { "entières", "relatives", "décimales", "dizaines", "centaines", "millièmes" }; // , "fractionnées" + public List listGameMode = new List { "entières", "relatives", "décimales", "dizaines", "centaines", "millièmes", "fractionnées" }; public List listNbStack = new List { 4, 6, 8 }; diff --git a/TheGameExtreme/view/MainPage.xaml.cs b/TheGameExtreme/view/MainPage.xaml.cs index a9d47b6..7bcd944 100644 --- a/TheGameExtreme/view/MainPage.xaml.cs +++ b/TheGameExtreme/view/MainPage.xaml.cs @@ -143,7 +143,7 @@ namespace TheGameExtreme.view if (stack.HitTest(point) || stack.HitTest(pointVisuCard)) { int indexPile = stackCollection.IndexOf(stack); - if (played(indexPile, decimal.Parse(bitmap.Value))) + if (played(indexPile, bitmap.Value.Value)) { bitmap.ProcessTouchEvent(args.Id, TouchActionType.Moved, stack.InitialPoint); bitmap.InitialPoint = stackCollection[stackCollection.IndexOf(stack)].InitialPoint; @@ -183,14 +183,14 @@ namespace TheGameExtreme.view SKPaint textPaint = new SKPaint(); float textWidth = textPaint.MeasureText("001"); float textSize = 0.05f * (float)DeviceDisplay.MainDisplayInfo.Width * textPaint.TextSize / textWidth; - SKPoint position = new SKPoint((float)((DeviceDisplay.MainDisplayInfo.Width * 0.9) / (viewmodel.getListOrderedStacks().Count * 2)) - textPaint.MeasureText("01"), (float)((DeviceDisplay.MainDisplayInfo.Height * 0.1) + (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.01 + 2 * textSize)); + SKPoint position = new SKPoint((float)((DeviceDisplay.MainDisplayInfo.Width * 0.9) / (viewmodel.getListOrderedStacks().Count * 2)) - textPaint.MeasureText("01") * 0.5f, (float)((DeviceDisplay.MainDisplayInfo.Height * 0.1) + (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.01 + 2 * textSize)); for (int i = 0; i < viewmodel.getListOrderedStacks().Count; i++) { textPaint = new SKPaint(); textPaint.TextSize = textSize; - stackCollection.Add(new TouchManipulationBitmap(textPaint, viewmodel.getListOrderedStacks()[i].Peek().Value) + stackCollection.Add(new TouchManipulationBitmap(textPaint, viewmodel.getListOrderedStacks()[i].Peek()) { Matrix = SKMatrix.MakeTranslation(position.X, position.Y), InitialMatrix = SKMatrix.MakeTranslation(position.X, position.Y), @@ -206,7 +206,7 @@ namespace TheGameExtreme.view SKPaint textPaint = new SKPaint(); float textWidth = textPaint.MeasureText("001"); float textSize = 0.05f * (float)DeviceDisplay.MainDisplayInfo.Width * textPaint.TextSize / textWidth; - SKPoint position = new SKPoint((float)((DeviceDisplay.MainDisplayInfo.Width * 0.9) / (viewmodel.CurrentHand.Count * 2)) - textPaint.MeasureText("01"), (float)((DeviceDisplay.MainDisplayInfo.Height * 0.9) - (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.2 - textSize)); + SKPoint position = new SKPoint((float)((DeviceDisplay.MainDisplayInfo.Width * 0.9) / (viewmodel.CurrentHand.Count * 2)) - textPaint.MeasureText("01") * 0.5f + (float)(DeviceDisplay.MainDisplayInfo.Width * 0.01), (float)((DeviceDisplay.MainDisplayInfo.Height * 0.9) - (DeviceDisplay.MainDisplayInfo.Height * 0.9) * 0.1 - 2 * textSize)); for (int i = 0; i < viewmodel.CurrentHand.Count; i++) { @@ -214,7 +214,7 @@ namespace TheGameExtreme.view textPaint.TextSize = textSize; position.X -= (float)(textWidth * 0.5); - textCollection.Add(new TouchManipulationBitmap(textPaint, viewmodel.CurrentHand[i].Value) + textCollection.Add(new TouchManipulationBitmap(textPaint, viewmodel.CurrentHand[i]) { Matrix = SKMatrix.MakeTranslation(position.X, position.Y), InitialMatrix = SKMatrix.MakeTranslation(position.X, position.Y), diff --git a/TheGameExtreme/view/TouchManipulationBitmap.cs b/TheGameExtreme/view/TouchManipulationBitmap.cs index 2872b2f..d23cfab 100644 --- a/TheGameExtreme/view/TouchManipulationBitmap.cs +++ b/TheGameExtreme/view/TouchManipulationBitmap.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; using SkiaSharp; +using TheGameExtreme.viewmodel; using TouchTracking; +using Xamarin.Essentials; namespace TheGameExtreme.view { @@ -10,26 +12,24 @@ namespace TheGameExtreme.view public SKPoint InitialPoint { get; set; } SKPaint textPaint; Dictionary touchDictionary = new Dictionary(); - public string Value; + public CardVM Value; + public string display; - public TouchManipulationBitmap(SKPaint textPaint, decimal value) + public TouchManipulationBitmap(SKPaint textPaint, CardVM value) { this.textPaint = textPaint; - Value = value.ToString(); + Value = value; + display = Value.ToString(); - if (!Value.Contains(",") && !Value.Contains(".")) + if (!display.Contains(",") && !display.Contains(".") && !display.Contains("/")) { - if (value < 0 && value > -10) + if (decimal.Parse(display) < 0 && decimal.Parse(display) > -10) { - Value = "-0" + Math.Abs(value).ToString(); + display = "-0" + Math.Abs(decimal.Parse(display)).ToString(); } - else if (value >= 0 && value < 10) + else if (decimal.Parse(display) >= 0 && decimal.Parse(display) < 10) { - Value = "0" + value.ToString(); - } - else - { - Value = value.ToString(); + display = "0" + display; } } @@ -56,14 +56,13 @@ namespace TheGameExtreme.view textPaint.Style = SKPaintStyle.Stroke; SKRect textBounds = new SKRect(); - textPaint.MeasureText(Value, ref textBounds); + textPaint.MeasureText(display, ref textBounds); SKRoundRect card = new SKRoundRect(textBounds, 1f, 1f); - card.Inflate(35, 55); - + card.Inflate(0.01f * (float)DeviceDisplay.MainDisplayInfo.Width, 0.01f * (float)DeviceDisplay.MainDisplayInfo.Width * 5f); canvas.DrawRoundRect(card, textPaint); - canvas.DrawText(Value, 0, 0, textPaint); + canvas.DrawText(display, 0, 0, textPaint); canvas.Restore(); @@ -85,7 +84,7 @@ namespace TheGameExtreme.view SKPoint transformedPoint = inverseMatrix.MapPoint(location); // Check if it's in the untransformed bitmap rectangle - SKRect rect = new SKRect(-70, -110, textPaint.MeasureText(Value) + 35, textPaint.TextSize); + SKRect rect = new SKRect(-70, -110, textPaint.MeasureText(display) + 35, textPaint.TextSize); return rect.Contains(transformedPoint); } return false; diff --git a/TheGameExtreme/viewmodel/CardVM.cs b/TheGameExtreme/viewmodel/CardVM.cs index bfedae8..465b995 100644 --- a/TheGameExtreme/viewmodel/CardVM.cs +++ b/TheGameExtreme/viewmodel/CardVM.cs @@ -36,7 +36,9 @@ namespace TheGameExtreme.viewmodel Value = view.Value; } - - + public override string ToString() + { + return View.ToString(); + } } } diff --git a/TheGameExtreme/viewmodel/Main.cs b/TheGameExtreme/viewmodel/Main.cs index 1efacde..5f5f3ca 100644 --- a/TheGameExtreme/viewmodel/Main.cs +++ b/TheGameExtreme/viewmodel/Main.cs @@ -76,9 +76,9 @@ namespace TheGameExtreme.viewmodel case 5: gameMode = new GameMode(new PilesMoins5To5(nbPile), new MilliemeDeck()); break; - //case 6: - // // Fraction - // break; + case 6: + gameMode = new GameMode(new FractionPiles(nbPile), new FractionDeck()); + break; default: gameMode = new GameMode(new ClassicPiles(nbPile), new ClassicDeck()); break;