📄 gameengine.java
字号:
package com.j2medev.chapter5;
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
public class GameEngine implements Runnable {
private GoBang midlet = null;
private MainCanvas canvas = null;
//与服务器连接的socket
private SocketConnection socket = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
//玩家从服务器获得的playerID
private int playerID = -1;
//游戏的状态,这里定义了等待和游戏中两种
private int state = WAITING;
private boolean turn = false;
private boolean stop = false;
//15*15的五子棋盘,0=无旗子 playerID=自己 其他=对手
public int[][] grid = new int[15][15];
public static final int WAITING = 101;
public static final int PLAYING = 102;
public GameEngine(GoBang _midlet) {
midlet = _midlet;
}
//重新初始化棋盘
private void initGrid(){
for(int i = 0;i<15;i++){
for(int j = 0;j<15;j++){
grid[i][j] = 0;
}
}
}
//更新棋盘的单元
public void update(int x,int y){
if(grid[x][y]==0)
grid[x][y] = playerID;
else{
//不能走这步
Alert alert = new Alert("waring","you can't put here",null,AlertType.WARNING);
alert.setTimeout(2000);
GoBang.setCurrent(alert,canvas);
return;
}
canvas.repaint();
canvas.serviceRepaints();
int data = Protocol.STEP<<24|playerID<<16|x<<8|y;
//等对手走棋
turn = false;
//向服务器发送数据
send(data);
//判断输赢,如果赢则向服务器发送数据
boolean win = isGameOver(x,y);
if(win){
int over = Protocol.WIN<<24|playerID<<16;
send(over);
}
}
//向服务器发送数据
private void send(int data){
try {
dos.writeInt(data);
dos.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/*检查游戏是否结束,参数为此步的落点
*检查的算法是判断此点为中心,横向、纵向、左下到右上、右下到左上
*四个方向,是否满足5个同样的棋子相连
*/
private boolean isGameOver(int x,int y){
int num = 1;
//检查横向 -
for(int c = x-1;c>=0;c--){
if(grid[x][y]-grid[c][y]!=0)
break;
if(++num >= 5)
return true;
}
for(int c = x+1;c<=14;c++){
if(grid[x][y]-grid[c][y]!=0)
break;
if(++num >= 5)
return true;
}
num = 1;
//检查纵向 |
for(int c = y-1;c>=0;c--){
if(grid[x][y]-grid[x][c] !=0)
break;
if(++num >= 5)
return true;
}
for(int c=y+1;c<=14;c++){
if(grid[x][y]-grid[x][c]!=0)
break;
if(++num >=5)
return true;
}
num = 1;
//检查从左下到右上 /
for(int c = x+1,d = y-1;c<14&d>0;c++,d--){
if(grid[x][y]-grid[c][d] != 0)
break;
if(++num >= 5)
return true;
}
for(int c = x -1,d = y+1;c>0&d<14;c--,d++){
if(grid[x][y]-grid[c][d] != 0)
break;
if(++num >= 5)
return true;
}
num = 1;
//检查从右下到左上 \
for(int c = x- 1,d = y -1;c>0&d>0;c--,d--){
if(grid[x][y]-grid[c][d] != 0)
break;
if(++num >= 5)
return true;
}
for(int c = x+1,d = y+1;x<14&y<14;c++,d++){
if(grid[x][y]-grid[c][d] != 0)
break;
if(++num >=5)
return true;
}
return false;
}
//向服务器端发送退出游戏的数据
public void exitGame(){
send(Protocol.QUIT<<24|playerID<<16);
}
//结束游戏
private void endGame(String message){
initGrid();
state = WAITING;
try {
if(dos != null)
dos.close();
if(dis != null)
dis.close();
if(socket != null)
socket.close();
dos = null;
dis = null;
socket = null;
} catch (IOException ex) {
ex.printStackTrace();
}
Alert alert = new Alert("information",message,null,AlertType.INFO);
alert.setTimeout(2000);
GoBang.setCurrent(alert,midlet.getMenu());
}
public int getState(){
return state;
}
public void setState(int _state){
this.state = _state;
}
public int getPlayerID(){
return playerID;
}
public void setPlayerID(int _playerID){
this.playerID = _playerID;
}
public boolean getTurn(){
return turn;
}
public void setTurn(boolean _turn){
this.turn = _turn;
}
public void run(){
String message = "";
try {
//连接服务器
socket = (SocketConnection)Connector.open("socket://localhost:2200");
dos = socket.openDataOutputStream();
dis = socket.openDataInputStream();
//向服务器发送注册数据
dos.writeInt(Protocol.SINGUP<<24);
dos.flush();
//读取服务器的返回数据
while(!stop){
int msg = dis.readInt();
int type = msg>>>24;
switch(type){
case Protocol.FULL:{
message = "the server is full";
stop = true;
break;
}
//注册成功
case Protocol.ACK_SINGUP:{
playerID = (msg&0x00FF0000)>>>16;
System.out.println("Receiver id = "+playerID);
//收到服务器分配的id,创建MainCanvas
canvas = new MainCanvas(this,playerID);
state = WAITING;
GoBang.setCurrent(canvas);
break;
}
//收到服务器开始游戏的通知
case Protocol.START:{
int id = (msg&0x00FF0000)>>16;
state = PLAYING;
//先登录的玩家先走
if(playerID == id){
turn = true;
}
canvas.repaint();
canvas.serviceRepaints();
break;
}
//对手走了一步,更新grid数组和MainCanvas
case Protocol.STEP:{
int id = (msg&0x00FF0000)>>16;
int x = (msg&0x0000FF00)>>8;
int y = msg&0x000000FF;
grid[x][y]= id;
turn =true;
canvas.repaint();
canvas.serviceRepaints();
break;
}
//游戏结束
case Protocol.QUIT:{
stop = true;
message = "game is over";
break;
}
//胜负已分,游戏结束
case Protocol.WIN:{
stop = true;
int id = (msg&0x00FF0000)>>16;
System.out.println(id+" win");
if(id != playerID)
message = "sorry,you lose";
else
message = "congratulations,you win";
break;
}
//未知信
default:{
break;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
message = ex.getMessage();
}
endGame(message);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -