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

📄 heibaiqi.java

📁 Java编写的简易黑白翻转棋 实现人机对战 适合初学者学习
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class HeiBaiQi extends Applet implements MouseListener
{
    private static int BASE = 25;
    private int gamegrid[][];
    private boolean playing;
    private boolean playersMove;
    private boolean playerPlaysWhite;
    private boolean lastMoveWasWhite;
    private boolean allowPlay;

     public void init()
    {
        gamegrid = new int[8][8];
        resize(16*BASE, 16*BASE);
        this.addMouseListener(this);
    }
    public void start()
    {   //初始化棋盘(8*8),-1表示没有放棋子,1为放黑子,0为放白子
        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;

        lastMoveWasWhite = false;
        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 grey10 = new Color(0xe5e5e5);
        Color white  = new Color(0xffffff);
        Color green  = new Color(0x669966);
        Color darkgreen = new Color(0x336600);
        int radius = 20;
        //设置棋盘颜色
        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(gamegrid[x][y] == 0)
                {
                    g.setColor(grey80);
                    g.fillOval((xpos - radius) + 5, (ypos - radius) + 5, 
radius * 2, radius * 2);
                    g.setColor(grey10);
                    g.fillOval(xpos - radius, ypos - radius, radius * 2, 
radius * 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, radius * 2, radius * 2);
                        g.setColor(grey90);
                        g.fillOval(xpos - radius, ypos - radius, radius * 
2, radius * 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);
                    }
             }
    }

    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited (MouseEvent e) {}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e)
    {
        if(!allowPlay)//如果输了,就start()重新开始;
        {
            start();
            repaint();
            return;
        }
        int gridx =e.getX()/(BASE*2);
        int gridy =e.getY()/(BASE*2);
        if(playersMove)
        {        //在开始时,玩家放在不同的地方,则以为选择了不同颜色的棋子
            if(!playing)
                switch(gridx)
                {
                case 2:
                       if(gridy == 3)
                       {  playerPlaysWhite = true;  playing = true; }
                       else if(gridy == 4)
                       { playerPlaysWhite = false;  playing = true; }
                       else   playing = false;
                       break;
                case 3:
                      if(gridy == 2)
                      { playerPlaysWhite = true;   playing = true;}
                      else if(gridy == 5)
                      { playerPlaysWhite = false;  playing = true; }
                      else    playing = false;
                      break;
                case 4:
                      if(gridy == 2)
                      { playerPlaysWhite = false;   playing = true;}
                      else if(gridy == 5)
                      { playerPlaysWhite = true;  playing = true; }
                      else    playing = false;
                      break;
               case 5:
                      if(gridy == 3)
                      { playerPlaysWhite = false;   playing = true;}
                      else if(gridy == 4)
                      { playerPlaysWhite = true;  playing = true; }
                      else    playing = false;
                      break;
                default:
                    playing = false;
            }
            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;
            }
        }
    }
     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);//不这样做的话会闪烁的。
       //hangon();//此处加上该函数会明显看出界面的变化情况
       paint(g);
       r = null;
    }
    //若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;
        BreakUp: for(int p = -1; p < 2; p++)
            for(int q = -1; q < 2; q++)
                if(countindirection(x, y, p, q, !white) > 0)
                {
                    validway = true;
                    break BreakUp;//中断最外层的for循环!
                }
        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);

        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);

⌨️ 快捷键说明

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