📄 five1.java.bak
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
public class Five1 extends MIDlet implements CommandListener
{
private Display display;
private final static Command exit=new Command("Exit",Command.EXIT,1);
private myCanvas c;
//////////构造函数,添加退出按钮
public Five1 (){
display=Display.getDisplay(this);
c=new myCanvas();
c.addCommand(exit);
c.setCommandListener(this);
System.out.println("Beginning……");
}
public void startApp(){
display.setCurrent(c);
c.startGame();
}
public void pauseApp(){
c.stopGame();
}
public void destroyApp(boolean unconditional){
c.stopGame();
}
public void commandAction(Command c,Displayable s){
if(c==exit){
destroyApp(false);
notifyDestroyed();
}
}
};
class myCanvas extends GameCanvas implements Runnable
{
private boolean gameOver;//游戏状态
private Graphics g;//用来画图的对象
private int width,height;//屏幕长宽
//x,y坐标
private int x,y,r,mapx,mapy,gap,length;
private Sprite sprite;//棋子对象
private int[][] chess;//棋盘
private boolean chessIsWhite;//判断游戏双方角色。
public myCanvas(){
super(true);
gameOver=true;
g=getGraphics();
x=6;
y=6;
width=this.getWidth();
height=this.getHeight();
r=3;
mapx=10;
mapy=10;
gap=6;
length=84;
Image img=null;
try{
img=Image.createImage("/opl.PNG");
}
catch(java.io.IOException e){System.out.println("Can not find opl.png!");}
sprite=new Sprite(img,9,9);//棋子宽9高9
chess=new int[15][15];
chessIsWhite=false;///////////黑子,人先下
}
public void startGame(){
gameOver=false;
Thread myThread=new Thread(this);
myThread.start();
}
public void stopGame(){
gameOver=true;
}
public void run(){
//游戏运行的循环
///////////////////////////// begin of while ///////////////////////////
while(!gameOver){
///////////////////////////// begin of person ///////////////////////////
int keyState=this.getKeyStates();
//LEFT
if((keyState&LEFT_PRESSED)!=0){
x=x-gap;
if(x<6)
x=6;
}
//right
if((keyState&RIGHT_PRESSED)!=0){
x=x+gap;
if(x>6+gap*14)
x=6+gap*14;
}
//up
if((keyState&UP_PRESSED)!=0){
y=y-gap;
if(y<6)
y=6;
}
//down
if((keyState&DOWN_PRESSED)!=0){
y=y+gap;
if(y>6+gap*14)
y=6+gap*14;
}
//fire
if((keyState&FIRE_PRESSED)!=0){
//若未下子,下子,已下子,忽略fire
if(chess[(y-6)/gap][(x-6)/gap]==0){
chess[(y-6)/gap][(x-6)/gap]=1;//人下字,设置为1
chessIsWhite=!chessIsWhite;//下一个下子者为机器
judge();
//judge(),游戏结束返回true,否则返回false
if(judge())
{
gameOver=true;
}
}
///////////////////////////////////绘制缓冲区//////////////////////////////////////
//底色
g.setColor(255,255,255);
g.fillRect(0,0,width,height);//填充底色
g.setColor(228,228,131);
g.fillRect(0,0,10+(gap+1)*14,10+(gap+1)*14);
//小球
//棋盘
g.setColor(0,0,0);
mapx=10;
mapy=10;
gap=6;
length=84;
for(int i=0;i<15;i++)
{
g.drawLine(mapx,mapy,mapx+length,mapy);
mapy=mapy+gap;
}
mapx=10;
mapy=10;
gap=6;
length=84;
for(int i=0;i<15;i++){
g.drawLine(mapx,mapy,mapx,mapy+length);
mapx=mapx+gap;
}
//棋子
for(int i=0;i<15;i++)//begin of for_outside
for(int j=0;j<15;j++)
{
//begin of for_inner
if(chess[i][j]==1)
{
g.setColor(0,0,0);
g.fillArc(10+gap*j-r,10+gap*i-r,r+r,r+r,0,360);
}
if(chess[i][j]==2)
{
g.setColor(255,255,255);
g.fillArc(10+gap*j-r,10+gap*i-r,r+r,r+r,0,360);
}
}
//end of for_inner and end of for_outside
//选择框
sprite.setPosition(x,y);
if(chess[(y-6)/gap][(x-6)/gap]==0)
sprite.setFrame(0);
if(chess[(y-6)/gap][(x-6)/gap]==1)
sprite.setFrame(1);
if(chess[(y-6)/gap][(x-6)/gap]==2)
sprite.setFrame(2);
sprite.paint(g);
//信息
if(gameOver&&!chessIsWhite){
g.setColor(255,0,0);
g.drawString("The white is winner!",10,100,Graphics.LEFT|Graphics.TOP);
}
}
flushGraphics();
try{
Thread.sleep(200);
}
catch(InterruptedException ie){}
//////////////////////////////// end of person /////////////////////////////////////////
/*
//////////////////////////////// begin of machine ////////////////////////////////////////
if(!gameOver)
computerdo(width,height);
*/
//////////////////////////////// end of machine ////////////////////////////////////////
}////////////////////////////end of while ////////////////////////////////
}//end of run()
/*
public void computerdo(int width,int height){
int max_black,max_white,max_temp,max=0;
for(int i = 0; i <= (width-6)/gap; i++)
{
for(int j = 0; j <= (height-6)/gap; j++)
{
if(chess[i][j]!=0)
{
max_white=checkMax(i,j,2);//判断白子的最大值
max_black=checkMax(i,j,1);//判断黑子的最大值
if(max_white<=max_black)
max_temp=max_black;
else
max_temp=max_white;
if(max_temp>max)
{
max=max_temp;
this.x=i;
this.y=j;
}
}
}
}
chess[(y-6)/gap][(x-6)/gap]=1;
chessIsWhite=!chessIsWhite;
}
*/
public boolean judge(){
int sum=1;
int tempx=(y-6)/gap;
int tempy=(x-6)/gap;
//横联判断
for(int i=1;i<=4;i++)
{
if(tempy+i<=14)
{
if(chess[tempx][tempy+i]==chess[tempx][tempy]&&chess[tempx][tempy]!=0)
{
sum++;
}
else
break;
}
else
break;
}
for(int i=1;i<=4;i++)
{
if(tempy-i>=0)
{
if(chess[tempx][tempy-i]==chess[tempx][tempy]&&chess[tempx][tempy]!=0)
sum++;
else
break;
}
else
break;
}
if(sum==5)
return true;
System.out.println("1********************");
//熟练判断
sum=1;
for(int i=1;i<=4;i++)
{
if(tempx+i<=14)
{
if(chess[tempx+i][tempy]==chess[tempx][tempy]&&chess[tempx][tempy]!=0)
{
sum++;
}
else
break;
}
else
break;
}
for(int i=1;i<=4;i++)
{
if(tempx-i>=0)
{
if(chess[tempx-i][tempy]==chess[tempx][tempy])
{
sum++;
}
else
break;
}
else
break;
}
if(sum==5)
return true;
System.out.println("2********************");
//判断左高右低
sum=1;
for(int i=1;i<=4;i++)
{
if(tempx+i<=14&&tempy+i<=14&&chess[tempx+i][tempy+i]==chess[tempx][tempy]&&chess[tempx][tempy]!=0)
{
sum++;
}
else
break;
}
for(int i=1;i<=4;i++)
{
if(tempx-i>=0&&tempy-i>=0&&chess[tempx-i][tempy-i]==chess[tempx][tempy])
{
sum++;
}
else
break;
}
if(sum==5)
return true;
System.out.println("3********************");
//判断右高左低
sum=1;
for(int i=1;i<=4;i++)
{
if(tempx-i>=0&&tempy+i<=14&&chess[tempx-i][tempy+i]==chess[tempx][tempy]&&chess[tempx][tempy]!=0)
{
sum++;
}
else
break;
}
for(int i=1;i<=4;i++)
{
if(tempx+i<=14&&tempy-i>=0&&chess[tempx+i][tempy-i]==chess[tempx][tempy])
{
sum++;
}
else
break;
}
System.out.println("sum="+sum);
if(sum==5)
return true;
else
return false;
}
public int checkMax(int x, int y,int black_or_white){
int num=0,max_num,max_temp=0;
int x_temp=x,y_temp=y;
int x_temp1=x_temp,y_temp1=y_temp;
/////////////////////////////////////水平方向///////////////////////////////////////
//计算右边
for(int i=1;i<5;i++){
x_temp1+=gap;
if(x_temp1>width)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
//计算左边
x_temp1=x_temp;
for(int i=1;i<5;i++){
x_temp1-=gap;
if(x_temp1<0)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
//
if(num<5)
max_temp=num;
////////////////////////////////////////垂直方向///////////////////////////////////////////
//计算上边
x_temp1=x_temp;
y_temp1=y_temp;
num=0;
for(int i=1;i<5;i++){
y_temp1-=gap;
if(y_temp1<0)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
//计算下边
y_temp1=y_temp;
for(int i=1;i<5;i++){
y_temp1+=gap;
if(y_temp1>height)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;
//////////////////////////////////////////315度方向/////////////////////////////////////////
//计算左上
x_temp1=x_temp;
y_temp1=y_temp;
num=0;
for(int i=1;i<5;i++){
x_temp1-=gap;
y_temp1-=gap;
if(y_temp1<0 || x_temp1<0)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
//计算右下
x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<5;i++){
x_temp1+=gap;
y_temp1+=gap;
if(y_temp1>height || x_temp1>width)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;
/////////////////////////////////////////////45度方向//////////////////////////////////////////////////////
//计算右上
x_temp1=x_temp;
y_temp1=y_temp;
num=0;
for(int i=1;i<5;i++){
x_temp1+=gap;
y_temp1-=gap;
if(y_temp1<0 || x_temp1>this.width)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
//计算左下
x_temp1=x_temp;
y_temp1=y_temp;
for(int i=1;i<5;i++){
x_temp1-=gap;
y_temp1+=gap;
if(y_temp1>height || x_temp1<0)
break;
if(chess[(x_temp1-6)/gap][(y_temp1-6)/gap]==black_or_white)
num++;
else
break;
}
if(num>max_temp&&num<5)
max_temp=num;
max_num=max_temp;
return max_num;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -