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

📄 fivelogic.java

📁 这是五子棋!很好的代码!里面的有模拟器就可以运行!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:


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

public class FiveLogic
{
    private FiveCanvas cavFive;
    private int borderSize;
    private boolean computerFirst;
    private int degree;
    public static int STONE_NONE = 0;
    public static int STONE_COMPUTER = 1;
    public static int STONE_MAN = 2;
    private int stones[][];
    private Dot lastDot;
    private int stoneCounter[];
    private Stack steps;
    private Dot triedDot;
    private static int GAIN_BY_COMPUTER = 5;
    private static int GAIN_BY_NONE = 1;
    private boolean endOfGame;
    private boolean computerWon;
    private boolean thinking;
    private Random rnd;
    public FiveLogic(FiveCanvas cav, int bSize, boolean cFirst, int deg)
    {

    	
        borderSize = 11;
        computerFirst = true;
        degree = 1;
        cavFive = cav;
        borderSize = bSize;
        computerFirst = cFirst;
        degree = deg;
        stones = new int[bSize][bSize];
        for(int r = 0; r < borderSize; r++)
        {
            for(int c = 0; c < borderSize; c++)
                stones[r][c] = 0;

        }

        lastDot = new Dot(borderSize);
        stoneCounter = new int[3];
        stoneCounter[1] = 0;
        stoneCounter[2] = 0;
        stoneCounter[0] = borderSize * borderSize;
        steps = new Stack();
        triedDot = new Dot(-1, -1);
        endOfGame = false;
        thinking = false;
        rnd = new Random();
    }

    public int[][] stones()
    {
        return stones;
    }

    public Dot lastDot()
    {
        return lastDot;
    }

    public Dot triedDot()
    {
        return triedDot;
    }

    public boolean gameEnd()
    {
        return endOfGame;
    }

    public boolean computerWon()
    {
        return computerWon;
    }

    public boolean thinking()
    {
        return thinking;
    }

    private boolean endOfGame()
    {
        endOfGame = false;
        for(int r = 0; r < borderSize; r++)
        {
            for(int c = 0; c < borderSize; c++)
            {
                if(stones[r][c] == 0 || existNLineWithMinFree(r, c, 5, 0, -1) == -1)
                    continue;
                endOfGame = true;
                computerWon = stones[r][c] == 1;
                break;
            }

            if(endOfGame)
                break;
        }

        if(endOfGame)
            cavFive.notifyGameEnd();
        return endOfGame;
    }

    public void manGo(int row, int col)
    {
        if(row >= 0 && row < borderSize && col >= 0 && col < borderSize && stones[row][col] == 0)
        {
            goAt(row, col, 2);
            if(endOfGame())
            {
                if(computerWon)
                    cavFive.setStatus("你输了!", 0xff0000, 2);
                else
                    cavFive.setStatus("\u4F60\u8D62\u4E86!", 65280, 1);
            } else
            {
                computerGo();
            }
        }
    }

    private void goAt(int row, int col, int playerStone)
    {
        int lastRow = lastDot.row;
        int lastCol = lastDot.col;
        stones[row][col] = playerStone;
        lastDot.setRowCol(row, col);
        cavFive.repaintAt(lastRow, lastCol);
        cavFive.repaintAt(row, col);
        switch(playerStone)
        {
        case 1: // '\001'
            stoneCounter[1]++;
            break;

        case 2: // '\002'
            stoneCounter[2]++;
            break;
        }
        stoneCounter[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());
            stones[d.row][d.col] = 0;
            cavFive.repaintAt(d.row, d.col);
            d.copyFrom((Dot)steps.pop());
            stones[d.row][d.col] = 0;
            cavFive.repaintAt(d.row, d.col);
            d.copyFrom((Dot)steps.peek());
            lastDot.copyFrom(d);
            cavFive.repaintAt(d.row, d.col);
            return true;
        } else
        {
            return false;
        }
    }

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

    public void think()
    {
        thinking = true;
        Dot dc = null;
        if((dc = thinkInNumber(1, 5)) == null && (dc = thinkInNumber(2, 5)) == 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 || stoneCounter[0] == 0)
        {
            cavFive.setStatus("平局!", 255, 3);
        } else
        {
            System.out.println("Gone!");
            goAt(dc.row, dc.col, 1);
            if(endOfGame())
            {
                if(computerWon)
                    cavFive.setStatus("\u4F60\u8F93\u4E86!", 0xff0000, 2);
                else
                    cavFive.setStatus("\u4F60\u8D62\u4E86 !", 65280, 1);
            } else
            {
                cavFive.setStatus("\u8BF7\u4E0B\u5B50...");
            }
        }
        thinking = false;
    }

    private Dot to4B(int playerStone)
    {
        if(stoneCounter[playerStone] < 3)
            return null;
        Dot dot = null;
        int maxGain = 0;
        for(int r = 1; r < borderSize - 1; r++)
        {
            for(int c = 1; c < borderSize - 1; c++)
                if(stones[r][c] == 0)
                {
                    int cd[] = connectedIn8D(playerStone, r, c);
                    int ed[] = expandedIn8D(playerStone, 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 playerStone)
    {
        if(stoneCounter[playerStone] < 2)
            return null;
        Dot dot = null;
        for(int r = 0; r < borderSize; r++)
        {
            for(int c = 0; c < borderSize; c++)
            {
                if(stones[r][c] != 0 || find4S_3B_2N1BAt(r, c, playerStone, -1) == -1)
                    continue;
                dot = new Dot(r, c);
                break;
            }

            if(dot != null)
                break;
        }

        return dot;
    }

    private Dot toDouble4S_3B_2N1B(int playerStone, boolean only4S)
    {
        if(stoneCounter[playerStone] < 4)
            return null;
        Dot dot = null;
        for(int rTest = 0; rTest < borderSize; rTest++)
        {
            for(int cTest = 0; cTest < borderSize; cTest++)
            {
                if(stones[rTest][cTest] != 0)
                    continue;
                int cd[] = connectedIn8D(playerStone, 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);
                stones[rTest][cTest] = playerStone;
                boolean found = false;
                int dFirst = find4S_3B_2N1B(playerStone, -1, rTest, cTest, only4S);
                if(dFirst != -1 && find4S_3B_2N1B(playerStone, dFirst, rTest, cTest, false) != -1)
                    found = true;
                stones[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 playerStone, int exceptDirection)
    {
        int dFond = -1;
        int cd[] = connectedIn8D(playerStone, row, col);
        int ed[] = expandedIn8D(playerStone, row, col);
        for(int d = 0; d < 4; d++)
        {
            if(d == exceptDirection || stones[row][col] != playerStone)
                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 playerStone, int exceptDirection)
    {
        int dFond = -1;
        int cd[] = connectedIn8D(playerStone, row, col);

⌨️ 快捷键说明

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