📄 eight_queen.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class Eight_Queen extends MIDlet
{
private Display display;
public void startApp()
{
display = Display.getDisplay(this);
Eight_QueenCanvas queenCanvas = new Eight_QueenCanvas();
queenCanvas.start();
display.setCurrent(queenCanvas);
}
public Display getDisplay()
{
return display;
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
System.gc();
destroyApp(false);
notifyDestroyed();
}
}
class Eight_QueenCanvas extends GameCanvas implements Runnable
{
private boolean isPlay;
private boolean isPlaying;
private int width;
private int height;
static final int Queen_num = 8;
static final int Result_Nums = 92;
int Min_Length;
int queenLength;
int NCounts ;
public Eight_QueenCanvas()
{
super(true);
width = getWidth();
height = getHeight();
if (width > height) Min_Length = height;
else Min_Length = width;
queenLength = Min_Length / 10;
NCounts = 0;
}
// Automatically start thread for game loop
public void start()
{
isPlay = true;
isPlaying = true;
Thread t = new Thread(this);
t.start();
}
public void stop() { isPlaying = false; }
/************************************************/
public void run()
{
Graphics g = getGraphics();
while (isPlaying == true)
{
Juge_Press();
if (isPlay == false)
continue;
Draw_Grid(g);
queen(g);
isPlay = false;
}
}
// Method to Handle User Inputs
/****************************************************/
private void Juge_Press()
{
int keyStates = getKeyStates();
if ((keyStates & RIGHT_PRESSED) !=0 )
isPlay = true;
if ((keyStates & DOWN_PRESSED) !=0)
isPlay = true;
}
/**************************************************/
void Draw_Grid(Graphics g)//画格子
{
int x, y, z, i;
g.setColor(0, 255, 0);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0,0,255);
for(i = 0; i <= Queen_num; i++)
{
g.drawLine(queenLength, queenLength + i*queenLength, Min_Length - queenLength, queenLength + i*queenLength);
g.drawLine(queenLength + i*queenLength, queenLength, queenLength + i*queenLength, Min_Length - queenLength);
}
flushGraphics();
}
/********************************************/
void Draw_Queen(int row, int col, Graphics g)//画皇后
{
int left, top;
int i;
int length;
length = queenLength / 10;
left = (col+1) * queenLength;
top = (row+1) * queenLength;
g.setColor(255, 0,0);
g.fillRoundRect(left + length, top + length, queenLength - 2*length, queenLength - 2*length, queenLength , queenLength );
flushGraphics();
}
/**********************************************************/
void queen(Graphics g)//主程序
{
g.setColor(255,0,0);
g.drawString(" 8 皇 后",0,0,0);
int i, cur_counts, j, k, m;
int col, row;
if (NCounts++ > Result_Nums)
{
NCounts = 0;
return;
}
int[] arrCol = new int[Queen_num];
int[] arrRow = new int[Queen_num];
for (i = 0; i < Queen_num; i++)
{
arrCol[i] = -1;
arrRow[i] = -1;
}
cur_counts = 0;
col = 0;
row = 0;
while (true)
{
for (; col < Queen_num; col++)
{
if (arrCol[col] == -1) break;
}
if (col == Queen_num)
{
row--;
if (row < 0) break;
arrCol[arrRow[row]] = -1;
col = arrRow[row] + 1;
arrRow[row] = -1;
continue;
}
k = row + 1;
for (i = 0; i < row; i++)
{
j = col - arrRow[i];
k--;
if (!(j == k || j == -k)) continue;
if (((j + k) & 1) == 0) break;
}
if (i != row)
{
col++;
continue;
}
if (row == Queen_num - 1)
{
cur_counts++;
arrRow[row] = col;
if (cur_counts >= NCounts)
{
for (i = 0; i < Queen_num; i++)
{
try {
Thread.sleep(100);
Draw_Queen(i, arrRow[i], g);
}
catch (InterruptedException ie) {}
}
return;
}
arrRow[row] = -1;
col++;
}
else
{
arrCol[col] = row;
arrRow[row] = col;
row++;
col = 0;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -