#java 五子棋 java实现五子棋AI算法详解

本文详细介绍如何使用Java实现五子棋的人工智能算法。包括棋盘绘制、棋子放置及胜负判断的基础知识,并深入探讨了利用HashMap进行棋局评估的AI设计方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java实现五子棋 AI算法详解

学习本文内容需要先准备的知识:窗体编写、窗体元素添加、窗体绘制线条、窗体绘制填充圆

1、 前期准备:

(1)、棋盘的绘制:在一个窗口里绘制一定的水平线和垂直线,使之交叉构成棋盘;

(2)、棋子的呈现方法:鼠标点击指定位置的时候,获取指定位置的坐标,找到与所点击位置距离最近的棋盘上的线条交叉点,在此绘制一个填充圆(注意,java画圆的方法需要提供的参数有4个:圆左上角坐标、圆的宽度和圆的高度,所以不能直接将棋盘上某个点的坐标作为传入的参数值);

(3)、判断输赢的方法:在每次下棋的这个点位向八个方向寻找,找到连续五个棋子颜色一致则判断胜利。

2、 需要用到的新知识——HashMap
HashMap是一个用于存储Key-Value键值对的集合,基于哈希表的 Map 接口的实现。
(1)、语法:

HashMap<键的参数类型,值的参数类型> HashMap名 = new HashMap<键的参数类型,值的参数类型>();

注意:键和值的类型只能为出了基本参数类型外的自定义参数类型,所以不能使用int、char、double等关键字作为键和值的类型,但可以使用它们对应的类名,如整形可以用Integer来替换。

(2)、为HashMap添加内容:HashMap名.put(键,值);

(3)、利用键从HashMap里面获取对应的值:HashMap名.get(键);
注意:从HashMap对象里面获取到的值的类型和HashMap定义时值的类型一致,如果需要获取并保存这个值,需要定义一个和HashMap值类型同类型的对象。

例:

HashMap<String,Integer> hm = new HashMap<String,Integer>(); //创建一个HashMap对象,键为字符串类型,值为整形
    hm.put("1",20); //给该HashMap对象添加一个键为”1”,值为20的数据;
Integer value = hm.get(“1”);  //获取该HashMap对象里面键为”1”对应的值。

3、 AI算法解析
(1)、文字说明:
第一步:在棋盘上没有棋子的位置向八个方向(上、下、左、右、左斜向上、左斜向下、右斜向上、右斜向下)搜索相同颜色的棋子,对手(相对于机器人,也就是玩家)的颜色用1表示,自己的用2表示,搜索到没有棋子的位置就停止搜索;
第二步:将附近最多4个棋子的颜色对应的数字连接成一个字符串,将其作为HashMap的键,按照对手连续棋子的数量为不同的键设置不同的值,连续的棋子越多值越大,周围没有棋子或者该点已经有棋子,则值为0;
第三步:将所有空白棋位的八个方向的值相加,记录起来,机器在值最大的点下棋。

(2)、代码示例:

private int[][] chessValue = new int[15][15];    //用于储存各个棋位的值
private HashMap<String, Integer> hm = new HashMap<String, Integer>();  //创建一个HashMap对象保存键和值

//为不同的键添加不同的值
public void setValue() {    
		hm.put("1", 20);
		hm.put("11", 200);
		hm.put("111", 2000);
		hm.put("1111", 3000);
		hm.put("12", 10);
		hm.put("112", 100);
		hm.put("1112", 1000);
		hm.put("11112", 2000);
}

