Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #99 from MninaTB/feat/game_screen_lade_fragen
Browse files Browse the repository at this point in the history
feat(src/controller): ermoegliche beantworten von Fragen
  • Loading branch information
miyunari authored Jun 7, 2021
2 parents 5f8f6d4 + ad96481 commit 4c8adfb
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/controller/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void initStartButton(Share share) {
}

private void updateStartButton(Share share) {
final int levelFK = 10;
final int levelFK = 9;
final int maxLevel = 5;
StateBuilder s = new StateBuilder(store);
if (s.verify(this.selectedCategories, levelFK, maxLevel, startLevel)) {
Expand Down
154 changes: 103 additions & 51 deletions src/controller/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class Game implements Controller {

private Switcher switcher;
private view.Game view;
private State state;
private Question question;
private Share share;

public Game(Switcher s, QuestionStore store) {
this.switcher = s;
Expand All @@ -34,6 +37,7 @@ public Game(Switcher s, QuestionStore store) {
* Initialisiert das Start view element
*/
public void init(Share share) {
this.share = share;
this.view = new view.Game();

final String key = "KEY_GAME_STATE";
Expand All @@ -43,28 +47,22 @@ public void init(Share share) {
System.out.println("missing game state");
System.exit(1);
}
State s = (State) share.get(key);
this.state = (State) share.get(key);

Question q = s.next();
if (q == null) {
this.next();
if (this.question == null) {
// TODO: maybe we should allow a screen switch from
// the init func.
System.out.println("game: question is null");
System.exit(1);
}
ArrayList<String> answers = q.getAnswers();

this.initQuestion(share);

this.initHomeButton();
this.initTitleLabel();
this.initExitButton();
this.initQuestionLabel();
this.initAnswerNo1Button(answers.get(0));
this.initAnswerNo2Button(answers.get(1));
this.initAnswerNo3Button(answers.get(2));
this.initAnswerNo4Button(answers.get(3));
this.initCategoryLabel();
this.initCurrentCategoryLabel();
this.initLevelLabel();
this.initLevelNo1Label();
this.initLevelNo2Label();
Expand All @@ -73,6 +71,65 @@ public void init(Share share) {
this.initLevelNo5Label();
this.initJokerButton();
}

private void next() {
this.question = this.state.next();
this.initQuestion(this.share);
}

private void initQuestion(Share share) {

ActionListener right = new ActionListener() {
public void actionPerformed(ActionEvent e) {
share.put("KEY_GAME_WIN", new Object());
next();
if (question == null) {
switcher.next(Screen.SCREEN_RESULT);
}
}
};

ActionListener wrong = new ActionListener() {
public void actionPerformed(ActionEvent e) {
share.put("KEY_GAME_QUESTION", question);
switcher.next(Screen.SCREEN_RESULT);
}
};


ArrayList<String> answers = this.question.getAnswers();

this.initQuestionLabel(this.question.getQuestion());

this.initCurrentCategoryLabel(this.question.getCategory().toString());
switch (this.question.getSolution()) {
case 0:
this.initAnswerNo1Button(answers.get(0), right);
this.initAnswerNo2Button(answers.get(1), wrong);
this.initAnswerNo3Button(answers.get(2), wrong);
this.initAnswerNo4Button(answers.get(3), wrong);
break;
case 1:
this.initAnswerNo1Button(answers.get(0), wrong);
this.initAnswerNo2Button(answers.get(1), right);
this.initAnswerNo3Button(answers.get(2), wrong);
this.initAnswerNo4Button(answers.get(3), wrong);
break;
case 2:
this.initAnswerNo1Button(answers.get(0), wrong);
this.initAnswerNo2Button(answers.get(1), wrong);
this.initAnswerNo3Button(answers.get(2), right);
this.initAnswerNo4Button(answers.get(3), wrong);
break;
case 3:
this.initAnswerNo1Button(answers.get(0), wrong);
this.initAnswerNo2Button(answers.get(1), wrong);
this.initAnswerNo3Button(answers.get(2), wrong);
this.initAnswerNo4Button(answers.get(3), right);
break;
default:
}
}

/**
* Initialisiert den Home-Button
Expand All @@ -82,13 +139,6 @@ public void initHomeButton() {
this.view.getHomeButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switcher.next(Screen.SCREEN_START);

// Reaktiviert Joker Button wenn man zurueck zum Start Screen geht und spiel neu
// startet
// Dies muss auch am Ende eines Spiels passieren also immer wenn man Game Screen
// verlaesst
view.getJokerButton().setEnabled(true);
view.getJokerButton().setText("50/50");
}
});
}
Expand All @@ -115,60 +165,44 @@ public void actionPerformed(ActionEvent e) {
/**
* Initialisiert das Question-Label
*/
public void initQuestionLabel() {
this.view.getQuestionLabel().setText("Wie lautet die erste Frage?");// TODO muss automatisiert werden mit Frage
// laden
public void initQuestionLabel(String q) {
this.view.getQuestionLabel().setText(q);
}

/**
* Initialisiert den AnswerNo1-Button
*/
public void initAnswerNo1Button(String a) {
public void initAnswerNo1Button(String a, ActionListener al) {
this.view.getAnswerNo1Button().setText(a);
this.view.getAnswerNo1Button().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Temporaer Result-Screen auf AnswerNo1-Button gelegt zum Testen, wieder
// entfernen bei Spiel-Implementation
switcher.next(Screen.SCREEN_RESULT);
// TODO hier Spielfunktion am besten auf eine gemeinsame Methode implementieren
}
});
this.view.getAnswerNo1Button().setVisible(true);
this.view.getAnswerNo1Button().addActionListener(al);
}

/**
* Initialisiert den AnswerNo2-Button
*/
public void initAnswerNo2Button(String a) {
public void initAnswerNo2Button(String a, ActionListener al) {
this.view.getAnswerNo2Button().setText(a);
this.view.getAnswerNo2Button().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO hier Spielfunktion am besten auf eine gemeinsame Methode implementieren
}
});
this.view.getAnswerNo2Button().setVisible(true);
this.view.getAnswerNo2Button().addActionListener(al);
}

/**
* Initialisiert den AnswerNo3-Button
*/
public void initAnswerNo3Button(String a) {
public void initAnswerNo3Button(String a, ActionListener al) {
this.view.getAnswerNo3Button().setText(a);
this.view.getAnswerNo3Button().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO hier Spielfunktion am besten auf eine gemeinsame Methode implementieren
}
});
this.view.getAnswerNo3Button().setVisible(true);
this.view.getAnswerNo3Button().addActionListener(al);
}

/**
* Initialisiert den AnswerNo4-Button
*/
public void initAnswerNo4Button(String a) {
public void initAnswerNo4Button(String a, ActionListener al) {
this.view.getAnswerNo4Button().setText(a);
this.view.getAnswerNo4Button().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO hier Spielfunktion am besten auf eine gemeinsame Methode implementieren
}
});
this.view.getAnswerNo4Button().setVisible(true);
this.view.getAnswerNo4Button().addActionListener(al);
}

/**
Expand All @@ -181,8 +215,8 @@ public void initCategoryLabel() {
/**
* Initialisiert das CurrentCategory-Label
*/
public void initCurrentCategoryLabel() {
this.view.getCurrentCategoryLabel().setText(" WGP");// TODO muss automatisiert werden mit Frage laden
public void initCurrentCategoryLabel(String c) {
this.view.getCurrentCategoryLabel().setText(" " + c);
}

/**
Expand Down Expand Up @@ -231,11 +265,29 @@ public void initLevelNo5Label() {
* Initialisiert den Joker-Button
*/
public void initJokerButton() {
this.view.getJokerButton().setEnabled(true);
this.view.getJokerButton().setText("50/50");
this.view.getJokerButton().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO hier Jokerfunktion implementieren

switch (question.getSolution()) {
case 0:
view.getAnswerNo3Button().setVisible(false);
view.getAnswerNo4Button().setVisible(false);
break;
case 1:
view.getAnswerNo3Button().setVisible(false);
view.getAnswerNo4Button().setVisible(false);
break;
case 2:
view.getAnswerNo1Button().setVisible(false);
view.getAnswerNo4Button().setVisible(false);
break;
case 3:
view.getAnswerNo1Button().setVisible(false);
view.getAnswerNo2Button().setVisible(false);
break;
default:
}
view.getJokerButton().setEnabled(false);
view.getJokerButton().setText("X");
view.getJokerButton().setFont(new Font("Arial", Font.BOLD, 150));
Expand Down
44 changes: 27 additions & 17 deletions src/controller/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javax.swing.JPanel;

import model.Question;
import questions.QuestionStore;

/**
Expand All @@ -28,26 +29,35 @@ public Result(Switcher s, QuestionStore store) {
*/
public void init(Share share) {
this.view = new view.Result();
this.initResultLabel();
this.initLevelLeftLabel();
this.initLevelRightLabel();
this.initScoreLeftLabel();
this.initScoreRightLabel();
this.initQuestionLeftLabel();
this.initQuestionRightLabel();
this.initPlayerAnswerLeftLabel();
this.initPlayerAnswerRightLabel();
this.initCorrectAnswerLeftLabel();
this.initCorrectAnswerRightLabel();

Question q = (Question) share.get("KEY_GAME_QUESTION");
boolean win = share.get("KEY_GAME_WIN") != null;
this.initResultLabel(win);
if (!win) {
this.initLevelLeftLabel();
this.initLevelRightLabel();
this.initScoreLeftLabel();
this.initScoreRightLabel();
this.initQuestionLeftLabel();
this.initQuestionRightLabel(q.getAnswers().get(q.getSolution()));
this.initPlayerAnswerLeftLabel();
this.initPlayerAnswerRightLabel();
this.initCorrectAnswerLeftLabel();
this.initCorrectAnswerRightLabel(q.getAnswers().get(q.getSolution()));
}
this.initHomeButton();
this.initExitButton();
}

/**
* Initialisiert das Result-Label
*/
public void initResultLabel() {
this.view.getResultLabel().setText("Verloren");
public void initResultLabel(boolean win) {
this.view.getResultLabel().setText("Verloren!");
if (win) {
this.view.getResultLabel().setText("Gewonnen!");
}

}

/**
Expand Down Expand Up @@ -88,8 +98,8 @@ public void initQuestionLeftLabel() {
/**
* Initialisiert das QuestionRight-Label
*/
public void initQuestionRightLabel() {
this.view.getQuestionRightLabel().setText("Wie lautet die erste Frage?");
public void initQuestionRightLabel(String question) {
this.view.getQuestionRightLabel().setText(question);
}

/**
Expand All @@ -116,8 +126,8 @@ public void initCorrectAnswerLeftLabel() {
/**
* Initialisiert das CorrectAnswerRight-Label
*/
public void initCorrectAnswerRightLabel() {
this.view.getCorrectAnswerRightLabel().setText("Vierte Antwort");
public void initCorrectAnswerRightLabel(String solution) {
this.view.getCorrectAnswerRightLabel().setText(solution);
}

/**
Expand Down

0 comments on commit 4c8adfb

Please sign in to comment.