?? maingameplaywithai.java
字號:
/*
*與機器人對戰游戲畫面
*作者:肖昶
*
*/
package fivegame;
import java.io.IOException;
import javax.microedition.lcdui.*;
//Referenced classes of package fivepack:
// AIThread, FiveMidlet, Menu
public class MainGamePlayWithAI extends Canvas implements CommandListener {
static MainGamePlayWithAI instance;
public static byte Board[][];
int side;
static int N = 15;
int Width;
int Height;
static final int dxs[] = { 1, 1, 0, -1, -1, -1, 0, 1 };
static final int dys[] = { 0, 1, 1, 1, 0, -1, -1, -1 };
static final int cursorColor = 0xff0000;
static final int whiteColor = 0xffffff;
static final int blackColor = 0;
static final int boardColor = 0xf9a902;
static int cursorX;
static int cursorY;
static int StepIndex;
int StepRecord[][];
int UserColor;
public Command BackCmd;
public Command ReturnCmd;
public Command NOCmd;
public Command YESCmd;
static boolean ShowLost;
static boolean ShowWin;
Image youImage;
Image robotImage;
Image winImage;
Image lostImage;
public void clearBoard() {
cursorX = 7;
cursorY = 7;
StepIndex = -1;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++)
Board[i][j] = 0;
}
repaint();
}
public MainGamePlayWithAI() {
side = AIThread.userSide;
Width = getWidth();
Height = getHeight();
Board = new byte[N][N];
try {
winImage = Image.createImage("/Win.PNG");
lostImage = Image.createImage("/Lost.PNG");
youImage = Image.createImage("/YouTurn.PNG");
robotImage = Image.createImage("/RobotTurn.PNG");
} catch (IOException x) {
System.out.println("Load Image Error!" + x.getMessage());
}
StepRecord = new int[50][2];
BackCmd = new Command("返回", 2, 1);
ReturnCmd = new Command("悔棋", 4, 1);
NOCmd = new Command("否", 2, 1);
YESCmd = new Command("是", 4, 1);
addCommand(BackCmd);
addCommand(ReturnCmd);
setCommandListener(this);
}
public void commandAction(Command cmd, Displayable display) {
if (cmd == BackCmd || cmd == NOCmd) {
instance = null;
System.gc();
FiveGame.display.setCurrent(MainMenu.getInstance());
} else if (cmd == ReturnCmd)
ReturnOneStep();
else if (cmd == YESCmd) {
ShowLost = false;
ShowWin = false;
removeCommand(NOCmd);
removeCommand(YESCmd);
addCommand(BackCmd);
addCommand(ReturnCmd);
clearBoard();
}
}
public void checkWin(byte abyte0[][], int i, int j) {
try {
StringBuffer stringbuffer = new StringBuffer();
byte byte0 = AIThread.computerSide;
boolean flag = false;
boolean flag1 = false;
for (int k = 0; k < dxs.length; k++) {
int l = dxs[k];
int i1 = dys[k];
stringbuffer.delete(0, stringbuffer.length());
int j1 = i - l * 6;
int k1 = j - i1 * 6;
for (int l1 = 0; l1 < 12;) {
char c = 'X';
if (j1 >= 0 && j1 < 15 && k1 >= 0 && k1 < 15)
if (abyte0[j1][k1] == 0)
c = '.';
else if (abyte0[j1][k1] == byte0)
c = 'M';
else if (abyte0[j1][k1] == 3 - byte0)
c = 'W';
stringbuffer.append(c);
l1++;
j1 += l;
k1 += i1;
}
String s = stringbuffer.toString();
if (s.indexOf("MMMMM") >= 0)
flag1 = true;
else if (s.indexOf("WWWWW") >= 0)
flag = true;
}
if (flag1) {
ShowLost = true;
removeCommand(BackCmd);
removeCommand(ReturnCmd);
addCommand(NOCmd);
addCommand(YESCmd);
repaint();
side = AIThread.userSide;
}
if (flag) {
ShowWin = true;
removeCommand(BackCmd);
removeCommand(ReturnCmd);
addCommand(NOCmd);
addCommand(YESCmd);
repaint();
side = AIThread.userSide;
}
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
public void paint(Graphics g) {
g.setClip(0, 0, Width, Height);
g.setColor(0xf9a902);
g.fillRect(0, 0, Width, Height);
g.setColor(255);
if (side == AIThread.userSide && !ShowLost && !ShowWin)
g.drawImage(youImage, (getWidth() - youImage.getWidth()) / 2, 3, 0);
else if (side == AIThread.computerSide && !ShowLost && !ShowWin)
g.drawImage(robotImage, (getWidth() - robotImage.getWidth()) / 2,
3, 0);
int a = (Height - 6 - youImage.getHeight()) < Width ? (Height - 6 - youImage
.getHeight())
: Width;// 棋盤正方形的邊長
a = a - 8;// 留出邊界空隙
int d = a / 14;// 小格子邊長
int startX = (Width - (14 * d)) / 2;
int startY = (Height - 6 - youImage.getHeight() - (14 * d)) / 2
+ (6 + youImage.getHeight());
g.setColor(0);// 畫網格
for (int i = 0; i < 15; i++) {
g.drawLine(startX + i * d, startY, startX + i * d, startY + 14 * d);
g.drawLine(startX, startY + i * d, startX + 14 * d, startY + i * d);
}
g.setColor(0xff0000);
int x = startX + cursorX * d;
int y = startY + cursorY * d;
int r = d / 2;
// 畫光標
g.drawLine(x - r, y - r, x + r, y - r);
g.drawLine(x - r, y - r, x - r, y + r);
g.drawLine(x + r, y + r, x + r, y - r);
g.drawLine(x + r, y + r, x - r, y + r);
// 畫棋子
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++) {
if (Board[i][j] == 1)
g.setColor(0xffffff);
else if (Board[i][j] == 2)
g.setColor(0);
else
continue;
g.fillArc((startX + i * d) - (r - 1), (startY + j * d)
- (r - 1), 2 * (r - 1), 2 * (r - 1), 0, 360);
}
if (ShowLost)
g.drawImage(lostImage, (getWidth() - lostImage.getWidth()) / 2,
(getHeight() - lostImage.getHeight()) / 2, 0);
if (ShowWin)
g.drawImage(winImage, (getWidth() - winImage.getWidth()) / 2,
(getHeight() - winImage.getHeight()) / 2, 0);
}
protected void keyPressed(int key) {
if (side != AIThread.userSide || ShowLost || ShowWin)
return;
int k = getGameAction(key);
switch (k) {
case 3: // '\003'
case 4: // '\004'
case 7: // '\007'
default:
break;
case 1: // '\001'
if (cursorY > 0)
cursorY--;
break;
case 6: // '\006'
if (cursorY < 14)
cursorY++;
break;
case 2: // '\002'
if (cursorX > 0)
cursorX--;
break;
case 5: // '\005'
if (cursorX < 14)
cursorX++;
break;
case 8: // '\b'
if (side == AIThread.userSide && Board[cursorX][cursorY] == 0) {
Board[cursorX][cursorY] = AIThread.userSide;
StepIndex++;
StepRecord[StepIndex][0] = cursorX * 15 + cursorY;
side = AIThread.computerSide;
checkWin(Board, cursorX, cursorY);
AIThread AIThread = new AIThread(this);
AIThread.start();
}
break;
}
repaint();
}
public void ReturnOneStep() {
if (StepIndex != -1 && side == AIThread.userSide) {
int x1 = StepRecord[StepIndex][0] / 15;
int y1 = StepRecord[StepIndex][0] % 15;
int x2 = StepRecord[StepIndex][1] / 15;
int y2 = StepRecord[StepIndex][1] % 15;
StepIndex--;
cursorX = x2;
cursorY = y2;
try {
Thread.currentThread();
Thread.sleep(1000L);
} catch (Exception exception) {
}
repaint();
Board[x2][y2] = 0;
cursorX = x1;
cursorY = y1;
Board[x1][y1] = 0;
repaint();
}
}
public static MainGamePlayWithAI getInstance() {
if (instance == null)
instance = new MainGamePlayWithAI();
cursorX = 7;
cursorY = 7;
StepIndex = -1;
ShowLost = false;
ShowWin = false;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++)
Board[i][j] = 0;
}
return instance;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -