Commit 8c124c6e authored by fpeterfalvi's avatar fpeterfalvi
Browse files

Refactoring of Question class and loading questions.

parent 189c6d35
Melyik hónap 31 napos? július február június november
Mi Olaszország fővárosa? Róma London Berlin Moszkva
Melyik nem emlős? szula oroszlán farkas rozmár
Melyik kontinensen található Peru? Dél-Amerika Észak-Amerika Ázsia Afrika
Melyik város van legtávolabb Budapesttől? Sydney Makó Jeruzsálem London
Melyik nem biztosan trapéz? deltoid paralelogramma négyzet téglalap
Melyik NEM gázbolygó? Föld Jupiter Uránusz Neptunusz
Melyik folyó partján fekszik Budapest? Duna Jangce Nílus Szajna
egyéb Melyik hónap 31 napos? július február június november disabled
földrajz Mi Olaszország fővárosa? Róma London Berlin Moszkva enabled
földrajz Hány bolygó van a Naprendszerben? 8 enabled
egyéb Melyik nem emlős? szula oroszlán farkas rozmár enabled
földrajz Melyik kontinensen található Peru? Dél-Amerika Észak-Amerika Ázsia Afrika enabled
földrajz Melyik város van legtávolabb Budapesttől? Sydney Makó Jeruzsálem London enabled
egyéb Hány napos hónap a március? 31 enabled
egyéb Melyik nem biztosan trapéz? deltoid paralelogramma négyzet téglalap enabled
földrajz Melyik NEM gázbolygó? Föld Jupiter Uránusz Neptunusz disabled
földrajz Melyik folyó partján fekszik Budapest? Duna Jangce Nílus Szajna enabled
package onlab.kvizserver;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
......@@ -17,21 +15,20 @@ import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import onlab.kvizserver.model.ClientModel;
import onlab.kvizserver.model.GuessQuestion;
import onlab.kvizserver.model.MultipleChoiceQuestion;
import onlab.kvizserver.model.PlayerAnswer;
import onlab.kvizserver.model.Question;
public class GameActivity extends AppCompatActivity {
private List<MultipleChoiceQuestion> questions = new ArrayList<>();
private List<Question> questions;
private int questionIndex = 0;
......@@ -45,7 +42,7 @@ public class GameActivity extends AppCompatActivity {
final List<String> answers = new ArrayList<>();
int[] playerAnswers;
PlayerAnswer[] playerAnswers;
int correctAnswer;
......@@ -64,23 +61,25 @@ public class GameActivity extends AppCompatActivity {
updateConversationHandler = new Handler();
BufferedReader buffreader;
InputStream inputStream = null;
try {
if (questionFileName == null) {
buffreader = new BufferedReader(new InputStreamReader(getAssets().open("multiple.txt")));
inputStream = getAssets().open("multiple.txt");
} else {
buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(questionFileName))));
}
String line;
while ((line = buffreader.readLine()) != null) {
String[] parts = line.split("\t");
MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion(
parts[0], parts[1], parts[2], parts[3], parts[4]);
questions.add(multipleChoiceQuestion);
inputStream = new FileInputStream(new File(questionFileName));
}
} catch (IOException e) {
e.printStackTrace();
}
questions = readQuestions(inputStream);
//nem feleletválasztós kérdések eltávolítása
Iterator<Question> it = questions.iterator();
while( it.hasNext() ) {
Question q = it.next();
if(q.getType() != Question.MULTIPLE_CHOICE)
it.remove();
}
Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion);
Button correctAnswerButton = (Button) findViewById(R.id.CorrectAnswer);
......@@ -95,11 +94,14 @@ public class GameActivity extends AppCompatActivity {
numberOfPlayers = ClientHolder.size();
playerAnswers = new int[numberOfPlayers];
playerAnswers = new PlayerAnswer[numberOfPlayers];
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i] = new PlayerAnswer(ClientHolder.get(i).getName());
}
for (int i=0;i<numberOfPlayers;i++) {
final TextView playerAnswerTextView = new TextView(this);
playerAnswerTextView.setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
playerAnswerTextView.setText("The answer of " + playerAnswers[i].getPlayerName() + ": ");
playerAnswerTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
playerAnswersLinearLayout.addView(playerAnswerTextView);
playerAnswerTextViews.add(playerAnswerTextView);
......@@ -122,13 +124,13 @@ public class GameActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
if (questionIndex < questions.size()) {
MultipleChoiceQuestion question = questions.get(questionIndex);
Question question = questions.get(questionIndex);
questionString = question.getQuestionText();
questionText.setText(questionString);
correctAnswerTextView.setText("Correct answer: " + question.getCorrectAnswer());
for (int i=0;i<numberOfPlayers;i++) {
playerAnswers[i] = -1;
playerAnswers[i].setValue(-1);
}
answers.clear();
......@@ -145,7 +147,7 @@ public class GameActivity extends AppCompatActivity {
}
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
playerAnswerTextViews.get(i).setText("The answer of " + playerAnswers[i].getPlayerName() + ": ");
outputs.get(i).println(questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##-1##-1##-1" );
}
......@@ -164,7 +166,7 @@ public class GameActivity extends AppCompatActivity {
public void onClick(View view) {
for (int i=0;i<outputs.size();i++) {
outputs.get(i).println(questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + Integer.toString(playerAnswers[i])
+ "##" + answers.get(2) + "##" + answers.get(3) + "##" + Integer.toString(playerAnswers[i].getValue())
+ "##" + "-1" + "##" + Integer.toString(correctAnswer) );
}
}
......@@ -199,7 +201,8 @@ public class GameActivity extends AppCompatActivity {
Log.d("GameActivity", "Klienstől kapott üzenet: " + read);
String[] strings = read.split("##");
int answerInt = Integer.parseInt(strings[0]);
playerAnswers[index] = answerInt;
playerAnswers[index].setValue(answerInt);
playerAnswers[index].setTime(Double.parseDouble(strings[1]));
updateConversationHandler.post(new GameActivity.updateUIThread(index,
answers.get(answerInt) + " (" + strings[1] + "s)"));
}
......@@ -222,8 +225,25 @@ public class GameActivity extends AppCompatActivity {
@Override
public void run() {
playerAnswerTextViews.get(index).setText("The answer of the " + Integer.toString(index + 1) + ". player: " + msg);
playerAnswerTextViews.get(index).setText("The answer of " + playerAnswers[index].getPlayerName() + ": " + msg);
}
}
public static List<Question> readQuestions(InputStream inputStream) {
List<Question> loadedQuestions = new ArrayList<>();
BufferedReader buffreader;
try {
buffreader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = buffreader.readLine()) != null) {
String[] parts = line.split("\t");
Question question = new Question(parts);
loadedQuestions.add(question);
}
} catch (IOException e) {
e.printStackTrace();
}
return loadedQuestions;
}
}
......@@ -14,19 +14,21 @@ import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import onlab.kvizserver.model.MultipleChoiceQuestion;
import onlab.kvizserver.model.Question;
public class QuizUpActivity extends AppCompatActivity {
private List<MultipleChoiceQuestion> questions = new ArrayList<>();
private List<Question> questions;
private int round = 0;
private int numberOfPlayers;
private final List<String> answers = new ArrayList<>();
......@@ -34,6 +36,7 @@ public class QuizUpActivity extends AppCompatActivity {
private int correctAnswer;
private String questionString;
private int[] playerScores;
private String[] playerNames;
private List<BufferedReader> inputs = new ArrayList<>();
private List<PrintWriter> outputs = new ArrayList<>();
......@@ -57,23 +60,25 @@ public class QuizUpActivity extends AppCompatActivity {
updateConversationHandler = new Handler();
BufferedReader buffreader;
InputStream inputStream = null;
try {
if (questionFileName == null) {
buffreader = new BufferedReader(new InputStreamReader(getAssets().open("multiple.txt")));
inputStream = getAssets().open("multiple.txt");
} else {
buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(questionFileName))));
}
String line;
while ((line = buffreader.readLine()) != null) {
String[] parts = line.split("\t");
MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion(
parts[0], parts[1], parts[2], parts[3], parts[4]);
questions.add(multipleChoiceQuestion);
inputStream = new FileInputStream(new File(questionFileName));
}
} catch (IOException e) {
e.printStackTrace();
}
questions = GameActivity.readQuestions(inputStream);
//nem feleletválasztós kérdések eltávolítása
Iterator<Question> it = questions.iterator();
while( it.hasNext() ) {
Question q = it.next();
if(q.getType() != Question.MULTIPLE_CHOICE)
it.remove();
}
questionText = (TextView) findViewById(R.id.QuestionText);
answerTextViews = new ArrayList<>();
......@@ -88,6 +93,11 @@ public class QuizUpActivity extends AppCompatActivity {
playerAnswers = new int[numberOfPlayers];
playerNames = new String[numberOfPlayers];
for (int i=0;i<numberOfPlayers;i++) {
playerNames[i] = ClientHolder.get(i).getName();
}
playerScores = new int[2];
for (int i=0;i<numberOfPlayers;i++) {
......@@ -132,7 +142,7 @@ public class QuizUpActivity extends AppCompatActivity {
private void sendNextQuestion() {
if (round < 7) {
MultipleChoiceQuestion question = questions.get(round);
Question question = questions.get(round);
questionString = question.getQuestionText();
questionText.setText(questionString);
......@@ -156,7 +166,7 @@ public class QuizUpActivity extends AppCompatActivity {
correctAnswerTextView.setText("The correct answer: ");
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
playerAnswerTextViews.get(i).setText("The answer of " + playerNames[i] + ": ");
String message = "question##" + Integer.toString(playerScores[i]) + "##"
+ Integer.toString(playerScores[1-i]) + "##" + questionString
+ "##" + answers.get(0) + "##" + answers.get(1)
......@@ -303,9 +313,9 @@ public class QuizUpActivity extends AppCompatActivity {
if (index == -1) {
correctAnswerTextView.setText("The correct answer: " + answers.get(correctAnswer));
} else {
playerAnswerTextViews.get(index).setText("The answer of the " + Integer.toString(index + 1) + ". player: " + msg);
playerAnswerTextViews.get(index).setText("The answer of " + playerNames[index] + ": " + msg);
playerScoreTextViews.get(index).setText(
"The score of the " + Integer.toString(index + 1) + ". player: " + Integer.toString(playerScores[index]));
"The score of " + playerNames[index] + ": " + Integer.toString(playerScores[index]));
}
}
}
......
package onlab.kvizserver.model;
public class GuessQuestion {
private String questionText;
private int correctAnswer;
public GuessQuestion(String questionText, int correctAnswer) {
this.questionText = questionText;
this.correctAnswer = correctAnswer;
}
public String getQuestionText() {
return questionText;
}
public int getCorrectAnswer() {
return correctAnswer;
}
}
package onlab.kvizserver.model;
public class PlayerAnswer {
private String playerName;
private int value;
private double time;
public PlayerAnswer(String playerName) {
this.playerName = playerName;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public double getTime() {
return time;
}
public void setTime(double time) {
this.time = time;
}
}
......@@ -4,17 +4,24 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MultipleChoiceQuestion {
public class Question {
private String topic;
private String questionText;
private String correctAnswer;
private List<String> otherAnswers = new ArrayList<>();
private boolean enabled;
public MultipleChoiceQuestion(String questionText, String correctAnswer, String ans2, String ans3, String ans4) {
this.questionText = questionText;
this.correctAnswer = correctAnswer;
otherAnswers.add(ans2);
otherAnswers.add(ans3);
otherAnswers.add(ans4);
public static final int MULTIPLE_CHOICE = 0;
public static final int GUESS = 1;
public Question(String[] data) {
topic = data[0];
questionText = data[1];
correctAnswer = data[2];
for (int i=3;i<data.length-1;i++) {
otherAnswers.add(data[i]);
}
enabled = data[data.length-1].equals("enabled");
}
public String getQuestionText() {
......@@ -32,4 +39,23 @@ public class MultipleChoiceQuestion {
Collections.shuffle(list);
return list;
}
public String getTopic() {
return topic;
}
public boolean isEnabled() {
return enabled;
}
public int getType() {
if (otherAnswers.size() == 3) {
return MULTIPLE_CHOICE;
} else if (otherAnswers.size() == 0) {
return GUESS;
} else {
return -1;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment