Java 五子棋源程序 实现了基本的功能

还有一个要注意的地方,这个得在jdk1.7的环境下编译运行,还得有jmf包
//This is a Gobang program designed by 焦仁瑜 who is a Chinese.Yet,it is a easy work to many black-horse,
//however I am proud of me! It costs me a lot of time to do it. During my design of this work I was also 
//learned many things about Java, and I will continue learning Java until I could undercontroll it! Thank you!


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
 
import java.io.File; 
import javax.media.Manager; 
import javax.media.MediaLocator; 
import javax.media.Player; 


class BlackChess extends Canvas //黑色棋子
{
	public BlackChess(){}

	public void paint(Graphics g){
		g.setColor(Color.black);
		g.fillOval(0,0,24,24);
	}
}

class WhiteChess extends Canvas //白色棋子
{
	public WhiteChess(){}

	public void paint(Graphics g){
		g.setColor(Color.white);
		g.fillOval(0,0,24,24);
	}
}

class itemListener implements ItemListener{//建立一个监听器类
	Music music=null;						
	String para=null;
	static boolean record;
	public void itemStateChanged(ItemEvent e){
			
			if (e.getStateChange() == ItemEvent.SELECTED) {//当选项改变时,会触发两次,
				JComboBox jcb = (JComboBox) e.getSource(); //一是取消旧的选择,二是添加新的选择
			
				int k=jcb.getSelectedIndex();//当前控件的选项索引
				para=new String(e.getItem().toString());//获得所选择的项的字符串
				
				switch(para)
				{
					case "MUSIC-1":
					case "MUSIC-2":
											if(music!=null)
											{
														music.stop();
														music=null;
											}
											switch(k)
											{
												case 1:
														music=new Music("E:\\acm\\java\\music.mp3");
														break;
												case 2:
														music=new Music("E:\\acm\\java\\music2.mp3");
														break;
											}
											break;
					case "red":
					case "blue":
					case "default":
											
											
											switch(k)
											{
												case 1:
															Windows.setBground(new Color(159,71,38));
															break;
												case 2:
															Windows.setBground(Color.red);
															break;
												case 3:
															Windows.setBground(Color.blue);
															break;
											}
											break;
									
					
				}
			} 
    }
}

class Music//构造音乐类
{
		Player player=null;
		public Music(String str) { 
			 
			File mufile = new File(str); 
			try { 
				if (player == null) { 
					if (mufile.exists()) { 
						MediaLocator locator = new MediaLocator("file:" 
						+ mufile.getAbsolutePath()); 
						player = Manager.createRealizedPlayer(locator); 
						player.prefetch(); 
					} 
				} 
					player.start();// 开始播放 
				
				} catch (Exception e) { 
					e.getStackTrace(); 
			    } 
		} 

		public void stop(){
			player.stop();
			player=null;
		}
}

class Background extends JPanel
{//棋盘大小为25*25
	
	public Background(int x, int y, int width, int height, Color color){
		setBounds(x,y,width,height);
		setBackground(color);
	}

	public void paint(Graphics g){
			super.paint(g);//这个必须要用,不然背景颜色就画不出来,其实我也不知道这其中的缘由,还有待继续深入研究
			g.setColor(Color.green);
				
			for(int i=0;i<=24;i++){//绘制棋盘
				g.drawLine(30,30+30*i,750,30+30*i);
				g.drawLine(30+30*i,30,30+30*i,750);
			}

			g.fillOval(30+30*12-5,30+30*12-5,10,10);//绘制棋盘里的五个标志点
			g.fillOval(30+30*6-5,30+30*6-5,10,10);
			g.fillOval(30+30*6-5,30+30*18-5,10,10);
			g.fillOval(30+30*18-5,30+30*6-5,10,10);
			g.fillOval(30+30*18-5,30+30*18-5,10,10);
	}
}

class GobangMenu extends JPanel//棋盘右面的设置菜单,可以选择音乐,棋盘背景颜色
{
	JTextField current;//提示当前该哪一方走和标志哪一方输赢1,2,3,4
	public GobangMenu(int x, int y, int width, int height, Color color){
		
		setLayout(null);
		setBounds(780,0,220,780);
		setBackground(Color.YELLOW);
		
		JComboBox music=new JComboBox();
		music.addItem("背景音乐");
		music.addItem("MUSIC-1");
		music.addItem("MUSIC-2");
		music.setBounds(10,30,150,30);
		music.addItemListener(new itemListener());
		add(music);
		
		JComboBox bgcolor=new JComboBox();
		bgcolor.addItem("背景颜色");
		bgcolor.addItem("default");
		bgcolor.addItem("red");
		bgcolor.addItem("blue");
		bgcolor.setBounds(10,70,150,30);
		bgcolor.addItemListener(new itemListener());
		add(bgcolor);

		current=new JTextField("黑方先下");
		current.setBounds(10,110,150,30);
		current.setEditable(false);
		add(current);
		
		JRadioButton type1=new JRadioButton();
		type1.setBounds(10,150,20,20);
		type1.setSelected(true);
		add(type1);

		JRadioButton type2=new JRadioButton();
		type2.setBounds(10,180,20,20);
		add(type2);

		ButtonGroup group=new ButtonGroup();
		group.add(type1);
		group.add(type2);

		JLabel label1=new JLabel("人-人");
		JLabel label2=new JLabel("人-机");
		label1.setBounds(30,150,100,20);
		label2.setBounds(30,180,100,20);
		add(label1);
		add(label2);
	}
    
	
	public void setText(int a[][],int record){
		switch(record){
			case 0:
					current.setText("该黑方下");
					break;
			case 1:
					current.setText("该白方下");
					break;

			case 3:
					current.setText("黑方胜");
					JOptionPane.showMessageDialog(this,"Black one win!");
					break;
			case 4:
					current.setText("白方胜");
					JOptionPane.showMessageDialog(this,"White one win!");
					break;
		}
	
	}

	public void paint(Graphics g){
			super.paint(g);
	}
}

class Windows extends JPanel implements MouseListener,ActionListener//创建一个面板,存放先前两个面板
{

	static Background panel1;
	static GobangMenu panel2;
	JButton reset,exit;
   
	int a[][];//记录当前格的棋子
	int current=0;//为零择黑方下,1则白方下
	int i,j;
	static int begin;//标记游戏是否已经开始,0代表没有开始,1代表开始

	public Windows(){
		setBounds(0,0,1000,800);
		a= new int[26][26];
		init(a);//把数组a初始化为零,标记该位置没有棋子

		panel1=new Background(0,0,780,780,new Color(159,71,38));
		panel2=new GobangMenu(780,0,220,780,Color.black);
		panel1.addMouseListener(this);

		reset=new JButton("Play Again");
		reset.setBounds(10,220,150,30);
		reset.addActionListener(this);
		panel2.add(reset);

		exit=new JButton("Exit");
		exit.setBounds(10,260,150,30);
		exit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				System.exit(0);
			}	
		});
		panel2.add(exit);

		add(panel1);
		add(panel2);
	}
	
	public static void setBground(Color color){
		if(begin==0)
			panel1.setBackground(color);
	}

	public void paintComponent(Graphics g){
		panel1.repaint();
	}
	
	public void actionPerformed(ActionEvent e){
		begin=0;
		init(a);
		panel1.removeAll();
		current=0;
		panel2.setText(a,current);
	}

	public void init(int a[][]){
		
		for(i=0;i<26;i++)
			for(j=0;j<26;j++)
				a[i][j]=0;
	}

	public void mouseClicked(MouseEvent e){//鼠标单击事件
		int	x=e.getX();
		int	y=e.getY();
		begin=1;
		if(current!=3&¤t!=4){
			if(x>15&&y>15&&x<765&&y<765&&a[(x+15)/30][(y+15)/30]==0)
			{
				if(current==0){
					BlackChess black=new BlackChess();
					panel1.add(black);
					black.setBounds(30*((x+15)/30)-12,30*((y+15)/30)-12,24,24);
					a[(x+15)/30][(y+15)/30]=1;
					current=1;
				}
				else{
					WhiteChess white=new WhiteChess();
					panel1.add(white);
					white.setBounds(30*((x+15)/30)-12,30*((y+15)/30)-12,24,24);
					current=0;
					a[(x+15)/30][(y+15)/30]=2;
				}
				current=judge(x,y,a);
				panel2.setText(a,current);
			}
		}
		else{
				panel2.setText(a,current);
			}
	}

	public void mousePressed(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mouseReleased(MouseEvent e){}   

	public int judge(int x, int y, int a[][]){//判断输赢
		int i,j;
		int x1,y1;
		int count=0;

		x1=(x+15)/30;
		y1=(y+15)/30;
		

		for(i=x1;a[i][y1]==a[x1][y1]&&i<=26;i++)
			count++;
		for(i=x1-1;a[i][y1]==a[x1][y1]&&i>0;i--)
			count++;
		if(count>=5)
			return (a[x1][y1]+2);
		
		count=0;
		for(i=y1;a[x1][i]==a[x1][y1]&&i<=26;i++)
			count++;
		for(i=y1-1;a[x1][i]==a[x1][y1]&&i>0;i--)
			count++;
		if(count>=5)
			return (a[x1][y1]+2);

		count=0;
		for(i=x1,j=y1;a[i][j]==a[x1][y1]&&i<=26&&j<=26;i++,j++)
			count++;
		for(i=x1-1,j=y1-1;a[i][j]==a[x1][y1]&&i>0&&j>0;i--,j--)
			count++;
		if(count>=5)
			return (a[x1][y1]+2);

		count=0;
		for(i=x1,j=y1;a[i][j]==a[x1][y1]&&i<=26&&j>0;i++,j--)
			count++;
		for(i=x1-1,j=y1+1;a[i][j]==a[x1][y1]&&i>0&&j<=26;i--,j++)
			count++;
		if(count>=5)
			return (a[x1][y1]+2);

		return 2-a[x1][y1];
	}
}

public class  Gobang extends JFrame//还有一个问题没有解决,就是窗口一缩小,原来画的棋盘和按钮都不见啦!
{
	public Gobang(){
		setSize(1000,800);
		setVisible(true);
		setLayout(null);
		
		Windows panel=new Windows();
		
		add(panel);
		panel.repaint();
		
		setDefaultLookAndFeelDecorated(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) 
	{
		Gobang gobang=new Gobang();
	}
}

 
java五子棋 五子棋 java 五子棋代码直接和电脑下对战 Public class FiveChessAI { public int data_a[][] = new int[5][3];// 用于储存进攻值 public int data_d[][] = new int[5][3];// 用于储存防守值 FiveChessAI() { // 进攻值的初始化 data_a[1][1] = 2; data_a[1][2] = 3; data_a[2][1] = 10; data_a[2][2] = 110; data_a[3][1] = 2500; data_a[3][2] = 3000; data_a[4][1] = 99999; data_a[4][2] = 99999; // 防守值的初始化 data_d[1][1] = 1; data_d[1][2] = 2; data_d[2][1] = 1; data_d[2][2] = 100; data_d[3][1] = 100; data_d[3][2] = 500; data_d[4][1] = 20000; data_d[4][2] = 50000; } public FiveChessMap g1 = new FiveChessMap(); public int x, y; void find()// 查找最大值 { int max = 0; for (int i = 0; i < 15; ++i) { for (int j = 0; j < 15; ++j) { if (max < g1.data[i][j]) { max = g1.data[i][j]; } } } for (int i = 0; i < 15; ++i) { for (int j = 0; j < 15; ++j) { if (max == g1.data[i][j]) { x = i; y = j; return; } } } } int getx()// 返回x坐标值 { return x; } int gety()// 返回y坐标值 { return y; } boolean judge_result(int x, int y, int who, FiveChessMap gm)// 判断胜负 { int m, n, i, lianzi = 0; for (m = -1; m <= 1; m++) for (n = -1; n <= 1; n++) { if (m != 0 || n != 0) { for (i = 1; i <= 4; i++) { if (x + i * m >= 0 && x + i * m < 15 && y + i * n >= 0 && y + i * n < 15 && gm.data[x + i * m][y + i * n] == who) { lianzi++; } else { break; } } for (i = -1; i >= -4; i--) { if (x + i * m >= 0 && x + i * m < 15 && y + i * n >= 0 && y + i * n < 15 && gm.data[x + i * m][y + i * n] == who) { lianzi++; } else { break; } } if (lianzi >= 4) { return true; } else { lianzi = 0; } } } return false; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值