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

📄 firclient.java

📁 手机游戏对战平台。自己做的一个东东。 支持多个手机联网打游戏;采用socket链接通信。 平台:J2ME&JAVA
💻 JAVA
字号:
package control;

/*
 * EfeiCanvas.java	04-8-9
 *
 * @author Efei
 * @version 1.0
 */
import javax.microedition.lcdui.*;

import model.INFO;

import clientInf.ClientInterface;
import clientInf.GameClient;
import clientInf.Sender;
import clientInf.Serializable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import view.MidletGameHall;

public class FIRClient extends Canvas implements
        ClientInterface {

    final static int BaseX = 50;
    final static int BaseY = 20;
    final static int GridSize = 10;
    final static int BackColor = 0x006699cc;
    final static int lineColor = 0xeeeeeeee;
    final static int WhiteChess = 0xffffffff;
    final static int BlackChess = 0x00000000;
    final static int FocusColor = 0xff996633;
    String userAccount = "";
    int winner = 0;
    int CurrentX = 7;
    int CurrentY = 7;
    int PreX = 7;
    int PreY = 7;
    String IP;
    int step = -1;
    FIRMessage sendmsg = null;
    FIRMessage remsg = null;
    Sender sender;
    int port;
    GameClient gameclient = null;
    MidletGameHall gamehall;
    
    int[][] table = new int[15][15];

    public FIRClient(String IP, int port, String userAccount, MidletGameHall gamehall) {
        this.gamehall = gamehall;
        this.userAccount = userAccount;
        remsg = new FIRMessage(userAccount, 0, 0, 0);
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                table[i][j] = 0;
            }
        }
        this.IP = IP;
        if (gameclient == null) {
            gameclient = new GameClient(IP, port, remsg, this);
        }
    }

    public void show() {
        this.gamehall.switchDisplayable(null, this);
    }

    public void back() {
        gameclient.stop();
        gamehall.finishGame();
    }

    public void getSender(Sender sender) {
        this.sender = sender;
    }

    public void sendMessage(int x, int y, int step) {
        System.out.println("send msg " + x + " " + y + " " + step);
        if (winner != 0) {
            return;
        }
        sendmsg = new FIRMessage(userAccount, x, y, step);
        sender.send(sendmsg);
    }

    public void receiveMessage(Serializable message) {
        remsg = (FIRMessage) message;
        System.out.println("rec msg " + remsg.x + " " + remsg.y + " " + remsg.step);
        
        if (winner != 0) {
            return;
        //some one leave for error reason
        }
        if (remsg.step == -1000 && winner == 0) {

            try {
                GameService gs = new GameService();
                gs.setRecord(INFO.userAccount, 0, 1, true);
                gameclient.stop();
                winner=step % 2 + 1;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            return;
        } else if (remsg.step == 0 || remsg.step == 1) {            
            this.step = remsg.step;
            repaint();
            return;
        }

        if (remsg.step == this.step + 1) {
            table[remsg.x][remsg.y] = remsg.step % 2 + 1;
        }
        if (remsg.step == this.step + 2) {
            step += 2;
            table[remsg.x][remsg.y] = remsg.step % 2 + 1;
        }
        winner = checkWin(remsg.x, remsg.y);
        if(winner!=0){
        if (winner == 1) {
            GameService gs = new GameService();
            boolean isWinner;
            if (winner == step % 2 + 1) {
                isWinner = true;
            } else {
                isWinner = false;
            }
            try {
                if (isWinner==true)
                    gs.setRecord(userAccount, 0, 1, true);
                else
                    gs.setRecord(userAccount, 0, 0, false);
                gameclient.stop();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (winner == 2) {
            GameService gs = new GameService();
            boolean isWinner;
            if (winner == step % 2 + 1) {
                isWinner = true;
            } else {
                isWinner = false;
            }
            try {
                gs.setRecord(userAccount, 0, 1, isWinner);
                gameclient.stop();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
       
        }
        repaint();
        
         
    }

    private int checkWin(int x, int y) {
        int now = table[x][y];
        int count = 0;
        int tmpx = x;
        int tmpy = y;
        while (tmpx<14 && table[tmpx][tmpy] == now) {
            tmpx++;
        }
        while (tmpx > 0 && tmpx < 15 && tmpy >= 0 && tmpy < 15) {
            tmpx--;
            if (table[tmpx][tmpy] == now) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return now;
        }
        count = 0;
        tmpx = x;
        tmpy = y;
        while (tmpy<14 && table[tmpx][tmpy] == now) {
            tmpy++;
        }
        while (tmpx >= 0 && tmpx < 15 && tmpy > 0 && tmpy < 15) {
            tmpy--;
            if (table[tmpx][tmpy] == now) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return now;
        }
        count = 0;
        tmpx = x;
        tmpy = y;
        while (tmpx<14 && tmpy<14 && table[tmpx][tmpy] == now) {
            tmpy++;
            tmpx++;
        }
        while (tmpx > 0 && tmpx < 15 && tmpy > 0 && tmpy < 15) {
            tmpy--;
            tmpx--;
            if (table[tmpx][tmpy] == now) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return now;
        }
        count = 0;
        tmpx = x;
        tmpy = y;
        while (tmpy>0 && tmpx<14 && table[tmpx][tmpy] == now) {
            tmpy--;
            tmpx++;
        }
        while (tmpx > 0 && tmpx < 15 && tmpy >= 0 && tmpy < 14) {
            tmpy++;
            tmpx--;
            if (table[tmpx][tmpy] == now) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return now;
        }
        return 0;
    }

    protected void paint(Graphics g) {
        g.setColor(BackColor);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(lineColor);
        for (int i = 0; i < 15; i++) {
            g.drawLine(BaseX, BaseY + i * GridSize, BaseX + 14 * GridSize, BaseY + i * GridSize);
            g.drawLine(BaseX + i * GridSize, BaseY, BaseX + i * GridSize, BaseY + 14 * GridSize);
        }
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 15; j++) {
                int x = BaseX + i * GridSize;
                int y = BaseY + j * GridSize;
                if (table[i][j] == 1) {
                    g.setColor(WhiteChess);
                    g.fillArc(x - GridSize * 2 / 5, y - GridSize * 2 / 5,
                            GridSize * 4 / 5, GridSize * 4 / 5, 0, 360);
                } else if (table[i][j] == 2) {
                    g.setColor(BlackChess);
                    g.fillArc(x - GridSize * 2 / 5, y - GridSize * 2 / 5,
                            GridSize * 4 / 5, GridSize * 4 / 5, 0, 360);
                }
            }
        }
        g.setColor(FocusColor);
        System.out.println(CurrentX + " " + CurrentY);
        g.drawRect(BaseX + CurrentX * GridSize - 2 * GridSize / 5,
                BaseY + CurrentY * GridSize - 2 * GridSize / 5,
                4 * GridSize / 5,
                4 * GridSize / 5);
        if (this.step==0){
            g.drawString("You First.", BaseX + 6 * GridSize, BaseY + 16 * GridSize, Graphics.TOP | Graphics.LEFT);
        } else if (this.step==1){
            g.drawString("You Latter.", BaseX + 6 * GridSize, BaseY + 16 * GridSize, Graphics.TOP | Graphics.LEFT);
        }
        if (winner == 1) {
            //g.setFont(Font.)
            g.drawString("WhiteChess Win!!!\nPress any Key Back", BaseX + 6 * GridSize, BaseY + 16 * GridSize, Graphics.TOP | Graphics.LEFT);
            
        } else if (winner == 2) {
            g.drawString("BlackChess Win!!!\nPress any Key Back", BaseX + 6 * GridSize, BaseY + 16 * GridSize, Graphics.TOP | Graphics.LEFT);
        
        }
    }

    protected void keyPressed(int keyCode) {
        if(winner!=0) {
            
            back();
        }
        switch (getGameAction(keyCode)) {
            case Canvas.UP:
                if (CurrentY > 0) {
                    CurrentY--;
                }
                break;
            case Canvas.DOWN:
                if (CurrentY < 14) {
                    CurrentY++;
                }
                break;
            case Canvas.LEFT:
                if (CurrentX > 0) {
                    CurrentX--;
                }
                break;
            case Canvas.RIGHT:
                if (CurrentX < 14) {
                    CurrentX++;
                }
                break;
            case Canvas.FIRE:
                System.out.println("FIRE");
                if (table[CurrentX][CurrentY] == 0) {
                    sendMessage(CurrentX, CurrentY, step + 2);
                }
                break;
        }
        repaint();
    }
}

class FIRMessage implements Serializable {

    public FIRMessage(String user, int x, int y, int step) {
        this.userName = user;
        this.x = x;
        this.y = y;
        this.step = step;
    }
    public String userName;
    public int x,  y,  step;

    public void deserialize(DataInputStream is) throws IOException {
        userName = is.readUTF();
        x = is.readInt();
        y = is.readInt();
        step = is.readInt();
    }

    public void serialize(DataOutputStream os) throws IOException {
        os.writeUTF(userName);
        os.writeInt(x);
        os.writeInt(y);
        os.writeInt(step);
    }
    
    public String toString(){
        return userName+" "+x+" "+y+" "+step;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -