?? gameengine.java
字號(hào):
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;
//與服務(wù)器連接的socket
private SocketConnection socket = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
//玩家從服務(wù)器獲得的playerID
private int playerID = -1;
//游戲的狀態(tài),這里定義了等待和游戲中兩種
private int state = WAITING;
private boolean turn = false;
private boolean stop = false;
//15*15的五子棋盤,0=無旗子 playerID=自己 其他=對(duì)手
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;
//等對(duì)手走棋
turn = false;
//向服務(wù)器發(fā)送數(shù)據(jù)
send(data);
//判斷輸贏,如果贏則向服務(wù)器發(fā)送數(shù)據(jù)
boolean win = isGameOver(x,y);
if(win){
int over = Protocol.WIN<<24|playerID<<16;
send(over);
}
}
//向服務(wù)器發(fā)送數(shù)據(jù)
private void send(int data){
try {
dos.writeInt(data);
dos.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/*檢查游戲是否結(jié)束,參數(shù)為此步的落點(diǎn)
*檢查的算法是判斷此點(diǎn)為中心,橫向、縱向、左下到右上、右下到左上
*四個(gè)方向,是否滿足5個(gè)同樣的棋子相連
*/
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;
}
//向服務(wù)器端發(fā)送退出游戲的數(shù)據(jù)
public void exitGame(){
send(Protocol.QUIT<<24|playerID<<16);
}
//結(jié)束游戲
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 {
//連接服務(wù)器
socket = (SocketConnection)Connector.open("socket://localhost:2200");
dos = socket.openDataOutputStream();
dis = socket.openDataInputStream();
//向服務(wù)器發(fā)送注冊(cè)數(shù)據(jù)
dos.writeInt(Protocol.SINGUP<<24);
dos.flush();
//讀取服務(wù)器的返回?cái)?shù)據(jù)
while(!stop){
int msg = dis.readInt();
int type = msg>>>24;
switch(type){
case Protocol.FULL:{
message = "the server is full";
stop = true;
break;
}
//注冊(cè)成功
case Protocol.ACK_SINGUP:{
playerID = (msg&0x00FF0000)>>>16;
System.out.println("Receiver id = "+playerID);
//收到服務(wù)器分配的id,創(chuàng)建MainCanvas
canvas = new MainCanvas(this,playerID);
state = WAITING;
GoBang.setCurrent(canvas);
break;
}
//收到服務(wù)器開始游戲的通知
case Protocol.START:{
int id = (msg&0x00FF0000)>>16;
state = PLAYING;
//先登錄的玩家先走
if(playerID == id){
turn = true;
}
canvas.repaint();
canvas.serviceRepaints();
break;
}
//對(duì)手走了一步,更新grid數(shù)組和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;
}
//游戲結(jié)束
case Protocol.QUIT:{
stop = true;
message = "game is over";
break;
}
//勝負(fù)已分,游戲結(jié)束
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);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -