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

📄 fivelinkmodel.java

📁 一款J2ME作得手机游戏
💻 JAVA
字号:
package org.javagarden.j2me.fivelink.client;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Image;


/**
 * 五子棋棋盘数据模型。
 * @author Turbo Chen
 * @created 2005-3-4
 */
public class FiveLinkModel
{
    public final static int NO_CHESS = 0;

    public final static int BLACK_CHESS = 1;

    public final static int WHITE_CHESS = -1;

    public final static int BOARD_SIZE = 15;

    private short[][] chess;
    
    public Player currentPlayer;

    public Player machine;

    public Player human;

    private boolean gameOver = false;
    
    private Vector history = new Vector();
    
    private Vector messages = new Vector( );

    private int level = 1;
    
    private Image[] S = new Image[4]; 
    
    public FiveLinkModel()
    {
        reset();
        try
        {
            S[0] = Image.createImage("/org/javagarden/j2me/fivelink/client/s1.jpg");
            S[1] = Image.createImage("/org/javagarden/j2me/fivelink/client/s2.jpg");
            S[2] = Image.createImage("/org/javagarden/j2me/fivelink/client/s3.jpg");
            S[3] = Image.createImage("/org/javagarden/j2me/fivelink/client/s4.jpg");
        }catch (IOException e)
        {
        }
    }
    
    public void upgrade()
    {
        if ( level >= 4 ) level = 1;
        else level++;
    }
    
    public Image getLevelBackground()
    {
        return S[level-1];
    }
    
    public void addHistoryRecord(Player player)
    {
        this.history.addElement(player);
    }
    
    public Player getHistoryRecord(int idx)
    {
        return (Player) this.history.elementAt(idx);
    }
    
    public int getHistoryRecordCount()
    {
        return this.history.size();
    }
    
    public void clearHistoryRecord()
    {
        this.history.removeAllElements();
    }

    public Vector getMessages()
    {
        return this.messages;
    }
    
    public void addMessage(String msg)
    {
        this.messages.addElement(msg);
    }
    
    public void clearMessage()
    {
        this.messages.removeAllElements();
    }
  
    public void exchangePlayer()
    {
        if ( currentPlayer == human )
            currentPlayer = machine;
        else if ( currentPlayer == machine )
            currentPlayer = human;
    }
    
    public void reset()
    {
        this.chess = new short[BOARD_SIZE][BOARD_SIZE];
    
        gameOver = false;
        
        Player p;
        if ( getHistoryRecordCount()>0 )
        {
            p = (Player) getHistoryRecord(getHistoryRecordCount()-1);
            if ( p.getId()==Player.HUMAN )
            {
                human = new Player(Player.HUMAN,Player.BLACK);
                machine = new Player(Player.MACHINE,Player.WHITE);
                upgrade();
            }else
            {
                human = new Player(Player.HUMAN,Player.WHITE);
                machine = new Player(Player.MACHINE,Player.BLACK);
            }
        }else
        {
            human = new Player(Player.HUMAN,Player.BLACK);
            machine = new Player(Player.MACHINE,Player.WHITE);
        }
            
        
        if ( human.getChessType()==Player.BLACK )
            currentPlayer = human;
        else
            currentPlayer = machine;
        
    }

    public boolean hasChess(int x,int y)
    {
        for ( int i=0;i<BOARD_SIZE;i++ )
            for ( int j=0;j<BOARD_SIZE;j++ )
            {
                if ( i==x&&j==y )
                {
                    if ( getChess()[i][j]!=NO_CHESS )
                        return true;
                }
            }
        return false;
    }
     
    /**
     * 在指定位置放棋子。
     * @param x
     * @param y
     * @return
     */
    public boolean putChess(int x, int y)
    {
        if ( ! hasChess(x,y) )
        {
            getChess()[x][y] = (short)currentPlayer.getChessType();
            return true;
        }else return false;
    }
    
    /**
     * @return
     */
    public boolean checkTie()
    {
        boolean b = AI.isTie(this.getChess());
        if ( b )
            addMessage("Tie this round.");
        return b;
    }

    public boolean checkWin(short x,short y,Player player)
    {
        //检测是否出现一方获胜
        //思想:
        //根据刚刚下的棋的的坐标,检测其周围是否存在连续5个相连的同颜色棋子.
        //传递进来的参数为:刚下棋子的所在行,列,颜色
        //棋盘:15X15
        
        short n=1;    //用来计数(1表示本身占1)
        int i;
        int j;
        
        //检测行
        for(i=x-1;i>=0;i--)
        {
            //检测左边
            if(getChess()[i][y]==player.getChessType())n++;
            else break;
        }
        for(i=x+1;i<BOARD_SIZE;i++)
        {
            //检测右边
            if(getChess()[i][y]==player.getChessType())n++;
            else break;
        }
        if(n>=5)
        {
            addMessage(currentPlayer + " Win!");
            return true;    // 返回player已胜
        }
        n=1;    //还原n
        
        //检测列
        for(i=y-1;i>=0;i--)
        {
            //检测上边
            if(getChess()[x][i]==player.getChessType())n++;
            else break;
        }
        for(i=y+1;i<BOARD_SIZE;i++)
        {
            //检测下边    
            if(getChess()[x][i]==player.getChessType())n++;
            else break;
        }
        if(n>=5)
        {
            addMessage(currentPlayer + " Win!");
            return true;    // 返回player已胜
        }

        n=1;

        //检测从左上往右下斜线
        for(i=x-1,j=y-1;i>=0&&j>=0;i--,j--)
        {
            //检测左上部分
            if(getChess()[i][j]==player.getChessType())n++;
            else break;
        }
        for(i=x+1,j=y+1;i<BOARD_SIZE&&j<BOARD_SIZE;i++,j++)
        {
            //检测右下部分
            if(getChess()[i][j]==player.getChessType())n++;
            else break;
        }
        if(n>=5)
        {
            addMessage(currentPlayer + " Win!");
            return true;    // 返回player已胜
        }
        n=1;

        //检测右上往左下斜线
        for(i=x+1,j=y-1;i<BOARD_SIZE&&j>=0;i++,j--)
        {
            //检测右上部分
            if(getChess()[i][j]==player.getChessType())n++;
            else break;
        }
        for(i=x-1,j=y+1;i>=0&&j<BOARD_SIZE;i--,j++)
        {
            //检测左下部分
            if(getChess()[i][j]==player.getChessType())n++;
            else break;
        }
        if(n>=5)
        {
            addMessage(currentPlayer + " Win!");
            return true;    // 返回player已胜
        }
        
        return false;

    }


	
    public boolean isGameOver()
    {
        return gameOver;
    }
    public void setGameOver(boolean gameOver)
    {
        if ( gameOver )
            this.addMessage("Game Over.");
        this.gameOver = gameOver;
    }

    /**
     * @param chess The chess to set.
     */
    public synchronized void setChess(short[][] chess)
    {
        this.chess = chess;
    }

    /**
     * @return Returns the chess.
     */
    public synchronized short[][] getChess()
    {
        return chess;
    }
    
    public synchronized void setChess(int x, int y, int chessType)
    {
        this.chess[x][y] = (short) chessType;
    }
    
}

⌨️ 快捷键说明

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