简易桌面五子棋对战游戏(java实现)

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
public class FiveChess extends JFrame{
	private char whoseTurn='w';//黑方为B,白方为W
	private JLabel jlblStatus=new JLabel("黑方下棋");	
	JPanel showWhoseTurn=new JPanel();
	public FiveChess(){			
		ChessConvas ChessPanel=new ChessConvas();
		add(ChessPanel,BorderLayout.CENTER);
		//add(jlblStatus,BorderLayout.NORTH);	
		jlblStatus.setFont(new Font("宋体",Font.BOLD,18));	
		showWhoseTurn.add(jlblStatus,new GridLayout(1,3));	
		add(showWhoseTurn,BorderLayout.NORTH);	
	}
	public static void main(String[] args){	
		JFrame fr=new FiveChess();	
		fr.setLocationRelativeTo(null);
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fr.setTitle("FiveChess");
		fr.setVisible(true);
		fr.setResizable(false);
		fr.setSize(650,650);
	}

//棋盘类
 	class ChessConvas extends JPanel{
 		//to move the ChessConvas by xMove and yMove
 		private int xMove=60;
 		private int yMove=60;
 		//to set the position chess
 		private int xPosition;
 		private int yPosition; 		
 		//private RoundButton1 []roundButtonArray=new RoundButton1[256];
 		private ArrayList rbList=new ArrayList(); 		
 		private char chessSide=' ';//执棋的一方 
 		private JPanel btPanel=new JPanel();
 		private JButton btReset=new JButton("重新开始");
 		//private JLabel jlblBackImage=new JLabel(new ImageIcon("chessFive.gif"));		
		public ChessConvas(){			
			addMouseListener(new MouseListener());			
			//jlblBackImage.setBounds(0,0,650,650);
			//add(jlblBackImage);	
			//btPanel.setBounds(new Rectangle(0,0,650,100));
			//btPanel.setLocation(0,5);		
			//btPanel.setBackground(Color.RED);
			//btPanel.setLayout(new GridLayout(1,3,5,5));
			//btPanel.add(btReset);
			//btReset.setLocation(490,10);为什么这个不行呢?那它又有什么用呢?它与setBounds有什么不同呢?
			btReset.setBounds(new Rectangle(440,40,100,30));
			btReset.addActionListener(new ResetEvent());
			add(btReset);		
			setLayout(null);//很关键,以在坐标上画组件,不然默认的是FlowLayout布局			
		}	
		public void setChessSide(char c){
			chessSide=c;
			repaint();
		}
		public char gerChessColor(){
			return chessSide;
		}
		public void setXPosition(int xp){
			xPosition=xp;
			repaint();
		}
		public int getXPosition(){
			return xPosition;
		}
		public void setYPosition(int yp){
			yPosition=yp;
			repaint();
		}
		public int getYPosition(){
			return yPosition;
		}	
	public boolean find(ArrayList rbList,int x,int y,Color bakColor){		
		for(int i=0;i<rbList.size();i++){			
			if((((RoundButton1)rbList.get(i)).getX()==x )&&(((RoundButton1)rbList.get(i)).getY()==y)&&(((RoundButton1)rbList.get(i)).getBackground()==bakColor)){
				return true;
				}			
		}
			return false;
	}
	public boolean isWinner(ArrayList rbList,int x,int y,Color bakColor){				
		for(int i=0,k=0;i<6;i++){
				//竖直方向				
			if(find(rbList,x+0*i,y+30*i,bakColor))k++;
			if(find(rbList,x+0*i,y-30*i,bakColor)){
					if(i!=0)k++;										
			 }
				System.out.println("竖直方向  k= "+k);
			if(!find(rbList,x+0*i,y+30*i,bakColor)&&!find(rbList,x+0*i,y-30*i,bakColor))
						break;//若竖直两个方向都走不通则跳出循环							
			if(k==5){
					jlblStatus.setText((bakColor==Color.BLACK)?"黑方获胜!!!":"白方获胜!!!");				
					return true;
			}			
		}
		for(int i=0,k=0;i<6;i++){
			//左上至右下方向				
			if(find(rbList,x-30*i,y-30*i,bakColor))k++;
			if(find(rbList,x+30*i,y+30*i,bakColor)){
					if(i!=0)k++;										
			}
			System.out.println("左上至右下方向 k= "+k);	
			if(!find(rbList,x-30*i,y-30*i,bakColor)&&!find(rbList,x+30*i,y+30*i,bakColor))
					break;	//若左上与右下两个方向都走不通则跳出循环				
				if(k==5){
					jlblStatus.setText((bakColor==Color.BLACK)?"黑方获胜!!!":"白方获胜!!!");
					return true;
				}			
			
		}
		
		for(int i=0,k=0;i<6;i++){
			//左下至右上方向
			if(find(rbList,x-30*i,y+30*i,bakColor))k++;
			if(find(rbList,x+30*i,y-30*i,bakColor)){
				if(i!=0)k++;
			}
			System.out.println("左下至右上方向k= "+k);	
			if(!find(rbList,x-30*i,y+30*i,bakColor)&&!find(rbList,x+30*i,y-30*i,bakColor))
			        break;//若左下至右上两个方向都走不通则跳出循环				
				if(k==5){
					jlblStatus.setText((bakColor==Color.BLACK)?"黑方获胜!!!":"白方获胜!!!");
					return true;
				}			
			
		}
		
		for(int i=0,k=0;i<6;i++){		
			//水平方向
			if(find(rbList,x-30*i,y+0*i,bakColor))k++;			
			if(find(rbList,x+30*i,y-0*i,bakColor)){
				if(i!=0)k++;
			}	
			System.out.println("水平方向  k= "+k);
			if(!find(rbList,x-30*i,y+0*i,bakColor)&&!find(rbList,x+30*i,y-0*i,bakColor))
			      break;		//若水平的两个方向都走不通则跳出循环
							
				if(k==5){
					jlblStatus.setText((bakColor==Color.BLACK)?"黑方获胜!!!":"白方获胜!!!");
					return true;
				}			
			
		}
		return false;
	}
	
