Package memo

Source Code of memo.Memorizator

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package memo;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
import org.netbeans.microedition.lcdui.SplashScreen;

/**
* @author Bohdan Penkovsky <bohdan777@penkovsky.mail.ua>
*/
public class Memorizator extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private MemorizatorGame game;
    private static final String STORAGE_NAME = "HighestMemoScores";

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private List list;
    private SplashScreen splashScreen;
    private SplashScreen splashScreen1;
    private TextBox textBox;
    private SplashScreen splashScreen2;
    private List list1;
    private TextBox textBox2;
    private Form form;
    private Form form1;
    private Command exitCommand;
    private Command okCommand;
    private Command backCommand;
    private Command okCommand1;
    private Command okCommand2;
    private Font font1;
    private Image image1;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The Memorizator constructor.
     */
    public Memorizator() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
        splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|0-initialize|1|0-postInitialize
        splashScreen.setTitle("Memorizator");
        splashScreen.setCommandListener(this);
        splashScreen.setFullScreenMode(true);
        splashScreen.setTextFont(getFont1());
        splashScreen.setTimeout(899);
        splashScreen.setAllowTimeoutInterrupt(false);
        splashScreen1 = new SplashScreen(getDisplay());
        splashScreen1.setTitle("Memorize!");
        splashScreen1.setCommandListener(this);
        splashScreen1.setFullScreenMode(true);
        splashScreen1.setTextFont(getFont1());
        splashScreen1.setAllowTimeoutInterrupt(false);
        textBox = new TextBox("Enter memorized", new String(), 1024, TextField.NUMERIC);
        textBox.addCommand(getOkCommand());
        textBox.addCommand(getExitCommand());
        textBox.setCommandListener(this);
        textBox2 = new TextBox("Enter your name", null, 100, TextField.ANY);
        textBox2.addCommand(getOkCommand1());
        textBox2.setCommandListener(this);
        form = new Form("High Scores");
        form.addCommand(getOkCommand2());
        form.setCommandListener(this);//GEN-END:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
        splashScreen.setColorSchema(MemorizatorColorSchema.getForDisplay(getDisplay()));
        splashScreen1.setColorSchema(MemorizatorColorSchema.getForDisplay(getDisplay()));
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getList());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
        switchDisplayable(null, getList());//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|101-preAction
            if (command == okCommand2) {//GEN-END:|7-commandAction|1|101-preAction
                // write pre-action user code here
                switchDisplayable(null, getList());//GEN-LINE:|7-commandAction|2|101-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|105-preAction
        } else if (displayable == form1) {
            if (command == okCommand2) {//GEN-END:|7-commandAction|3|105-preAction
                // write pre-action user code here
                switchDisplayable(null, getList());//GEN-LINE:|7-commandAction|4|105-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|16-preAction
        } else if (displayable == list) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|5|16-preAction
                // write pre-action user code here
                listAction();//GEN-LINE:|7-commandAction|6|16-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|44-preAction
        } else if (displayable == list1) {
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|7|44-preAction
                // write pre-action user code here
                list1Action();//GEN-LINE:|7-commandAction|8|44-postAction
                // write post-action user code here
            } else if (command == backCommand) {//GEN-LINE:|7-commandAction|9|53-preAction
                // write pre-action user code here
                switchDisplayable(null, getList());//GEN-LINE:|7-commandAction|10|53-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|11|24-preAction
        } else if (displayable == splashScreen) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|11|24-preAction
                // write pre-action user code here

                game.outputForMemorization(this,splashScreen1);

