📄 ticktacktoe.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;
public class TickTackToe extends MIDlet
{
Display display;
canvasTTT canvasttt;
public TickTackToe()
{
canvasttt=new canvasTTT();
display=Display.getDisplay(this);
}
public void startApp()
{
display.setCurrent(canvasttt);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
class canvasTTT extends Canvas implements CommandListener
{
Image handImage;
Image crossesImage;
Image noughtsImage;
Image ticktacktoeImage;
int gameaction;
int moveXY;
int i;
int j;
int k;
int kk;
int points;
boolean pcWin;
boolean pcCheck;
boolean youWin;
boolean youCheck;
boolean ok;
boolean gameOVRE;
boolean again;
boolean playAgain;
Command exitCommand;
Random pcrand;
mobilePlay mobileplay;
int[] array;
int[][] winGame={{0,1,2},{0,4,8},{0,3,6},{1,4,7},{2,4,6},{2,5,8},{3,4,5},{6,7,8}};
int[][] drawMap={{18,29},{68,29},{118,29},{18,79},{68,79},{118,79},{18,129},{68,129},{118,129}};
public canvasTTT()
{
array=new int[9];
pcrand=new Random();
try{
handImage=Image.createImage("/hand.png");
crossesImage=Image.createImage("/crosses.png");
noughtsImage=Image.createImage("/noughts.png");
ticktacktoeImage=Image.createImage("/ticktacktoe.png");
}catch(Exception ex){}
exitCommand = new Command("离开", Command.BACK, 1);
addCommand(exitCommand);
setCommandListener(this);
}
public void keyPressed(int keyCode)
{
gameaction=getGameAction(keyCode);
switch(gameaction)
{
case UP:
moveXY=moveXY-3;
if(moveXY<0) moveXY=0;
repaint();
break;
case DOWN:
moveXY=moveXY+3;
if(moveXY>6) moveXY=6;
repaint();
break;
case LEFT:
moveXY--;
if(moveXY<0) moveXY=0;
repaint();
break;
case RIGHT:
moveXY++;
if(moveXY>8) moveXY=8;
repaint();
break;
case FIRE:
if(playAgain)
{
init();
repaint();
}else
if(array[moveXY]==0)
{
array[moveXY]=1;
repaint();
gameStop();
if(again)
{
mobileplay=new mobilePlay();
mobileplay.start();
mobileplay=null;
}
else{
gameOVRE=true;
playAgain=true;
repaint();
}
}
break;
}
}
private void init()
{
gameOVRE=playAgain=youWin=pcWin=false;
for(i=0;i<9;i++)
array[i]=0;
}
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0);
g.drawImage(ticktacktoeImage, 0, 0, Graphics.LEFT | Graphics.TOP);
for(i=0; i<9; i++)
if(array[i]==1)
g.drawImage(noughtsImage, drawMap[i][0], drawMap[i][1], Graphics.LEFT | Graphics.TOP);
else if(array[i]==4)
g.drawImage(crossesImage, drawMap[i][0], drawMap[i][1], Graphics.LEFT | Graphics.TOP);
g.drawImage(handImage, 38+(moveXY%3)*50, 41+(moveXY/3)*50, Graphics.TOP | Graphics.HCENTER);
if(gameOVRE)
{
g.setColor(0x0000ff);
g.fillRect(40,50,100,50);
g.setColor(0xff0000);
if(youWin)
g.drawString("你赢了!",70,60,Graphics.LEFT | Graphics.TOP);
else if(pcWin)
g.drawString("你输了!",70,60,Graphics.LEFT | Graphics.TOP);
else
g.drawString("你们平手!",70,60,Graphics.LEFT | Graphics.TOP);
g.drawString("再玩一次",70,85,Graphics.LEFT | Graphics.TOP);
g.drawImage(handImage, 80, 90, Graphics.LEFT | Graphics.TOP);
}
}
class mobilePlay extends Thread
{
public void run()
{
checkAI(8);
if(pcCheck)
{
for(i=0;i<3;i++)
if(array[winGame[kk][i]]==0)
array[winGame[kk][i]]=4;
}
checkAI(2);
if(youCheck)
{
for(i=0;i<3;i++)
if(array[winGame[kk][i]]==0)
array[winGame[kk][i]]=4;
}
if(!pcCheck && !youCheck)
playgame();
youCheck=false;
pcCheck=false;
again=false;
winAI(3);
winAI(12);
if(youWin || pcWin)
gameOVRE=playAgain=true;
repaint();
}
}
private void gameStop()
{
for(i=0;i<9;i++)
if(array[i]==0)
again=true;
}
private void playgame()
{
points=(int)Math.abs(pcrand.nextInt()%9);
while(array[points]!=0)
points=(int)Math.abs(pcrand.nextInt()%9);
array[points]=4;
}
private void winAI(int checkwin)
{
k=0;
while(k<8)
{
if(array[winGame[k][0]]+array[winGame[k][1]]+array[winGame[k][2]]==checkwin)
{
if(checkwin==12)
pcWin=true;
else
youWin=true;
break;
}
k++;
}
}
private void checkAI(int checkStatus)
{
kk=0;
while(kk<8)
{
if(array[winGame[kk][0]]+array[winGame[kk][1]]+array[winGame[kk][2]]==checkStatus)
{
if(checkStatus==2 && (array[winGame[kk][0]]==0 || array[winGame[kk][1]]==0 || array[winGame[kk][2]]==0))
youCheck=true;
else if(checkStatus==8 && (array[winGame[kk][0]]==0 || array[winGame[kk][1]]==0 || array[winGame[kk][2]]==0))
pcCheck=true;
break;
}
kk++;
}
}
public void commandAction(Command cmd, Displayable disp)
{
if(cmd == exitCommand)
{
destroyApp(false);
notifyDestroyed();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -