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

📄 game.java

📁 java五子棋人机对战代码
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class Game extends JFrame implements ActionListener{

	public static final int  SIZE = 15;
	public int[][] board;
	private Gobang gobang;
	private JMenu menu1 = new JMenu("游戏");
	private JMenu menu2 = new JMenu("设置");
	private JMenu menu3 = new JMenu("帮助");
	private JMenu menu1_1 = new JMenu("新游戏");
	private JMenu menu2_1 = new JMenu("等级");
	private JMenuItem item1_1_1 = new JMenuItem("玩家先");
	private JMenuItem item1_1_2 = new JMenuItem("电脑先");
	private JMenuItem item1_2 = new JMenuItem("悔棋");
	private JMenuItem item1_3 = new JMenuItem("退出");
	private JRadioButtonMenuItem item2_1_1 = new JRadioButtonMenuItem("低级");
	private JRadioButtonMenuItem item2_1_2 = new JRadioButtonMenuItem("高级");
	private JMenuItem item3_1=new JMenuItem("关于...");
	private JMenuBar bar=new JMenuBar();
	private ButtonGroup group = new ButtonGroup();
	private int level = Gobang.EAZY;
	private int 	thisRow = SIZE,
					thisCol = SIZE;
	public Chessboard panel;
	
	public Game() {
		super("JAVA五子棋");
		board = new int[SIZE][SIZE];
		panel = new Chessboard();
		gobang = new Gobang(this, board, Gobang.HUM_VS_AI, level);		
		item1_1_1.addActionListener(this);
		item1_1_2.addActionListener(this);
		item1_2.addActionListener(this);
		item1_3.addActionListener(this);
		item2_1_1.addActionListener(this);
		item2_1_1.addActionListener(this);
		item3_1.addActionListener(this);
		item2_1_1.setSelected(true);
		group.add(item2_1_1);
		group.add(item2_1_2);
		menu1.add(menu1_1);
		menu1.add(item1_2);
		menu1.add(item1_3);
		menu2.add(menu2_1);
		menu3.add(item3_1);
		menu1_1.add(item1_1_1);
		menu1_1.add(item1_1_2);
		menu2_1.add(item2_1_1);
		menu2_1.add(item2_1_2);
		bar.add(menu1);
		bar.add(menu2);
		bar.add(menu3);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().add(panel);
		this.setJMenuBar(bar);
		this.setSize(535,580);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
    }

	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == item1_1_1)  
			gobang = new Gobang(this, board, Gobang.HUM_VS_AI, level);		
		
		if(e.getSource() == item1_1_2)  
			gobang = new Gobang(this, board, Gobang.AI_VS_HUM, level);	
		
		if(e.getSource() == item1_2)
			gobang.back();
		
		if(e.getSource() == item1_3)
		System.exit(0);
		
		if(e.getSource() == item2_1_1){
			level = Gobang.EAZY;
			gobang.setLevel(level);	
		}
		
		if(e.getSource() == item2_1_2){
			level = Gobang.HAND;
			gobang.setLevel(level);
		}
		
		if(e.getSource() == item3_1)
			JOptionPane.showMessageDialog(this,
					"2005级计算机科学与技术专业邢广琳实训设计",
					"学号:20055171030",
					JOptionPane.INFORMATION_MESSAGE);
	}
	
	public void rep(int r,int c,int[][] b)
	{
		for(int i = 0; i<SIZE; i++)
			for(int j = 0; j<SIZE; j++)
				board[i][j] = b[i][j];
		thisRow = r;
		thisCol = c;
		panel.repaint();
	}
    
    public static void main(String[] args) {
        new Game();
    }
    
	class Chessboard extends JPanel {

		private BufferedImage  chessboardImage = null,
	    						blackPawnImage = null,
	    						whitePawnImage = null;
		
		public static final int	LOCATION_REVISED = 3,
	        						PAWNWIDTE = 35;
		
	
		public Chessboard() {
	    	try {
				chessboardImage = ImageIO.read(new File("image/chessboard.jpg"));
				blackPawnImage = ImageIO.read(new File("image/b.jpg"));
				whitePawnImage = ImageIO.read(new File("image/w.jpg"));
			} catch (IOException e) {
				e.printStackTrace();
			}
	    	this.addMouseListener(new CellSelector());
		}
	
		protected void paintComponent(Graphics g) {
	    	super.paintComponent(g);
	    	g.setColor(Color.RED);
	    	g.drawImage(chessboardImage,0,0,this);
	    	for (int row = 0; row < board.length; row++)
	    		for (int col = 0; col < board[0].length; col++)
	    			switch(board[row][col])	{
	    				case Gobang.BLACK : g.drawImage(	blackPawnImage,
	    													col*PAWNWIDTE+LOCATION_REVISED,
	    													row*PAWNWIDTE+LOCATION_REVISED,
	    													this);break;
	    				case Gobang.WHITE : g.drawImage(	whitePawnImage,
	    													col*PAWNWIDTE+LOCATION_REVISED,
	    													row*PAWNWIDTE+LOCATION_REVISED,
	    													this);break;
	    			}
	    	if(thisRow != SIZE && thisCol != SIZE)
	    	{
	    		g.drawRect(	thisCol*PAWNWIDTE+LOCATION_REVISED-1,
	    					thisRow*PAWNWIDTE+LOCATION_REVISED-1, 
	    					PAWNWIDTE-1, 
	    					PAWNWIDTE-1);
	    	}
		}
		
		
		class CellSelector extends MouseAdapter {
			public void mouseClicked(MouseEvent e) {
				int row = (e.getPoint().y-LOCATION_REVISED)/PAWNWIDTE;
				int col = (e.getPoint().x-LOCATION_REVISED)/PAWNWIDTE;
				gobang.oneRound(row,col);
				
			}
		}
	
	}
	
} 

⌨️ 快捷键说明

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