		protected void paintComponent(Graphics g){
			super.paintComponent(g);
			Graphics2D g2 = (Graphics2D)g;
			for(int i=1;i<=16;i++){
			 g2.draw(new Line2D.Double(30+xMove,30*i+yMove,16*30+xMove,30*i+yMove));
			 g2.draw(new Line2D.Double(30*i+xMove,30+yMove,30*i+xMove,16*30+yMove));
			}		
		}
		private class MouseListener extends MouseAdapter{
			public void mouseClicked(MouseEvent e){
				if(chessSide==' '&&whoseTurn!=' '){
					setChessSide(whoseTurn);//set ChessSide in the chessCovas
				}					
				xPosition=e.getX();
				yPosition=e.getY();	
				
				if(xPosition > 30+xMove && xPosition < 16*30+xMove && yPosition>30+yMove && yPosition< 16*30+yMove) {
         	jlblStatus.setText((whoseTurn=='B')?"黑方下棋":"白方下棋");
					whoseTurn=(whoseTurn=='B')?'W':'B';	         
         int X = xPosition/30;
         int Y = yPosition/30;
         
         int centerX ;
         int centerY ;
         //棋子落点的判断
         if(xPosition%30>30/2){
         		centerX=(X+1)*30;
         		if(yPosition%30>30/2)
         			centerY=(Y+1)*30;
         		else
         			centerY=Y*30;
         	 }
         else{
         		centerX=X*30;
         		if(yPosition%30>30/2)
         			centerY=(Y+1)*30;
         		else
         			centerY=Y*30;
         		}         
         RoundButton1 rb=new RoundButton1("");
        
         //roundButtonArray[i]=rb;
          //i++;         
         rbList.add(rb);
          add(rb);          
         //棋子大小         
         rb.setBounds(new Rectangle(centerX-10,centerY-10,20,20));
         if(whoseTurn=='B'){
         		rb.setBackground(Color.BLACK);
         		System.out.println("黑棋  x: "+rb.getX()+"  y: "+rb.getY());
         		boolean flag=isWinner(rbList,rb.getX(),rb.getY(),Color.BLACK);
         		System.out.println(flag);
         		if(flag==true){
         			removeMouseListener(this);//当一方获胜后则不再响应鼠标事件
         		 }         	
         	}
         else{          	   	
         		rb.setBackground(Color.WHITE);
         		System.out.println("白棋  x: "+rb.getX()+"  y: "+rb.getY());
         		boolean flag=isWinner(rbList,rb.getX(),rb.getY(),Color.WHITE);
         		System.out.println(flag); 
         		if(flag==true){
         			removeMouseListener(this);//当一方获胜后则不再响应鼠标事件
         		 }         	         	
				}          
         repaint();         
				}
			}
		}
	private class ResetEvent implements ActionListener{
		public void actionPerformed(ActionEvent e){
			for(int i=0;i<rbList.size();i++){
				remove((RoundButton1)rbList.get(i));
			}			
			rbList.clear();
			repaint();//若不加repaint(),组件不会自动消失,等窗体稍有改动才会消失,为什么?
		addMouseListener(new MouseListener());//以便重新开始后重新响应鼠标事件
		}
	}
 }
}
//用圆形BUTTON实现棋子类
 class RoundButton1 extends JButton {
 	
 		  
   public RoundButton1(){}
    public RoundButton1(String label) {
        super(label);
       
        //下面的语句讲述这个按钮变为一个圆形而不是椭圆形
        Dimension size = getPreferredSize();
        size.width = size.height = Math.max(size.width, size.height);
        setPreferredSize(size);
        //不让JButton画背景而允许我们去画一个圆背景
        setContentAreaFilled(false);

    }
	
    // 画出圆的背景和标签
    protected void paintComponent(Graphics g) {
        if (getModel().isArmed()) {
            g.setColor(Color.lightGray);
        } else {
            g.setColor(getBackground());
        }
        g.fillOval(0, 0, getSize().width-1, getSize().height-1);
        // 在焦点上画出一个标签
        super.paintComponent(g);

    }


    // 画出一个边框
    protected void paintBorder(Graphics g) {
        g.setColor(getForeground());
        g.drawOval(0, 0, getSize().width-1, getSize().height-1);
    }



    // 侦察单击区域
    Shape shape;

    public boolean contains(int x, int y) {
        // 如果按钮改变了尺寸将重新创建一个Shape
        if (shape == null || !shape.getBounds().equals(getBounds())) {
            shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
        }
        return shape.contains(x, y);
    } 
  }   

 

转载于:https://my.oschina.net/flyyourdream/blog/866597

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值