使用js实现黑白翻转棋

声明:之下代码是来自CSDN中的一个博客

主页:HaQiaME

黑白翻转棋:黑白棋,又叫翻转棋(Reversi)、奥赛罗棋(Othello)、苹果棋或反棋(Anti reversi)。

      黑白棋在西方和日本很流行。游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。  

关于使用JavaScript实现黑白翻转棋的代码有很多,不过整体算法大致相同

游戏规则 
如何玩 :

点击棋盘落子。


如何赢 :

使自己颜色棋子最多。


游戏目的 :

游戏通过相互翻转对方的棋子,最后以棋盘上谁的棋子多来判断胜负。


下棋方法 :

开始时在棋盘正中有两白两黑四个棋子交叉放置,黑棋总是先下子。

下子的方法 :

把自己颜色的棋子放在棋盘的空格上,而当自己放下的棋子在横、竖、斜八个方向内有一个自己的棋子,则被夹在中间的全部翻转会成为自己的棋子。并且,只有在可以翻转棋子的地方才可以下子。

胜负判定条件 :

如果玩家在棋盘上没有地方可以下子,则该玩家对手可以连下。双方都没有棋子可以下时棋局结束,以棋子数目来计算胜负,棋子多的一方获胜。

在棋盘还没有下满时,如果一方的棋子已经被对方吃光,则棋局也结束。将对手棋子吃光的一方获胜

安利一个在线体验的网页:https://js-game.github.io/othello/ (还是挺有趣的)


下面就是代码,这个代码有点长,还没有详细的去研究一下具体实现,有时间看看
import java.awt.*;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.awt.event.MouseEvent;  
import java.awt.event.MouseListener;  
import java.awt.image.BufferedImage;  
  
import javax.swing.*;  
  
public class Reversi extends JFrame implements ActionListener{  
  
    JButton jbStart = new JButton("开始游戏");  
    JButton jbStop = new JButton("结束游戏");  
    MyPanel panel = new MyPanel();  
    Container c = this.getContentPane();  
      
    int width = Toolkit.getDefaultToolkit().getScreenSize().width;  
    int height = Toolkit.getDefaultToolkit().getScreenSize().height;  
    boolean canPlay = false;  
    boolean isBlack = false;  
    int count = 0;  
    int f = 1;  
    int cntBlack = 0;  
    int cntWhite = 0;  
    int reversi[][] = new int[9][9];  
      
    public Reversi() {  
        Init();  
        addListener();  
    }  
      
    public void Init() {  
        this.setTitle("Reversi");  
        this.setSize(500, 500);  
        this.setResizable(false);  
        this.setLayout(null);  
        this.setVisible(true);  
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);  
        this.setLocation((width - 500)/2, (height - 500)/2);  
        jbStart.setBounds(135, 430, 100, 30);  
        jbStop.setBounds(265, 430, 100, 30);  
        panel.add(jbStart);  
        panel.add(jbStop);  
          
        panel.setLayout(null);  
        panel.setSize(500, 500);  
        c.add(panel);  
    }  
      
    public void addListener() {  
        this.jbStart.addActionListener(this);  
        this.jbStop.addActionListener(this);  
        this.addMouseListener(new MouseListener() {  
              
            public void mouseReleased(MouseEvent e) {  
                // TODO Auto-generated method stub  
                  
            }  
              
            public void mousePressed(MouseEvent e) {  
                // TODO Auto-generated method stub  
                  
                if(canPlay) {  
                    if(e.getX() >= 50 && e.getX() <= 450 && e.getY() >= 50 && e.getY() <= 450) {  
                        //System.out.println(e.getX() + "---" + e.getY());  
                        int x = e.getX();  
                        int y = e.getY();  
                        x = (x - 50) / 50;  
                        y = (y - 50) / 50;  
//                      System.out.println(x + " - " + y);  
                        if(reversi[x][y] == 0) {  
                            if(isBlack) {  
                                if(!isOK(x, y, 1))  
                                    return;  
                                reversi[x][y] = 1;  
                                //System.out.println(x + "-" + y + reversi[x][y]);  
                                //System.out.println(reversi[4][3]);  
                                isEated(x, y);  
                                repaint();  
                                //System.out.println(reversi[4][3]);  
                                isBlack = false;  
                                if(Check(2)) {  
                                    if(cntBlack > cntWhite)  
                                        JOptionPane.showMessageDialog(null, "黑子多于白子 ,黑方赢");  
                                    else if(cntBlack < cntWhite)  
                                        JOptionPane.showMessageDialog(null, "白子多余黑子,白方赢");  
                                    else  
                                        JOptionPane.showMessageDialog(null, "双方不分胜负!");  
                                }  
                                System.out.println(cntBlack + "---" + cntWhite);  
                            }  
                            else {  
                                if(!isOK(x, y, 2))  
                                    return;  
                                reversi[x][y] = 2;  
                                isEated(x, y);  
                                repaint();  
                                  
                                isBlack = true;  
                                if(Check(1)) {  
                                    if(cntBlack > cntWhite)  
                                        JOptionPane.showMessageDialog(null, "黑子多于白子 ,黑方赢");  
                                    else if(cntBlack < cntWhite)  
                                        JOptionPane.showMessageDialog(null, "白子多余黑子,白方赢");  
                                    else  
                                        JOptionPane.showMessageDialog(null, "双方不分胜负!");  
                                }  
                                System.out.println(cntBlack + "---" + cntWhite);  
                            }  
                        }  
                          
                          
                        repaint();  
                    }  
                }  
            }  
              
            public void mouseExited(MouseEvent e) {  
                // TODO Auto-generated method stub  
                  
            }  
              
            public void mouseEntered(MouseEvent e) {  
                // TODO Auto-generated method stub  
                  
            }  
              
            public void mouseClicked(MouseEvent e) {  
                // TODO Auto-generated method stub  
                  
            }  
        });  
    }  
      
    public void actionPerformed(ActionEvent e) {  
          
        if(e.getSource() == jbStart) {  
            canPlay = true;  
            jbStart.setEnabled(false);  
            InitFrame();  
            repaint();  
        }  
        if(e.getSource() == jbStop) {  
            System.exit(0);  
        }  
    }  
      
    public class MyPanel extends JPanel {  
        public void paintComponent(Graphics scr){  
              
            BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);  
            Graphics g = bi.createGraphics();  
              
            g.setColor(Color.BLACK);  
              
            for(int i = 1; i <= 9; i++) {  
                g.drawLine(i*50, 20, i*50, 420);  
            }  
            for(int i = 1; i <= 9; i++) {  
                g.drawLine(50, i*50-30, 450, i*50-30);  
            }  
              
            for(int i = 0; i <= 7; i++) {  
                for(int j = 0; j <= 7; j++) {  
                    int x;  
                    int y;  
                    x = i * 50 + 50;  
                    y = j * 50 + 20;  
                    if(reversi[i][j] == 1) {  
                        g.fillOval(x, y, 50, 50);  
                    }  
                    else if(reversi[i][j] == 2) {  
                        g.setColor(Color.WHITE);  
                        g.fillOval(x, y, 50, 50);  
                        g.setColor(Color.BLACK);  
                        g.drawOval(x, y, 50, 50);  
                    }  
                }  
            }  
            scr.drawImage(bi, 0, 0, this);  
        }  
    }  
      
    public void isEated(int x, int y) {  
          
        int color;  
          
        if(isBlack)  
            color = 1;  
        else  
            color = 2;  
          
        int flag = 0;  
        int tempx = x;  
        int tempy = y;  
        while(tempx > 0) { //
            tempx--;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = tempx + 1; i <= x; i++) {  
                    if(reversi[i][tempy] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = tempx + 1; i <= x; i++) {  
                    reversi[i][tempy] = color;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx < 7) { //
            tempx++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = x + 1; i <= tempx; i++) {  
                    if(reversi[i][tempy] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x + 1; i <= tempx; i++) {  
                    reversi[i][tempy] = color;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempy > 0) { //
            tempy--;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = tempy + 1; i <= y; i++) {  
                    if(reversi[tempx][i] == 0)   
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = tempy + 1; i <= y; i++) {  
                    reversi[tempx][i] = color;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempy < 7) { //
            tempy++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = y + 1; i <= tempy; i++) {  
                    if(reversi[tempx][i] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = y + 1; i <= tempy; i++) {  
                    reversi[tempx][i] = color;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx > 0 && tempy > 0) { //左上  
            tempx--;  
            tempy--;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = tempx + 1, j  = tempy + 1; i <= x && j <= y; i++, j++) {  
                    if(reversi[i][j] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = tempx + 1, j  = tempy + 1; i <= x && j <= y; i++, j++) {  
                    reversi[i][j] = color;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx > 0 && tempy < 7) { //左下  
            tempx--;  
            tempy++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {  
                    if(reversi[i][j] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {  
                    reversi[i][j] = color;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx < 7 && tempy > 0) { //右上  
            tempx++;  
            tempy--;  
            if(reversi[tempx][tempy] == color) {  
                //System.out.println("11");  
                for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {  
                    //System.out.println(i + "--" + j + reversi[i][j]);  
                    if(reversi[i][j] == 0) {  
                        flag = 1;  
                        System.out.println(i + "----" + j + reversi[i][j]);  
                    }  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {  
                    reversi[i][j] = color;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx < 7 && tempy < 7) { //右下  
            tempx++;  
            tempy++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = x + 1, j  = y + 1; i <= tempx && j <= tempy; i++, j++) {  
                    if(reversi[i][j] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x + 1, j  = y + 1; i <= tempx && j <= tempy; i++, j++) {  
                    reversi[i][j] = color;  
                }  
                break;  
            }  
        }  
    }  
      
public boolean isOK(int x, int y, int color) {  
          
        f = 0;  
        int flag = 0;  
        int tempx = x;  
        int tempy = y;  
        while(tempx > 0) { //
            tempx--;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = tempx + 1; i < x; i++) {  
                    if(reversi[i][tempy] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = tempx + 1; i < x; i++) {  
                    //reversi[i][tempy] = color;  
                    f = 1;  
                }  
                  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx < 7) { //
            tempx++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = x + 1; i < tempx; i++) {  
                    if(reversi[i][tempy] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x + 1; i < tempx; i++) {  
                    //reversi[i][tempy] = color;  
                    f = 1;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempy > 0) { //
            tempy--;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = tempy + 1; i < y; i++) {  
                    if(reversi[tempx][i] == 0)   
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = tempy + 1; i < y; i++) {  
                    //reversi[tempx][i] = color;  
                    f = 1;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempy < 7) { //
            tempy++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = y + 1; i < tempy; i++) {  
                    if(reversi[tempx][i] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = y + 1; i < tempy; i++) {  
                    //reversi[tempx][i] = color;  
                    f = 1;  
                }  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx > 0 && tempy > 0) { //左上  
            tempx--;  
            tempy--;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = tempx + 1, j  = tempy + 1; i < x && j < y; i++, j++) {  
                    if(reversi[i][j] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = tempx + 1, j  = tempy + 1; i < x && j < y; i++, j++) {  
                    //reversi[i][j] = color;  
                    f = 1;  
                }  
                  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx > 0 && tempy < 7) { //左下  
            tempx--;  
            tempy++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {  
                    if(reversi[i][j] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x - 1, j = y + 1; j < tempy; i--, j++) {  
                //  reversi[i][j] = color;  
                    f = 1;  
                }  
                  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx < 7 && tempy > 0) { //右上  
            tempx++;  
            tempy--;  
            if(reversi[tempx][tempy] == color) {  
                //System.out.println("11");  
                for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {  
                    //System.out.println(i + "--" + j + reversi[i][j]);  
                    if(reversi[i][j] == 0) {  
                        flag = 1;  
                        //System.out.println(i + "----" + j + reversi[i][j]);  
                    }  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x + 1, j  = y - 1; i < tempx; i++, j--) {  
                    //reversi[i][j] = color;  
                    f = 1;  
                }  
                  
                break;  
            }  
        }  
        flag = 0;  
        tempx = x;  
        tempy = y;  
        while(tempx < 7 && tempy < 7) { //右下  
            tempx++;  
            tempy++;  
            if(reversi[tempx][tempy] == color) {  
                for(int i = x + 1, j  = y + 1; i < tempx && j < tempy; i++, j++) {  
                    if(reversi[i][j] == 0)  
                        flag = 1;  
                }  
                if(flag == 1)  
                    break;  
                for(int i = x + 1, j  = y + 1; i < tempx && j < tempy; i++, j++) {  
                    //reversi[i][j] = color;  
                    f = 1;  
                }  
                  
                break;  
            }  
        }  
        if(f == 1)  
            return true;  
        else  
            return false;  
    }  
      
    public boolean Check(int color) {  
          
//      for(int i = 0; i <= 7; i++) {  
//          for(int j = 0; j <= 7; j++) {  
//              if(reversi[i][j] == color)   
//                  return false;  
//              else if(reversi[i][j] == color % 2 + 1)  
//                  count++;  
//          }  
//      }  
//      return true;  
        cntBlack = 0;  
        cntWhite = 0;  
        int cnt = 0;  
        for(int i = 0; i <= 7; i++) {  
            for(int j = 0; j <= 7; j++) {  
                  
                  
                if(reversi[i][j] == 1) {  
                    cntBlack++;  
                }  
                else if(reversi[i][j] == 2) {  
                    cntWhite++;  
                }  
                else {  
                    cnt++;  
                }  
            }  
        }  
          
        for(int i = 0; i <= 7; i++) {  
            for(int j = 0; j <= 7; j++) {  
                if(reversi[i][j] == 0 && isOK(i, j, color))  
                    return false;  
            }  
        }  
        JOptionPane.showMessageDialog(null, color == 1?"黑子无处可下,换白子行动":"白子无处可下,换黑子行动");  
        color = color % 2 + 1;  
          
        for(int i = 0; i <= 7; i++) {  
            for(int j = 0; j <= 7; j++) {  
                if(reversi[i][j] == 0 && isOK(i, j, color)) {  
                    if(color == 1) {  
                        isBlack = true;  
                    }  
                    else {  
                        isBlack = false;  
                    }  
                    return false;  
                }  
                      
            }  
        }  
        JOptionPane.showMessageDialog(null, "双方均无处可下,游戏结束");  
        return true;  
    }  
      
    public void InitFrame() {  
        reversi[3][3] = reversi[4][3] = 1;  
        reversi[3][4] = reversi[4][4] = 2;  
    }  
      
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        new Reversi();  
    }  
}  
原文地址:https://www.cnblogs.com/Code-ccc/p/9033324.html