JAvaヒューマンマシン対戦五子棋



import java.awt.*;
import java.awt.event.*;
import java.net.URL;

import javax.swing.*;

public class GobangGame {
	public static void main(String[] args) {
		GameF game = new GameF();
		game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		game.show();
	}
}

class GameF extends JFrame {
	public GameF() {
		Container contentPane = getContentPane();
		final Panel panel = new Panel();
		panel.setBackground(new Color(255, 182, 147));
		contentPane.setBackground(new Color(255, 182, 147));
		contentPane.add(panel);
		setSize(560, 560);
		setTitle("        1.0");
		setResizable(false);
		panel.setCursor(new Cursor(Cursor.HAND_CURSOR));
		JMenuBar menuBar = new JMenuBar();
		JMenu menu = new JMenu("  ");
		JMenuItem menuStart = new JMenuItem("    ");
		menuStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				panel.ResetGame();
				panel.repaint();
			}
		});
		JMenuItem menuExit = new JMenuItem("  ");
		menuExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		menuBar.add(menu);
		menu.add(menuStart);
		menu.add(menuExit);
		this.setJMenuBar(menuBar);
	}
}

class Panel extends JPanel {
	private URL blackImgURL = GobangGame.class.getResource("black.gif");

	private ImageIcon black = new ImageIcon(blackImgURL);

	private URL whiteImgURL = GobangGame.class.getResource("white.gif");

	private ImageIcon white = new ImageIcon(whiteImgURL);

	private URL currentImgURL = GobangGame.class.getResource("current.gif");

	private ImageIcon current = new ImageIcon(currentImgURL);

	private int i, j, k, m, n, icount;

	private int[][] board = new int[16][16];

	private boolean[][][] ptable = new boolean[16][16][672];

	private boolean[][][] ctable = new boolean[16][16][672];

	private int[][] cgrades = new int[16][16];

	private int[][] pgrades = new int[16][16];

	private int cgrade, pgrade;

	private int[][] win = new int[2][672];

	private int oldx, oldy;

	private int bout = 1;

	private int pcount, ccount;

	private boolean player, computer, over, pwin, cwin, tie, start;

	private int mat, nat, mde, nde;

	public Panel() {
		addMouseListener(new Xiazi());
		this.ResetGame();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (int i = 0; i < 16; i++)
			for (int j = 0; j < 16; j++) {
				g.drawLine(50, 50 + j * 30, 500, 50 + j * 30);
			}
		for (int i = 0; i < 16; i++)
			for (int j = 0; j < 16; j++) {
				g.drawLine(50 + j * 30, 50, 50 + j * 30, 500);
			}
		for (int i = 0; i < 16; i++) {
			String number = Integer.toString(i);
			g.drawString(number, 46 + 30 * i, 45);
		}
		for (int i = 1; i < 16; i++) {
			String number = Integer.toString(i);
			g.drawString(number, 33, 53 + 30 * i);
		}
		updatePaint(g);
	}

	class Xiazi extends MouseAdapter {
		public void mouseClicked(MouseEvent e) {
			if (!over) {
				oldx = e.getX();
				oldy = e.getY();
				mouseClick();
				repaint();
			}
		}
	}

	//      
	public void ResetGame() {
		//      
		for (i = 0; i < 16; i++)
			for (j = 0; j < 16; j++) {
				this.pgrades[i][j] = 0;
				this.cgrades[i][j] = 0;
				this.board[i][j] = 2;
			}
		//                
		//  
		for (i = 0; i < 16; i++)
			for (j = 0; j < 12; j++) {
				for (k = 0; k < 5; k++) {
					this.ptable[j + k][i][icount] = true;
					this.ctable[j + k][i][icount] = true;
				}
				icount++;
			}
		//  
		for (i = 0; i < 16; i++)
			for (j = 0; j < 12; j++) {
				for (k = 0; k < 5; k++) {
					this.ptable[i][j + k][icount] = true;
					this.ctable[i][j + k][icount] = true;
				}
				icount++;
			}
		//   
		for (i = 0; i < 12; i++)
			for (j = 0; j < 12; j++) {
				for (k = 0; k < 5; k++) {
					this.ptable[j + k][i + k][icount] = true;
					this.ctable[j + k][i + k][icount] = true;
				}
				icount++;
			}
		//   
		for (i = 0; i < 12; i++)
			for (j = 15; j >= 4; j--) {
				for (k = 0; k < 5; k++) {
					this.ptable[j - k][i + k][icount] = true;
					this.ctable[j - k][i + k][icount] = true;
				}
				icount++;
			}
		for (i = 0; i <= 1; i++)
			//                   
			for (j = 0; j < 672; j++)
				this.win[i][j] = 0;
		this.player = true;
		this.icount = 0;
		this.ccount = 0;
		this.pcount = 0;
		this.start = true;
		this.over = false;
		this.pwin = false;
		this.cwin = false;
		this.tie = false;
		this.bout = 1;
	}

	public void ComTurn() { //     (  )     
		for (i = 0; i <= 15; i++)
			//           
			for (j = 0; j <= 15; j++) {
				this.pgrades[i][j] = 0; //             
				if (this.board[i][j] == 2) //            
					for (k = 0; k < 672; k++)
						//                       ,             
						if (this.ptable[i][j][k]) {
							switch (this.win[0][k]) {
							case 1: //    
								this.pgrades[i][j] += 5;
								break;
							case 2: //    
								this.pgrades[i][j] += 50;
								break;
							case 3: //    
								this.pgrades[i][j] += 180;
								break;
							case 4: //    
								this.pgrades[i][j] += 400;
								break;
							}
						}
				this.cgrades[i][j] = 0;//              
				if (this.board[i][j] == 2) //            
					for (k = 0; k < 672; k++)
						//                       ,             
						if (this.ctable[i][j][k]) {
							switch (this.win[1][k]) {
							case 1: //    
								this.cgrades[i][j] += 5;
								break;
							case 2: //    
								this.cgrades[i][j] += 52;
								break;
							case 3: //    
								this.cgrades[i][j] += 100;
								break;
							case 4: //    
								this.cgrades[i][j] += 400;
								break;
							}
						}
			}
		if (this.start) { //          
			if (this.board[4][4] == 2) {
				m = 4;
				n = 4;
			} else {
				m = 5;
				n = 5;
			}
			this.start = false;
		} else {
			for (i = 0; i < 16; i++)
				for (j = 0; j < 16; j++)
					if (this.board[i][j] == 2) { //                      ,          
						if (this.cgrades[i][j] >= this.cgrade) {
							this.cgrade = this.cgrades[i][j];
							this.mat = i;
							this.nat = j;
						}
						if (this.pgrades[i][j] >= this.pgrade) {
							this.pgrade = this.pgrades[i][j];
							this.mde = i;
							this.nde = j;
						}
					}
			if (this.cgrade >= this.pgrade) { //                          ,                  ,    
				m = mat;
				n = nat;
			} else {
				m = mde;
				n = nde;
			}
		}
		this.cgrade = 0;
		this.pgrade = 0;
		this.board[m][n] = 1; //       
		ccount++;
		if ((ccount == 50) && (pcount == 50)) //     
		{
			this.tie = true;
			this.over = true;
		}
		for (i = 0; i < 672; i++) {
			if (this.ctable[m][n][i] && this.win[1][i] != 7)
				this.win[1][i]++; //                    
			if (this.ptable[m][n][i]) {
				this.ptable[m][n][i] = false;
				this.win[0][i] = 7;
			}
		}
		this.player = true; //     
		this.computer = false; //       
	}

	public void mouseClick() {
		if (!this.over)
			if (this.player) {
				if (this.oldx < 520 && this.oldy < 520) {
					int m1 = m, n1 = n;
					m = (oldx - 33) / 30;
					n = (oldy - 33) / 30;
					if (this.board[m][n] == 2) {
						this.bout++;
						this.board[m][n] = 0;
						pcount++;
						if ((ccount == 50) && (pcount == 50)) {
							this.tie = true;
							this.over = true;
						}
						for (i = 0; i < 672; i++) {
							if (this.ptable[m][n][i] && this.win[0][i] != 7)
								this.win[0][i]++; //                    
							if (this.ctable[m][n][i]) {
								this.ctable[m][n][i] = false;
								this.win[1][i] = 7;
							}
						}
						this.player = false;
						this.computer = true;
					} else {
						m = m1;
						n = n1;
					}
				}
			}
	}

	public void updatePaint(Graphics g) {
		if (!this.over) { //         
			if (this.computer)
				this.ComTurn(); //        
			//              ,    
			for (i = 0; i <= 1; i++)
				for (j = 0; j < 672; j++) {
					if (this.win[i][j] == 5)
						if (i == 0) { //   
							this.pwin = true;
							this.over = true; //     
							break;
						} else {
							this.cwin = true; //    
							this.over = true;
							break;
						}
					if (this.over) //             
						break;
				}
			g.setFont(new Font("    ", 0, 20));
			g.setColor(Color.RED);
			//           
			for (i = 0; i <= 15; i++)
				for (j = 0; j <= 15; j++) { //   board    0,        
					if (this.board[i][j] == 0) {
						g.drawImage(black.getImage(), i * 30 + 31, j * 30 + 31,
								black.getImage().getWidth(
										black.getImageObserver()) - 3, black
										.getImage().getHeight(
												black.getImageObserver()) - 3,
								black.getImageObserver());
					}
					//   board    1,        
					if (this.board[i][j] == 1) {
						g.drawImage(white.getImage(), i * 30 + 31, j * 30 + 31,
								white.getImage().getWidth(
										white.getImageObserver()) - 3, white
										.getImage().getHeight(
												white.getImageObserver()) - 3,
								white.getImageObserver());
					}
				}
			//     (  )     ,    
			if (this.board[m][n] != 2)
				g
						.drawImage(current.getImage(), m * 30 + 31,
								n * 30 + 31, current.getImage().getWidth(
										current.getImageObserver()) - 4,
								current.getImage().getHeight(
										current.getImageObserver()) - 4,
								current.getImageObserver());
			//       
			//   
			if (this.pwin)
				g.drawString("     !           ..", 20, 200);
			//    
			if (this.cwin)
				g.drawString("   ,   !           ..", 84, 190);
			//   
			if (this.tie)
				g.drawString("    !           ..", 80, 200);
			g.dispose();
		}
	}
}