⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tictactoe.java

📁 A very smaller script interpretor that java based system. Can work on j2se / j2me / win C++ platform
💻 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 + -