File: YesNoGame.java

package info (click to toggle)
kxml2 2.3.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,192 kB
  • sloc: java: 5,607; xml: 104; makefile: 6
file content (94 lines) | stat: -rw-r--r-- 2,802 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * @author Stefan Haustein
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 */
import java.io.*;
import org.xmlpull.v1.*;
import org.kxml2.io.*;

class Node {
    private String text;
    private Node yes;  // if null, it's an answer
    private Node no;
    
    Node(String answer) {
        this.text = answer;
    }
    
    Node(String question, Node yes, Node no) {
        this.text = question;
        this.yes = yes;
        this.no = no;        
    }
    
    void run () throws IOException {
       
        if (yes == null) 
            System.out.println ("Answer: "+text);
        else {
            System.out.println (text+ " (y/n)");    

            while (true) {
                int i = System.in.read();
                if (i == 'y' || i == 'Y') {
                    yes.run();
                    break;
                }
                else if (i == 'n' || i == 'N') {
                    no.run();            
                    break;
                }
            }
        }                
    }

}

public class YesNoGame {

    public static Node parseAnswer(XmlPullParser p) throws IOException, XmlPullParserException {
        p.require(XmlPullParser.START_TAG, "", "answer");
        Node result = new Node (p.nextText());
        p.require(XmlPullParser.END_TAG, "", "answer");
        return result;
    }

    public static Node parseQuestion(XmlPullParser p) throws IOException, XmlPullParserException {
        p.require(XmlPullParser.START_TAG, "", "question");
        String text = p.getAttributeValue("", "text");
        Node yes = parseNode (p);
        Node no = parseNode (p);
        p.nextTag();
        p.require(XmlPullParser.END_TAG, "", "question");
        return new Node (text, yes, no);
    }

    public static Node parseNode (XmlPullParser p) throws IOException, XmlPullParserException {
        p.nextTag ();
        p.require(XmlPullParser.START_TAG, "", null);
        if (p.getName().equals("question"))
            return parseQuestion(p);
        else 
            return parseAnswer(p);       
    }


    public static void main(String[] args) throws IOException, XmlPullParserException {
        String sample = "<question text='Is it round?'>\n"
                      + " <question text='Is it bright?'>\n"
                      + "  <answer>It is the Sun!</answer>\n"
                      + "  <answer>It is a ball!</answer>\n"
                      + " </question>\n"
                      + " <answer>I do not know!</answer>\n"
                      + "</question>\n";

        XmlPullParser p = new KXmlParser();
        p.setInput (new StringReader (sample));
        
        Node game = parseNode (p);
        
        game.run();        
    }
}