//GEN-LINE:|7-commandAction|12|24-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|13|27-preAction
        } else if (displayable == splashScreen1) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|13|27-preAction
                // write pre-action user code here
                textBox.setString("");

                while(!game.outputFinished()){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                    }
                }
                switchDisplayable(null, textBox);//GEN-LINE:|7-commandAction|14|27-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|15|38-preAction
        } else if (displayable == splashScreen2) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|15|38-preAction
                // write pre-action user code here
                switchDisplayable(null, getList());//GEN-LINE:|7-commandAction|16|38-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|17|31-preAction
        } else if (displayable == textBox) {
            if (command == exitCommand) {//GEN-END:|7-commandAction|17|31-preAction
                // write pre-action user code here
                switchDisplayable(null, getList1());//GEN-LINE:|7-commandAction|18|31-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|19|35-preAction
                // write pre-action user code here
                 //analyzeEnteredSequence should return comparison result and minus lives or add score
                if (game.analyzeEnteredSequence( textBox.getString() )) {
                    splashScreen.setText("Level " + game.getLevel());

                    switchDisplayable(null, splashScreen);
                } else {
                    if(game.hasLivesLeft()) {
                         switchDisplayable(new Alert(game.getSequence()), splashScreen);
                    } else {
                         switchDisplayable(new Alert("Lost! " + game.getSequence()), textBox2);
                    }
                }
//GEN-LINE:|7-commandAction|20|35-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|21|82-preAction
        } else if (displayable == textBox2) {
            if (command == okCommand1) {//GEN-END:|7-commandAction|21|82-preAction
                // write pre-action user code here
                updateHighScores();
                switchDisplayable(null, getList1());//GEN-LINE:|7-commandAction|22|82-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|23|7-postCommandAction
        }//GEN-END:|7-commandAction|23|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|24|
    //</editor-fold>//GEN-END:|7-commandAction|24|





    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: list ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initiliazed instance of list component.
     * @return the initialized component instance
     */
    public List getList() {
        if (list == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            list = new List("Memorizator v.1.2", Choice.IMPLICIT);//GEN-BEGIN:|14-getter|1|14-postInit
            list.append("Play", null);
            list.append("High Scores", null);
            list.append("Credits", null);
            list.append("Help", null);
            list.append("Exit", null);
            list.setCommandListener(this);
            list.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            list.setSelectedFlags(new boolean[] { false, false, false, false, false });//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return list;
    }
    //</editor-fold>//GEN-END:|14-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listAction ">//GEN-BEGIN:|14-action|0|14-preAction
    /**
     * Performs an action assigned to the selected list element in the list component.
     */
    public void listAction() {//GEN-END:|14-action|0|14-preAction
        // enter pre-action user code here
        String __selectedString = getList().getString(getList().getSelectedIndex());//GEN-BEGIN:|14-action|1|19-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Play")) {//GEN-END:|14-action|1|19-preAction
                // write pre-action user code here
                switchDisplayable(null, getList1());//GEN-LINE:|14-action|2|19-postAction
                // write post-action user code here
            } else if (__selectedString.equals("High Scores")) {//GEN-LINE:|14-action|3|69-preAction
                // write pre-action user code here
                form.deleteAll();
                form.append(loadHighScores());
                switchDisplayable(null, form);//GEN-LINE:|14-action|4|69-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Credits")) {//GEN-LINE:|14-action|5|36-preAction
                // write pre-action user code here
                switchDisplayable(null, getSplashScreen2());//GEN-LINE:|14-action|6|36-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Help")) {//GEN-LINE:|14-action|7|103-preAction
                // write pre-action user code here
                switchDisplayable(null, getForm1());//GEN-LINE:|14-action|8|103-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Exit")) {//GEN-LINE:|14-action|9|20-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|14-action|10|20-postAction
                // write post-action user code here
            }//GEN-BEGIN:|14-action|11|14-postAction
        }//GEN-END:|14-action|11|14-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|14-action|12|
    //</editor-fold>//GEN-END:|14-action|12|



    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 1);//GEN-LINE:|30-getter|1|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|30-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|34-getter|0|34-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|34-getter|0|34-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|34-getter|1|34-postInit
            // write post-init user code here
        }//GEN-BEGIN:|34-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|34-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen2 ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initiliazed instance of splashScreen2 component.
     * @return the initialized component instance
     */
    public SplashScreen getSplashScreen2() {
        if (splashScreen2 == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            splashScreen2 = new SplashScreen(getDisplay());//GEN-BEGIN:|37-getter|1|37-postInit
            splashScreen2.setTitle("Bohdan Penkovsky");
            splashScreen2.setCommandListener(this);
            splashScreen2.setFullScreenMode(true);
            splashScreen2.setImage(getImage1());
            splashScreen2.setTextFont(getFont1());//GEN-END:|37-getter|1|37-postInit
            // write post-init user code here
            splashScreen2.setColorSchema(MemorizatorColorSchema.getForDisplay(getDisplay()));
        }//GEN-BEGIN:|37-getter|2|
        return splashScreen2;
    }
    //</editor-fold>//GEN-END:|37-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initiliazed instance of image1 component.
     * @return the initialized component instance
     */
    public Image getImage1() {
        if (image1 == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|41-getter|1|41-@java.io.IOException
                image1 = Image.createImage("/pics/pba.png");
            } catch (java.io.IOException e) {//GEN-END:|41-getter|1|41-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|41-getter|2|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|3|
        return image1;
    }
    //</editor-fold>//GEN-END:|41-getter|3|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: font1 ">//GEN-BEGIN:|42-getter|0|42-preInit
    /**
     * Returns an initiliazed instance of font1 component.
     * @return the initialized component instance
     */
    public Font getFont1() {
        if (font1 == null) {//GEN-END:|42-getter|0|42-preInit
            // write pre-init user code here
            font1 = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);//GEN-LINE:|42-getter|1|42-postInit
            // write post-init user code here

        }//GEN-BEGIN:|42-getter|2|
        return font1;
    }
    //</editor-fold>//GEN-END:|42-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: list1 ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of list1 component.
     * @return the initialized component instance
     */
    public List getList1() {
        if (list1 == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            list1 = new List("Select game", Choice.IMPLICIT);//GEN-BEGIN:|43-getter|1|43-postInit
            list1.append("Normal", null);
            list1.append("Advanced", null);
            list1.append("One way", null);
            list1.append("Learn Pi", null);
            list1.addCommand(getBackCommand());
            list1.setCommandListener(this);
            list1.setSelectedFlags(new boolean[] { false, false, false, false });//GEN-END:|43-getter|1|43-postInit
            // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return list1;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: list1Action ">//GEN-BEGIN:|43-action|0|43-preAction
    /**
     * Performs an action assigned to the selected list element in the list1 component.
     */
    public void list1Action() {//GEN-END:|43-action|0|43-preAction
        // enter pre-action user code here
        String __selectedString = getList1().getString(getList1().getSelectedIndex());//GEN-BEGIN:|43-action|1|46-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Normal")) {//GEN-END:|43-action|1|46-preAction
                // write pre-action user code here
                game = new MemorizatorGameNormal();
                splashScreen.setText("Level " + game.getLevel());
                switchDisplayable(null, splashScreen);//GEN-LINE:|43-action|2|46-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Advanced")) {//GEN-LINE:|43-action|3|59-preAction
                // write pre-action user code here
                game = new MemorizatorGameAdvanced();
                splashScreen.setText("Level " + game.getLevel());
                switchDisplayable(null, splashScreen);//GEN-LINE:|43-action|4|59-postAction
                // write post-action user code here
            } else if (__selectedString.equals("One way")) {//GEN-LINE:|43-action|5|87-preAction
                // write pre-action user code here
                game = new MemorizatorGameOneway();
                splashScreen.setText("Level " + game.getLevel());
                switchDisplayable(null, splashScreen);//GEN-LINE:|43-action|6|87-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Learn Pi")) {//GEN-LINE:|43-action|7|47-preAction
                // write pre-action user code here

                //TextBox whatLevel = new TextBox("Choose score", "3", 3, TextField.NUMERIC);
                game = new MemorizatorGamePi(3); //Integer.parseInt(whatLevel.getString())
                //switchDisplayable(null, whatLevel);

                splashScreen.setText("Level " + game.getLevel());

                switchDisplayable(null, splashScreen);//GEN-LINE:|43-action|8|47-postAction
                // write post-action user code here
            }//GEN-BEGIN:|43-action|9|43-postAction
        }//GEN-END:|43-action|9|43-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|43-action|10|
    //</editor-fold>//GEN-END:|43-action|10|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of backCommand component.
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|52-getter|1|52-postInit
            // write post-init user code here
        }//GEN-BEGIN:|52-getter|2|
        return backCommand;
    }
    //</editor-fold>//GEN-END:|52-getter|2|











    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">//GEN-BEGIN:|81-getter|0|81-preInit
    /**
     * Returns an initiliazed instance of okCommand1 component.
     * @return the initialized component instance
     */
    public Command getOkCommand1() {
        if (okCommand1 == null) {//GEN-END:|81-getter|0|81-preInit
            // write pre-init user code here
            okCommand1 = new Command("Ok", Command.OK, 0);//GEN-LINE:|81-getter|1|81-postInit
            // write post-init user code here
        }//GEN-BEGIN:|81-getter|2|
        return okCommand1;
    }
    //</editor-fold>//GEN-END:|81-getter|2|









    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand2 ">//GEN-BEGIN:|100-getter|0|100-preInit
    /**
     * Returns an initiliazed instance of okCommand2 component.
     * @return the initialized component instance
     */
    public Command getOkCommand2() {
        if (okCommand2 == null) {//GEN-END:|100-getter|0|100-preInit
            // write pre-init user code here
            okCommand2 = new Command("Ok", Command.OK, 0);//GEN-LINE:|100-getter|1|100-postInit
            // write post-init user code here
        }//GEN-BEGIN:|100-getter|2|
        return okCommand2;
    }
    //</editor-fold>//GEN-END:|100-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: form1 ">//GEN-BEGIN:|104-getter|0|104-preInit
    /**
     * Returns an initiliazed instance of form1 component.
     * @return the initialized component instance
     */
    public Form getForm1() {
        if (form1 == null) {//GEN-END:|104-getter|0|104-preInit
            // write pre-init user code here
            form1 = new Form("Memorizator Help");//GEN-BEGIN:|104-getter|1|104-postInit
            form1.addCommand(getOkCommand2());
            form1.setCommandListener(this);//GEN-END:|104-getter|1|104-postInit
            // write post-init user code here
            form1.append("Your task is to enter all the numbers you see on the screen during the level.\n\nThis game allows you easily train all kinds of your memory.");
        }//GEN-BEGIN:|104-getter|2|
        return form1;
    }
    //</editor-fold>//GEN-END:|104-getter|2|











    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
        //TODO:saveState();
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }


    //Managing high scores
   
    private void updateHighScores() {
        RecordStore rs = null;

        try {
            rs = RecordStore.openRecordStore(STORAGE_NAME, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        MemoRecord prevGame = getThisGameTypeResult(rs, game.getGameType());

        //if not found
        if(prevGame.id == 0) {
            byte[] data = prepareOutData();
            try {
                rs.addRecord(data, 0, data.length);
            } catch (RecordStoreException ex1) {
                ex1.printStackTrace();
            }
        } else
            //if it is highest score
            if (game.getSequence().length() > prevGame.score) {
                byte[] data = prepareOutData();
                try {
                    rs.setRecord(prevGame.id, data, 0, data.length);
                } catch (RecordStoreException ex1) {
                    ex1.printStackTrace();
                }
            }

       

        try {
            rs.closeRecordStore();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String loadHighScores() {

        String resulting = "";
        RecordStore rs = null;

        MemoRecord record = new MemoRecord();
        try {
            rs = RecordStore.openRecordStore(STORAGE_NAME, true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
       
        try {
            for (int j = 1; j <= rs.getNumRecords(); ++j) {
                try {
                    record = getRecordById(rs, j);
                    resulting += record.gameType + "\n";
                    resulting += record.playerName + " : ";
                    resulting += record.score;
                    resulting += "\n";
                } catch (Exception e) {}
            }
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
       
        try {
            rs.closeRecordStore();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        return (resulting == null) ? "No results yet" : resulting;
    }

    /*
    public boolean existingStorage(String recordStoreName) {
        boolean existingOrNot = false;
        RecordStore rs = null;
        if (recordStoreName.length() > 32)
            return false;
        try {
            rs = RecordStore.openRecordStore(recordStoreName, false);
        } catch (RecordStoreNotFoundException e) {
            e.printStackTrace();
            existingOrNot = false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.closeRecordStore();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return existingOrNot;
    }
     *
     */

    private MemoRecord getRecordById(RecordStore rs, int i) {
        MemoRecord record = new MemoRecord();

        byte[] data = null;
        try {
            data = rs.getRecord(i);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        DataInputStream is = new DataInputStream(new ByteArrayInputStream(data));

        try {
            record.gameType = is.readUTF();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            record.playerName = is.readUTF();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            record.score = is.readInt();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            is.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return record;
    }
   
    /*
     * @returns byte[] data
     */
    private byte[] prepareOutData() {
        MemoRecord record = new MemoRecord();

        record.gameType = game.getGameType();
        record.playerName = textBox2.getString();
        record.score = game.getSequence().length();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream os = new DataOutputStream(baos);

        try {
            os.writeUTF(record.gameType);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            os.writeUTF(record.playerName);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            os.writeInt(record.score);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            os.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return baos.toByteArray();
    }

    private MemoRecord getThisGameTypeResult(RecordStore rs, String gameType) {
        MemoRecord record = new MemoRecord();
        try {
            for (int j = 1; j <= rs.getNumRecords(); ++j) {
                try {
                    record = getRecordById(rs, j);
                    if (record.gameType.equals(gameType))
                    {
                        record.id = j;
                        break;
                    }
                } catch (Exception e) {}
            }
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        return record;
    }



}
TOP

Related Classes of memo.Memorizator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.