Skip to content

Commit

Permalink
Created needy math module
Browse files Browse the repository at this point in the history
Abstracted math part to its own class. Emoji and needy math
modules inherit the math module.
  • Loading branch information
timtmok committed Jul 21, 2016
1 parent b68c9d2 commit e139412
Show file tree
Hide file tree
Showing 10 changed files with 7,017 additions and 747 deletions.
4,103 changes: 3,955 additions & 148 deletions Assets/Examples/example.unity

Large diffs are not rendered by default.

3,273 changes: 2,866 additions & 407 deletions Assets/Prefabs/Needy Math.prefab

Large diffs are not rendered by default.

145 changes: 0 additions & 145 deletions Assets/Scripts/EmojiMath.cs

This file was deleted.

76 changes: 76 additions & 0 deletions Assets/Scripts/EmojiMathModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class EmojiMathModule : MathModule
{
public TextMesh DisplayText;

private readonly string[] _emojiNumbers = new string[]
{
":)",
"=(",
"(:",
")=",
":(",
"):",
"=)",
"(=",
":|",
"|:"
};

void Start()
{
Init();
}

protected override void Init()
{
base.Init();
SetDisplay();
}

protected override void Solve()
{
if (Puzzle.CheckAnswer(Answer, Sign))
GetComponent<KMBombModule>().HandlePass();
else
GetComponent<KMBombModule>().HandleStrike();

Answer = string.Empty;
}

private void SetDisplay()
{
var convertedOperand1 = ConvertOperand(Puzzle.Operand1);
var convertedOperand2 = ConvertOperand(Puzzle.Operand2);
var operation = MathPuzzle.GetOperationString(Puzzle.Operator);
var questionText = convertedOperand1 + operation + convertedOperand2;

DisplayText.text = questionText;
}

private string ConvertOperand(int operand)
{
var convertedDigits = new List<string>();

while (operand > 0)
{
var digit = operand % 10;
convertedDigits.Add(_emojiNumbers[digit]);
operand = operand / 10;
}

convertedDigits.Reverse();

var builder = new StringBuilder();

foreach (var convertedDigit in convertedDigits)
{
builder.Append(convertedDigit);
}

return builder.ToString();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions Assets/Scripts/MathModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using UnityEngine;

public abstract class MathModule : MonoBehaviour
{
private const int Minus = 10;
private const int Enter = 11;
protected string Answer = string.Empty;
protected int Sign = 1;
protected MathPuzzle Puzzle;

public KMSelectable[] Buttons;

protected virtual void Init()
{
Puzzle = MathFactory.Instance.GenerateQuestion();
Debug.Log(Puzzle.Operand1 + MathPuzzle.GetOperationString(Puzzle.Operator) + Puzzle.Operand2);

SetUpNumberButtons();
SetUpMinusButton();
SetUpEnterButton();
}

private void SetUpEnterButton()
{
Buttons[Enter].OnInteract += delegate
{
Solve();
return false;
};
}

protected abstract void Solve();

private void SetUpMinusButton()
{
Buttons[Minus].OnInteract += delegate
{
Sign *= -1;
return false;
};
}

private void SetUpNumberButtons()
{
for (var i = 0; i < 10; i++)
{
var button = Buttons[i];
button.OnInteract += delegate
{
Answer += button.GetComponentInChildren<TextMesh>().text;
return false;
};
}
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/MathModule.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions Assets/Scripts/MathPuzzle.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System;
using Debug = UnityEngine.Debug;

public class MathPuzzle
{
public int Operand1;
Expand Down Expand Up @@ -35,4 +38,33 @@ public static Operation GetOperation(int type)
return Operation.Addition;
}
}

public bool CheckAnswer(string answer, int sign)
{
int rightAnswer;
switch (Operator)
{
case MathPuzzle.Operation.Addition:
rightAnswer = Operand1 + Operand2;
break;
case MathPuzzle.Operation.Subtraction:
rightAnswer = Operand1 - Operand2;
break;
default:
throw new ArgumentOutOfRangeException();
}

int parsedAnswer;
try
{
parsedAnswer = int.Parse(answer) * sign;
}
catch (FormatException e)
{
Debug.Log(e.Message);
return false;
}

return rightAnswer == parsedAnswer;
}
}
Loading

0 comments on commit e139412

Please sign in to comment.