?? efeicanvas.java
字號:
}
/*
* 畫格子,并畫棋子。把這部分獨立出來主要是要檢查邊界問題
* x 格子右上角x
* y 格子右上角y
* width 格子寬度
* height 格子高度
*/
private void mydrawRect(int x, int y, int width, int height) {
int a, b;
a = (x - baseX) / gridWidth + 1;
b = (y - baseY) / gridHeight + 1;
if (a < 1 || b < 1 || a > lineNumber || b > lineNumber)return;
if (a != lineNumber && b != lineNumber) {
gg.setColor(lineColor);
gg.drawRect(x, y + 1, width, height);
}
DrawChessman(x, y);
}
/*
* 畫棋子,x,y分別是圓的中心坐標(biāo),棋子的直徑為格子邊長(格子應(yīng)為正方形)的4/5
*/
public void DrawChessman(int x, int y) {
try {
int a;
int b;
a = (x - baseX) / gridWidth + 1;
b = (y - baseY) / gridHeight + 1;
//if(a>12&&b<4)System.out.println(a+","+b);
if (a > lineNumber || a < 1 || b > lineNumber || b < 1) {
return;
}
if (qipan[ (b - 1) * lineNumber + a - 1] == 1)
gg.setColor(0, 0, 0);
else if (qipan[ (b - 1) * lineNumber + a - 1] == 2)
gg.setColor(255, 255, 255);
else {
return;
}
gg.fillArc(x - gridWidth * 2 / 5, y - gridHeight * 2 / 5,
gridWidth * 4 / 5, gridHeight * 4 / 5, 0, 360);
if (LastChessmanA == a && LastChessmanB == b) {
gg.setColor(0x00ff0000);
gg.drawLine(x, y - gridHeight / 5, x, y + gridHeight / 5);
gg.drawLine(x - gridWidth / 5, y, x + gridWidth / 5, y);
}
repaint();
}
catch (Exception e) {
System.out.println("DrawChessman Error:" + e);
}
}
/*
* 鍵盤被按下。主要是移動焦點,以及落子的處理
*/
protected void keyPressed(int keyCode) {
if (intRunMode != 1)return;
switch (getGameAction(keyCode)) {
case Canvas.LEFT:
if (currentX < baseX)return;
currentX = currentX - gridWidth;
currentA--;
moveFoucs(currentX - gridWidth / 2, currentY - gridHeight / 2,
gridWidth * 3, gridHeight * 2);
//moveFoucs(currentX,currentY,gridWidth*2,gridHeight);
break;
case Canvas.RIGHT:
if (currentX > baseX + gridWidth * (lineNumber - 2))return;
currentX = currentX + gridWidth;
currentA++;
moveFoucs(currentX - gridWidth * 3 / 2, currentY - gridHeight / 2,
gridWidth * 3, gridHeight * 2);
//moveFoucs(currentX-gridWidth,currentY,gridWidth*2,gridHeight);
break;
case Canvas.UP:
if (currentY < baseY)return;
currentY = currentY - gridHeight;
currentB--;
moveFoucs(currentX - gridWidth / 2, currentY - gridHeight / 2,
gridWidth * 2, gridHeight * 3);
//moveFoucs(currentX,currentY,gridWidth,gridHeight*2);
break;
case Canvas.DOWN:
if (currentY > baseY + gridHeight * (lineNumber - 2))return;
currentY = currentY + gridHeight;
currentB++;
moveFoucs(currentX - gridWidth / 2, currentY - gridHeight * 3 / 2,
gridWidth * 2, gridHeight * 3);
//moveFoucs(currentX,currentY-gridHeight,gridWidth,gridHeight*2);
break;
case Canvas.FIRE:
if (intPlayer == 0) {
int gotoValue;
if (qipan[lineNumber * (currentB - 1) + currentA - 1] != 0) {
System.out.println("此處有子");
return;
}
qipan[lineNumber * (currentB - 1) + currentA - 1] = 1;
//LastChessmanA = currentA;
//LastChessmanB = currentB;
DrawChessman(currentX + gridWidth / 2, currentY + gridHeight / 2);
gotoValue = tk();
//檢查人是否勝利
if (checkVictory())return;
//模擬電腦下一個
intPlayer = 1;
int a = currentA;
int b = currentB;
qipan[gotoValue] = 2;
a = LastChessmanA;
b = LastChessmanB;
LastChessmanA = (gotoValue + 1) % lineNumber;
LastChessmanB = (gotoValue + 1 - LastChessmanA) / lineNumber + 1;
DrawChessman( (a - 1) * gridWidth + baseX,
(b - 1) * gridHeight + baseY);
DrawChessman( (LastChessmanA - 1) * gridWidth + baseX,
(LastChessmanB - 1) * gridHeight + baseY);
tk();
checkVictory();
//準(zhǔn)備人下
intPlayer = 0;
break;
}
}
}
/**
private int getNumber(int x,int y)
{
return (y-baseY)*lineNumber/gridHeight+(x-baseX)/gridWidth;
}
*/
/**
* 分析當(dāng)前是否有一方勝利。并找出下一步該走在何處
*/
private int tk() {
try {
int[] e = {
1, 2, 4, 12, 24};
int[] c = new int[3];
int g_b = 0; //此變量記錄電腦應(yīng)下于何處
int n = 0;
int p;
int a0;
int h;
int a;
int d = 0;
int z = 0;
for (p = 0; p < 3; p++) { //對兩個大數(shù)組清零
for (a0 = 0; a0 < lineNumber * lineNumber; a0++) {
q[p][a0] = 0;
m[p][a0] = 0;
}
}
for (a0 = 0; a0 < lineNumber * lineNumber; a0++) {
for (d = 0; d < 4; d++) {
if ( (a0 / lineNumber < (lineNumber - 4) || d == 0) &&
(a0 % lineNumber < (lineNumber - 4) || d == 1 || d == 2) &&
(a0 % lineNumber > 3 || d != 2)) {
c[1] = 0;
c[2] = 0;
for (z = 0; z < 5; z++) {
c[qipan[a0 + z * g[d]]]++;
}
if (c[1] == 0)
p = 2;
else if (c[2] == 0)
p = 1;
else
p = 0;
if (p != 0) {
for (z = 0; z < 5; z++) {
if (c[p] == 5)
//記錄五個子的坐標(biāo)
wuzi[z] = a0 + z * g[d];
else if (qipan[a0 + z * g[d]] == 0) {
a = a0 + z * g[d];
q[0][a] += e[c[p]];
if (c[p] >= 2)
q[p][a] += e[c[p]];
if (c[p] > m[p][a])
m[p][a] = c[p];
}
}
}
}
}
for (p = 1; p < 3; p++)
if (q[p][a0] >= e[4]) {
h = 2 * m[p][a0];
if (p == 2)
h++;
if (q[0][a0] < lineNumber * lineNumber)
q[0][a0] += lineNumber * lineNumber * h;
else if (q[0][a0] < lineNumber * lineNumber * h)
q[0][a0] = lineNumber * lineNumber * h;
}
if (q[0][a0] > q[0][g_b])
n = 0;
if (q[0][a0] >= q[0][g_b]) {
n++;
if ( (rnd.nextInt() & 0x7FFFFFFF) % 1000 * n / 1000 < 1)
g_b = a0;
}
}
return g_b;
}
catch (Exception e) {
System.out.println("tk Error:" + e);
return 0;
}
}
/*
* 檢查是否有一方勝出,并做出相應(yīng)處理
*/
private boolean checkVictory() {
if (wuzi[1] != 0) { //WuZi[1]!=0表示已經(jīng)大到5個了,一方勝利
/*
for(int z=0;z<5;z++)
{
if(qipan[wuzi[z]]==1)
System.out.println("人勝"+wuzi[z]);
else
System.out.println("bot勝"+wuzi[z]);
}
*/
String ShowString;
if (intPlayer == 0)
ShowString = "你贏了!";
else
ShowString = "電腦贏了!";
gg.drawString(ShowString, baseX, baseY, gg.TOP | gg.LEFT);
return true;
}
else {
return false;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -