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

📄 sudokume.java

📁 sudoku j2me手机游戏主要有游戏主类和闪屏类菜单类和模型类等
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        private void _workingInCell(Graphics g, int x, int y) {
            int vOff=3;
            int a=(game.working[x][y]<<2)+vOff;

			if(cursorX==x && cursorY==y)  a+=0x100;	// Factor in cursor position

            if (optPaintAll || a != optPaintPrevious[x][y]) {
                //System.out.println("in wInCell: x="+x+",y="+y+",working="+game.working[x][y]+",wDotX="+workingDotX+"  Redraw");
                g.setClip(x*cellW,y*cellH,cellW,cellH);
                g.setColor(0xff,0xff,0xff);
                g.fillRect(x*cellW,y*cellH,cellW,cellH);
                g.setColor(0xcc,0xcc,0xff);
                g.drawRect(x*cellW,y*cellH,cellW-1,cellH-1);
                if ((game.working[x][y] & (1 << game.gridSz)) > 0) {
                    g.setColor(0x00,0x00,0x00);
                } else {
                    g.setColor(0x80,0x80,0x80);
                }

                for (int i=0; i < game.gridSz; i++) {
                    if ((game.working[x][y]&(1 << i)) > 0) {
                        g.fillRect(x*cellW+workingDotX[i%3],y*cellH+workingDotY[i/3],workingDotW[i%3],workingDotH[i/3]);
                    }
                }
                optPaintPrevious[x][y] = a; optPaintDebug++;
            } else {
                //System.out.println("in wInCell: x="+x+",y="+y+",working="+game.working[x][y]+",wDotX="+workingDotX+"noRedraw");
            }

        }
            

		private void _working(Graphics g,int i) {	
            boolean b;
            // -----i'th bit position set?
            if (SudokuME.programmedKeyCodes.blankWorking && game.grid[cursorX][cursorY] > 0 ) {
                b = false;
            } else {
                b=(game.working[cursorX][cursorY] & (1<<i))!=0;
            }

			int a = b?0x1:0x0;				// On=1, off=0;
			if(workCursorX==i)  a+=0x100;	// Always redraw current cursor cell

			// -----drawImage is expensive on low spec phones.
			if(optPaintAll || a!=optPaintPrevious2[i]) {	
                if (horizWorking) {
                    g.setClip(i*cellW,workingY,cellW,cellH);
				    g.drawImage(digits , 0-cellW,workingY-(b?2:0)*cellH , TOPLEFT);
                } else {
                    g.setClip(workingX,i*cellH,cellW,cellH);
				    g.drawImage(digits , workingX-cellW*(i+1),(i-(b?2:0))*cellH , TOPLEFT);
                }
				optPaintPrevious2[i]=a;
			}
		}
		private void _centreMessage(Graphics g,String m)
		{	int fh=Font.getDefaultFont().getHeight();
			int w=game.gridSz*cellW , h=game.gridSz*cellH;
			g.setClip(0,h-2,w,fh+4);
			g.setColor(0xff,0xff,0xff);  g.fillRect(4,h-2,w-9,fh-1+4);
			g.setColor(0x00,0x00,0x00);  g.drawRect(4,h-2,w-9,fh-1+4);
			g.setColor(0xff,0x00,0x00);  g.drawString(m,w/2,h,CENTERED);
		}

        private void _paint_soft_keys(Graphics g) {
			int fh=Font.getDefaultFont().getHeight();
            int h = getHeight(), w=getWidth();
            String func;
            g.setColor(0xff,0xff,0xff);
            g.fillRect(0,h-fh-3,w,fh+3);
            g.setColor(0x00,0x00,0x00);  
            if (keyHelper.softKeyUsed[1]) {
                if (keyHelper.softKeyUsed[0] || keyHelper.softKeyUsed[2]) {
                    // 3 keys
                    if (keyHelper.softKeyUsed[0]) {
                        func = SudokuME.programmedKeyCodes.funcStr[(shiftFlag?keyHelper.softKeyFuncShifted[0]:keyHelper.softKeyFunc[0])];
                        g.setClip(0,h-fh-3,w/3-1,fh+3);
                        g.drawString(func, 0, h, Graphics.BOTTOM|Graphics.LEFT);
                    }
                    g.setClip(w/3,h-fh-3,w/3-1,fh+3);
                    func = SudokuME.programmedKeyCodes.funcStr[(shiftFlag?keyHelper.softKeyFuncShifted[1]:keyHelper.softKeyFunc[1])];
                    g.drawString(func, w/2, h, Graphics.BOTTOM|Graphics.HCENTER);

                    if (keyHelper.softKeyUsed[2]) {
                        g.setClip(2*w/3,h-fh-3,w/3+1,fh+3);
                        func = SudokuME.programmedKeyCodes.funcStr[(shiftFlag?keyHelper.softKeyFuncShifted[2]:keyHelper.softKeyFunc[2])];
                        g.drawString(func, w, h, Graphics.BOTTOM|Graphics.RIGHT);
                    }
                } else {
                    // 1 key
                    func = SudokuME.programmedKeyCodes.funcStr[(shiftFlag?keyHelper.softKeyFuncShifted[1]:keyHelper.softKeyFunc[1])];
                    g.drawString(func, w/2, h, Graphics.BOTTOM|Graphics.HCENTER);
                }
            } else {
                if (keyHelper.softKeyUsed[0] || keyHelper.softKeyUsed[2]) {
                    // 2 keys
                    if (keyHelper.softKeyUsed[0]) {
                        func = SudokuME.programmedKeyCodes.funcStr[(shiftFlag?keyHelper.softKeyFuncShifted[0]:keyHelper.softKeyFunc[0])];
                        g.setClip(0,h-fh-3,w/2-1,fh+3);
                        g.drawString(func, 0, h, Graphics.BOTTOM|Graphics.LEFT);
                    }

                    if (keyHelper.softKeyUsed[2]) {
                        g.setClip(w/2+1,h-fh-3,w/2,fh+3);
                        func = SudokuME.programmedKeyCodes.funcStr[(shiftFlag?keyHelper.softKeyFuncShifted[2]:keyHelper.softKeyFunc[2])];
                        g.drawString(func, w, h, Graphics.BOTTOM|Graphics.RIGHT);
                    }
                } else {
                    // no keys!
                }
            }

        }

		// -------------------------------------------------------------
		// Handle key press
        // 1) if in working mode, check for a match with LOCK_WORKING
        // 2) check for a match in programmed key list
        // 3) check if the key can be used to set a cell directly
        // 4) check for a close match on programmed key list
		// -------------------------------------------------------------
        public void keyPressed(int keyCode) {
            int cx=cursorX , cy=cursorY;
            int i;
            int num_pressed = -1, closeMatch=-1; // set to func if it matches apart from shift

            // if in working mode, check LOCK_WORKING especially - it gets preference in working
            // mode. in editGrid mode, all other key codes come before this one
            if (!editGrid && programmedKeyCodes.funcKeyCode[   programmedKeyCodes.LOCK_WORKING] == keyCode && 
                             programmedKeyCodes.funcKeyShifted[programmedKeyCodes.LOCK_WORKING] == shiftFlag) {
                keyFuncPressed(programmedKeyCodes.LOCK_WORKING);
                return;
            }

            for (i = 0; i < programmedKeyCodes.LEFT_SOFT; i++) {
                if (programmedKeyCodes.funcKeyCode[i] == keyCode) {
                    if (programmedKeyCodes.funcKeyShifted[i] == shiftFlag) {
                        keyFuncPressed(i);
                        return;
                    } else {
                        closeMatch = i;
                    }
                }
            }

            if (keyCode >= KEY_NUM1 && keyCode <= KEY_NUM9) {
                num_pressed = keyCode - KEY_NUM1 + 1;
            }
            if (game.gridSz > 9) {
                // add *,0,# as key 10,11,12
                if (keyCode == KEY_STAR) {
                    num_pressed = 10;
                } else if (keyCode == KEY_NUM0) {
                    num_pressed = 11;
                } else if (keyCode == KEY_POUND) {
                    num_pressed = 12;
                }
            }
            if (num_pressed > 0 && num_pressed <= game.gridSz) {
                if(editGrid) {	
                    if(!game.locked[cursorX][cursorY]) {
                        if (game.grid[cursorX][cursorY]==(byte) num_pressed) {
                            keyNumPressed(0);
                        } else {
                            keyNumPressed(num_pressed);
                        }
                    }
                } else {
                    game.toggleWorking(cursorX,cursorY,num_pressed-1);
                }
                closeMatch = -1;
            }
            
            if (closeMatch >= 0) {
                keyFuncPressed(closeMatch);
                return;
            }
            game.updateCollisions(cursorX,cursorY);
            undoUpdate(cx,cy);
            repaint();
        }

		public void keyNumPressed(int num_pressed) {	
            if (programmedKeyCodes.autoWorking) {
                if (programmedKeyCodes.workingInCell) {
                    game.updateWorking(num_pressed,cursorX,cursorY);
                } else {
                    game.grid[cursorX][cursorY]= (byte) num_pressed;
                    if (num_pressed == 0) {
                        game.updateWorkingCell(cursorX,cursorY);
                    }
                }
            } else {
                game.grid[cursorX][cursorY]= (byte) num_pressed;
            }
        }

		public void keyFuncPressed(int keyFunc) {	
            int cx=cursorX , cy=cursorY;
            if (keyFunc ==  programmedKeyCodes.MENU ) {
			    pauseTimer();		// Stop the timer
			    new Menu(display,this,menuStr,menuInt,-1,-1);
            } else if (keyFunc ==  programmedKeyCodes.SHIFT ) {
				shiftFlag=true;	// # key acts as a [SHIFT] type key
            } else if (keyFunc ==  programmedKeyCodes.LEFT ) {
                if(editGrid || keyHelper.useNumKeys)  {
                    cursorX=(cursorX-1+game.gridSz)%game.gridSz;
                } else  {
                    workCursorX=(workCursorX-1+game.gridSz)%game.gridSz;
                }
            } else if (keyFunc ==  programmedKeyCodes.RIGHT ) {
                if(editGrid || keyHelper.useNumKeys)  {
                    cursorX=(cursorX+1)%game.gridSz;
                } else  {
                    workCursorX=(workCursorX+1)%game.gridSz;
                }
            } else if (keyFunc ==  programmedKeyCodes.UP ) {
                if(editGrid || keyHelper.useNumKeys)  {
                    cursorY=(cursorY-1+game.gridSz)%game.gridSz;
                }
            } else if (keyFunc ==  programmedKeyCodes.DOWN ) {
                if(editGrid || keyHelper.useNumKeys)  {
                    cursorY=(cursorY+1)%game.gridSz;
                }
            } else if (keyFunc ==  programmedKeyCodes.WORKING ) {
                editGrid=!editGrid;  
                optPaintAll=true; 
            } else if (keyFunc ==  programmedKeyCodes.UNDO ) {
                undoRestore();
            } else if (keyFunc ==  programmedKeyCodes.TIMER ) {
                showTimer=true; 
            } else if (keyFunc ==  programmedKeyCodes.LOCK_CELL ) {
                if(editGrid && game.grid[cursorX][cursorY]>0) {
					game.locked[cursorX][cursorY]=!game.locked[cursorX][cursorY];
                }
            } else if (keyFunc ==  programmedKeyCodes.INCREMENT ) {
                if(editGrid) {	
                    if(!game.locked[cursorX][cursorY])
                        keyNumPressed( (game.grid[cursorX][cursorY]+1)%(game.gridSz+1) );
                } else  {
                    game.toggleWorking(cursorX,cursorY,workCursorX);
                }
            } else if (keyFunc ==  programmedKeyCodes.LOCK_WORKING ) {
                game.toggleWorking(cursorX, cursorY, game.gridSz);
            }
			game.updateCollisions(cursorX,cursorY);
			undoUpdate(cx,cy);
			repaint();
		}

		public void keyRepeated(int keyCode)
		{	
		}

		public void keyReleased(int keyCode) {	
            if (  keyCode == programmedKeyCodes.funcKeyCode[programmedKeyCodes.TIMER] ) {
				showTimer=false;
			    optPaintAll=true;  
            } else if ( keyCode == programmedKeyCodes.funcKeyCode[programmedKeyCodes.SHIFT] ) {
				shiftFlag=false;
			    optPaintAll=true;  
			}
            repaint();
		}
	

		// -------------------------------------------------------------
		// CommandListener: called when command soft button pressed
		// -------------------------------------------------------------
		public void commandAction(Command c,Displayable d) {	
            for (int i = 0; i < programmedKeyCodes.LEFT_SOFT; i++) {
                if (c == programmedKeyCodes.funcKeyCommand[i]) {
                    keyFuncPressed(i);
                    return;
                }
			}
		}		

		// -------------------------------------------------------------
		// MenuListener method: called when menu option selected
		// -------------------------------------------------------------
		public void menuSelection(int idCode) {	
            optPaintAll=true;	// Repaint entire grid next time
            Alert alert;
            int i;
            byte[][] tempGrid;
			switch(idCode)
			{	case 100 :		// Save game
					_saveGame();
					break;
				case 200 :		// Lock menu
					for(int x=0;x<game.gridSz;x++)
						for(int y=0;y<game.gridSz;y++)
							if(game.grid[x][y]>0)  game.locked[x][y]=true;
					break;
				case 300 :		// Unlock menu
					for(int x=0;x<game.gridSz;x++)
						for(int y=0;y<game.gridSz;y++)
							game.locked[x][y]=false;
					break;
				case 401 :		// Quit, save
					_saveGame();
					// Drops through to 402

⌨️ 快捷键说明

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