public static void Main(string[] args) { RationalNumber xyu = new RationalNumber(3, 4); RationalNumber xyu1 = new RationalNumber(1, 4); RationalNumber xyu2 = new RationalNumber(2, 4); RationalNumber xyu3 = new RationalNumber(1, 3); FractionSet FR = new FractionSet(); FR.addNumber(xyu); FR.addNumber(xyu1); FR.addNumber(xyu2); FR.addNumber(xyu3); Console.WriteLine(FR.maxFraction().numerator + " " + FR.maxFraction().denominator); Console.WriteLine(FR.minFraction().numerator + " " + FR.minFraction().denominator); }
/// <summary> /// Unity Function. Called once upon creation of the object. /// </summary> void Awake() { this.stabilityController = GetComponent <StabilityController> (); hollowBlocks = new List <HollowBlock> (); additionRight = new List <FractionSet> (); sessionList = new List <StabilizationSession> (); fractionSet = new FractionSet(); EventBroadcaster.Instance.AddObserver(EventNames.RECORD_HOLLOW_BLOCK, this.RecordData); EventBroadcaster.Instance.AddObserver(EventNames.RECORD_ON_AREA_STABLE, RecordSession); EventBroadcaster.Instance.AddObserver(EventNames.ON_BLOCK_SPAWN, this.RegisterBlock); EventBroadcaster.Instance.AddObserver(EventNames.ON_BLOCK_DESTROY, this.UnregisterBlock); EventBroadcaster.Instance.AddObserver(EventNames.REQUEST_UPDATE_SESSION, this.UpdateSession); SceneManager.sceneUnloaded += this.OnSceneUnloaded; InitSession(); }
// Use this for initialization void Start() { FractionSet set = new FractionSet(); set.fractions.AddRange(fractions); int index = 0; FractionData target = new FractionData(); target.numerator = 1; target.denominator = 2; foreach (FractionSet s in SubsetSumFractions.SubsetSum(set, target)) { string log = "Set " + index + ": "; foreach (FractionData fraction in s.fractions) { log += fraction.numerator.ToString() + "/" + fraction.denominator.ToString() + ", "; } Debug.Log(log); index++; } }
/// <summary> /// Record the data of the player's attempt. /// Evaluates if the attempt is right or wrong by comparing the set of fllled blocks with all the instances of the subset sum set. /// The comparision looks if the set of attempts are closer or farther from any subset sum. /// </summary> /// <param name="data"></param> private void RecordData(Parameters data) { HollowBlock block = data.GetHollowBlockExtra("hollowBlock"); bool solved = data.GetBoolExtra("solved", true); FractionSet oldFracSet = new FractionSet(); oldFracSet.fractions.AddRange(fractionSet.fractions); FractionData blockFraction = block.GetFraction(); if (solved) { fractionSet.fractions.Add(block.GetFraction()); } else { fractionSet.fractions.Remove(fractionSet.fractions.Find(x => x.numerator == block.GetFraction().numerator && x.denominator == block.GetFraction().denominator)); } Debug.Log("Old size: " + oldFracSet.fractions.Count); FractionSet newFracSet = new FractionSet(); newFracSet.fractions.AddRange(fractionSet.fractions); Debug.Log("New size: " + newFracSet.fractions.Count); bool found = false; bool improve = false; int oldCounter = 0; int newCounter = 0; foreach (FractionSet s in additionRight) { FractionSet oldTemp = oldFracSet; FractionSet newTemp = newFracSet; oldCounter = 0; newCounter = 0; for (int i = 0; i < s.fractions.Count; i++) { if (oldTemp.fractions.Remove(oldTemp.fractions.Find(x => x.numerator == s.fractions [i].numerator && x.denominator == s.fractions [i].denominator))) { oldCounter++; } else { oldCounter--; } if (newTemp.fractions.Remove(newTemp.fractions.Find(x => x.numerator == s.fractions [i].numerator && x.denominator == s.fractions [i].denominator))) { newCounter++; } else { newCounter--; } } oldCounter -= oldFracSet.fractions.Count; newCounter -= newFracSet.fractions.Count; Debug.Log("New: " + newCounter + " VS Old: " + oldCounter); if (newCounter > oldCounter) { improve = true; break; } } if (solved) //This means it is Addition { if (improve) { this.currentSession.Tally(blockFraction.denominator, true, Operation.ADDITION); } else { this.currentSession.Tally(blockFraction.denominator, false, Operation.ADDITION); } } else //Else, its Subtraction { if (improve) { this.currentSession.Tally(blockFraction.denominator, true, Operation.SUBTRACTION); } else { this.currentSession.Tally(blockFraction.denominator, false, Operation.SUBTRACTION); } } }
/// <summary> /// Takes all currently registered ghost blocks and checks their values to determine what topic the puzzle is. /// Also updates the stability number line based on the currently registered blocks' values. /// </summary> private void UpdateSession() { List <int> denominators = new List <int> (); FractionSet fset = new FractionSet(); bool similar = true; bool oddEquiv = true; bool evenEquiv = true; int currentDenom = (int)hollowBlocks [0].GetDenominator(); for (int i = 0; i < hollowBlocks.Count; i++) { denominators.Add((int)hollowBlocks[i].GetDenominator()); FractionData fraction = hollowBlocks [i].GetFraction(); if (i > 0) { if (similar && currentDenom == fraction.denominator) { similar = true; } else { similar = false; } if (evenEquiv && currentDenom % 2 == 0 && (currentDenom % fraction.denominator == 0 || fraction.denominator % currentDenom == 0 || (General.SimplifyFraction(fraction.numerator, fraction.denominator) [0] == 1.0f && General.SimplifyFraction(fraction.numerator, fraction.denominator) [1] == 2.0f))) { evenEquiv = true; } else { evenEquiv = false; } if (oddEquiv && currentDenom % 3 == 0 && (currentDenom % fraction.denominator == 0 || fraction.denominator % currentDenom == 0 || (General.SimplifyFraction(fraction.numerator, fraction.denominator) [0] == 1.0f && General.SimplifyFraction(fraction.numerator, fraction.denominator) [1] == 3.0f) || (General.SimplifyFraction(fraction.numerator, fraction.denominator) [0] == 2.0f && General.SimplifyFraction(fraction.numerator, fraction.denominator) [1] == 3.0f))) { oddEquiv = true; } else { oddEquiv = false; } // if (similar && fraction.denominator == (int)hollowBlocks [i - 1].GetDenominator ()) // similar = true; // else // similar = false; // // if (evenEquiv && (fraction.denominator % 2) == ((int)hollowBlocks [i - 1].GetDenominator () % 2)) // evenEquiv = true; // else // evenEquiv = false; // // if (oddEquiv && (fraction.denominator % 3) == ((int)hollowBlocks [i - 1].GetDenominator () % 3)) // oddEquiv = true; // else // oddEquiv = false; } fset.fractions.Add(fraction); } if (similar) { topic = SessionTopic.SIMILAR; } else if (evenEquiv || oddEquiv) { topic = SessionTopic.EQUIVALENT; } else { topic = SessionTopic.DISSIMILAR; } this.currentSession = new StabilizationSession(topic, this.sceneAvgSolveTime); if (numberLine == null) { this.numberLine = FindObjectOfType <StabilityNumberLine> (); } FractionData target = this.stabilityController.UpdateStabilityNumberLine(hollowBlocks); // FractionData target = new FractionData (); // target.numerator = (int)numberLine.GetTargetNumerator (); // target.denominator = (int)numberLine.GetTargetDenominator (); foreach (FractionSet s in SubsetSumFractions.SubsetSum(fset, target)) { additionRight.Add(s); } }