四子棋游戏--bingo game

最近自己编了一个四子棋的游戏。说明如下:

游戏简介: 一种常见的四子棋游戏,可以是人机对战,两人对战,或者网上对战.
游戏的双方轮流落子,每人持有21颗子.棋子共有6*7个位置,玩家的棋子总是落到
当前列的最底处.当落子后,在横,竖,正斜或着反斜方向上连成4子时,当前玩家则
胜利.当所有子均用完时,游戏结束.
类的说明:
com.lilijun.game.bingo.ui:
GameUI:  游戏的主界面

com.lilijun.game.bingo.util:
GameCtroller:包含启动游戏,终止游戏等常用操作
GameTable: 游戏数据,用来判断胜利,结束等
TestWin: 提供了判断游戏胜利的算法

com.lilijun.game.bingo.player:
Player: 所有玩家的抽象类
AIPlayer:代表电脑角色的玩家,能自动取最优的选择
ManPlayer:代表人的玩家
NetPlayer:网络上的对手 

目前只能是单机游戏,双人玩。网络游戏待研究。代码会在后面随后给出

主程序:

 

/** */
package  com.lilijun.game.bingo.ui;

import  javax.swing.SwingUtilities;
import  javax.swing.JPanel;
import  javax.swing.JFrame;
import  java.awt.Rectangle;
import  javax.swing.JButton;
import  javax.swing.JLabel;
import  javax.swing.BorderFactory;
import  javax.swing.border.BevelBorder;
import  javax.swing.JOptionPane;
import  java.awt.event. * ;
import  java.util. * ;

import  com.lilijun.game.bingo.player. * ;
import  com.lilijun.game.bingo.util.GameTable;
/**
 * 
@author Administrator
 *
 
*/

public   class  MainUI  extends  JFrame  implements  Observer {

    
private static final long serialVersionUID = 1L;
    
private boolean begin = false;
    
private boolean isPlayer1Now = true;

    
private JPanel jContentPane = null;

    
private JPanel jPanel1 = null;

    
private JButton jButtonBegin = null;

    
private JButton jButtonQuit = null;

    
private JPanel jPanel3 = null;

    
private JLabel jLabel = null;

    
private JLabel jLabel2 = null;

    
private JLabel jLabel3 = null;

    
private JLabel jLabel4 = null;

    
private JPanel jPanel2 = null;

    
private Player player1 = null;  //  @jve:decl-index=0:
    
    
private Player player2 = null;  //  @jve:decl-index=0:
    
    
/**
     * This method initializes jPanel1    
     *     
     * 
@return javax.swing.JPanel    
     
*/

    
private JPanel getJPanel1() {
        
if (jPanel1 == null{
            jPanel1 
= new JPanel();
            jPanel1.setLayout(
null);
            jPanel1.setBounds(
new Rectangle(-10115231));
            jPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            jPanel1.add(getJButtonBegin(), 
null);
            jPanel1.add(getJButtonQuit(), 
null);
        }

        
return jPanel1;
    }


    
/**
     * This method initializes jButtonBegin    
     *     
     * 
@return javax.swing.JButton    
     
*/

    
private JButton getJButtonBegin() {
        
if (jButtonBegin == null{
            jButtonBegin 
= new JButton();
            jButtonBegin.setBounds(
new Rectangle(171367423));
            jButtonBegin.setText(
"begin");
            jButtonBegin.addActionListener(
new ActionListener(){
                
public void actionPerformed(ActionEvent e){
                    beginGame();
                }

            }
);
        }

        
return jButtonBegin;
    }


    
/**
     * This method initializes jButtonQuit    
     *     
     * 
@return javax.swing.JButton    
     
*/

    
private JButton getJButtonQuit() {
        
if (jButtonQuit == null{
            jButtonQuit 
= new JButton();
            jButtonQuit.setBounds(
new Rectangle(181757225));
            jButtonQuit.setText(
"quit");
            jButtonQuit.addActionListener(
new ActionListener(){
                
public void actionPerformed(ActionEvent e){
                    endGame();
                    System.exit(
0);
                }

            }
);
        }

        
return jButtonQuit;
    }


    
/**
     * This method initializes jPanel3    
     *     
     * 
@return javax.swing.JPanel    
     
*/

    
private JPanel getJPanel3() {
        
if (jPanel3 == null{
            jLabel4 
= new JLabel();
            jLabel4.setBounds(
new Rectangle(3753025));
            jLabel4.setText(
"false");
            jLabel3 
= new JLabel();
            jLabel3.setBounds(
new Rectangle(3403027));
            jLabel3.setText(
"true");
            jLabel2 
= new JLabel();
            jLabel2.setBounds(
new Rectangle(38735027));
            jLabel2.setText(
"玩家2");
            jLabel 
= new JLabel();
            jLabel.setText(
"玩家1");
            jLabel.setBounds(
new Rectangle(38395025));
            jPanel3 
= new JPanel();
            jPanel3.setLayout(
null);
            jPanel3.setBounds(
new Rectangle(371094234));
            jPanel3.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            jPanel3.add(jLabel, 
null);
            jPanel3.add(jLabel2, 
null);
            jPanel3.add(jLabel3, 
null);
            jPanel3.add(jLabel4, 
null);
        }

        
return jPanel3;
    }


    
/**
     * This method initializes jPanel2    
     *     
     * 
@return bingo.ui.ChessBoardPanel    
     
*/

    
private JPanel getJPanel2() {
        
if (jPanel2 == null{
            jPanel2 
= new ChessBoardPanel();
            jPanel2.setBounds(
new Rectangle(1140258231));
            jPanel2.addMouseListener(
new MouseAdapter(){
                
public void mouseClicked(MouseEvent e){
                    
int mouseX = e.getX();
                    
//int mouseY = e.getY();
                    int i = mouseX/(jPanel2.getWidth()/7);
                    
//int j = mouseY/jPanel2.getHeight();
                    if(begin){
                        
if(isPlayer1Now)
                            getPlayer1().putChess(i);
                            isPlayer1Now 
= !isPlayer1Now;
                            jLabel3.setText(
""+false);
                        }

                        
else {
                            getPlayer2().putChess(i);
                            isPlayer1Now 
= !isPlayer1Now;
                            jLabel4.setText(
""+true);
                        }

                    }

                    jPanel2.repaint();
                }

            }
);
        }

        
return jPanel2;
    }

    
    
/**
     * This method initializes player1
     * 
     * 
@return bingo.player.Player
     
*/

    
private Player getPlayer1() {
        
if (player1 == null){
            player1 
= new ManPlayer();
        }

        
return player1;
    }

    
    
/**
     * This method initializes player2
     * 
     * 
@return bingo.player.Player
     
*/

    
private Player getPlayer2() {
        
if (player2 == null){
            player2 
= new ManPlayer();
        }

        
return player2;
    }

    
    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable() {
            
public void run() {
                MainUI thisClass 
= new MainUI(GameTable.getInstance());
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(
true);
            }

        }
);
    }

     
    
/**
     * This is the default constructor
     
*/

    
public MainUI(GameTable gameTable) {
        
super();
        gameTable.addObserver(
this);
        
        initialize();
    }


    
/**
     * This method initializes this
     * 
     * 
@return void
     
*/

    
private void initialize() {
        
this.setSize(475265);
        
this.setContentPane(getJContentPane());
        
this.setTitle("JFrame");
    }


    
/**
     * This method initializes jContentPane
     * 
     * 
@return javax.swing.JPanel
     
*/

    
private JPanel getJContentPane() {
        
if (jContentPane == null{
            jContentPane 
= new JPanel();
            jContentPane.setLayout(
null);
            jContentPane.add(getJPanel1());
            jContentPane.add(getJPanel3());
            jContentPane.add(getJPanel2());
        }

        
return jContentPane;
    }

    
/**
     * TO DO to begin the game
     
*/

    
private void beginGame(){
        getPlayer1().beginGame();
        getPlayer2().beginGame();
        isPlayer1Now 
= true;
        begin 
= true;
    }

    
/**
     * TO DO to end the game
     
*/

    
private void endGame(){
        player1.endGame();
        player2.endGame();
        begin 
= false;
    }

    
/**
     * 
     
*/

    
public void update(Observable ob,Object arg){
        
if(isPlayer1Now) {
            JOptionPane.showMessageDialog(
null,"Player1 win!!!");
        }

        
else{
            JOptionPane.showMessageDialog(
null,"Player2 win!!!");
        }
        
        beginGame();
    }

}
   //   @jve:decl-index=0:visual-constraint="26,29"

 

 

你可能感兴趣的:(My,Program)