211 lines
5.4 KiB
C#
211 lines
5.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class QuizHandler : MonoBehaviour
|
|
{
|
|
|
|
public TextAsset questions;
|
|
private string[] question;
|
|
private string[,] questionPart;
|
|
|
|
// UI Component
|
|
public Text txtQuetion, txtA, txtB, txtC, txtD, txtTA, txtTB;
|
|
|
|
public Text txtTitle;
|
|
|
|
public GameObject panel;
|
|
public GameObject imgScore, imgResult;
|
|
|
|
public Text txtFinalScore, txtCorrectScore, txtWrongScore;
|
|
|
|
int indexQuestion;
|
|
int maxQuestion;
|
|
bool isPossible;
|
|
char keyAnswer;
|
|
bool[] questionAlreadyUsed;
|
|
|
|
// Panel
|
|
bool isEnd;
|
|
private float duration;
|
|
public float durationResult;
|
|
|
|
// Score
|
|
int correctScore, wrongScore;
|
|
float finalScore;
|
|
|
|
// UI
|
|
public GameObject optionA;
|
|
public GameObject optionB;
|
|
public GameObject optionC;
|
|
public GameObject optionD;
|
|
public GameObject optionTA;
|
|
public GameObject optionTB;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
duration = durationResult;
|
|
question = questions.ToString().Split('#');
|
|
|
|
questionAlreadyUsed = new bool[question.Length];
|
|
|
|
questionPart = new string[question.Length, 6];
|
|
isPossible = true;
|
|
maxQuestion = 15;
|
|
QuestionManager();
|
|
ShowQuestion();
|
|
}
|
|
|
|
private void QuestionManager()
|
|
{
|
|
for (int i = 0; i < question.Length; i++)
|
|
{
|
|
string[] questionTemp = question[i].Split('+');
|
|
for (int j = 0; j < questionTemp.Length; j++)
|
|
{
|
|
questionPart[i, j] = questionTemp[j];
|
|
continue;
|
|
}
|
|
continue;
|
|
}
|
|
}
|
|
|
|
private void ShowQuestion()
|
|
{
|
|
|
|
if (indexQuestion < maxQuestion)
|
|
{
|
|
if (isPossible)
|
|
{
|
|
for (int i = 0; i < question.Length; i++)
|
|
{
|
|
int randomIndexQuestion = Random.Range(0, question.Length);
|
|
if (!questionAlreadyUsed[randomIndexQuestion])
|
|
{
|
|
txtQuetion.text = questionPart[randomIndexQuestion, 0];
|
|
txtA.text = questionPart[randomIndexQuestion, 1];
|
|
txtB.text = questionPart[randomIndexQuestion, 2];
|
|
txtC.text = questionPart[randomIndexQuestion, 3];
|
|
txtD.text = questionPart[randomIndexQuestion, 4];
|
|
keyAnswer = questionPart[randomIndexQuestion, 5][0];
|
|
|
|
optionA.SetActive(true);
|
|
optionC.SetActive(true);
|
|
|
|
print(randomIndexQuestion);
|
|
if (randomIndexQuestion > 39)
|
|
{
|
|
print("TRUE FALSE");
|
|
optionB.SetActive(false);
|
|
optionD.SetActive(false);
|
|
}
|
|
else
|
|
{
|
|
print("PILGAN");
|
|
optionB.SetActive(true);
|
|
optionD.SetActive(true);
|
|
}
|
|
|
|
questionAlreadyUsed[randomIndexQuestion] = true;
|
|
isPossible = false;
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void Option(string optionChar)
|
|
{
|
|
AnswerCheck(optionChar[0]);
|
|
if (indexQuestion == maxQuestion - 1)
|
|
{
|
|
isEnd = true;
|
|
}
|
|
else
|
|
{
|
|
indexQuestion++;
|
|
isPossible = true;
|
|
}
|
|
|
|
if (panel != null)
|
|
{
|
|
bool isActive = panel.activeSelf;
|
|
panel.SetActive(!isActive);
|
|
}
|
|
}
|
|
|
|
private float SumScore()
|
|
{
|
|
return finalScore = Mathf.Round((float)correctScore / maxQuestion * 100);
|
|
}
|
|
|
|
private void AnswerCheck(char data)
|
|
{
|
|
string title;
|
|
if (data.Equals(keyAnswer))
|
|
{
|
|
title = "Benar !";
|
|
correctScore++;
|
|
}
|
|
else
|
|
{
|
|
title = "Salah !";
|
|
wrongScore++;
|
|
}
|
|
txtTitle.text = title;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if (panel.activeSelf)
|
|
{
|
|
durationResult -= Time.deltaTime;
|
|
|
|
if (isEnd)
|
|
{
|
|
imgScore.SetActive(true);
|
|
imgResult.SetActive(false);
|
|
|
|
if (durationResult <= 0)
|
|
{
|
|
txtCorrectScore.text = correctScore.ToString();
|
|
txtWrongScore.text = wrongScore.ToString();
|
|
txtFinalScore.text = SumScore().ToString();
|
|
|
|
imgScore.SetActive(false);
|
|
imgResult.SetActive(true);
|
|
|
|
durationResult = 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
imgScore.SetActive(true);
|
|
imgResult.SetActive(false);
|
|
|
|
if (durationResult <= 0)
|
|
{
|
|
panel.SetActive(false);
|
|
durationResult = duration;
|
|
ShowQuestion();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void GoToMainMenu()
|
|
{
|
|
SceneManager.LoadScene("MainMenu");
|
|
}
|
|
}
|