-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstracted math part to its own class. Emoji and needy math modules inherit the math module.
- Loading branch information
Showing
10 changed files
with
7,017 additions
and
747 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
Assets/Scripts/EmojiMath.cs.meta → Assets/Scripts/EmojiMathModule.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.