⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 puzzlestore.java

📁 游戏名称:九宫阵 运行平台:j2me 规范:midp 说明:一款很经典的游戏源码
💻 JAVA
字号:
/*
 * Copyright (C) 2005-2006 Leopardo.f
 *
 * This file is part of M-SuDoKu, a J2ME version of SuDoKu.
 *
 * M-SuDoKu is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * M-SuDoKu is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with M-SuDoKu; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.  Or, visit http://www.gnu.org/copyleft/gpl.html
 */

package MSuDoKu;

import javax.microedition.rms.*;

/**
 * Class providing support for saving and loading puzzles at game shutdown/startup
 * @author Leopardo.f
 */
public class PuzzleStore {

    protected static final String sRecordName = "puzzle";
    protected static final int nMagicSize = 84;
    protected static final int nNewMagicSize = 165;
    protected static final int nBackTimeMagicSize = Long.toString (Long.MAX_VALUE).length() + 2;
    
    protected RecordStore rs;

    protected byte nCurrentLevel = 0;
    protected byte[] naCurrentPuzzle = {0, 0};
    protected byte yBack = 0;
    protected byte yInput = PuzzleModel.NUMPAD;
    protected long lTime = 0;
    protected String sPuzzle = null, sSolution = null;
    
    /** Creates a new instance of PuzzleStore */
    public PuzzleStore()
    {
        int l;
        
        try
        {
            rs = RecordStore.openRecordStore (sRecordName, true);
//          System.out.println ("Records in RecordStore: " + rs.getNumRecords());
            if (rs.getNumRecords() == 0)
                return;

            l = rs.getRecordSize (1);
            byte[] yaTemp = new byte[l];
            try
            {
                rs.getRecord (1, yaTemp, 0);
            } catch (ArrayIndexOutOfBoundsException aioobe)
            {
                return;
            }
            
            if ((l != nMagicSize) && (l != nNewMagicSize))
                return;
            
            nCurrentLevel = yaTemp[0];
            naCurrentPuzzle[0] = yaTemp[1];
            naCurrentPuzzle[1] = yaTemp[2];
            sPuzzle = new String (yaTemp, 3, 81);
            if (l == nNewMagicSize)
                sSolution = new String (yaTemp, 84, 81);
//          System.out.println ("Record length: " + l);
            
            if (rs.getNumRecords() == 1)
                return;

            l = rs.getRecordSize (2);
            yaTemp = new byte[l];
            try
            {
                rs.getRecord (2, yaTemp, 0);
            } catch (ArrayIndexOutOfBoundsException aioobe)
            {
                return;
            }
            
            if ((l != nBackTimeMagicSize) && (l != nBackTimeMagicSize - 1))
                return;
            
            yBack = (byte) (yaTemp[0] - 1);
            if (l == nBackTimeMagicSize)
                yInput = (byte) (yaTemp[1]);
            try
            {
                if (l == nBackTimeMagicSize)
                    lTime = Long.parseLong(new String (yaTemp, 2, l - 2));
                else
//                  Compatibility with 0.8.0
                    lTime = Long.parseLong(new String (yaTemp, 1, l - 1));
            }
            catch (NumberFormatException nfe)
            {
                lTime = 0;
            }
//          System.out.println ("Record length: " + l);
 
//          System.out.println ("Current background: " + yBack + "; elapsed time: " + lTime + "; input: " + yInput);
//          System.out.println ("Current level: " + nCurrentLevel + "; CurrentPuzzle: " + naCurrentPuzzle[0] + ", " + naCurrentPuzzle[1] + "\nString:" + sPuzzle);
        } catch (RecordStoreNotFoundException rsnfe)
        {
//            System.out.println ("RecordStoreNotFoundException: " + rsnfe.getMessage());
        } catch (RecordStoreFullException rsfe)
        {
//            System.out.println ("RecordStoreFullException: " + rsfe.getMessage());
        } catch (RecordStoreException rse)
        {
//            System.out.println ("RecordStoreException: " + rse.getMessage());
        }
    }

/** Returns the current level found in the RecordStore
 */
    public byte getCurrentLevel()
    {
        return nCurrentLevel;
    }

/** Returns the current background found in the RecordStore
 */
    public byte getBack()
    {
        return yBack;
    }

/** Returns the current input mode found in the RecordStore
 */
    public byte getInput()
    {
        return yInput;
    }

/** Returns the elapsed time found in the RecordStore
 */
    public long getTime()
    {
        return lTime;
    }

/** Returns the array containing the current puzzle for each level
 */
    public byte[] getCurrentPuzzleArray()
    {
        return naCurrentPuzzle;
    }
    
/** Returns the current puzzle found in the RecordStore coded as a String
 */
    public String getCurrentPuzzleAsString()
    {
        return sPuzzle;
    }
    
/** Returns the current solution found in the RecordStore coded as a String
 */
    public String getCurrentSolutionAsString()
    {
        return sSolution;
    }

 /** Saves the settings given as parameters
 * @param yCurr the current level
 * @param yaCurr the array containing the current puzzle for each level
 * @param sP the current puzzle coded as a String
 * @param sS the solution to the current puzzle coded as String
 * @param yB the current background
 * @param lT the elapsed time
 * @param yI the current input mode
 */
    public void save (byte yCurr, byte[] yaCurr, String sP, String sS, byte yB, long lT, byte yI)
    {
        byte[] ya2write = new byte[nNewMagicSize];
        byte[] yaBT2write = new byte[nBackTimeMagicSize];
        
        ya2write[0] = yCurr;
        ya2write[1] = yaCurr[0];
        ya2write[2] = yaCurr[1];
        System.arraycopy (sP.getBytes(), 0, ya2write, 3, 81);
        System.arraycopy (sS.getBytes(), 0, ya2write, 84, 81);
        
        yaBT2write[0] = (byte) (yB + 1);
        yaBT2write[1] = yI;
        System.arraycopy (formatTime (Long.toString (lT)).getBytes(), 0, yaBT2write, 2, nBackTimeMagicSize - 2);
        
        try
        {
            if (rs.getNumRecords() == 0)
//                System.out.println ("Added record: " + rs.addRecord (ya2write, 0, ya2write.length));
                rs.addRecord (ya2write, 0, ya2write.length);
            else
                rs.setRecord (1, ya2write, 0, ya2write.length);
            if (rs.getNumRecords() == 1)
//                System.out.println ("Added record: " + rs.addRecord (yaBT2write, 0, yaBT2write.length));
                rs.addRecord (yaBT2write, 0, yaBT2write.length);
            else
                rs.setRecord (2, yaBT2write, 0, yaBT2write.length);
            rs.closeRecordStore();
        } catch (RecordStoreException rse)
        {
//            System.out.println ("RecordStoreException while saving: " + rse.getMessage());
        }
    }
    
    protected String formatTime (String sT)
    {
        String sReturn = sT;
        int i, n = Long.toString (Long.MAX_VALUE).length() - sT.length();
        
        for (i = 0; i < n; i++)
            sReturn = "0" + sReturn;
        
        return sReturn;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -