?? tictactoe.java
字號:
import gscript.*;
import java.util.*;
import javax.microedition.lcdui.*;
/**
* 一個例子 , a tictactoe example
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2007</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class TicTacToe {
int[][] chess = new int[][] {
{
0, 0, 0}
, {
0, 0, 0}
, {
0, 0, 0}
};
public static final int
NONE = 0
, BLACK = 1
, RED = 2
;
public static final int
B_WIN = 0
, B_FAIL = 1
, B_NONE = 2
;
public static final String[]
RESULT_STR = new String[] {
"Green win"
, "Red win"
, "Nobody win"
};
MainCanvas mc = null;
Interpreter interpreter ;
int curX = 0, curY = 0; //光標位置 , cursor position
boolean isShowing = false, isRed = true;
int result = B_NONE;
/**
* construct the game
* @param mc MainCanvas
*/
TicTacToe(MainCanvas mc) {
this.mc = mc;
interpreter= new Interpreter();
interpreter.load("/tictactoe.txt");
}
public void draw(Graphics g) {
int winW = mc.getWidth();
int winH = mc.getHeight();
int tileH = (mc.getHeight() / 3);
int tileW = mc.getWidth() / 3;
g.setColor(0);
g.fillRect(0, 0, winW, winH);
//畫格子 draw grid
g.setColor(0xffffff);
for (int i = 0; i < 3; i++) {
g.drawLine(0, i * tileH, winW, i * tileH);
}
for (int i = 0; i < 3; i++) {
g.drawLine(i * tileW, 0, i * tileW, winH);
}
//畫棋 draw chess
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (chess[i][j] == RED) {
g.setColor(0xff0000);
}
else
if (chess[i][j] == BLACK) {
g.setColor(0x00ff00);
}
if (chess[i][j] != NONE) {
g.fillArc(i * tileW + tileW / 4, j * tileH + tileH / 4, tileW >> 1, tileH >> 1, 0, 360);
}
}
}
//是否顯示結果狀態 whether is showing result
if (!isShowing) {
//當前格 current position
if(isRed)g.setColor(0xff0000);
else g.setColor(0x00ff00);
g.drawRect(curX * tileW, curY * tileH, tileW, tileH);
if (mc.isPressed(MainCanvas.MASK_LEFT)) {
curX--;
if (curX < 0) {
curX = 2;
}
}
else
if (mc.isPressed(MainCanvas.MASK_RIGHT)) {
curX++;
if (curX > 2) {
curX = 0;
}
}
else
if (mc.isPressed(MainCanvas.MASK_DOWN)) {
curY++;
if (curY > 2) {
curY = 0;
}
}
else
if (mc.isPressed(MainCanvas.MASK_UP)) {
curY--;
if (curY < 0) {
curY = 2;
}
}
//當按了5鍵 when pressed fire key
if (mc.isPressed(MainCanvas.MASK_FIRE)) {
if (chess[curX][curY] == NONE) {
chess[curX][curY] = isRed ? RED : BLACK;
isRed = !isRed;
result = check();//腳本檢測 call script engine
if (result != B_NONE) {
isShowing = true;
}
}
}
}
else {
g.setColor(0xffff00);
g.drawString(RESULT_STR[result], winW / 2, winH / 2, g.TOP | g.HCENTER);
if (mc.isPressed(MainCanvas.MASK_FIRE)) {
isShowing = false;
chess = new int[3][3];
}
}
mc.resetKey();
}
/**
* 腳本來判定勝負 call script engine to assess whose 's winner
* @return int
*/
private int check() {
Hashtable env = new Hashtable();
Array arr = new Array(new int[] {3, 3} );
//把數組傳進去,把j2me數組轉為自定義的數組Array
//convert int[][] to script Array
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[0].length; j++) {
arr.setValue(new int[] {i, j}
, new Int(chess[i][j]));
}
}
//放入環境變量 put in enveroment varible chess that int[3][3]
env.put("chess", arr);
//執行腳本 execute script and get return
Object o = interpreter.start(env);
//檢測返回 check resule
if (o instanceof Int) {
int r = ( (Int) o).getVal();
System.out.println(r);
return r;
}
return B_NONE;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -