Home  >  Article  >  Java  >  Java backgammon game code implemented based on swing

Java backgammon game code implemented based on swing

黄舟
黄舟Original
2016-12-24 11:05:471271browse

The example in this article describes the backgammon game code implemented in Java based on swing. Share it with everyone for your reference.

The main function code is as follows

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Main extends JFrame implements ActionListener{
        private static final long serialVersionUID = 1L;
        JButton[][] point=new JButton[40][40];
        int[][] state=new int[40][40];
        int[][] value=new int[40][40];
        int[] place=new int[2];
        public Main(){
                this.setTitle("五子棋");
                this.setBounds(100,10,1000,1000);
                this.setLayout(new GridLayout(30,30));
                int i,j;
                for(i=0;i<=39;i++){
                        for(j=0;j<=39;j++){
                                state[i][j]=0;
                                value[i][j]=0;
                        }
                }
                for(i=5;i<=34;i++){
                        for (j=5;j<=34;j++){
                                point[i][j]=new JButton("");
                                this.add(point[i][j]);
                                point[i][j].addActionListener(this);
                        }
                }
                this.setVisible(true);
        }
        public void actionPerformed(ActionEvent e)
    {
            int i,j;
            for(i=5;i<=34;i++){
                    for(j=5;j<=34;j++){
                            if(e.getSource()==point[i][j]){
                                    point[i][j].setBackground(Color.RED);
                                    state[i][j]=1;
                                    point[i][j].setEnabled(false);
                                    value[i][j]=0;
                            }
                    }
            }
            for(i=5;i<=34;i++){
                    for(j=5;j<=34;j++){
                            value[i][j]=value(i,j);
                            if(((state[i][j]==1)&&(state[i][j+1]==1)&&(state[i][j+2]==1)&&(state[i][j+3]==1)&&(state[i][j+4]==1))||
                                           ((state[i][j]==1)&&(state[i+1][j]==1)&&(state[i+2][j]==1)&&(state[i+3][j]==1)&&(state[i+4][j]==1))||
                                           ((state[i][j]==1)&&(state[i-1][j+1]==1)&&(state[i-2][j+2]==1)&&(state[i-3][j+3]==1)&&(state[i-4][j+4]==1))||
                                           ((state[i][j]==1)&&(state[i+1][j+1]==1)&&(state[i+2][j+2]==1)&&(state[i+3][j+3]==1)&&(state[i+4][j+4]==1))){
                                    JOptionPane.showMessageDialog(null,"你太厉害了吧,我打不过你");
                            }

The above is the content of the backgammon game code implemented in Java based on swing. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn