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

📄 firlogic.java

📁 非常好的JAVA编程的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


import java.io.PrintStream;
import java.util.*;
import javax.microedition.lcdui.Canvas;

public class FIRLogic
{
    private FIRCanvas myCanvas;
    private int boardSize;
    private boolean isComputerFirst;
    private int degree;
    public static int PLAYER_NONE = 0;
    public static int PLAYER_COMPUTER = 1;
    public static int PLAYER_MAN = 2;
    private int table[][];
    private Dot lastDot;
    private int playerCounter[];
    private Stack steps;
    private Dot triedDot;
    private static int GAIN_BY_COMPUTER = 5;
    private static int GAIN_BY_NONE = 1;
    private boolean isGameOver;
    private boolean isComputerWon;
    private boolean isThinking;
    private Random rndNum;
    public FIRLogic(FIRCanvas canvas, int boardSize, boolean isComputerFirst, int degree)
    {

    	
        this.boardSize = 15;
        isComputerFirst = true;
        degree = 1;
        myCanvas = canvas;
        this.boardSize = boardSize;

        this.isComputerFirst = isComputerFirst;
        this.degree = degree;
        table = new int[boardSize][boardSize];
        for(int r = 0; r < boardSize; r++)
        {
            for(int c = 0; c < boardSize; c++)
                table[r][c] = 0;

        }

        
        playerCounter = new int[3];
        playerCounter[0] = boardSize * boardSize;
        playerCounter[1] = 0;
        playerCounter[2] = 0;
        lastDot = new Dot(boardSize);
        steps = new Stack();
        triedDot = new Dot(-1, -1);
        isGameOver = false;
        isThinking = false;
        rndNum = new Random();
    }

    public int[][] getTable()
    {
        return table;
    }

    public Dot lastDot()
    {
        return lastDot;
    }

    public Dot triedDot()
    {
        return triedDot;
    }

    public boolean checkGameOver()
    {
        return isGameOver;
    }

    public boolean isComputerWon()
    {
        return isComputerWon;
    }

    public boolean isThinking()
    {
        return isThinking;
    }

    private boolean isGameOver()
    {
        isGameOver = false;
        for(int r = 0; r < boardSize; r++)
        {
            for(int c = 0; c < boardSize; c++)
            {
                if(table[r][c] == 0 || checkFiveInRow(r, c, 5, -1) == -1)
                    continue;
                isGameOver = true;
                isComputerWon = table[r][c] == 1;
                break;
            }

            if(isGameOver)
                break;
        }

        if(isGameOver)
            myCanvas.notifyGameEnd();
        return isGameOver;
    }

    public void manGo(int row, int col)
    {
        if(row >= 0 && row < boardSize && col >= 0 && col < boardSize && table[row][col] == 0)
        {
            goAt(row, col, 2);
            if(isGameOver())
            {
                if(isComputerWon)
                    myCanvas.setStatus("真遗憾!", 0xff0000, 2);
                else
                    myCanvas.setStatus("恭喜你!", 65280, 1);
            } else
            {
                computerGo();
            }
        }
    }

    private void goAt(int row, int col, int player)
    {
        int lastRow = lastDot.row;
        int lastCol = lastDot.col;
        table[row][col] = player;
        lastDot.setRowCol(row, col);
        myCanvas.repaintAt(lastRow, lastCol);
        myCanvas.repaintAt(row, col);
        switch(player)
        {
        case 1: 
            playerCounter[1]++;
            break;

        case 2: 
            playerCounter[2]++;
            break;
        }
        playerCounter[0]--;
        if(steps.size() > 10)
            steps.removeElementAt(0);
        steps.push(new Dot(row, col));
    }

    public boolean undo()
    {
        if(steps.size() >= 3)
        {
            Dot d = new Dot();
            d.copyFrom((Dot)steps.pop());
            table[d.row][d.col] = 0;
            myCanvas.repaintAt(d.row, d.col);
            d.copyFrom((Dot)steps.pop());
            table[d.row][d.col] = 0;
            myCanvas.repaintAt(d.row, d.col);
            d.copyFrom((Dot)steps.peek());
            lastDot.copyFrom(d);
            myCanvas.repaintAt(d.row, d.col);
            return true;
        } else
        {
            return false;
        }
    }

    public void computerGo()
    {
        myCanvas.setStatus("思考中...", 0, 0);
        myCanvas.serviceRepaints();
        think();
    }

    public void think()
    {
        isThinking = true;
        Dot dc = null;
        if((dc = to5L(1)) == null && (dc = to5L(2)) == null && (dc = to4B(1)) == null && (dc = to4B(2)) == null && (dc = toDouble4S_3B_2N1B(1, true)) == null && (dc = toDouble4S_3B_2N1B(2, true)) == null && (dc = toDouble4S_3B_2N1B(1, false)) == null && (dc = toDouble4S_3B_2N1B(2, false)) == null && (dc = toSingle4S_3B_2N1B(1)) == null)
            dc = toSingle4S_3B_2N1B(2);
        if(dc == null)
            dc = maxGainedDot();
        if(dc == null || playerCounter[0] == 0)
        {
            myCanvas.setStatus("平局!", 255, 3);
        } else
        {
            System.out.println("Gone!");
            goAt(dc.row, dc.col, 1);
            if(isGameOver())
            {
                if(isComputerWon)
                    myCanvas.setStatus("真遗憾!", 0xff0000, 2);
                else
                    myCanvas.setStatus("真厉害!", 65280, 1);
            } else
            {
                myCanvas.setStatus("请落子...");
            }
        }
        isThinking = false;
    }

    private Dot to4B(int player)
    {
        if(playerCounter[player] < 3)
            return null;
        Dot dot = null;
        int maxGain = 0;
        for(int r = 1; r < boardSize - 1; r++)
        {
            for(int c = 1; c < boardSize - 1; c++)
                if(table[r][c] == 0)
                {
                    int cd[] = connectedIn8D(player, r, c);
                    int ed[] = expandedIn8D(player, r, c);
                    for(int i = 0; i < 4; i++)
                        if(ed[i] > cd[i] && ed[i + 4] > cd[i + 4] && cd[i] + cd[i + 4] + 1 >= 4)
                        {
                            int gain = gainAt(r, c);
                            if(gain > maxGain || gain > 0 && gain == maxGain && randomTrue())
                            {
                                maxGain = gain;
                                dot = new Dot(r, c);
                            }
                        }

                }

        }

        return dot;
    }

    private Dot toSingle4S_3B_2N1B(int player)
    {
        if(playerCounter[player] < 2)
            return null;
        Dot dot = null;
        for(int r = 0; r < boardSize; r++)
        {
            for(int c = 0; c < boardSize; c++)
            {
                if(table[r][c] != 0 || find4S_3B_2N1BAt(r, c, player, -1) == -1)
                    continue;
                dot = new Dot(r, c);
                break;
            }

            if(dot != null)
                break;
        }

        return dot;
    }

    private Dot toDouble4S_3B_2N1B(int player, boolean only4S)
    {
        if(playerCounter[player] < 4)
            return null;
        Dot dot = null;
        for(int rTest = 0; rTest < boardSize; rTest++)
        {
            for(int cTest = 0; cTest < boardSize; cTest++)
            {
                if(table[rTest][cTest] != 0)
                    continue;
                int cd[] = connectedIn8D(player, rTest, cTest);
                if(cd[0] + cd[1] + cd[2] + cd[3] + cd[4] + cd[5] + cd[6] + cd[7] <= 0)
                    continue;
                triedDot.setRowCol(rTest, cTest);
                table[rTest][cTest] = player;
                boolean found = false;
                int dFirst = find4S_3B_2N1B(player, -1, rTest, cTest, only4S);
                if(dFirst != -1 && find4S_3B_2N1B(player, dFirst, rTest, cTest, false) != -1)
                    found = true;
                table[rTest][cTest] = 0;
                triedDot.setRowCol(-1, -1);
                if(!found)
                    continue;
                dot = new Dot(rTest, cTest);
                break;
            }

            if(dot != null)
                break;
        }

        return dot;
    }

    private int find4SAt(int row, int col, int player, int exceptDirection)
    {
        int dFond = -1;
        int cd[] = connectedIn8D(player, row, col);
        int ed[] = expandedIn8D(player, row, col);
        for(int d = 0; d < 4; d++)
        {
            if(d == exceptDirection || table[row][col] != player)
                continue;
            int nConnect = cd[d] + cd[d + 4] + 1;
            int nFree1 = ed[d] - cd[d];
            int nFree2 = ed[d + 4] - cd[d + 4];
            boolean b4S = nConnect >= 4 && (nFree1 >= 1 || nFree2 >= 1);
            if(!b4S)
                continue;
            dFond = d;
            break;
        }

        return dFond;
    }

    private int find4S_3B_2N1BAt(int row, int col, int player, int exceptDirection)
    {
        int dFond = -1;
        int cd[] = connectedIn8D(player, row, col);

⌨️ 快捷键说明

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