import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.List;
public class WzqCanvas extends Canvas implements CommandListener{
private int bianjie = 15; //边界宽度
private int cellWidth = 15; //单元格宽度
private int screenWidth; //屏幕宽度
private int cellnumb = 15; //单元个数量
private int[][] point; //标记每个点的占用情况,1:白方占用 0:未被占用 -1:黑方占用
private int x,y; //标记点二维数组的坐标
private int curX; //光标方框的左上角的x坐标
private int curY; //光标方框的左上角的y坐标
private boolean flag ; //标记该哪方下棋子,true:白方 false:黑方
private boolean blackWin ;
private boolean whiteWin ;
public WzqCanvas(){
screenWidth = cellnumb*cellWidth; //得到单元个数量
addCommand(new Command("重新开始",Command.SCREEN,1));
setCommandListener(this);
initGame();
}
/* 初始化变量 */
public void initGame(){
point = new int[cellnumb + 1][cellnumb + 1]; //初始化标记点二维数组
x = (int)(cellnumb/2.0);
y = x;
curX = bianjie + (int)(cellnumb/2.0)*(cellWidth) - cellWidth/2; //光标方框的左上角的x坐标
curY = bianjie + (int)(cellnumb/2.0)*(cellWidth) - cellWidth/2; //光标方框的左上角的y坐标
flag = true;
blackWin = false;
whiteWin = false;
}
protected void paint(Graphics g) {
g.setColor(128,128,128); //设置背景颜色
g.fillRect(0, 0, 250, 300); //填充背景色
/* 画网格 */
g.setColor(180, 10, 0); //设置网格颜色
for(int i = 0;i< cellnumb ;i++){
g.drawLine(bianjie, bianjie + i*cellWidth, screenWidth, bianjie + i*cellWidth); //画横线
g.drawLine(bianjie + i*cellWidth, bianjie , bianjie + i*cellWidth ,screenWidth); //画竖线
}
g.setColor(255, 255, 0);
g.drawRect(curX, curY, cellWidth, cellWidth); //光标方框
/**
* 已经占用的点
*/
for(int i =0 ;i<=cellnumb ;i++){
for(int j = 0;j<=cellnumb ;j++){
if(point[i][j] == 1){
g.setColor(0, 0, 0); //黑
g.fillArc((int)(bianjie + cellWidth*i - 0.5*cellWidth), (int)(bianjie + cellWidth*j - 0.5*cellWidth), cellWidth, cellWidth, 0, 360);
}
if(point[i][j] == -1){
g.setColor(255, 255, 255);//白
g.fillArc((int)(bianjie + cellWidth*i - 0.5*cellWidth), (int)(bianjie + cellWidth*j - 0.5*cellWidth), cellWidth, cellWidth, 0, 360);
}
}
}
if(blackWin){
g.setColor(0, 0, 0);
g.drawString("黑方胜利!!!", bianjie, screenWidth+10, Graphics.LEFT | Graphics.TOP);
} else if(whiteWin){
g.setColor(255,255,255);
g.drawString("白方胜利!!!", bianjie, screenWidth+10, Graphics.LEFT | Graphics.TOP);
}
}
protected void keyPressed(int keyCode) {
if(!(blackWin || whiteWin)){ //还没有获胜方
switch(getGameAction(keyCode)){
/* 触动方向键 */
case LEFT:
if(curX > bianjie){
curX -= cellWidth;
x--;
repaint();
}
break;
case RIGHT:
if(curX < screenWidth){
curX += cellWidth;
x++;
repaint();
}
break;
case DOWN:
if(curY < screenWidth){
curY += cellWidth;
y++;
repaint();
}
break;
case UP:
if(curY > bianjie){
curY -= cellWidth;
y--;
repaint();
}
break;
/* 触动开火键 */
case FIRE:
if(point[x][y] == 0){
if(flag){
point[x][y] = 1;
serviceRepaints();
blackWin = judgWin();
flag = false;
} else{
point[x][y] = -1;
serviceRepaints();
whiteWin = judgWin();
flag = true;
}
repaint();
}
break;
}
}
}
public boolean judgWin(){
int judg = point[x][y];
int x1 = x, y1 = y;
int count = 0;
/* 纵向判断 */
while(point[x1][y1] == judg){
count++;
y1--;
if(y1 < 0) break;
}
y1 = y+1;
while(point[x1][y1] == judg){
count++;
y1++;
if(y1 > cellnumb+1) break;
}
if(count == 5)
return true;
/* 横向判断 */
count = 0;
y1 = y;
while(point[x1][y1] == judg){
count++;
x1--;
if(x1 < 0) break;
}
x1 = x+1;
while(point[x1][y1] == judg){
count++;
x1++;
if(x1 > cellnumb+1) break;
}
if(count == 5)
return true;
/* 反对角线方向判断 */
count = 0;
x1 = x;
y1 = y;
while(point[x1][y1] == judg){
count++;
x1--;
y1++;
if(x1 < 0 || y1>cellnumb+1) break;
}
x1 = x+1;
y1 = y-1;
while(point[x1][y1] == judg){
count++;
x1++;
y1--;
if(x1 > cellnumb+1 || y1<0) break;
}
if(count == 5)
return true;
/* 对角线方向判断 */
count = 0;
x1 = x;
y1 = y;
while(point[x1][y1] == judg){
count++;
x1++;
y1++;
if(x1 >cellnumb+1 || y1>cellnumb+1) break;
}
x1 = x-1;
y1 = y-1;
while(point[x1][y1] == judg){
count++;
x1--;
y1--;
if(x1 <0 || y1<0) break;
}
if(count == 5)
return true;
return false;
}
public void commandAction(Command c, Displayable arg1) {
if(c.getLabel().equals("重新开始")){
initGame();
repaint();
}
}
}