|
12 | 12 | import org.teachingkidsprogramming.recipes.quizzes.graders.AdLibsQuizAdapter.Pieces; |
13 | 13 |
|
14 | 14 | public class AdLibsQuizGrader implements Paintable { |
15 | | - private static class Model { |
16 | | - public String three; |
17 | | - |
18 | | - public Model(String three) { |
19 | | - this.three = three; |
20 | | - } |
21 | | - } |
22 | | - |
23 | | - private boolean[] answers; |
24 | | - public static int TURTLE_SPEED = 9; |
25 | | - private AdLibsQuizAdapter quiz; |
26 | | - |
27 | | - private void displayScreen() { |
28 | | - QuizUtils.prepareScoringScreen(answers, this, TURTLE_SPEED); |
29 | | - } |
30 | | - |
31 | | - public void grade(AdLibsQuizAdapter quiz) { |
32 | | - this.quiz = quiz; |
33 | | - answers = new boolean[] { grade1You(), grade2Won(), grade3The(), |
34 | | - grade4Game() }; |
35 | | - displayScreen(); |
36 | | - } |
37 | | - |
38 | | - public void paint(Graphics2D g, JPanel caller) { |
39 | | - QuizUtils.displayScores(g, 300, answers); |
40 | | - Tortoise.hide(); |
41 | | - drawRewardShape(g); |
42 | | - } |
43 | | - |
44 | | - public void drawRewardShape(Graphics2D g) { |
45 | | - drawYou(g); |
46 | | - drawWin(g); |
47 | | - drawThe(g); |
48 | | - drawGame(g); |
49 | | - } |
50 | | - |
51 | | - private void drawGame(Graphics2D g) { |
52 | | - quiz.template4 = ""; |
53 | | - Pieces pieces = new Pieces(); |
54 | | - quiz.question4(pieces); |
55 | | - pieces.middle = "am"; |
56 | | - String word = Parser.parse(quiz.template4, pieces); |
57 | | - drawWord(g, word, 0, 4, true); |
58 | | - } |
59 | | - |
60 | | - private void drawThe(Graphics2D g) { |
61 | | - quiz.word3 = ""; |
62 | | - Pieces model = new Pieces(); |
63 | | - model.middle = "H"; |
64 | | - quiz.question3("T{middle}E", model); |
65 | | - drawWord(g, quiz.word3, 3, 2, false); |
66 | | - } |
67 | | - |
68 | | - private void drawWin(Graphics2D g) { |
69 | | - quiz.word2 = "WO"; |
70 | | - quiz.question2("n"); |
71 | | - drawWord(g, quiz.word2, 1, 0, false); |
72 | | - } |
73 | | - |
74 | | - private void drawWord(Graphics2D g, String word, int x, int y, |
75 | | - boolean horizontal) { |
76 | | - char[] letters = word.toUpperCase().toCharArray(); |
77 | | - int dx = horizontal ? 1 : 0; |
78 | | - int dy = horizontal ? 0 : 1; |
79 | | - for (int i = 0; i < letters.length; i++) { |
80 | | - char c = letters[i]; |
81 | | - drawLetter(getPosition(x + dx * i), getPosition(y + dy * i), c, g); |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - private int getPosition(int i) { |
86 | | - return 100 + i * 53; |
87 | | - } |
88 | | - |
89 | | - private void drawYou(Graphics2D g) { |
90 | | - quiz.word1 = "NOT"; |
91 | | - quiz.question1("y", "u"); |
92 | | - drawWord(g, quiz.word1, 0, 1, true); |
93 | | - } |
94 | | - |
95 | | - private void drawLetter(int x, int y, char c, Graphics2D g) { |
96 | | - g.setColor(PenColors.Browns.BurlyWood); |
97 | | - g.drawRect(x, y, 50, 50); |
98 | | - g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36)); |
99 | | - int charWidth = g.getFontMetrics().charWidth(c); |
100 | | - int charHeight = g.getFontMetrics().getAscent(); |
101 | | - int textY = y + (40 - charHeight) / 2 + charHeight; |
102 | | - int textX = x + (50 - charWidth) / 2; |
103 | | - g.drawString("" + c, textX, textY); |
104 | | - } |
105 | | - |
106 | | - private boolean grade1You() { |
107 | | - quiz.word1 = "fake"; |
108 | | - quiz.question1("f", "o"); |
109 | | - return "foo".equals(quiz.word1); |
110 | | - } |
111 | | - |
112 | | - private boolean grade2Won() { |
113 | | - quiz.word2 = "passe"; |
114 | | - quiz.question2("d"); |
115 | | - return "passed".equals(quiz.word2); |
116 | | - } |
117 | | - |
118 | | - private boolean grade3The() { |
119 | | - quiz.word3 = "fake"; |
120 | | - quiz.question3("12{three}4", new Model("3")); |
121 | | - return "1234".equals(quiz.word3); |
122 | | - } |
123 | | - |
124 | | - private boolean grade4Game() { |
125 | | - quiz.template4 = "fake"; |
126 | | - quiz.question4(new Pieces()); |
127 | | - return "g{middle}e".equals(quiz.template4); |
128 | | - } |
| 15 | + private static class Model { |
| 16 | + @SuppressWarnings("unused") |
| 17 | + public String three; |
| 18 | + |
| 19 | + public Model(String three) { |
| 20 | + this.three = three; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + private boolean[] answers; |
| 25 | + public static int TURTLE_SPEED = 9; |
| 26 | + private AdLibsQuizAdapter quiz; |
| 27 | + |
| 28 | + private void displayScreen() { |
| 29 | + QuizUtils.prepareScoringScreen(answers, this, TURTLE_SPEED); |
| 30 | + } |
| 31 | + |
| 32 | + public void grade(AdLibsQuizAdapter quiz) { |
| 33 | + this.quiz = quiz; |
| 34 | + answers = new boolean[] { grade1You(), grade2Won(), grade3The(), |
| 35 | + grade4Game() }; |
| 36 | + displayScreen(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void paint(Graphics2D g, JPanel caller) { |
| 41 | + QuizUtils.displayScores(g, 300, answers); |
| 42 | + Tortoise.hide(); |
| 43 | + drawRewardShape(g); |
| 44 | + } |
| 45 | + |
| 46 | + public void drawRewardShape(Graphics2D g) { |
| 47 | + drawYou(g); |
| 48 | + drawWin(g); |
| 49 | + drawThe(g); |
| 50 | + drawGame(g); |
| 51 | + } |
| 52 | + |
| 53 | + private void drawGame(Graphics2D g) { |
| 54 | + quiz.template4 = ""; |
| 55 | + Pieces pieces = new Pieces(); |
| 56 | + quiz.question4(pieces); |
| 57 | + pieces.middle = "am"; |
| 58 | + String word = Parser.parse(quiz.template4, pieces); |
| 59 | + drawWord(g, word, 0, 4, true); |
| 60 | + } |
| 61 | + |
| 62 | + private void drawThe(Graphics2D g) { |
| 63 | + quiz.word3 = ""; |
| 64 | + Pieces model = new Pieces(); |
| 65 | + model.middle = "H"; |
| 66 | + quiz.question3("T{middle}E", model); |
| 67 | + drawWord(g, quiz.word3, 3, 2, false); |
| 68 | + } |
| 69 | + |
| 70 | + private void drawWin(Graphics2D g) { |
| 71 | + quiz.word2 = "WO"; |
| 72 | + quiz.question2("n"); |
| 73 | + drawWord(g, quiz.word2, 1, 0, false); |
| 74 | + } |
| 75 | + |
| 76 | + private void drawWord(Graphics2D g, String word, int x, int y, |
| 77 | + boolean horizontal) { |
| 78 | + char[] letters = word.toUpperCase().toCharArray(); |
| 79 | + int dx = horizontal ? 1 : 0; |
| 80 | + int dy = horizontal ? 0 : 1; |
| 81 | + for (int i = 0; i < letters.length; i++) { |
| 82 | + char c = letters[i]; |
| 83 | + drawLetter(getPosition(x + dx * i), getPosition(y + dy * i), c, g); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private int getPosition(int i) { |
| 88 | + return 100 + i * 53; |
| 89 | + } |
| 90 | + |
| 91 | + private void drawYou(Graphics2D g) { |
| 92 | + quiz.word1 = "NOT"; |
| 93 | + quiz.question1("y", "u"); |
| 94 | + drawWord(g, quiz.word1, 0, 1, true); |
| 95 | + } |
| 96 | + |
| 97 | + private void drawLetter(int x, int y, char c, Graphics2D g) { |
| 98 | + g.setColor(PenColors.Browns.BurlyWood); |
| 99 | + g.drawRect(x, y, 50, 50); |
| 100 | + g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 36)); |
| 101 | + int charWidth = g.getFontMetrics().charWidth(c); |
| 102 | + int charHeight = g.getFontMetrics().getAscent(); |
| 103 | + int textY = y + (40 - charHeight) / 2 + charHeight; |
| 104 | + int textX = x + (50 - charWidth) / 2; |
| 105 | + g.drawString("" + c, textX, textY); |
| 106 | + } |
| 107 | + |
| 108 | + private boolean grade1You() { |
| 109 | + quiz.word1 = "fake"; |
| 110 | + quiz.question1("f", "o"); |
| 111 | + return "foo".equals(quiz.word1); |
| 112 | + } |
| 113 | + |
| 114 | + private boolean grade2Won() { |
| 115 | + quiz.word2 = "passe"; |
| 116 | + quiz.question2("d"); |
| 117 | + return "passed".equals(quiz.word2); |
| 118 | + } |
| 119 | + |
| 120 | + private boolean grade3The() { |
| 121 | + quiz.word3 = "fake"; |
| 122 | + quiz.question3("12{three}4", new Model("3")); |
| 123 | + return "1234".equals(quiz.word3); |
| 124 | + } |
| 125 | + |
| 126 | + private boolean grade4Game() { |
| 127 | + quiz.template4 = "fake"; |
| 128 | + quiz.question4(new Pieces()); |
| 129 | + return "g{middle}e".equals(quiz.template4); |
| 130 | + } |
129 | 131 | } |
0 commit comments