package com.j2medev.game;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
public class GameEngine implements Runnable{
private GoBang midlet = null;
private MainCanvas canvas = null;
private SocketConnection socket = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private int playerID = -1;
private int state = WAITING;
private boolean turn = false;
private boolean stop = false;
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;
}
turn = false;
canvas.repaint();
canvas.serviceRepaints();
int data = Protocol.STEP<<24|playerID<<16|x<<8|y;
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(Exception e)
{
e.printStackTrace();
}
}
//未判定游戏已经结束
private boolean isGameOver(int x,int y)
{
if((leftX(x,y)+rightX(x,y))>=4||(topUp(x,y)+topDown(x,y))>=4||(xUp(x,y)+xDown(x,y))>=4||(yUp(x,y)+yDown(x,y))>=4)
{
return true;
}
return false;
}
private int leftX(int x,int y) //判定这个棋子的左边有几个同色的棋子
{
int tempLeftX = 0;
while(--x>0)
{
if(grid[x][y] == playerID)
{
tempLeftX ++;
}
else
{
return tempLeftX;
}
}
return tempLeftX;
}
private int rightX(int x,int y) //判定这个棋子的右边有几个同色的棋子
{
int tempRightX = 0;
while(++x<15)
{
if(grid[x][y] == playerID)
{
tempRightX ++;
}
else
{
return tempRightX;
}
}
return tempRightX;
}
private int topUp(int x,int y) //判定这个棋子的左边有几个同色的棋子
{
int tempTopUp = 0;
while(--y>0)
{
if(grid[x][y] == playerID)
{
tempTopUp ++;
}
else
{
return tempTopUp;
}
}
return tempTopUp;
}
private int topDown(int x,int y) //判定这个棋子的右边有几个同色的棋子
{
int temptopDown = 0;
while(++y<15)
{
if(grid[x][y] == playerID)
{
temptopDown ++;
}
else
{
return temptopDown;
}
}
return temptopDown;
}
private int xUp(int x,int y)
{
int tempxUp = 0;
while(++x<15&&++y<15)
{
if(grid[x][y] == playerID)
{
tempxUp ++;
}
else
{
return tempxUp;
}
}
return tempxUp;
}
private int xDown(int x,int y)
{
int tempxDown = 0;
while(--x>0&&--y>0)
{
if(grid[x][y] == playerID)
{
tempxDown++;
}
else
{
return tempxDown;
}
}
return tempxDown;
}
private int yUp(int x,int y)
{
int tempyUp = 0;
while(--x>0&&++y<15)
{
if(grid[x][y] == playerID)
{
tempyUp++;
}
else
{
return tempyUp;
}
}
return tempyUp;
}
private int yDown(int x,int y)
{
int tempyDown = 0;
while(++x<15&&--y>0)
{
if(grid[x][y] == playerID)
{
tempyDown++;
}
else
{
return tempyDown;
}
}
return tempyDown;
}
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(Exception e)
{
e.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 boolean getTurn()
{
return turn;
}
public void setTurn(boolean _turn)
{
this.turn = _turn;
}
public void run()
{
String message = "";
try{
socket = (SocketConnection)Connector.open("socket://127.0.0.1:9999");
dos = socket.openDataOutputStream();
dis = socket.openDataInputStream();
dos.writeInt(Protocol.SINGUP<<24);
dos.flush();
while(!stop)
{
System.out.println("输出");
int msg = dis.readInt();
int type = (msg&0xFF000000)>>24;
switch(type)
{
case Protocol.FULL:{
message = "the server is full";
stop = true;
break;
}
case Protocol.ACK_SINGUP:{
System.out.println("注册成功");
playerID = (msg&0x00FF0000)>>16;
System.out.println("Receiver id ="+playerID);
canvas = new MainCanvas(this,playerID);
state = WAITING;
GoBang.setCurrent(canvas);
break;
}
case Protocol.START:{
System.out.println("游戏开始");
int id = (msg&0x00FF0000)>>16;
state = PLAYING;
System.out.println("id = "+id+" playerId="+playerID);
if(playerID == id)
{
turn = true;
}
canvas.repaint();
canvas.serviceRepaints();
break;
}
case Protocol.STEP:{
System.out.println("另一个客户端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:{
System.out.println("客户端收到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 = "congratualations,you win";
}
break;
}
default:{
break;
}
}
}
}catch(Exception e)
{
e.printStackTrace();
}
endGame(message);
}
}
评论5
最新资源