//在棋盘上空白位置向八个方向搜索连续棋子,下面以向右方向为例
public void AI(int plaColor) {    传入对方棋子的颜色(1为黑,2为白)
		setValue(); //自定义的函数,用于将之前棋盘上各点的值chessValue[i][j]清空
		for (int i = 0; i < chess.length; i++) {  //chess是自定义的一个整形数组,存有各个棋位上棋子的颜色,黑色为1,白色为2,没有棋子为0
			for (int j = 0; j < chess[i].length; j++) {    //两个for循环,历遍所有的棋盘位置
				if (chess[i][j] == 0) {       //如果当前位置没有棋子则继续判断
					String code = "";
					int color = 0;
					// 从该位置向右,判断棋子颜色是否一样
					for (int k = i + 1; k < chess.length; k++) {
						if (chess[k][j] == 0) {   //如果遇到了没有棋子的棋位,跳出本次循环
							break;
						} else {
							if (color == plaColor) { // 判断右边第一个棋子的颜色是否是对手的颜色
								color = chess[k][j]; // 保存颜色
								code += 1; // 同色,记为1,组合进字符串
							} else if (chess[k][j] == plaColor) { // 右边第二,三..同颜色棋子
								code += 1; // 同色,记为1,组合进字符串
							} else { // 右边不同颜色
								code += 2; // 不同色,记为2,组合进字符串
								break;   //遇到一个不同色的即可跳出循环
							}
						}
                      //参照上面的方法可以写出向左、向上、向下、左斜向上、右斜向上、左斜向下、右斜向下的方法(具体见下面全部代码里的robot.java)
					}
					// 根据code取出hm对应的权值
					Integer value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
              }
       }
}

4、全部的代码:

–ChessBoard.java–

package com.antony.wuziqi0908;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ChessBoard {

	public static void main(String[] args) {
		ReFrame ChessBoard = new ReFrame();
		ChessBoard.setTitle("五子棋");
		ChessBoard.setSize(910, 910);
		ChessBoard.setDefaultCloseOperation(3);
		FlowLayout flow = new FlowLayout();
		BorderLayout boder=new BorderLayout();
		ChessBoard.setLayout(boder);
		JPanel p=new JPanel();
		p.setLayout(flow);
		Dimension d1 = new Dimension(60, 300);
		JLabel jsl_pla = new JLabel();
		jsl_pla.setPreferredSize(d1);
		JButton jsb_Whi = new JButton("玩家先下");
		JButton jsb_Bla = new JButton("机器先下");
		JButton jsb_Rob = new JButton("双人对战");
		JButton jsb_Ret = new JButton("悔棋");
		JButton jsb_Exi = new JButton("退出");
		Dimension d2 = new Dimension(60, 910);
		p.setPreferredSize(d2);
		p.add(jsl_pla);
		p.add(jsb_Whi);
		p.add(jsb_Bla);
		p.add(jsb_Rob);
		p.add(jsb_Ret);
		p.add(jsb_Exi);
		ChessBoard.getContentPane().add("East",p);
		Color boardColor = new Color(225, 176, 63);
		ChessBoard.getContentPane().setBackground(boardColor);
		p.setBackground(boardColor);
		ChessBoard.setLocationRelativeTo(null);
	    ChessBoard.setResizable(false);
		ChessBoard.setVisible(true);
		Graphics g = ChessBoard.getGraphics();
		DrawListener dl = new DrawListener(ChessBoard.SC,ChessBoard.SCF, g, ChessBoard);
		jsb_Whi.addActionListener(dl);
		jsb_Bla.addActionListener(dl);
		jsb_Rob.addActionListener(dl);
		jsb_Ret.addActionListener(dl);
		jsb_Exi.addActionListener(dl);
		ChessBoard.addMouseListener(dl);
	}
}

–DrawListener.java–

package com.antony.wuziqi0908;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class DrawListener extends MouseAdapter implements ActionListener {
	private boolean canPlay = false, isPlayer = false;
	private int x, y, xx, yy;
	private Graphics g;
	private JFrame j;
	private int tWidth;
	public StoreChess[][] SC;
	public StoreChess[] SCF;
	private Robot r;
	// private int count = 0;
	private boolean isChess = true; // 判断点击的地方有没有已经下了的棋子
	private boolean isBlack = true; // 判断下一棋子是不是黑棋
	private String BT = null;
	private String oBT = null;
	private int count = 0;

	DrawListener(StoreChess[][] s, StoreChess[] sf, Graphics g, JFrame j) {
		this.g = g;
		this.j = j;
		SC = s;
		SCF = sf;
		r = new Robot(SC);
	}

	public int getTWidth() {
		int width = j.getWidth();
		int length = j.getHeight();
		if (length > width)
			tWidth = width;
		else
			tWidth = length;
		return tWidth;
	}

	public void mouseClicked(MouseEvent e) {
		r.clearValue();
		x = e.getX();
		y = e.getY();
		switch (BT) {
		case "玩家先下":
			oBT = BT;
			if (isPlayer) {
				playChess(x, y);
				isPlayer = false;
			}
			if (!isPlayer) {
				if (isBlack)
					r.AI(2);
				else
					r.AI(1);
				robPlayChess(r.getX(), r.getY());
				isPlayer = true;
			}
			break;

		case "机器先下":
			oBT = BT;
			if (isPlayer) {
				playChess(x, y);
				isPlayer = false;
			}
			if (!isPlayer) {
				if (isBlack)
					r.AI(2);
				else
					r.AI(1);
				robPlayChess(r.getX(), r.getY());
				isPlayer = true;
			}
			break;
		case "双人对战":
			oBT = BT;
			playChess(x, y);
			break;
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		BT = e.getActionCommand();
		if (BT.equals("玩家先下")) {
			for (int i = 0; i < count - 1; i++) {
				SCF[i] = null;
			}
			count = 0;
			for (int i = 0; i < SC.length; i++) {
				for (int j = 0; j < SC[i].length; j++) {
					if (SC[i][j] != null) {
						SC[i][j] = null;
					}
				}
			}
			j.repaint();
			canPlay = true;
			isChess = false;
			if (count == 0)
				isPlayer = true;
		} else if (BT.equals("机器先下")) {
			for (int i = 0; i < count - 1; i++) {
				SCF[i] = null;
			}
			count = 0;
			for (int i = 0; i < SC.length; i++) {
				for (int j = 0; j < SC[i].length; j++) {
					if (SC[i][j] != null) {
						SC[i][j] = null;
					}
				}
			}
			j.repaint();
			canPlay = true;
			isChess = false;
			if (count == 0) {
				int[] rp = r.firstPlay();
				playChess(rp[0], rp[1]);
				isPlayer = true;
			}

		} else if (BT.equals("双人对战")) {
			for (int i = 0; i < count - 1; i++) {
				SCF[i] = null;
			}
			count = 0;
			for (int i = 0; i < SC.length; i++) {
				for (int j = 0; j < SC[i].length; j++) {
					if (SC[i][j] != null) {
						SC[i][j] = null;
					}
				}
			}
			j.repaint();
			canPlay = true;
			isChess = false;
		} else if (BT.equals("悔棋") && canPlay) {
			for (int i = 0; i < SC.length; i++) {
				for (int k = 0; k < SC[i].length; k++) {
					if (SC[i][k] != null) {
						if (SC[i][k].equals(SCF[count - 1])) {
							SC[i][k] = null;
						}
					}
				}
			}
			if (count > 0) {
				if (SCF[count - 1].getColor().equals(Color.BLACK)) {
					isBlack = true;
				} else {
					isBlack = false;
				}
				SCF[count - 1] = null;
				count--;
			}
			if(isPlayer==true){
				for (int i = 0; i < SC.length; i++) {
					for (int k = 0; k < SC[i].length; k++) {
						if (SC[i][k] != null) {
							if (SC[i][k].equals(SCF[count - 1])) {
								SC[i][k] = null;
							}
						}
					}
				}
				if (count > 0) {
					if (SCF[count - 1].getColor().equals(Color.BLACK)) {
						isBlack = true;
					} else {
						isBlack = false;
					}
					SCF[count - 1] = null;
					count--;
				}
			}
			BT = oBT;
			canPlay = true;
			isPlayer = true;
			// if (count <= 1) {
			// count = 0;
			// } else {
			// count = count - 2;
			// }
			j.repaint();
		}
	}

	public void playChess(int x, int y) {
		int d = (getTWidth() - 150) / 28;
		for (int i = 0; i < 15; i++) {
			int y1 = 100 + 2 * d * i;
			if (Math.abs(y - y1) < (getTWidth() - 150) / 28) {
				y = y1;
				yy = (y - 100) / (2 * d);
				break;
			}
		}
		for (int i = 0; i < 15; i++) {
			int x1 = 75 + 2 * d * i;
			if (Math.abs(x - x1) < (getTWidth() - 150) / 28) {
				x = x1;
				xx = (x - 75) / (2 * d);
				break;
			}
		}
		// g.setColor(BLACK);

		for (int i = 0; i < SC.length; i++) {
			for (int j = 0; j < SC[i].length; j++) {
				if (SC[i][j] != null) {
					if (x - d == SC[i][j].getX() && y - d == SC[i][j].getY()) {
						isChess = true;
						break;
					}
				}
			}
		}
		if ((!isChess) && canPlay) {
			if (isBlack) {
				g.setColor(Color.BLACK);
				g.fillOval(x - d, y - d, 2 * d, 2 * d);
				StoreChess chess = new StoreChess(x - d, y - d, 2 * d, g.getColor());

				// chess.draw(g);
				SC[xx][yy] = chess;
				SCF[count++] = chess;
				checkWinner();
				isBlack = false;
			} else {
				g.setColor(Color.WHITE);
				g.fillOval(x - d, y - d, 2 * d, 2 * d);
				StoreChess chess = new StoreChess(x - d, y - d, 2 * d, g.getColor());

				// chess.draw(g);
				SC[xx][yy] = chess;
				SCF[count++] = chess;
				checkWinner();
				isBlack = true;
			}
		}
		isChess = false;
	}

	public void robPlayChess(int x, int y) {
		int d = (j.getWidth() - 150) / 14;
		if ((!isChess) && canPlay) {
			if (isBlack) {
				g.setColor(Color.BLACK);
				g.fillOval(75 + x * d - d / 2, 100 + y * d - d / 2, d, d);
				StoreChess chess = new StoreChess(75 + x * d - d / 2, 100 + y * d - d / 2, d, g.getColor());

				SC[x][y] = chess;
				SCF[count++] = chess;
				checkWinner();
				isBlack = false;
			} else {
				g.setColor(Color.WHITE);
				g.fillOval(75 + x * d - d / 2, 100 + y * d - d / 2, d, d);
				StoreChess chess = new StoreChess(75 + x * d - d / 2, 100 + y * d - d / 2, d, g.getColor());

				SC[x][y] = chess;
				SCF[count++] = chess;
				checkWinner();
				isBlack = true;
			}
		}
		isChess = false;
	}

	public void checkWinner() {
		int d = (getTWidth() - 150) / 28;
		for (int i = 0; i < SC.length; i++) {
			for (int j = 0; j < SC[i].length - 4; j++) {
				if (SC[i][j] != null && SC[i][j + 1] != null && SC[i][j + 2] != null && SC[i][j + 3] != null
						&& SC[i][j + 4] != null) {
					if (SC[i][j].getColor().equals(SC[i][j + 1].getColor())
							&& SC[i][j].getColor().equals(SC[i][j + 2].getColor())
							&& SC[i][j].getColor().equals(SC[i][j + 3].getColor())
							&& SC[i][j].getColor().equals(SC[i][j + 4].getColor())) {
						SC[i][j].draw(g, Color.RED);
						SC[i][j + 1].draw(g, Color.RED);
						SC[i][j + 2].draw(g, Color.RED);
						SC[i][j + 3].draw(g, Color.RED);
						SC[i][j + 4].draw(g, Color.RED);
						isChess = true;
						canPlay = false;
					}
				}
			}
		}
		for (int i = 0; i < SC.length - 4; i++) {
			for (int j = 0; j < SC[i].length; j++) {
				if (SC[i][j] != null && SC[i + 1][j] != null && SC[i + 2][j] != null && SC[i + 3][j] != null
						&& SC[i + 4][j] != null) {
					if (SC[i][j].getColor().equals(SC[i + 1][j].getColor())
							&& SC[i][j].getColor().equals(SC[i + 2][j].getColor())
							&& SC[i][j].getColor().equals(SC[i + 3][j].getColor())
							&& SC[i][j].getColor().equals(SC[i + 4][j].getColor())) {
						SC[i][j].draw(g, Color.RED);
						SC[i + 1][j].draw(g, Color.RED);
						SC[i + 2][j].draw(g, Color.RED);
						SC[i + 3][j].draw(g, Color.RED);
						SC[i + 4][j].draw(g, Color.RED);
						isChess = true;
						canPlay = false;
					}
				}
			}
		}
		for (int i = 0; i < SC.length - 4; i++) {
			for (int j = 0; j < SC[i].length - 4; j++) {
				if (SC[i][j] != null && SC[i + 1][j + 1] != null && SC[i + 2][j + 2] != null && SC[i + 3][j + 3] != null
						&& SC[i + 4][j + 4] != null) {
					if (SC[i][j].getColor().equals(SC[i + 1][j + 1].getColor())
							&& SC[i][j].getColor().equals(SC[i + 2][j + 2].getColor())
							&& SC[i][j].getColor().equals(SC[i + 3][j + 3].getColor())
							&& SC[i][j].getColor().equals(SC[i + 4][j + 4].getColor())) {
						SC[i][j].draw(g, Color.RED);
						SC[i + 1][j + 1].draw(g, Color.RED);
						SC[i + 2][j + 2].draw(g, Color.RED);
						SC[i + 3][j + 3].draw(g, Color.RED);
						SC[i + 4][j + 4].draw(g, Color.RED);
						isChess = true;
						canPlay = false;
					}
				}
			}
		}
		for (int i = 4; i < SC.length - 4; i++) {
			for (int j = 0; j < SC[i].length; j++) {
				if (SC[i][j] != null && SC[i + 1][j - 1] != null && SC[i + 2][j - 2] != null && SC[i + 3][j - 3] != null
						&& SC[i + 4][j - 4] != null) {
					if (SC[i][j].getColor().equals(SC[i + 1][j - 1].getColor())
							&& SC[i][j].getColor().equals(SC[i + 2][j - 2].getColor())
							&& SC[i][j].getColor().equals(SC[i + 3][j - 3].getColor())
							&& SC[i][j].getColor().equals(SC[i + 4][j - 4].getColor())) {
						SC[i][j].draw(g, Color.RED);
						SC[i + 1][j - 1].draw(g, Color.RED);
						SC[i + 2][j - 2].draw(g, Color.RED);
						SC[i + 3][j - 3].draw(g, Color.RED);
						SC[i + 4][j - 4].draw(g, Color.RED);
						isChess = true;
						canPlay = false;
					}
				}
			}
		}
	}

}

–robot.java–

package com.antony.wuziqi0908;

import java.awt.Color;
import java.awt.Graphics;
import java.util.HashMap;
import java.util.Random;

public class Robot {
	private StoreChess[][] Chess;
	private int[][] chess = new int[15][15];
	private int[][] chessValue = new int[15][15];
	private int x,y;
	
	public int getX(){
		return x;
	}

	public int getY(){
		return y;
	}
	
	Robot(StoreChess[][] chess) {
		Chess = chess;
	}
	
	public void setChess(){
		for(int i=0;i<Chess.length;i++){
			for(int j = 0;j<Chess[i].length;j++){
				chess[i][j]=0;
				if(Chess[i][j]!=null){
					if(Chess[i][j].getColor()==Color.BLACK){
						chess[i][j]=1;
					}
					else if(Chess[i][j].getColor()==Color.WHITE){
						chess[i][j]=2;
					}
				}
			}
		}
	}

	private HashMap<String, Integer> hm = new HashMap<String, Integer>();

	public void setValue() {
		hm.put("1", 20);
		hm.put("11", 200);
		hm.put("111", 2000);
		hm.put("1111", 3000);
		hm.put("12", 10);
		hm.put("112", 100);
		hm.put("1112", 1000);
		hm.put("11112", 2000);
	}

	public int[] firstPlay(){
		int[] point = new int[2];
		Random rand = new Random();
		int x = 405+rand.nextInt(100);
		int y = 405+rand.nextInt(100);
		point[0]=x;
		point[1]=y;
		return point;
	}
	
	public void clearValue(){
		for(int i=0;i<chessValue.length;i++){
			for(int j=0;j<chessValue[i].length;j++){
				chessValue[i][j]=0;
			}
		}
	}
	
	public void AI(int plaColor) {
		setChess();
		setValue();
		for (int i = 0; i < chess.length; i++) {
			for (int j = 0; j < chess[i].length; j++) {
				if (chess[i][j] == 0) {
					String code = "";
					int color = 0;
					// 向右
					for (int k = i + 1; k < chess.length; k++) {
						if (chess[k][j] == 0) {
							break;
						} else {
							if (color == plaColor) { // 右边第一颗棋子
								color = chess[k][j]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[k][j] == plaColor) { // 右边第二,三..同颜色棋子
								code += 1; // 保存棋局
							} else { // 右边不同颜色
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					Integer value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value;// 权值累加
					}

					// 向左
					for (int k = i - 1; k >= 0; k--) {
						if (chess[k][j] == 0) {
							break;
						} else {
							if (color == plaColor) { 
								color = chess[k][j]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[k][j] == plaColor) {
								code += 1; // 保存棋局
							} else { 
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
					
					// 向上
					for (int k = j + 1; k < chess[i].length; k++) {
						if (chess[i][k] == 0) {
							break;
						} else {
							if (color == plaColor) { 
								color = chess[i][k]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[i][k] == plaColor) { 
								code += 1; // 保存棋局
							} else {
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
					
					// 向下
					for (int k = j - 1; k > 0; k--) {
						if (chess[i][k] == 0) {
							break;
						} else {
							if (color == plaColor) {
								color = chess[i][k]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[i][k] == plaColor) { 
								code += 1; // 保存棋局
							} else { 
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
					// 左斜向上
					for (int k = 1; i-k >0&&j-k>0 ; k++) {
						if (chess[i-k][j-k] == 0) {
							break;
						} else {
							if (color == plaColor) { 
								color = chess[i-k][j-k]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[i-k][j-k] == plaColor) { 
								code += 1; // 保存棋局
							} else {
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
					// 右斜向下
					for (int k = 1; i+k<chess.length&&j+k<chess[i].length ; k++) {
						if (chess[i+k][j+k] == 0) {
							break;
						} else {
							if (color == plaColor) {
								color = chess[i+k][j+k]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[i+k][j+k] == plaColor) { 
								code += 1; // 保存棋局
							} else { 
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
					// 右斜向上
					for (int k = 1; j-k>0&&i+k<chess[i].length ; k++) {
						if (chess[i+k][j-k] == 0) {
							break;
						} else {
							if (color == plaColor) {
								color = chess[i+k][j-k]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[i+k][j-k] == plaColor) {
								code += 1; // 保存棋局
							} else { 
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
					// 左斜向下
					for (int k = 1; j+k<chess.length&&i-k>0; k++) {
						if (chess[i-k][j+k] == 0) {
							break;
						} else {
							if (color == plaColor) {
								color = chess[i-k][j+k]; // 保存颜色
								code += 1; // 保存棋局
							} else if (chess[i-k][j+k] == plaColor) {
								code += 1; // 保存棋局
							} else {
								code += 2; // 保存棋局
								break;
							}
						}
					}
					// 根据code取出hm对应的权值
					value = hm.get(code);
					if (value != null) {
						chessValue[i][j] += value; // 权值累加
					}
				}
				else{
					chessValue[i][j] = 0;
				}
			}
			
		}
		int max=0;
		for(int i = 0; i<chessValue.length-1;i++){
			for(int j=0; j<chessValue[i].length-1; j++){
				if(max<chessValue[i][j]){
					max = chessValue[i][j];
					x=i;
					y=j;
				}
			}
		}
	}
}

–ReFrame.java–

package com.antony.wuziqi0908;

import java.awt.Graphics;

import javax.swing.JFrame;

public class ReFrame extends JFrame {
	public StoreChess[][] SC = new StoreChess[15][15];
	public StoreChess[] SCF = new StoreChess[255];
	public void paint(Graphics g) {
		super.paint(g);
		drawBoard(g);
		drawChess(g);
		System.out.println("刷新棋盘!");
	}

	public void drawBoard(Graphics g) {
		int length = this.getHeight();
		int width = this.getWidth();
		int tWidth = 0;
		if (length > width)
			tWidth = width;
		else
			tWidth = length;
		for (int i = 0; i < 15; i++) {
			int x1 = 75 + (tWidth - 150) / 14 * 0;
			int y1 = 100 + (tWidth - 150) / 14 * i;
			int x2 = 75 + (tWidth - 150) / 14 * 14;
			int y2 = y1;
			g.drawLine(x1, y1, x2, y2);
			// System.out.println("x1:"+x1+",y1:"+y1+",x2:"+x2+",y2:"+y2+",(tWidth
			// - 150) / 14="+((tWidth - 150) / 14));
		}
		for (int i = 0; i < 15; i++) {
			int x1 = 75 + (tWidth - 150) / 14 * i;
			int x2 = x1;
			int y1 = 100 + (tWidth - 150) / 14 * 0;
			int y2 = 100 + (tWidth - 150) / 14 * 14;
			g.drawLine(x1, y1, x2, y2);
		}
	}
	// public int getTWidth(){
	// int length = this.getHeight();
	// int width = this.getWidth();
	// int tWidth = 0;
	// if (length > width)
	// tWidth = width;
	// else
	// tWidth = length;
	// return tWidth;
	// }

	public void drawChess(Graphics g) {
		for (int i = 0; i < SC.length; i++) {
			for (int j = 0; j < SC[i].length; j++) {
				if (SC[i][j]!= null) {
					SC[i][j].draw(g,SC[i][j].getColor());
				}
			}
		}
	}
}

–StoreChess.java–

package com.antony.wuziqi0908;

import java.awt.Color;
import java.awt.Graphics;

public class StoreChess {
	private int x, y,colorNum;
	private int width;
	private Color c;
	
	
    public int getX(){
    	return x;
    }
    
    public int getY(){
    	return y;
    }
    
    
    public Color getColor(){
    	return c;
    }
    
	StoreChess(int x, int y, int w, Color c) {
		this.x = x;
		this.y = y;
//		System.out.println("x="+ x);
//		System.out.println("y="+ y);
		this.c = c;
		width = w;
	}

	void draw(Graphics g,Color nC) {
		g.setColor(nC);
//		for (int i = 0; i < 15; i++) {
//			double y1 = 100 + (nTWidth - 150) / 14 * i;
//			if (Math.abs(y - y1) < (nTWidth - 150) / 28) {
//				y = y1;
//				break; 
//			}
//		}
//		for (double i = 0; i < 15; i++) {
//			double x1 = 75 + (nTWidth - 150) / 14 * i;
//			if (Math.abs(x - x1) < (nTWidth - 150) / 28) {
//				x = x1;
//				break;
//			}
//		}

//		System.out.println("nTWidth="+nTWidth);
//		System.out.println("FTW="+FTW);
//		System.out.println("proportion = "+proportion);
//		System.out.println("x - d="+(x - d));
//		System.out.println("y - d="+(y - d));
		
//		System.out.println("draw:"+((xx*r)+75 - d) + "," + ((yy*r)+100 - d));
//		System.out.println("r:"+r+", xx:"+xx+", d:"+d);
		g.fillOval(x,y,width,width);
	}
}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值