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

📄 five.java

📁 一款五子棋游戏
💻 JAVA
字号:
package five;
import five.btt;
import five.dialog;
import five.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.MouseListener;

public class Five extends JFrame implements ActionListener, MouseListener{
	
	dialog dia = new dialog(this);
	
	Image Pic,Photo_w,Photo_b; //标题图标
	Image Pic_ChessBoard;	//棋盘背景
	Font f = new Font("宋体",Font.BOLD,12);//设置字体
	String u_w,u_b;
	
	
	
	Boolean who = false; //黑白子轮流放下
	Boolean who_win = true;
	int px,py;
	static int w_go,b_go; //黑白子各自的步数
	//////////////////////////////////////
	//黑白棋数组
	Chess chw = new Chess("images/bf.gif");
	Chess chb = new Chess("images/hf.gif");
	Chess[] ch_w = new Chess[150];
	Chess[] ch_b = new Chess[150];
	int[][] station = new int[16][16];
	/////////////////////////////////////
	///////Stack///////////////////////
	StackLi stack = new StackLi();
	//////////////////////////////////
	btt start= new btt("images/start1.gif","images/start3.gif","images/start2.gif");//开始
	btt end = new btt("images/end1.gif","images/end3.gif","images/end2.gif");     //结束
	btt surrender_w = new btt("images/surrender1.gif","images/surrender2.gif","images/surrender1.gif");//认输
	btt surrender_b = new btt("images/surrender1.gif","images/surrender2.gif","images/surrender1.gif");
	btt back_w = new btt("images/back1.gif","images/back2.gif","images/back1.gif");//认输
	btt back_b = new btt("images/back1.gif","images/back2.gif","images/back1.gif");

	Five(){  //构造函数
		
		u_w = dia.use_w;
		u_b = dia.use_b;
		
		w_go = 0;
		b_go = 0;
		//初始化station
		for(int t=0;t<16;t++)
		{
			for(int t1=0;t1<16;t1++)
			{
				station[t][t1]=0;	
			}	
		}
		try{  //获得图片信息
			Pic = Toolkit.getDefaultToolkit().getImage("images/Icon.gif");
			Pic_ChessBoard = Toolkit.getDefaultToolkit().getImage("images/ChessBoard.gif");
			Photo_w = Toolkit.getDefaultToolkit().getImage("images/w.gif");
			Photo_b = Toolkit.getDefaultToolkit().getImage("images/b.gif");
			}catch(Exception e){System.out.printf(e.getMessage());}

		//设置按钮属性
		start.setBounds(177,433,100,30);
		start.setFocusable(false);
		end.setBounds(318,433,100,30);
		end.setFocusable(false);

		surrender_w.setBounds(12,347,88,31);
		surrender_w.setFocusable(false);

		surrender_b.setBounds(495,346,88,31);
		surrender_b.setFocusable(false);
		
		back_w.setBounds(12,395,88,31);
		back_w.setFocusable(false);
		
		back_b.setBounds(495,395,88,31);
		back_b.setFocusable(false);
		
		for(int w=0;w<150;w++) //创建黑白实例
		{
		ch_w[w] = new Chess("images/write.gif");
		ch_w[w].X=20;
		ch_w[w].Y=20;
		ch_w[w].setBounds(-50,-50,20,20);
		
		ch_b[w] = new Chess("images/black.gif");
		ch_b[w].X=20;
		ch_b[w].Y=20;
		ch_b[w].setBounds(-50,-50,20,20);
		}
		chw.setBounds(36,170,35,155);
		chw.setVisible(false);
		chw.X=35;
		chw.Y=155;
		chb.setBounds(520,173,35,155);
		chb.setVisible(false);
		chb.X=35;
		chb.Y=155;
		//添加按钮
		Container c = this.getContentPane();
		c.setLayout(null);
		c.add(start);	c.add(end);
		c.add(surrender_w);	c.add(surrender_b);
		c.add(chw); c.add(chb);
		c.add(back_w);c.add(back_b);
		for(int s=0;s<150;s++)//添加到JFrame中
		{
			c.add(ch_w[s]);
			c.add(ch_b[s]);
		}
		//窗体设置
		this.setSize(600,500);
		//获得屏幕的宽和高
			this.setLocation(
			(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width-600)/2
			,(java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-500)/2);
			//获得屏幕的宽和高
		this.setResizable(false);
		this.setTitle("五子连珠");
		start.setEnabled(false);
		this.setIconImage(Pic);
		this.setVisible(true);
		
		start.addActionListener(this);
		end.addActionListener(this);
		surrender_w.addActionListener(this);
		surrender_b.addActionListener(this);
		back_w.addActionListener(this);
		back_b.addActionListener(this);
		this.addMouseListener(this);
		}
		 
		//画图和绘字
		public void paint(Graphics g){
		g.drawImage(Pic_ChessBoard,0,20,600,500,this);
		g.drawImage(Photo_w,20,120,62,62,this);
		g.drawImage(Photo_b,510,120,62,62,this);
		g.setFont(f);
		g.setColor(Color.BLACK);
		g.drawString("白方:"+u_w,15,100);
		g.drawString("黑方:"+u_b,500,100);
		
		for(int i=0,tmp=58;i<=16;i++,tmp=tmp+24)
		g.drawLine(108,tmp,492,tmp);
		for(int i=0,tmp=108;i<=16;i++,tmp=tmp+24)
		g.drawLine(tmp,58,tmp,441);
			}
	public static void main(String argv[]){  //MAIN函数
			new Five();
		}

	public void mw_station(int x,int y)
	{//得到横向的9个数字
		int[] sta= new int[9];//横向数组
		int[] sta_z = new int[9];//纵向数组
		int[] sta_zc = new int[16];//正侧数组
		int[] sta_fc = new int[16];//反侧数组
		int sum=0,temp;
		///////////////////////////////横向//////////////////////////
		who_win=true;
		if(who_win==true)
		{
		if((y-4)<0)
			temp=0;
		else
			temp=y-4;
			
			for(int r=0;r<9;r++)
			{
				sta[r]=station[x][temp];
				temp++;
				if(temp==16)
					break;
			}
			///////////////判断
			int i=0,j=1;
			do
			{
				if(sta[i]==sta[i+1]&&sta[i]!=0&&sta[i+1]!=0)
				{ i++;
				  j++;
				  if(j==5)
				 {		
					System.out.printf("win");
					who_win=false;					
					chw.setVisible(false);
					chb.setVisible(false);
					if(sta[i]==1)
					new agin_dialog(this,1);
					else
					new agin_dialog(this,2);
					new Five();
					this.dispose();
				 	 break;
				 }
				}
				else
				{
					j=1;
					i++;
				}

			}while(i<8);
		}
			///////////////////////////////横向//////////////////////////
			
			//////////////////////////////纵向///////////////////////////
			if(who_win==true)
			{
			if((x-4)<0)
			temp=0;
			else
			temp=x-4;
			
			for(int r=0;r<9;r++)
			{
				sta_z[r]=station[temp][y];
				temp++;
				if(temp==16)
					break;
			}
			///////////////判断
			int m=0,n=1;
			do
			{
				if(sta_z[m]==sta_z[m+1]&&sta_z[m]!=0&&sta_z[m+1]!=0)
				{ m++;
				  n++;
				  if(n==5)
				 {		
					System.out.printf("win");
					who_win=false;
					chw.setVisible(false);
					chb.setVisible(false);
					if(sta_z[m]==1)
					new agin_dialog(this,1);
					else
					new agin_dialog(this,2);
					new Five();
					this.dispose();
				 	 break;
				 }
				}
				else
				{
					n=1;
					m++;
				}

			}while(m<8);
		}
			//////////////////////////////纵向///////////////////////////
			
			//////////////////////////////正侧///////////////////////////
			if(who_win==true)
			{
			temp=x+y;
			if(temp<16)
			{
			for(int r=0;r<=temp;r++)
			{
				sta_zc[r]=station[temp-r][r];	
			}
			}
			else
			{
				temp=-(15-temp);
				for(int r=0;r<16;r++)
			{
				sta_zc[r]=station[15-r][temp];
					temp++;
				if(temp==16)
					break;
			}
			}
			///////////////判断
			int p=0,q=1;
			do
			{
				if(sta_zc[p]==sta_zc[p+1] && sta_zc[p]!=0 && sta_zc[p+1]!=0)
				{ p++;
				  q++;
				  if(q==5)
				 {		
					System.out.printf("win");
					who_win=false;
					chw.setVisible(false);
					chb.setVisible(false);
					if(sta_zc[p]==1)
					new agin_dialog(this,1);
					else
					new agin_dialog(this,2);
					new Five();
					this.dispose();
				 	 break;
				 }
				}
				else
				{
					q=1;
					p++;
				}

			}while(p<15);
		}
			//////////////////////////////正侧///////////////////////////
			
			//////////////////////////////反侧///////////////////////////
			if(who_win==true)
			{
			temp=x-y;
			if(temp>=0)
			{
			for(int r=0;r<16;r++)
			{
				sta_fc[r]=station[temp][r];
				temp++;
				if(temp>=16)
					break;	
			}
			}
			else if(temp<0)
			{
				temp=-temp;
				for(int r=0;r<16;r++)
			{
				sta_fc[r]=station[r][temp];
				temp++;
				if((temp)>=16)
					break;	
			}	
			}
			///////////////判断
			int x1=0,y1=1;
			do
			{
				if(sta_fc[x1]==sta_fc[x1+1] && sta_fc[x1]!=0 && sta_fc[x1+1]!=0)
				{ x1++;
				  y1++;
				  if(y1==5)
				 {		
					System.out.printf("win");
					who_win=false;
					chw.setVisible(false);
					chb.setVisible(false);
					if(sta_fc[x1]==1)
					new agin_dialog(this,1);
					else
					new agin_dialog(this,2);
					new Five();
					this.dispose();
				 	 break;
				 }
				}
				else
				{
					y1=1;
					x1++;
				}

			}while(x1<15);
		}
			//////////////////////////////反侧///////////////////////////
			//////////////////判断输赢//////////////////////////////////
					
			//////////////////判断输赢//////////////////////////////////			
	}



		public void Setbounds(int x,int y)
		{//A_X_y:
			if(x>=109 && x<=492 && y>=58 && y<=451)
			{
				for(int A_X=0,A_X_y=58,A_X_y1=39;A_X<16;A_X++)
				{
				if (x>=109 && x<=492 && y>=A_X_y && y<=A_X_y+24)//判断哪个横行
				{
					for(int A=0,AX=109;A<16;A++)  //横行
					{
						if(x>=AX && x<=AX+22 && y<=A_X_y+24)
						{
							if(who==false)
							{
								ch_w[w_go+1].setBounds(AX-1,A_X_y1-2,20,20);
								w_go=w_go+1;
								station[A_X][A]=1;//A_X:x坐标  A:y坐标
								mw_station(A_X,A);
								stack.push(A_X,A,1,w_go);//PUSH
								who = true;	
								chb.setVisible(true);
								chw.setVisible(false);
							}
							else
							{
								ch_b[b_go+1].setBounds(AX-1,A_X_y1-2,20,20);
								b_go=b_go+1;
								station[A_X][A]=2;
								mw_station(A_X,A);
								stack.push(A_X,A,2,b_go);//PUSH
								who = false;
								chb.setVisible(false);
								chw.setVisible(true);
							}		
						}
						AX=AX+24;
					}
				}
				A_X_y1=A_X_y1+24;
				A_X_y=A_X_y+24;
				}
			}			
		}
	/**
	 * Method actionPerformed
	 *
	 *
	 * @param e
	 *
	 */
	public void actionPerformed(ActionEvent e) {
		// TODO: Add your code here
		if (e.getSource().equals(start)){
			new Five();
			this.dispose();
			
		}
			else if(e.getSource().equals(end))
				System.exit(0);
				else if(e.getSource().equals(surrender_w))
				{
					new agin_dialog(this,2);
					new Five();
					this.dispose();
				}
				else if(e.getSource().equals(surrender_b))
				{
					new agin_dialog(this,1);
					new Five();
					this.dispose();
				}
				else if(e.getSource().equals(back_w))
				{System.out.println("back_W");
					//白子悔棋操作
					stack.pop();
					station[stack.back_x][stack.back_y] = 0;
					if(stack.back_value==1)
					{
						ch_w[stack.back_count].setBounds(-50,-50,20,20);
						w_go = stack.back_count;
						who = false;
						chb.setVisible(false);
						chw.setVisible(true);						
					}
					else if(stack.back_value==2)
					{
						ch_b[stack.back_count].setBounds(-50,-50,20,20);
						b_go = stack.back_count;
						who = true;	
						chb.setVisible(true);
						chw.setVisible(false);	
					}
					
				}
				else if(e.getSource().equals(back_b))
				{System.out.println("back_B");
				
					stack.pop();
					station[stack.back_x][stack.back_y] = 0;
					if(stack.back_value==1)
					{
						ch_w[stack.back_count].setBounds(-50,-50,20,20);
						w_go = stack.back_count;
						who = false;
						chb.setVisible(false);
						chw.setVisible(true);						
					}
					else if(stack.back_value==2)
					{
						ch_b[stack.back_count].setBounds(-50,-50,20,20);
						b_go = stack.back_count;
						who = true;	
						chb.setVisible(true);
						chw.setVisible(false);	
					}
				}
	}

	/**
	 * Method mouseClicked
	 *
	 *
	 * @param e
	 *
	 */
	public void mouseClicked(MouseEvent e) {
		// TODO: Add your code here
		px = (int)e.getPoint().getX();
		py = (int)e.getPoint().getY();
		this.Setbounds(px,py);
	}

	/**
	 * Method mousePressed
	 *
	 *
	 * @param e
	 *
	 */
	public void mousePressed(MouseEvent e) {
		// TODO: Add your code here
		
	}

	/**
	 * Method mouseReleased
	 *
	 *
	 * @param e
	 *
	 */
	public void mouseReleased(MouseEvent e) {
		// TODO: Add your code here

	}

	/**
	 * Method mouseEntered
	 *
	 *
	 * @param e
	 *
	 */
	public void mouseEntered(MouseEvent e) {
		// TODO: Add your code here
	}

	/**
	 * Method mouseExited
	 *
	 *
	 * @param e
	 *
	 */
	public void mouseExited(MouseEvent e) {
		// TODO: Add your code here
	}
	}
	

⌨️ 快捷键说明

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