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

📄 turncoat2.java

📁 黑白棋游戏的java实现
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.applet.Applet;
import java.awt.*;
import java.net.URL;

public class Turncoat2 extends Applet
{

    public void init()
    {
            //初始化棋盘(8*8),-1 表示没有放棋子   ,1为放黑子,0为放白子
        if(largeboard)
            BASE = 25;
        else
            BASE = 15;
        resize(16 * BASE, 16 * BASE);
        for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
                gamegrid[x][y] = -1;

        }
        gamegrid[3][3] = 1;
        gamegrid[3][4] = 0;
        gamegrid[4][3] = 0;
        gamegrid[4][4] = 1;

        
        //String legit1 = new String("www.darkfish.com");
        //String legit2 = new String("darkfish.com");
        //String theHost = getCodeBase().getHost();
        //if(theHost.equals(legit1) || theHost.equals(legit2))
        //    stolen = false;
        // else
        //    stolen = false;
    }

    public void start()
    {
        for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
                gamegrid[x][y] = -1;

        }

        gamegrid[3][3] = 1;
        gamegrid[3][4] = 0;
        gamegrid[4][3] = 0;
        gamegrid[4][4] = 1;
        playing = false;
        playersmove = true;
        playerplayswhite = false;
        allowplay = true;
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        Color black = new Color(0);
        Color grey90 = new Color(0x191919);
        Color grey80 = new Color(0x333333);
        Color grey60 = new Color(0x666666);
        Color grey40 = new Color(0x999999);
        Color grey20 = new Color(0xcccccc);
        Color grey15 = new Color(0xd8d8d8);
        Color grey10 = new Color(0xe5e5e5);
        Color white = new Color(0xffffff);
        Color green = new Color(0x669966);
        Color darkgreen = new Color(0x336600);
        int radius = 20;
        if(BASE == 15)
            radius = 12;
        //设置棋盘颜色
        g.setColor(darkgreen);
        g.fillRect(0, 0, 16 * BASE, 16 * BASE);
        //画分隔线
        g.setColor(green);
        for(int i = 1; i < 8; i++)
        {
            g.drawLine(0, i * BASE * 2, 16 * BASE, i * BASE * 2);
            g.drawLine(i * BASE * 2, 0, i * BASE * 2, 16 * BASE);
        }

        for(int x = 0; x < 8; x++)
        {
            for(int y = 0; y < 8; y++)
            {
                int xpos = x * BASE * 2 + BASE;
                int ypos = y * BASE * 2 + BASE;
                if(largeboard)
                {
                    //画黑棋子 ,两次填充,形成轮廓的效果
                    if(gamegrid[x][y] == 0)
                    {
                        g.setColor(grey80);
                        g.fillOval((xpos - radius) + 5, (ypos - radius) + 5, r
adius * 2, radius * 2);
                        g.setColor(grey10);
                        g.fillOval(xpos - radius, ypos - radius, radius * 2, r
adius * 2);
                        g.setColor(white);
                        g.fillOval(xpos - 19, ypos - 19, 36, 36);
                        g.setColor(grey40);
                        g.fillOval(xpos - 17, ypos - 17, 36, 36);
                        g.setColor(grey10);
                        g.fillOval(xpos - 18, ypos - 18, 36, 36);
                    } else
                    if(gamegrid[x][y] == 1)
                    {
                        g.setColor(grey80);
                        g.fillOval((xpos - radius) + 5, (ypos - radius) + 5, r
adius * 2, radius * 2);
                        g.setColor(grey90);
                        g.fillOval(xpos - radius, ypos - radius, radius * 2, r
adius * 2);
                        g.setColor(grey60);
                        g.fillOval(xpos - 19, ypos - 19, 36, 36);
                        g.setColor(black);
                        g.fillOval(xpos - 17, ypos - 17, 36, 36);
                        g.setColor(grey90);
                        g.fillOval(xpos - 18, ypos - 18, 36, 36);
                    }
                } else
                if(gamegrid[x][y] == 0)
                {
                    g.setColor(grey80);
                    g.fillOval((xpos - radius) + 3, (ypos - radius) + 3, radiu
s * 2, radius * 2);
                    g.setColor(grey10);
                    g.fillOval(xpos - radius, ypos - radius, radius * 2, radiu
s * 2);
                    g.setColor(white);
                    g.fillOval(xpos - 12, ypos - 12, 22, 22);
                    g.setColor(grey40);
                    g.fillOval(xpos - 10, ypos - 10, 22, 22);
                    g.setColor(grey10);
                    g.fillOval(xpos - 11, ypos - 11, 22, 22);
                } else
                if(gamegrid[x][y] == 1)
                {
                    g.setColor(grey80);
                    g.fillOval((xpos - radius) + 3, (ypos - radius) + 3, radiu
s * 2, radius * 2);
                    g.setColor(grey90);
                    g.fillOval(xpos - radius, ypos - radius, radius * 2, radiu
s * 2);
                    g.setColor(grey60);
                    g.fillOval(xpos - 12, ypos - 12, 22, 22);
                    g.setColor(black);
                    g.fillOval(xpos - 10, ypos - 10, 22, 22);
                    g.setColor(grey90);
                    g.fillOval(xpos - 11, ypos - 11, 22, 22);
                }
            }

        }

    }

    private void paintsquare(int x, int y)
    {
        Rectangle r = new Rectangle(x * BASE * 2, y * BASE * 2, BASE * 2, BASE
 * 2);
        Graphics g = getGraphics();
        g.clipRect(r.x, r.y, r.width, r.height);
        paint(g);
        r = null;
    }

    public boolean mouseUp(Event e, int x, int y)
    {
        if(stolen)
        {
            showStatus("Use of this applet is restricted to license holders on
ly.");
            return true;
        }
        if(!allowplay)
        {
            start();
            repaint();
            return true;
        }
        int gridx = gridify(x);
        int gridy = gridify(y);
        if(playersmove)
        {
            //在开始时,玩家放在不同的地方,则以为选择了不同颜色的棋子
            if(!playing)
                switch(gridx)
                {
                case 2: // '\002'
                    if(gridy == 3)
                    {
                        playerplayswhite = true;
                        playing = true;
                        break;
                    }
                    if(gridy == 4)
                    {
                        playerplayswhite = false;
                        playing = true;
                    } else
                    {
                        playing = false;
                    }
                    break;

                case 3: // '\003'
                    if(gridy == 2)
                    {
                        playerplayswhite = true;
                        playing = true;
                        break;
                    }
                    if(gridy == 5)
                    {
                        playerplayswhite = false;
                        playing = true;
                    } else
                    {
                        playing = false;
                    }
                    break;

                case 4: // '\004'
                    if(gridy == 2)
                    {
                        playerplayswhite = false;
                        playing = true;
                        break;
                    }
                    if(gridy == 5)
                    {
                        playerplayswhite = true;
                        playing = true;
                    } else
                    {
                        playing = false;
                    }
                    break;

                case 5: // '\005'
                    if(gridy == 3)
                    {
                        playerplayswhite = false;
                        playing = true;
                        break;
                    }
                    if(gridy == 4)
                    {
                        playerplayswhite = true;
                        playing = true;
                    } else
                    {
                        playing = false;
                    }
                    break;

                default:
                    playing = false;
                    break;
                }
            if(moveisvalid(gridx, gridy, !playerplayswhite))
            {
                plantapiece(gridx, gridy, playerplayswhite);
                showStatus("My move.");
                playersmove = false;
                lastmovewaswhite = playerplayswhite;
                nextMove();
            } else
            {
                showStatus("You can't move there. Try again.");
                playersmove = true;
            }
        }
        return true;
    }
    //此函数输入一个int值,该值是坐标点,返回在棋盘中的格子数
    private int gridify(int p)
    {
        int returnvalue = 0;
        if(p < BASE * 2)
            returnvalue = 0;
        if(p >= BASE * 2 && p < BASE * 4)
            returnvalue = 1;
        if(p >= BASE * 4 && p < BASE * 6)
            returnvalue = 2;
        if(p >= BASE * 6 && p < BASE * 8)
            returnvalue = 3;
        if(p >= BASE * 8 && p < BASE * 10)
            returnvalue = 4;
        if(p >= BASE * 10 && p < BASE * 12)
            returnvalue = 5;
        if(p >= BASE * 12 && p < BASE * 14)
            return 6;
        if(p >= BASE * 14)
            returnvalue = 7;
        return returnvalue;
    }
    //若x,y位置可以放置子,就返回true
    private boolean moveisvalid(int x, int y, boolean white)
    {
            //valid表示x,y位置是否已经放子
        boolean valid = true;
        if(gamegrid[x][y] != -1)
            valid = false;
        //validway表示x,y位置是否符合能够吃子的条件
        boolean validway = false;
        for(int p = -1; p < 2; p++)
        {
        //这里会不会出现validway被多次改变的情况??????????????

            for(int q = -1; q < 2; q++)
                if(countindirection(x, y, p, q, !white) > 0)
                    validway = true;

        }

        if(validway && valid)
            valid = true;
        else
            valid = false;
        return valid;
    }
    //这个函数是向上下左右,左上,左下,右上,右下八个方向寻找相反颜色的旗子

    private int countindirection(int x, int y, int deltax, int deltay, boolean
 black)
    {
        int returnvalue = 0;
        if(x + deltax < 0 || x + deltax > 7 || y + deltay < 0 || y + deltay > 
7)
            return 0;
        if(deltax == 0 && deltay == 0)
            return 0;
        boolean maybetrue = false;
        if(gamegrid[x + deltax][y + deltay] == 1)
            if(black)
                maybetrue = true;
            else
                return 0;
        if(gamegrid[x + deltax][y + deltay] == 0)
            if(!black)
                maybetrue = true;
            else
                return 0;
        if(gamegrid[x + deltax][y + deltay] == -1)
            return 0;
        if(maybetrue)
        {
            boolean continueloop = true;
            boolean itsopposite = false;
            int nextx = x + deltax;
            int nexty = y + deltay;
            while(continueloop)
            {
                if(gamegrid[nextx][nexty] == 1)
                    if(black)
                    {
                        returnvalue++;
                    } else
                    {
                        itsopposite = true;
                        continueloop = false;
                    }
                if(gamegrid[nextx][nexty] == 0)
                    if(!black)
                    {
                        returnvalue++;
                    } else
                    {
                        itsopposite = true;
                        continueloop = false;
                    }
                if(gamegrid[nextx][nexty] == -1)
                    continueloop = false;
                nextx += deltax;
                nexty += deltay;
                if(nextx < 0 || nextx > 7 || nexty < 0 || nexty > 7)
                    continueloop = false;
            }
            if(!itsopposite)
                returnvalue = 0;
        }
        return returnvalue;
    }
    //该函数输入置子位置和玩家执子颜色,将棋盘上的异色子吃掉
    private void plantapiece(int x, int y, boolean white)
    {
        if(white)
            gamegrid[x][y] = 0;
        else
            gamegrid[x][y] = 1;
        paintsquare(x, y);
        boolean black = true;
        if(white)
            black = false;
        for(int deltax = -1; deltax < 2; deltax++)
        {
            for(int deltay = -1; deltay < 2; deltay++)
                if(countindirection(x, y, deltax, deltay, white) > 0)
                {
                    int nextx = x + deltax;
                    int nexty = y + deltay;
                    for(boolean continueloop = true; continueloop;)
                    {
                        if(gamegrid[nextx][nexty] == 1)
                        {
                            if(white)
                            {
                                gamegrid[nextx][nexty] = 0;
                                paintsquare(nextx, nexty);
                            } else
                            {
                                continueloop = false;
                            }
                        } else
                        if(gamegrid[nextx][nexty] == 0)
                            if(!white)
                            {
                                gamegrid[nextx][nexty] = 1;
                                paintsquare(nextx, nexty);
                            } else
                            {
                                continueloop = false;
                            }
                        nextx += deltax;
                        nexty += deltay;
                        if(nextx < 0 || nextx > 7 || nexty < 0 || nexty > 7)

                            continueloop = false;
                    }

⌨️ 快捷键说明

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