All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.stevesoft.pat.apps.ReGame Maven / Gradle / Ivy

The newest version!
//
// This software is now distributed according to
// the Lesser Gnu Public License.  Please see
// http://www.gnu.org/copyleft/lesser.txt for
// the details.
//    -- Happy Computing!
//
package com.stevesoft.pat.apps;
import com.stevesoft.pat.*;
import java.awt.*;
import java.util.*;
import java.net.*;

/** This is the applet for my ReGame page.*/
public class ReGame extends java.applet.Applet {
    static final String hide_ans = "????";
    /** @serial */
    Label pat_msg,ans_msg;
    /** @serial */
    public TextField pat, ans_txt;
    /** @serial */
    public TestGroup[] tgroup,tgroup2;
    /** @serial */
    public RegRes[] answers;
    /** @serial */
    int score;
    /** @serial */
    Label unreg;
    /** @serial */
    public TextField score_txt;
    /** @serial */
    public Button home_btn, redraw;
    String unescme(String in) {
        StringBuffer sb = new StringBuffer();
        int i;
        for(i=0;i');
                else if(c == 'b') sb.append('\\');
                else sb.append('_');
            } else sb.append(in.charAt(i));
        }
        return sb.toString();
    }
    void setScore(int s) {
        score = s;
        String st = "score: "+s;
        if(score_txt == null) {
            score_txt = new TextField(st);
            score_txt.setEditable(false);
        } else score_txt.setText(st);
    }
    void addScore(int s) {
        setScore(score + s);
    }
    /** @serial */
    boolean inited = false;
    public void init() {
        Panel user = new Panel();
        Panel ans  = new Panel();
        Panel header = new Panel();
        Panel body = new Panel();

        user.setBackground(Color.red);
        ans.setBackground(Color.green);
        body.setBackground(Color.blue);
        header.setBackground(Color.white);

        add(header);
        add(body);
        body.setLayout(new GridLayout(1,2));
        body.add(user);
        body.add(ans);

        pat_msg = new Label("Pattern");
        pat = new TextField();
        ans_msg = new Label("Answer");
        ans_txt = new TextField();
        ans_txt.setEditable(false);
        ans_txt.setText(hide_ans);

        // set title
        String umsg = null;
        umsg = getParameter("Title");
        if(umsg == null) umsg = "ReGame";
        unreg = new Label(umsg);

        unreg.setAlignment(unreg.CENTER);
        setScore(0);
        home_btn = new Button("About");
        redraw = new Button("Redraw");
        header.add(unreg);
        user.add(score_txt);
        score_txt.setEditable(false);
        ans.add(home_btn);
        ans.add(redraw);
        Panel p = new Panel();
        p.add(pat_msg);
        p.add(pat);
        user.add(p);
        Panel p2 = new Panel();
        p2.add(ans_msg);
        p2.add(ans_txt);
        ans.add(p2);

        // determine # of text groups
        int ngroups =
            (new Integer(getParameter("NGroups"))).intValue();
        tgroup = new TestGroup[ngroups];
        tgroup2 = new TestGroup[ngroups];
        answers = new RegRes[ngroups];
        int i;
        for(i=0;i 0) {
            d--;
            quizes.draw();
        }
        setScore(0);
        getquiz();
        game_over = false;
    }
    /** @serial */
    public Random qrand = new Random();
    /** @serial */
    int quizno = 0,p_len = 0;
    /** @serial */
    boolean game_over = false;
    /** @serial */
    public Deck quizes = null;
    Regex getquiz() {
        if(quizes.ncards() == 0) {
            score_txt.setText("Game Over: Score "+score+" of "+max_score);
            game_over = true;
            return null;
        }
        quizno = quizes.draw();

        pat.setText("");
        ans_txt.setText(hide_ans);

        String ps = unescme(getParameter("pat"+quizno));
        p_len = ps.length();
        Regex r = new Regex();
        RegSyntax rs = null;
        try {
            r.compile(ps);
        } catch(RegSyntax rst) {
            rs = rst;
        }
        //pat.setText(ps);
        for(int i=0;i p_len) {
                        sc = "Long Match: 5 pts";
                        addScore(5);
                    } else if(p.length() == p_len) {
                        sc = "Match: 10 pts";
                        addScore(10);
                    } else {
                        sc = "Short Match: 12 pts";
                        addScore(12);
                    }
                    mes.v.addElement(new Label(sc));
                } else {
                    System.out.println("user: "+res);
                    System.out.println("answ: "+answers[i]);
                    mes.v.addElement(new Label("No Match"));
                }
                max_score += 10;
            }
            //mes.v.addElement(new Label("answer: "+
                //unescme(getParameter("pat"+quizno))));
            mes.v.addElement(new Button("OK"));
            mes.ask(this);
            return true;
        }
        return false;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy