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

📄 chesses.java

📁 j2me源代码
💻 JAVA
字号:
package wzq;

import java.util.*;

/**
 * <p>Title:五子棋的数据信息类 </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company:Star Group </p>
 *
 * @author wangyaobsz
 * @version 1.0
 */
public class Chesses {
    public final static int NO_CHESS = 0; //定义常量NO_CHESS,表示当前位置没有棋子
    public final static int BLACK_CHESS = 1; //定义常量BLACK_CHESS,当前位置为黑子
    public final static int WHITE_CHESS = -1; //定义常量WHITE_CHESS,当前位置为白子
    public final static int BOARD_SIZE = 15; ////定义常量BOARD_SIZE,棋盘的大小

    private short[][] chess; //棋子信息数组
    private boolean bHumanPlayer; //当前是人还是机器在下棋
    private boolean bHumanFirst; // 是人先手还是机器先手
    private boolean bGameOver; //游戏是否结束
    private short nChessType; // 当前的棋子的类型
    private Vector messages = new Vector(); //消息队列


    public Chesses() {
        reset();
        bHumanFirst = true;
        setHumanFirst(bHumanFirst);
    }

    public Chesses(boolean bHumanFirst) {
        reset();
        setHumanFirst(bHumanFirst);
    }

    /**
     * 设置人先手的标志<p>
     * @param bManFirst
     * @return
     */
    public void setHumanFirst(boolean bManFirst) {
        this.bHumanFirst = bManFirst;
        this.bHumanPlayer = bManFirst;
    }

    /**
     * 重置棋局<p>
     */
    public void reset() {
        this.chess = new short[BOARD_SIZE + 1][BOARD_SIZE + 1];
        clearMessage();

        bGameOver = false;
        nChessType = BLACK_CHESS; //执黑先手
    }

    public Vector getMessages() {
        return this.messages;
    }

    public void addMessage(String msg) {
        this.messages.addElement(msg);
    }

    public void clearMessage() {
        this.messages.removeAllElements();
    }

    /**
     * 交换棋子类型<p>
     */
    public void changeChessType() {
        if (nChessType == WHITE_CHESS) {
            nChessType = BLACK_CHESS;
        } else {
            nChessType = WHITE_CHESS;
        }
        this.bHumanPlayer = !this.bHumanPlayer;
    }

    /**
     * 检测当前位置上是否已经有棋子<p>
     * @param x
     * @param y
     */
    public boolean bHasChess(int x, int y) {
        if (getChess()[x][y] != NO_CHESS) {
            return true;
        }
        return false;
    }

    /**
     * 检测当前棋局是否已经某一方获胜<p>
     * @param x
     * @param y
     */
    public boolean checkWin(int x, int y) {
        //检测是否出现一方获胜
        //根据刚刚下的棋的的坐标,检测其周围是否存在连续5个相连的同颜色棋子.
        //传递进来的参数为:刚下棋子的所在行,列
        //棋盘:15X15

        short n = 1; //用来计棋子的相连数目
        int i;
        int j;
        String strCurrentUser = (bHumanPlayer) ? "You" : "Machine";

        //检测行
        for (i = x - 1; i >= 0; i--) {//检测左边
            if (getChess()[i][y] == nChessType) {
                n++;
            } else {
                break;
            }
        }
        for (i = x + 1; i < BOARD_SIZE; i++) { //检测右边
            if (getChess()[i][y] == nChessType) {
                n++;
            } else {
                break;
            }
        }
        if (n >= 5) {
            addMessage(strCurrentUser + " Win!");
            return true; // 返回获胜标志
        }

        n = 1; //还原n
        //检测列
        for (i = y - 1; i >= 0; i--) {//检测上边
            if (getChess()[x][i] == nChessType) {
                n++;
            } else {
                break;
            }
        }
        for (i = y + 1; i < BOARD_SIZE; i++) {//检测下边
            if (getChess()[x][i] == nChessType) {
                n++;
            } else {
                break;
            }
        }
        if (n >= 5) {
            addMessage(strCurrentUser + " Win!");
            return true; // 返回获胜标志
        }

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

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

    /**
     *在当前位置上放置棋子<p>
     * @param x
     * @param y
     */
    public boolean putChess(int x, int y) {
        if (!bHasChess(x, y)) {
            getChess()[x][y] = nChessType;
            if (checkWin(x, y) == true) {
                setGameOver(true);
            }
            changeChessType();
            return true;
        }
        return false;
    }

    public boolean isGameOver() {
        return bGameOver;
    }

    public void setGameOver(boolean gameOver) {
        if (gameOver) {
            this.addMessage("Game Over.");
        }
        this.bGameOver = 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 short getCurrentType() {
        return this.nChessType;
    }

    /**
     * 在某个位置上预先放置棋子,以探测权值的大小,专为AI提供<p>
     * @param x
     * @param y
     * @param chessType
     */
    public synchronized void setChessFeeler(int x, int y, int chessType) {
        this.chess[x][y] = (short) chessType;
    }

}

⌨️ 快捷键说明

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