diff --git a/src/components/Game/Game.jsx b/src/components/Game/Game.jsx index 9cb50fb..f14d685 100644 --- a/src/components/Game/Game.jsx +++ b/src/components/Game/Game.jsx @@ -8,7 +8,7 @@ class Game extends Component { constructor(props) { super(props); this.state = { - stage: 0, + stage:0, showScreen: '' } this.showQuestion = this.showQuestion.bind(this); diff --git a/src/components/Game/Question.jsx b/src/components/Game/Question.jsx index 8e6ac30..d11ae74 100644 --- a/src/components/Game/Question.jsx +++ b/src/components/Game/Question.jsx @@ -1,6 +1,6 @@ import React, { Component } from 'react'; import { kanaDictionary } from '../../data/kanaDictionary'; -import { removeFromArray, arrayContains, shuffle } from '../../data/helperFuncs'; +import { findRomajisAtKanaKey, removeFromArray, arrayContains, shuffle } from '../../data/helperFuncs'; import './Question.scss'; class Question extends Component { @@ -13,6 +13,7 @@ class Question extends Component { answerOptions: [] } this.setNewQuestion = this.setNewQuestion.bind(this); + this.handleAnswer = this.handleAnswer.bind(this); } getRandomKanas(amount, include, exclude) { @@ -24,28 +25,20 @@ class Question extends Component { randomizedKanas = removeFromArray(include, randomizedKanas); // console.log(randomizedKanas); shuffle(randomizedKanas); - randomizedKanas = randomizedKanas.slice(0,amount-1); + randomizedKanas = randomizedKanas.slice(0, amount-1); randomizedKanas.push(include); shuffle(randomizedKanas); } else { shuffle(randomizedKanas); - randomizedKanas = randomizedKanas.slice(0,amount); + randomizedKanas = randomizedKanas.slice(0, amount); } return randomizedKanas; //return this.askableKanaKeys[Math.floor(Math.random() * this.askableKanaKeys.length)]; } - setPreviousQuestion() { - // console.log("settings previous"); - this.previousQuestion = this.currentQuestion; - this.setState({previousQuestion: this.previousQuestion}); - } - setNewQuestion() { - if(this.props.stage<=2) document.activeElement.blur(); // reset answer button's :active - if(this.currentQuestion) this.setPreviousQuestion(); - this.currentQuestion = this.getRandomKanas(1,false,this.previousQuestion); + this.currentQuestion = this.getRandomKanas(1, false, this.previousQuestion); this.setState({currentQuestion: this.currentQuestion}); this.setAnswerOptions(); this.setAllowedAnswers(); @@ -53,13 +46,25 @@ class Question extends Component { } setAnswerOptions() { - this.answerOptions = this.getRandomKanas(3,this.currentQuestion[0],false); + this.answerOptions = this.getRandomKanas(3, this.currentQuestion[0], false); this.setState({answerOptions: this.answerOptions}); // console.log(this.answerOptions); } setAllowedAnswers() { + if(this.props.stage==1) this.allowedAnswers = findRomajisAtKanaKey(this.currentQuestion); + if(this.props.stage==2) this.allowedAnswers = this.currentQuestion; + // console.log("allowed answers: "+this.allowedAnswers); + } + handleAnswer(answer) { + if(this.props.stage<=2) document.activeElement.blur(); // reset answer button's :active + this.previousQuestion = this.currentQuestion; + this.setState({previousQuestion: this.previousQuestion}); + this.previousAnswer = answer; + this.setState({previousAnswer: this.previousAnswer}); + this.previousAllowedAnswers = this.allowedAnswers; + this.setNewQuestion(); } initializeCharacters() { @@ -67,6 +72,7 @@ class Question extends Component { this.askableKanaKeys = []; this.askableRomajis = []; this.previousQuestion = ''; + this.previousAnswer = ''; Object.keys(kanaDictionary).map(function(whichKana) { // console.log(whichKana); // 'hiragana' or 'katakana' Object.keys(kanaDictionary[whichKana]).map(function(groupName) { @@ -75,15 +81,10 @@ class Question extends Component { if(arrayContains(groupName, this.props.decidedGroups)) { // let's merge the group to our askableKanas this.askableKanas = Object.assign(this.askableKanas, kanaDictionary[whichKana][groupName]['characters']); - Object.keys(kanaDictionary[whichKana][groupName]['characters']).map(function(key) { // let's add all askable kana keys to array this.askableKanaKeys.push(key); this.askableRomajis.push(kanaDictionary[whichKana][groupName]['characters'][key][0]); - // console.log(kanaDictionary[whichKana][groupName]['characters'][key][0]); - // kanaDictionary[whichKana][groupName]['characters'][key].map(function(romaji) { - // console.log(romaji); - // }, this); }, this); } }, this); @@ -91,6 +92,37 @@ class Question extends Component { // console.log(this.askableKanas); } + getAnswerType() { + if(this.props.stage==2) return 'kana'; + else return 'romaji'; + } + + getShowableQuestion() { + if(this.getAnswerType()=='kana') + return findRomajisAtKanaKey(this.state.currentQuestion)[0]; + else return this.state.currentQuestion; + } + + getPreviousResult() { + let resultString; + // console.log(this.previousAnswer); + if(this.previousQuestion=='' || this.previousAnswer=='') + resultString =