---
title: "简单的五子棋"
author: "hsm.java"
date: "2011-04-25T22:46:28Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-04-25T23:12:04Z"
id: 1017583
url: "https://hsm-java.iteye.com/blog/1017583"
---

<span style="color:darkred">五子棋的开发</span>   

  一.五子棋开发的难点
     1.算法    
        具体是五子棋排放，判断输赢的方法。       
     2.代码结构复杂度提高
	
  二.技术点:
    1.JFrame, Graphics, 按钮
     2.事件
     3.重绘
     4.算法
   
     其实学习五子棋是为了复习巩固以前学习的一些内容。掌握JFrame的基本操作，过去画板，添加按钮，在按钮这个事件上添加监听器。掌握继承的方法。实现重绘，能够展现一个简单的五子棋对战的小游戏。最后能够实现人机对战。编写方法，用算法实现这个。掌握五子棋的具体算法。

  三.过程分解
      1.在窗体的画板上，画上直线与横线形成一棋盘格子(建立坐标)
      应用坐标的关系，画出五子棋的线条布局，实现一个五子棋的基本框架。

     2.在窗体的画板上绘制棋子(事件处理)
      给事件添加接口，实现接口，能够绘制五子棋，黑白相间的。

     3.人人对战完成胜负判断(下一颗白子接着一颗黑子)
     写出算法，能够判断输赢，实现自己和自己下棋。

     4.人机对战(机器人下棋策略，也就是五子算法)	



```java

```

```java
package cn.hsm.java;


import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

public class FiveChessTest extends JFrame 
{
 public static void main(String[] args)
 {
  FiveChessTest fc = new FiveChessTest();
  fc.showUI();
 }
 
 private int[][] location = new int[name.height][name.width];
 private Graphics g;

 public void showUI() {
  this.setTitle(&quot;hsm五子棋···&quot;);
  this.setSize(700, 700);
  this.setLocationRelativeTo(null);
  this.setDefaultCloseOperation(3);
  this.setVisible(true);
  
  Graphics g = getGraphics();  
  MouseListenerImpl mouseListener = new MouseListenerImpl(g ,location);
  this.addMouseListener(mouseListener);
 }
 /*
  * 重写paint函数
  */
 public void paint(Graphics g)
 {
  super.paint(g);
  //画棋盘的横线
  for(int i = 0; i 
```

-----------------------------------------------------------------


```java

package cn.hsm.java;

import java.awt.Color;
import java.awt.Graphics;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseListenerImpl extends MouseAdapter{
	private Graphics g;
	private int x1,y1;
	private int choose = 1;
	private int[][] location;
	public MouseListenerImpl(Graphics g ,int[][] location) 
	{
		this.g =g;
		this.location = location;
	}

	
	
	 public void mouseReleased(MouseEvent e) 
	 {
		//鼠标点击的位置
		 x1 = e.getX();
		 y1 = e.getY();
		 int x0; //棋盘交叉点的x坐标
		 int y0; //棋盘交叉点的y坐标
		 for (int a = 0; a < Name.WIDTH; a++)
		 {
			for (int b = 0; b < Name.HEIGHT; b++) 
			{				
				x0 = Name.X0 + a * Name.CELL_SIZE;
				y0 = Name.Y0 + b * Name.CELL_SIZE;
				if((Math.abs(x1 - x0) < Name.CELL_SIZE / 2) && (Math.abs(y1 - y0) < Name.CELL_SIZE / 2) &&location[a][b] == 0) {
					
					
					//画出棋子，且分出颜色：1为黑色，-1为白色;判断当前画的棋子颜色，并将其存储到二维数组中	
					if(choose == 1)
					{
						g.setColor(Color.BLACK);
						g.fillOval(x0 - Name.CHESS_SIZE/2, y0 - Name.CHESS_SIZE/2, Name.CHESS_SIZE, Name.CHESS_SIZE);
						location[a][b] = 1;
						win01Ways(a, b, choose);
						win02Ways(a, b, choose);
						win03Ways(a, b, choose);
						choose = -1;
						System.out.println("black");
						return;
					}
					if (choose == -1)
					{
						g.setColor(Color.WHITE);
						g.fillOval(x0 - Name.CHESS_SIZE/2, y0 - Name.CHESS_SIZE/2, Name.CHESS_SIZE, Name.CHESS_SIZE);
						location[a][b] = -1;
						win01Ways(a, b, choose);
						win02Ways(a, b, choose);
						win03Ways(a, b, choose);
						choose = 1 ;
						System.out.println("white");
					}
				}
			} 		
		 }
	 }



	private void win01Ways(int x, int y, int choose) {
		int count = 1;
		//判断横向是否连珠
		if(choose == 1) {
			for(int i = 1; i < 5; i++) {
				if(location[x-i][y] == 1) {
					count++;
				}else{
					break;
				}
			}
			for(int j = 1; j < 5; j++) {
				if(location[x+j][y] == 1) {
					count++;
				}else{
					break;
				}
			}
		}else {
			for(int i = 1; i < 5; i++) {
				if(location[x-i][y] == -1) {
					count++;
				}else{
					break;
				}
			}
			for(int j = 1; j < 5; j++) {
				if(location[x+j][y] == -1) {
					count++;
				}else{
					break;
				}
			}
		}
        if (count > 4)
        {
        	System.out.println("有人获胜啦！！");
        }
	}
	
	private void win02Ways(int x, int y, int choose) {
		int count = 1;
		//判断纵向是否连珠
		if(choose == 1) {
			for(int i = 1; i < 5; i++) {
				if(location[x][y-i] == 1) {
					count++;
				}else{
					break;
				}
			}
			for(int j = 1; j < 5; j++) {
				if(location[x][y+j] == 1) {
					count++;
				}else{
					break;
				}
			}
		}else {
			for(int i = 1; i < 5; i++) {
				if(location[x][y-i] == -1) {
					count++;
				}else{
					break;
				}
			}
			for(int j = 1; j < 5; j++) {
				if(location[x][y+j] == -1) {
					count++;
				}else{
					break;
				}
			}
		}
        if (count > 4)
        {
        	System.out.println("有人获胜啦！！");
        }
	}
	
	private void win03Ways(int x, int y, int choose) {
		int count = 1;
		//判断纵向是否连珠
		if(choose == 1) {
			for(int i = 1; i < 5; i++) {
				if(location[x-i][y-i] == 1) {
					count++;
				}else{
					break;
				}
			}
			for(int j = 1; j < 5; j++) {
				if(location[x+j][y+j] == 1) {
					count++;
				}else{
					break;
				}
			}
		}else {
			for(int i = 1; i < 5; i++) {
				if(location[x-i][y-i] == -1) {
					count++;
				}else{
					break;
				}
			}
			for(int j = 1; j < 5; j++) {
				if(location[x+j][y+j] == -1) {
					count++;
				}else{
					break;
				}
			}
		}
        if (count > 4)
        {
        	System.out.println("有人获胜啦！！");
        }
	}
	
		 	 
}
```

------------------------------------------------------------------



```java

package cn.hsm.java;

public class Name {
	public static int X0 = 50;  		//横坐标的偏移量 
	public static int Y0 = 50;  		//纵坐标的偏移量
	public static int CELL_SIZE= 50; 	// 方格的大小
	public static int HEIGHT = 12;    	//高的数目
	public static int WIDTH = 12;       //宽的数目
	public static int CHESS_SIZE = 40;  //棋子的大小
}
```