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

📄 gamedata.java

📁 sudoku j2me手机游戏主要有游戏主类和闪屏类菜单类和模型类等
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                for (y=0;y<gridSz;y++) {
                    if (curGrid[x][y] == 0) {
                        solved = false;
                        numWorking = 0;
                        for (int i=0; i<gridSz; i++) {
                            if ((curWorking[x][y] & (1 << i)) > 0) {
                                numWorking++;
                            }
                        }

                        if (numWorking == 0) {
                            // if (SudokuME.debug) System.out.println("solve: calling pop() at x="+x+",y="+y);
                            
                            SolutionStack popValue = head.pop();
                            if (popValue.grid != null) {
                                bestX = popValue.x;
                                bestY = popValue.y;
                                bestNumWorking = popValue.numWorking;
                                curGrid = popValue.grid;
                                curWorking = popValue.working;
                                break thisIteration;
                            } else {
                                // no solution 
                                // if (SudokuME.debug) System.out.println("solve: no solution found");
                                return numSolutions;
                            }
                        } else if (numWorking < bestNumWorking) {
                            bestX = x;
                            bestY = y;
                            bestNumWorking = numWorking;
                            if (numWorking == 1) {
                                // if (SudokuME.debug) System.out.println("solve:  found numWorking=1 at x="+x+",y="+y);
                                break thisIteration;
                            }
                        }
                    }
                }
                
            }
            if (solved) {
                if (numSolutions == 0) {
                    for (x=0;x<gridSz; x++) {
                        for (y=0;y<gridSz;y++) {
                            inGrid[x][y] = curGrid[x][y];
                        }
                    }
                }
                numSolutions++;
                if (numSolutions >= maxSolutions) {
                    return numSolutions;
                }

                SolutionStack popValue = head.pop();
                if (popValue.grid != null) {
                    bestX = popValue.x;
                    bestY = popValue.y;
                    bestNumWorking = popValue.numWorking;
                    curGrid = popValue.grid;
                    curWorking = popValue.working;
                } else {
                    // no more solutions 
                    // if (SudokuME.debug) System.out.println("solve: no solution found");
                    return numSolutions;
                }

            }

            // calculate value to set
            newVal = -1;
            for (int i=0; i<gridSz; i++) {
                if ((curWorking[bestX][bestY] & (1 << i)) > 0) {
                    newVal = i;
                    break;
                }
            }
            if (newVal >= 0) {
                // if (SudokuME.debug) System.out.println("solve: updating x="+bestX+",y="+bestY+" with "+newVal);

                curWorking[bestX][bestY] &= (short) ~(1 << newVal);
                if (bestNumWorking > 1) {
                    try {
                        // if (SudokuME.debug) System.out.println("solve: calling push() numWorking="+(bestNumWorking-1));
                        head.push(curGrid, curWorking, bestX, bestY, bestNumWorking-1);
                    } catch (OutOfMemoryError e) {
                        if (SudokuME.debug) {
                            System.out.println("Error in solve - Ran out of memory, failing");
                        }
                        return numSolutions;
                    }
                }
                updateWorking(newVal+1, bestX, bestY, curGrid, curWorking) ;
            } else {
                if (SudokuME.debug) {
                    System.out.println("Error in solve - failed to find working value");
                }
                return numSolutions;
            }
        }
    }

	/*void solve()
	{	final int[] flags = { 0x7, 0x1, 0x2, 0x4 };
		int cnt;
		do
		{	cnt=0;
			// -----Look for cells with only one possible value
			for(int y=0;y<gridSz;y++)
			{	for(int x=0;x<gridSz;x++)
				{	if(grid[x][y]==0)
					{	int a=_findSolvable(x,y,0x7);
						if(a>0) { grid[x][y]=(byte)a;  cnt++; }
					}
				}
			}
		}while(cnt>0);
	}
	private short _findSolvable(int x,int y,int flags)
	{	int sox=x-(x%setW) , soy=y-(y%setH); // Set subgrid offset
		// -----For each value which 'intersects' this cell, set a bit in mask
		short mask=0;
		for(int a=0;a<gridSz;a++)
		{	if((flags&0x1)>0 && grid[x][a]>0)  mask|=(1<<(grid[x][a]-1));
			if((flags&0x2)>0 && grid[a][y]>0)  mask|=(1<<(grid[a][y]-1));
			int sx=(a%setW)+sox , sy=(a/setW)+soy;
			if((flags&0x4)>0 && grid[sx][sy]>0)  mask|=(1<<(grid[sx][sy]-1));
		}
		// -----Now invert the mask, so missing values are ones
		mask=(short)~mask;
		// -----Chop off high bits
		short maskFilter=0;
		for(int a=0;a<gridSz;a++)  maskFilter|=(1<<a);
		mask=(short)(mask&maskFilter);
		//System.out.println(Integer.toHexString(mask));

		// -----Do we have a hit?
		for(int a=0;a<gridSz;a++)
			if(mask==(1<<a))  return (short)(a+1);
		return 0;
	}*/


	// ============================================ load / store ============================================================
	// STATIC: load record store as GameData objects
	// -----------------------------------------------------------------
    private static class _findRecordId implements RecordFilter {
        public boolean matches(byte[] candidate) {
            return (candidate[0] < 4 || candidate[1] == SudokuME.SAVE_GAME_RECORD);
        }
    }

	static RecordEntry[] catalogue()
	{	RecordStore rs=null;
		try
		{	// -----Attempt to read our saved games from record store
			rs = RecordStore.openRecordStore(SudokuME.REC_STORE_NAME,false);
			RecordEnumeration re = rs.enumerateRecords(new _findRecordId(),null,false);
			RecordEntry[] cat = new RecordEntry[re.numRecords()];

			// -----In order to save object churn I'm going to attempt to
			// -----read the header from each record into the same byte
			// -----array and re-read it using a single DIS/BAIS chain.
			byte[] recordBuffer = new byte[42+256+32+512];	// Max pos. record size
			ByteArrayInputStream bais = new ByteArrayInputStream(recordBuffer);
			DataInputStream dis = new DataInputStream(bais);

			int cnt=0;
			while(re.hasNextElement())
			{	int recId=re.nextRecordId();
				int len=rs.getRecordSize(recId);

				rs.getRecord(recId,recordBuffer,0);
				bais.reset();	// Move pointer back to start of array (I hope!)
				int v = dis.readByte();				// Version number
                if (v < 4 || dis.readByte() == SudokuME.SAVE_GAME_RECORD) {
                    dis.readLong();  dis.readLong();	// Dates (skip)
                    int timer=0;
                    String title="Untitled game";
                    if(v>=3)
                    {	timer = dis.readInt();
                        title=_readASCIIString(20,dis);
                    }

                    cat[cnt] = new RecordEntry(recId,len,title,timer);
                    cnt++;
                }
			}

			dis.close();

			return cat;
		}
		catch(Exception e)
		{	// FIX: Better error handling
			// -----Failed, so create an empty array
			if(!(e instanceof RecordStoreNotFoundException) && SudokuME.debug)  e.printStackTrace();
			return new RecordEntry[0];
		}
		finally
		{	try { rs.closeRecordStore(); }catch(Exception e) {}
		}
	}

	/*static GameData[] load()
	{	RecordStore rs=null;
		try
		{	// -----Attempt to read our saved games from record store
			rs = RecordStore.openRecordStore(SudokuME.REC_STORE_NAME,false);
			RecordEnumeration re = rs.enumerateRecords(null,null,false);
			GameData[] games = new GameData[re.numRecords()];
			int cnt=0;
			while(re.hasNextElement())
			{	int recId=re.nextRecordId();
				games[cnt++] = new GameData(recId,rs.getRecord(recId));
			}
			return games;
		}
		catch(Exception e)
		{	// -----Failed, so create an empty array
			if(!(e instanceof RecordStoreNotFoundException))  e.printStackTrace();
			return new GameData[0];
		}
		finally
		{	try { rs.closeRecordStore(); }catch(Exception e) {}
		}
	}*/

	// -----------------------------------------------------------------
	// STATIC: Load a saved game from the record store, and from a named
	// resource within our MIDlet.
	// -----------------------------------------------------------------
	static GameData load(RecordEntry entry)
	{	RecordStore rs=null;
		try
		{	// -----Attempt to read our saved games from record store
			rs = RecordStore.openRecordStore(SudokuME.REC_STORE_NAME,false);
			return new GameData(entry.recordId , rs.getRecord(entry.recordId));
		}
		catch(Exception e)
		{	// -----Failed, so create an empty array
			if(!(e instanceof RecordStoreNotFoundException))  e.printStackTrace();
			return new GameData(9);
		}
		finally
		{	try { rs.closeRecordStore(); }catch(Exception e) {}
		}
	}
	static GameData load(String rName)
	{	try
		{	return new GameData
			(	new DataInputStream(new GameData().getClass().getResourceAsStream(rName))
			);
		}catch(Exception e) { e.printStackTrace();  return new GameData(9); }
	}

	// -----------------------------------------------------------------
	// STATIC: Delete a given entry in the record store.
	// -----------------------------------------------------------------
	static boolean delete(RecordEntry entry)
	{	RecordStore rs=null;
		try
		{	rs = RecordStore.openRecordStore(SudokuME.REC_STORE_NAME,false);
			rs.deleteRecord(entry.recordId);
			return true;
		}
		catch(Exception e)
		{	e.printStackTrace();
			lastErrorMessage="Exception:"+e.toString();
			return false;
		}
		finally
		{	try { rs.closeRecordStore(); }catch(Exception e) {}
		}
	}

	/*static void dump(int id)
	{	try
		{	RecordStore rs=RecordStore.openRecordStore(SudokuME.REC_STORE_NAME,false);
			DataInputStream dis = new DataInputStream(new ByteArrayInputStream(rs.getRecord(id)));
			int sz=rs.getRecordSize(id);
			for(int i=0;i<sz;i++)
				System.out.print(Integer.toHexString((dis.readByte()&0xff))+",");
			dis.close();
			rs.closeRecordStore();
		}catch(Exception e) { e.printStackTrace(); }
	}*/
}

⌨️ 快捷键说明

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