import javax.microedition.lcdui.*;
import java.io.*;
import java.util.Random;
/************************************************
* 类的功能:实现画图的功能。目前版本1.0
* 修订时间:2007.3.20
* @author 肖松
* @version 1.0
************************************************/
class OmokCanvas extends Canvas {
//定义几种状态:初始化、连接状态,两边的状态,结果
private final int INIT = 0,
CONNECT = 1,
TURN0 = 2,
TURN1 = 3,
RESULT = 4;
private int gameMode;//游戏所处于的状态
int id0, id1;//对战双方的标记。id0为第一个,id1为第二个
private OmokBoard board;//画板
private Cursor cursor;//光标
private ServerConnector connector;//;连接器
private String message;//消息
private int row, col;
int num=20;
int second=15;
boolean islogin=false;
public OmokCanvas() {
board = new OmokBoard();//创建画板
cursor = new Cursor();//创建光标
connector = new ServerConnector(this);
setGameMode(INIT);//设置模式
}
public void start() {
if(gameMode == INIT)
setGameMode(CONNECT);
else if(gameMode == RESULT)
setGameMode(INIT);
}
public void stop() {
if(gameMode != INIT && gameMode != RESULT)
connector.disconnect(id1);
}
//设置游戏模式
private void setGameMode(int gameMode) {
this.gameMode = gameMode;
switch(gameMode) {
case INIT :
id0 = id1 = -1;//设置标记变量初始值
row = col = 6;//初始化棋盘大小
board.init();
break;
case CONNECT :
Thread cthread = new Thread()
{
public void run()
{
repaint();
if(islogin)
{connect();
System.out.println("执行connect();");
}
else
{loginGame();
System.out.println("执行login();");
}
}
};
cthread.start();
break;
case TURN0 :
cursor.setLocation(row, col);
break;
case TURN1 :
connector.receiveData(id0);
break;
}
repaint();
}
//连接
void connect() {
id0 = connector.connect();
// Display.getDisplay(Omok.instance).setCurrent(Omok.theCanvas);
//连接失败
if (id0 == -1) {
message = "失败";
setGameMode(RESULT);
}
//为偶数
else if (id0%2 == 0) {
id1 = id0 + 1;
connector.receiveData(id0);
}
//为奇数
else {
id1 = id0 - 1;
connector.sendData(id1, "C");
connector.receiveData(id0);
}
}
//登陆
void loginGame()
{ int flag=0;
System.out.println("验证前flag:"+flag);
id0=flag=connector.login(Omok.logo.UserId,Omok.logo.Psw);
System.out.println("验证后flag:"+flag);
if(flag != -1)
{
Display.getDisplay(Omok.instance).setCurrent(Omok.theCanvas);
islogin=true;
if (id0%2 == 0)
{
id1 = id0 + 1;
connector.receiveData(id0);
}
//为奇数
else
{
id1 = id0 - 1;
connector.sendData(id1, "C");
connector.receiveData(id0);
}
}
else
{
String s="帐号或者密码错误";
Alert alert = new Alert("警告!!!!");
alert.setTimeout(2000);
alert.setString(s);
Display.getDisplay(Omok.instance).setCurrent(alert);
System.out.println("退出程序");
Omok.instance.destroyApp(true);
}
}
//画图
public void paint(Graphics g) {
board.paint(g);
if(gameMode == INIT)
paintMessage(g, "棋局初始化");
else if (gameMode == CONNECT)
paintMessage(g, "连接中...");
else if (gameMode == TURN0)
{
paintMessage(g, "本方出棋.");
cursor.paint(g);
Thread athread = new Thread()
{
public void run()
{
while (true)
{
try
{
Thread.sleep(50);
}
catch (Exception e)
{
System.out.println("run is error: " + e);
}
}
}
};
athread.start();
} else if (gameMode == TURN1)
{
paintMessage(g, "等待对方出棋.");
Thread bthread = new Thread()
{
public void run()
{
while (true)
{
try
{
Thread.sleep(50);
}
catch (Exception e)
{
System.out.println("run is error: " + e);
}
}
}
};
bthread.start();
}
else if (gameMode == RESULT)
paintMessage(g, "结束");
}
//打印信息
private void paintMessage(Graphics g, String message) {
g.setColor(0, 0, 255);
g.fillRect(0, 184, 95, 16);
g.setColor(255, 255, 255);
g.drawString(message, 48, 184, g.HCENTER|g.TOP);
}
//按键处理
protected void keyPressed(int keyCode) {
if (gameMode != TURN0) return;
int action = getGameAction(keyCode);
switch(action) {
case LEFT : cursor.setX(cursor.getX() - 1);
break;
case RIGHT : cursor.setX(cursor.getX() + 1);
break;
case UP : cursor.setY(cursor.getY() - 1);
break;
case DOWN : cursor.setY(cursor.getY() + 1);
break;
case FIRE : if(dropStone(cursor.getX(), cursor.getY(), id0%2)) {
String x = null;
String y = null;
StringBuffer tx = new StringBuffer();
StringBuffer ty = new StringBuffer();
if(cursor.getX() < 10)
x = tx.append(0).append(cursor.getX()).toString();
else
x = tx.append(cursor.getX()).toString();
if(cursor.getY() < 10)
y = ty.append(0).append(cursor.getY()).toString();
else
y = ty.append(cursor.getY()).toString();
connector.sendData(id1, "D" + x + y);
}
break;
}
repaint();
}
//接收数据
public void receiveData(String data) {
//验证状态
if (data.substring(0, 1).equals("C")) {
if (id0%2 == 0) {
connector.sendData(id1, "C");
setGameMode(TURN1);
} else
setGameMode(TURN0);
}//接收数据状态
else if (data.substring(0, 1).equals("D")) {
row = Integer.parseInt(data.substring(1,3));
col = Integer.parseInt(data.substring(3,5));
dropStone(row, col, id1%2);
cursor.setLocation(row, col);
}
else if (data.substring(0, 1).equals("E")) {
message = "棋局结束";
setGameMode(RESULT);
}
}
private boolean dropStone(int row, int col, int player) {
if (!board.setStone(row, col, player))
return fals
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
先在PC机上安装jdk-1_5_0_04-nb-4_1-win.exe版本或者以上版本,然后在安装三星j2me_wireless_toolkit-2_2-ml-windows.exe。然后安装tomcat5.5和sql server 2000 1、把客户端的文件夹复制到\WTK\apps\,然后把服务器端的文件夹复制到\Tomcat\webapps\下面,把数据库文件导入到数据库中 2、开启tomcat、sql2000,打开三星模拟器,点击打开项目,找到Omok项目,打开,运行
资源推荐
资源详情
资源评论
收起资源包目录
程序代码.rar (63个子文件)
服务器端代码
test_Data.MDF 1024KB
servlet
WEB-INF
classes
OmokServlet.class 4KB
lib
proxool-0.8.3.jar 465KB
jstl.jar 17KB
mysql-connector-java-3.0.9-stable-bin.jar 221KB
cos.jar 55KB
mail.jar 320KB
activation.jar 53KB
servlet-api.jar 90KB
standard.jar 287KB
jsp-api.jar 48KB
jdom.jar 132KB
myjdbc.jar 59KB
src
OmokServlet.java 2KB
web.xml 972B
build.xml 1KB
test_Log.LDF 1024KB
客户端代码
Omok
classes
OmokBoard.class 2KB
OmokCanvas.class 5KB
Omok.class 2KB
Logo.class 2KB
Help.class 1KB
OmokCanvas$1.class 639B
Cursor.class 717B
OmokCanvas$2.class 772B
OmokCanvas$3.class 772B
ServerConnector$2.class 391B
ServerConnector.class 3KB
ServerConnector$1.class 904B
Omok$main.class 2KB
project.properties 162B
lib
Omok.pro 266B
src
Omok.java 4KB
Cursor.java 923B
OmokCanvas.java 8KB
OmokBoard.java.bak 4KB
Logo.java 2KB
Omok.java.bak 4KB
Help.java 1002B
OmokCanvas.java.bak 8KB
Help.java.bak 1006B
OmokBoard.java 4KB
ServerConnector.java.bak 3KB
ServerConnector.java 3KB
Logo.java.bak 2KB
res
Thumbs.db 18KB
1.png 6KB
bin
MANIFEST.MF 160B
Omok.jad 208B
Omok.jar 28KB
tmplib
tmpclasses
OmokBoard.class 2KB
OmokCanvas.class 5KB
Omok.class 2KB
Logo.class 2KB
Help.class 1KB
OmokCanvas$1.class 602B
Cursor.class 670B
OmokCanvas$2.class 732B
OmokCanvas$3.class 732B
ServerConnector$2.class 391B
ServerConnector.class 2KB
ServerConnector$1.class 864B
Omok$main.class 2KB
共 63 条
- 1
资源评论
wddblog
- 粉丝: 1522
- 资源: 260
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功