精华内容
下载资源
问答
  • JAVA 五子棋人机对战

    2011-05-23 20:15:48
    JAVA 五子棋 人机对战 一些简单的人工智能,分析各个位置下棋的权值来判断最佳位置。
  • java五子棋人机对战

    2021-03-13 19:54:53
    java写的五子棋小程序,界面优美,人机对战,人人对战。
  • 本项目使用了JavaSwing实现桌面五子棋应用,人机对战部分用a-B剪枝算法实现,有一定的智能。
  • java 五子棋 人机对战

    2009-02-07 15:56:00
    贪心算法实现电脑下棋算法,只能看一步,比较简单。练习
  • 学习JAVA做的练习 做得还算漂亮 根据MLDN的教程改编,人机对战 我自己对五子棋技术很有限 所以写的算法也很有限 欢迎各位指导
  • 自己写的人机对战,思路是给每个未下的棋子进行评分,活二、活三、冲四等分别代表不同分数,取最高分数进行落子 简单写了个前端页面,大家可以玩玩看 https://www.linzhehao.cn/Gobang/single 下面是Java的核心...

    自己写的人机对战,思路是给每个未下的棋子进行评分,活二、活三、冲四等分别代表不同分数,取最高分数进行落子

    简单写了个前端页面,大家可以玩玩看

    https://www.linzhehao.cn/gobang/single

    下面是Java的核心代码及用法

    一、示例用法

    public class MyTest {
    	public static void main(String[] args) {
    		// 初始化服务
    		ComputerService service = new ComputerService();
    		
    		// 设置玩家的数字(1或2),不设置的话默认是1
    		// 如果设置成1,电脑就是2,如果设置成2,电脑就是1
    		service.setPlayerType(1);
    		
    		// 构造一个15*15的二维数组棋盘
    		Integer[][] checkBox = {
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
    		
    		// 让电脑下一颗棋子保存到r对象
    		R r = service.getResult(checkBox);
    		
    		// 如果电脑获胜
    		if("电脑获胜".equals(r.getState())) {
    			// 输出电脑下的棋子
    			System.out.println(r);
    			System.out.println(r.getState());
    		}
    		// 如果玩家获胜
    		else if ("玩家获胜".equals(r.getState())) {
    			System.out.println(r.getState());
    		} else {
    			// 输出电脑下的棋子
    			System.out.println(r);
    		}
    	}
    }

    二、机器人代码

    import java.util.*;
    
    public class ComputerService {
    	// 临时储存分数
    	private int count1;
    	private int count2;
    	private int count3;
    	private int count4;
    	private int count5;
    	// 电脑五子
    	private int computerFive = 1000;
    	// 玩家五子
    	private int playerFive = 980;
    	// 电脑活四
    	private int computerActiveFour = 900;
    	// 电脑冲四
    	private int computerChongFour = 800;
    	// 玩家活四
    	private int playerActiveFour = 600;
    	// 玩家冲四
    	private int playerChongFour = 450;
    	// 电脑活三
    	private int computerActiveThree = 350;
    	// 玩家活三
    	private int playerActiveThree = 300;
    	// 电脑眠三
    	private int computerSleepThree = 200;
    	// 玩家眠三
    	private int playerSleepThree = 150;
    	// 玩家多个活二
    	private int playerManyActiveTwo = 100;
    	// 电脑活二
    	private int computerActiveTwo = 50;
    	// 周围有棋子
    	private int hasAround = 10;
    
    	private int[][] scores = new int[15][15]; // 分数
    	public int x, y;                         // 当前的x和y值
    	private Integer[][] checkBox;
    
    	private RegularFactory factory = new RegularFactory();
    
    	// 判断电脑是黑棋还是白棋
    	private enum Type {
    		black(1), white(2);
    		private int num;
    		Type(int num) {
    			this.num = num;
    		}
    		public int getNum() {
    			return num;
    		}
    	}
    
    	// 人类和电脑的颜色
    	private Type player = Type.black;
    	private Type computer = Type.white;
    
    	/**
    	 * 设置玩家的类型
    	 * @param type 1或2
    	 */
    	public void setPlayerType(int type) {
    		player = type == 1 ? Type.black : Type.white;
    		computer = type == 2 ? Type.black : Type.white;
    	}
    
    	/**
    	 * 获取结果
    	 * @param checkBox 棋盘数据
    	 * @return 电脑下的坐标
    	 */
    	public R getResult(Integer[][] checkBox) {
    		this.checkBox = checkBox;
    		factory.setCheckBox(checkBox);
    		long startTime = System.currentTimeMillis();
    
    		// 如果玩家胜利
    		R playerIsWin = playerIsWin();
    		if(playerIsWin != null) {
    			return playerIsWin;
    		}
    
    		// 如果电脑胜利
    		R computerIsWin = computerIsWin();
    		if(computerIsWin != null) {
    			return computerIsWin;
    		}
    
    		// 获取分数
    		int maxScore = -1; // 最大分数
    		for(int i=0;i<15;i++) {
    			for(int j=0;j<15;j++) {
    				scores[i][j] = getScore(i, j);
    				// 统计最大分数
    				if(scores[i][j] > maxScore) {
    					maxScore = scores[i][j];
    				}
    			}
    		}
    
    		// 统计所有最大分数的坐标
    		List<R> maxR = new ArrayList<>();
    		for(int i=0;i<15;i++) {
    			for(int j=0;j<15;j++) {
    				if(scores[i][j] == maxScore) {
    					maxR.add(new R(i, j));
    				}
    			}
    		}
    
    		// 输出每一步的棋盘信息和分数信息
    		// output();
    
    		// 随机返回一个R
    		int random = new Random().nextInt(maxR.size());
    		long stopTime = System.currentTimeMillis();
    		System.out.println("计算完成,耗时" + (stopTime - startTime) + "毫秒");
    		return maxR.get(random);
    	}
    
    	/**
    	 * 打分
    	 * @param x x轴
    	 * @param y y轴
    	 * @return 分数
    	 */
    	private int getScore(int x, int y) {
    		this.x = x;
    		this.y = y;
    		// 如果已经下了棋子
    		if(checkBox[x][y] != 0) {
    			return -1;
    		}
    
    		// 电脑成为五子
    		if(becomeFive(computer.getNum(), x, y, true)) {
    			return computerFive;
    		}
    
    		// 玩家成为五子
    		if(becomeFive(player.getNum(), x, y, true)) {
    			return playerFive;
    		}
    
    		// 电脑活四
    		count1 = becomeActiveFour(computer.getNum());
    		if(count1 > 0) {
    			// 电脑冲四
    			count2 = becomeChongFour(computer.getNum());
    			if(count2 > 0) {
    				// 电脑活三
    				count3 = becomeActiveThree(computer.getNum());
    				if(count3 > 0) {
    					// 电脑眠三
    					count4 = becomeSleepThree(computer.getNum());
    					if(count4 > 0) {
    						// 电脑活二
    						count5 = becomeActiveTwo(computer.getNum());
    						if(count5 > 0) {
    							return computerActiveFour + count1 + count2 + count3 + count4 + count5;
    						}
    						return computerActiveFour + count1 + count2 + count3 + count4;
    					}
    					// 电脑活二
    					count4 = becomeActiveTwo(computer.getNum());
    					if(count4 > 0) {
    						return computerActiveFour + count1 + count2 + count3 + count4;
    					}
    					return computerActiveFour + count1 + count2 + count3;
    				}
    				// 电脑眠三
    				count3 = becomeSleepThree(computer.getNum());
    				if(count3 > 0) {
    					// 电脑活二
    					count5 = becomeActiveTwo(computer.getNum());
    					if(count5 > 0) {
    						return computerActiveFour + count1 + count2 + count3 + count4 + count5;
    					}
    					return computerActiveFour + count1 + count2 + count3;
    				}
    				// 电脑活二
    				count3 = becomeActiveTwo(computer.getNum());
    				if(count3 > 0) {
    					return computerActiveFour + count1 + count2 + count3;
    				}
    				return computerActiveFour + count1 + count2;
    			}
    			// 电脑活三
    			count2 = becomeActiveThree(computer.getNum());
    			if(count2 > 0) {
    				// 电脑眠三
    				count3 = becomeSleepThree(computer.getNum());
    				if(count3 > 0) {
    					// 电脑活二
    					count4 = becomeActiveTwo(computer.getNum());
    					if(count4 > 0) {
    						return computerActiveFour + count1 + count2 + count3 + count4;
    					}
    					return computerActiveFour + count1 + count2 + count3;
    				}
    				// 电脑活二
    				count3 = becomeActiveTwo(computer.getNum());
    				if(count3 > 0) {
    					return computerActiveFour + count1 + count2 + count3;
    				}
    				return computerActiveFour + count1 + count2;
    			}
    			// 电脑眠三
    			count2 = becomeSleepThree(computer.getNum());
    			if(count2 > 0) {
    				// 电脑活二
    				count3 = becomeActiveTwo(computer.getNum());
    				if(count3 > 0) {
    					return computerActiveFour + count1 + count2 + count3;
    				}
    				return computerActiveFour + count1 + count2;
    			}
    			// 电脑活二
    			count2 = becomeActiveTwo(computer.getNum());
    			if(count2 > 0) {
    				return computerActiveFour + count1 + count2;
    			}
    			return computerActiveFour + count1;
    		}
    
    		// 电脑冲四
    		count1 = becomeChongFour(computer.getNum());
    		if(count1 > 0) {
    			// 电脑活三
    			count2 = becomeActiveThree(computer.getNum());
    			if(count2 > 0) {
    				// 电脑眠三
    				count3 = becomeSleepThree(computer.getNum());
    				if(count3 > 0) {
    					// 电脑活二
    					count4 = becomeActiveTwo(computer.getNum());
    					if(count4 > 0) {
    						return computerChongFour + count1 + count2 + count3 + count4;
    					}
    					return computerChongFour + count1 + count2 + count3;
    				}
    				// 电脑活二
    				count3 = becomeActiveTwo(computer.getNum());
    				if(count3 > 0) {
    					return computerChongFour + count1 + count2 + count3;
    				}
    				return computerChongFour + count1 + count2;
    			}
    			// 电脑眠三
    			count2 = becomeSleepThree(computer.getNum());
    			if(count2 > 0) {
    				// 电脑活二
    				count3 = becomeActiveTwo(computer.getNum());
    				if(count3 > 0) {
    					return computerChongFour + count1 + count2 + count3;
    				}
    				return computerChongFour + count1 + count2;
    			}
    			// 电脑活二
    			count2 = becomeActiveTwo(computer.getNum());
    			if(count2 > 0) {
    				return computerChongFour + count1 + count2;
    			}
    			return computerChongFour + count1;
    		}
    
    		// 玩家活四
    		count1 = becomeActiveFour(player.getNum());
    		if(count1 > 0) {
    			// 玩家冲四
    			count2 = becomeChongFour(player.getNum());
    			if(count2 > 0) {
    				// 玩家活三
    				count3 = becomeActiveThree(player.getNum());
    				if(count3 > 0) {
    					// 玩家眠三
    					count4 = becomeSleepThree(player.getNum());
    					if(count4 > 0) {
    						// 玩家活二
    						count5 = becomeActiveTwo(player.getNum());
    						if(count5 > 0) {
    							return playerActiveFour + count1 + count2 + count3 + count4 + count5;
    						}
    						return playerActiveFour + count1 + count2 + count3 + count4;
    					}
    					// 玩家活二
    					count4 = becomeActiveTwo(player.getNum());
    					if(count4 > 0) {
    						return playerActiveFour + count1 + count2 + count3 + count4;
    					}
    					return playerActiveFour + count1 + count2 + count3;
    				}
    				// 玩家眠三
    				count3 = becomeSleepThree(player.getNum());
    				if(count3 > 0) {
    					// 玩家活二
    					count5 = becomeActiveTwo(player.getNum());
    					if(count5 > 0) {
    						return playerActiveFour + count1 + count2 + count3 + count4 + count5;
    					}
    					return playerActiveFour + count1 + count2 + count3;
    				}
    				// 玩家活二
    				count3 = becomeActiveTwo(player.getNum());
    				if(count3 > 0) {
    					return playerActiveFour + count1 + count2 + count3;
    				}
    				return playerActiveFour + count1 + count2;
    			}
    			// 玩家活三
    			count2 = becomeActiveThree(player.getNum());
    			if(count2 > 0) {
    				// 玩家眠三
    				count3 = becomeSleepThree(player.getNum());
    				if(count3 > 0) {
    					// 玩家活二
    					count4 = becomeActiveTwo(player.getNum());
    					if(count4 > 0) {
    						return playerActiveFour + count1 + count2 + count3 + count4;
    					}
    					return playerActiveFour + count1 + count2 + count3;
    				}
    				// 玩家活二
    				count3 = becomeActiveTwo(player.getNum());
    				if(count3 > 0) {
    					return playerActiveFour + count1 + count2 + count3;
    				}
    				return playerActiveFour + count1 + count2;
    			}
    			// 玩家眠三
    			count2 = becomeSleepThree(player.getNum());
    			if(count2 > 0) {
    				// 玩家活二
    				count3 = becomeActiveTwo(player.getNum());
    				if(count3 > 0) {
    					return playerActiveFour + count1 + count2 + count3;
    				}
    				return playerActiveFour + count1 + count2;
    			}
    			// 玩家活二
    			count2 = becomeActiveTwo(player.getNum());
    			if(count2 > 0) {
    				return playerActiveFour + count1 + count2;
    			}
    			return playerActiveFour + count1;
    		}
    
    		// 玩家冲四
    		count1 = becomeChongFour(player.getNum());
    		if(count1 > 0) {
    			// 玩家活三
    			count2 = becomeActiveThree(player.getNum());
    			if(count2 > 0) {
    				// 玩家眠三
    				count3 = becomeSleepThree(player.getNum());
    				if(count3 > 0) {
    					// 玩家活二
    					count4 = becomeActiveTwo(player.getNum());
    					if(count4 > 0) {
    						return playerChongFour + count1 + count2 + count3 + count4;
    					}
    					return playerChongFour + count1 + count2 + count3;
    				}
    				// 玩家活二
    				count3 = becomeActiveTwo(player.getNum());
    				if(count3 > 0) {
    					return playerChongFour + count1 + count2 + count3;
    				}
    				return playerChongFour + count1 + count2;
    			}
    			// 玩家眠三
    			count2 = becomeSleepThree(player.getNum());
    			if(count2 > 0) {
    				// 电脑活二
    				count3 = becomeActiveTwo(player.getNum());
    				if(count3 > 0) {
    					return playerChongFour + count1 + count2 + count3;
    				}
    				return playerChongFour + count1 + count2;
    			}
    			// 玩家活二
    			count2 = becomeActiveTwo(player.getNum());
    			if(count2 > 0) {
    				return playerChongFour + count1 + count2;
    			}
    			return playerChongFour + count1;
    		}
    
    		// 电脑活三
    		count1 = becomeActiveThree(computer.getNum());
    		if(count1 > 0) {
    			// 电脑眠三
    			count2 = becomeSleepThree(computer.getNum());
    			if(count2 > 0) {
    				// 电脑活二
    				count3 = becomeActiveTwo(computer.getNum());
    				if(count3 > 0) {
    					return computerActiveThree + count1 + count2 + count3;
    				}
    				return computerActiveThree + count1 + count2;
    			}
    			// 电脑活二
    			count2 = becomeActiveTwo(computer.getNum());
    			if(count2 > 0) {
    				return computerActiveThree + count1 + count2;
    			}
    			return computerActiveThree + count1;
    		}
    
    		// 玩家活三
    		count1 = becomeActiveThree(player.getNum());
    		if(count1 > 0) {
    			// 玩家眠三
    			count2 = becomeSleepThree(player.getNum());
    			if(count2 > 0) {
    				// 玩家活二
    				count3 = becomeActiveTwo(player.getNum());
    				if(count3 > 0) {
    					return playerActiveThree + count1 + count2 + count3;
    				}
    				return playerActiveThree + count1 + count2;
    			}
    			// 玩家活二
    			count2 = becomeActiveTwo(player.getNum());
    			if(count2 > 0) {
    				return playerActiveThree + count1 + count2;
    			}
    			return playerActiveThree + count1;
    		}
    
    		// 电脑眠三
    		count1 = becomeSleepThree(computer.getNum());
    		if(count1 > 0) {
    			// 电脑活二
    			count2 = becomeActiveTwo(computer.getNum());
    			if(count2 > 0) {
    				return computerSleepThree + count1 + count2;
    			}
    			return computerSleepThree + count1;
    		}
    
    		// 玩家眠三
    		count1 = becomeSleepThree(player.getNum());
    		if(count1 > 0) {
    			// 电脑活二
    			count2 = becomeActiveTwo(player.getNum());
    			if(count2 > 0) {
    				return playerSleepThree + count1 + count2;
    			}
    			return playerSleepThree + count1;
    		}
    
    		// 玩家多个活二
    		count1 = becomeActiveTwo(player.getNum());
    		if(count1 > 1) {
    			return playerManyActiveTwo + count1;
    		}
    
    		// 电脑活二
    		count1 = becomeActiveTwo(computer.getNum());
    		if(count1 > 0) {
    			return computerActiveTwo + count1;
    		}
    
    		// 如果玩家有棋子
    		if(hasAround(player.getNum())) {
    			return hasAround;
    		}
    		return 0;
    	}
    
    	// 判断周围是否有对方的棋子
    	private boolean hasAround(int user) {
    		// 所有周围的坐标
    		R[] surrounding = new R[] {
    			new R(x - 1, y),
    			new R(x + 1, y),
    			new R(x, y - 1),
    			new R(x, y + 1),
    			new R(x - 1, y - 1),
    			new R(x - 1, y + 1),
    			new R(x + 1, y - 1),
    			new R(x + 1, y + 1)
    		};
    		// 遍历这些坐标
    		for(R surround: surrounding) {
    			// 如果坐标存在
    			if(piecesIsExists(user, surround)) {
    				return true;
    			}
    		}
    		return false;
    	}
    
    	// 成为活二
    	private int becomeActiveTwo(int user) {
    		factory.setCheckBox(checkBox);
    		factory.set(user, x, y);
    		factory.addRegular(new Integer[]{-2}, new Integer[]{-3, -1, +1});
    		factory.addRegular(new Integer[]{-1}, new Integer[]{-3, -2, +1});
    		factory.addRegular(new Integer[]{-1}, new Integer[]{-2, +1, +2});
    		factory.addRegular(new Integer[]{+1}, new Integer[]{-2, -1, +2});
    		factory.addRegular(new Integer[]{+1}, new Integer[]{-1, +2, +3});
    		factory.addRegular(new Integer[]{+2}, new Integer[]{-1, +1, +3});
    		return getCount();
    	}
    
    	// 成为眠三
    	private int becomeSleepThree(int user) {
    		factory.setCheckBox(checkBox);
    		factory.set(user, x, y);
    		factory.addRegular(new Integer[]{-1, -2}, new Integer[]{-3, -4}, +1);
    		factory.addRegular(new Integer[]{+1, -1}, new Integer[]{-2, -3}, +2);
    		factory.addRegular(new Integer[]{+1, +2}, new Integer[]{-1, -2}, +3);
    		factory.addRegular(new Integer[]{-2, -3}, new Integer[]{-1, -4}, +1);
    		factory.addRegular(new Integer[]{-1, -3}, new Integer[]{-2, -4}, +1);
    		factory.addRegular(new Integer[]{-2, +1}, new Integer[]{-1, -3}, +2);
    		factory.addRegular(new Integer[]{-1, +2}, new Integer[]{-2, +1}, +3);
    		factory.addRegular(new Integer[]{-2, -3}, new Integer[]{-1, -4}, -4);
    		factory.addRegular(new Integer[]{-1, -3}, new Integer[]{-2, -4}, -4);
    		factory.addRegular(new Integer[]{-2, +1}, new Integer[]{-1, -3}, -3);
    		factory.addRegular(new Integer[]{-1, +2}, new Integer[]{-2, +1}, -2);
    		return getCount();
    	}
    
    	// 成为活三
    	private int becomeActiveThree(int user) {
    		factory.set(user, x, y);
    		factory.addRegular(new Integer[]{-2, -1}, new Integer[]{-3, +1});
    		factory.addRegular(new Integer[]{-1, +1}, new Integer[]{-2, +2});
    		factory.addRegular(new Integer[]{+1, +2}, new Integer[]{-1, +3});
    		factory.addRegular(new Integer[]{-3, -2}, new Integer[]{-4, -1, +1});
    		factory.addRegular(new Integer[]{-3, -1}, new Integer[]{-4, -2, +1});
    		factory.addRegular(new Integer[]{+3, +2}, new Integer[]{+4, +1, -1});
    		factory.addRegular(new Integer[]{+3, +1}, new Integer[]{+4, +2, -1});
    		factory.addRegular(new Integer[]{-2, +1}, new Integer[]{-3, +2, -1});
    		factory.addRegular(new Integer[]{-1, +2}, new Integer[]{-2, +1, +3});
    
    		List<Boolean> rowResult = factory.getRowResult();
    		List<Boolean> columnResult = factory.getColumnResult();
    		List<Boolean> leftResult = factory.getLeftResult();
    		List<Boolean> rightResult = factory.getRightResult();
    		// 统计数量
    		int count = 0;
    //		count += hasTrue(rowResult);
    //		count += hasTrue(columnResult);
    //		count += hasTrue(leftResult);
    //		count += hasTrue(rightResult);
    		count += rowResult.get(0) || rowResult.get(1) || rowResult.get(2) ? 3 : (rowResult.get(3) || rowResult.get(4) || rowResult.get(5) || rowResult.get(6) || rowResult.get(7) || rowResult.get(8) ? 2 : 0);
    		count += columnResult.get(0) || columnResult.get(1) || columnResult.get(2) ? 3 : (columnResult.get(3) || columnResult.get(4) || columnResult.get(5) || columnResult.get(6) || columnResult.get(7) || columnResult.get(8) ? 2 : 0);
    		count += leftResult.get(0) || leftResult.get(1) || leftResult.get(2) ? 3 : (leftResult.get(3) || leftResult.get(4) || leftResult.get(5) || leftResult.get(6) || leftResult.get(7) || leftResult.get(8) ? 2 : 0);
    		count += rightResult.get(0) || rightResult.get(1) || rightResult.get(2) ? 3 : (rightResult.get(3) || rightResult.get(4) || rightResult.get(5) || rightResult.get(6) || rightResult.get(7) || rightResult.get(8) ? 2 : 0);
    		return count;
    	}
    
    	// 判断电脑是否胜利
    	private R computerIsWin() {
    		for(int i=0;i<15;i++) {
    			for(int j=0;j<15;j++) {
    				if(checkBox[i][j] == 0 && becomeFive(computer.getNum(), i, j, true)) {
    					return new R("电脑获胜", i, j);
    				}
    			}
    		}
    		return null;
    	}
    
    	// 判断玩家是否胜利
    	private R playerIsWin() {
    		for(int i=0;i<15;i++) {
    			for(int j=0;j<15;j++) {
    				if(checkBox[i][j] == player.getNum() && becomeFive(player.getNum(), i, j, false)) {
    					return new R("玩家获胜", i, j);
    				}
    			}
    		}
    		return null;
    	}
    
    	// 成为冲四
    	private int becomeChongFour(int user) {
    		factory.set(user, x, y);
    		factory.addRegular(new Integer[]{-3, -2, -1}, new Integer[]{-4}, +1);
    		factory.addRegular(new Integer[]{-3, -2, -1}, new Integer[]{+1}, -4);
    		factory.addRegular(new Integer[]{-2, -1, +1}, new Integer[]{-3}, +2);
    		factory.addRegular(new Integer[]{-2, -1, +1}, new Integer[]{+2}, -3);
    		factory.addRegular(new Integer[]{-1, +1, +2}, new Integer[]{-2}, +3);
    		factory.addRegular(new Integer[]{-1, +1, +2}, new Integer[]{+3}, -2);
    		factory.addRegular(new Integer[]{+1, +2, +3}, new Integer[]{-1}, +4);
    		factory.addRegular(new Integer[]{+1, +2, +3}, new Integer[]{+4}, -1);
    		return getCount();
    	}
    
    	// 成为活四的数量
    	private int becomeActiveFour(int user) {
    		factory.setCheckBox(checkBox);
    		factory.set(user, x, y);
    		factory.addRegular(new Integer[]{-3, -2, -1}, new Integer[]{-4, +1});
    		factory.addRegular(new Integer[]{-2, -1, +1}, new Integer[]{-3, +2});
    		factory.addRegular(new Integer[]{-1, +1, +2}, new Integer[]{-2, +3});
    		factory.addRegular(new Integer[]{+1, +2, +3}, new Integer[]{-1, +4});
    
    		int count = 0;
    		count += hasTrue(factory.getRowResult());
    		count += hasTrue(factory.getColumnResult());
    		count += hasTrue(factory.getLeftResult());
    		count += hasTrue(factory.getRightResult());
    		return count;
    	}
    
    	/**
    	 * 成为五子或更多
    	 * @param scoring true:用来打分,false:用来判断输赢
    	 */
    	private boolean becomeFive(int user, int x, int y, boolean scoring) {
    		// 假设棋子已下
    		if(scoring) {
    			checkBox[x][y] = user;
    		}
    
    		// 横向判断
    		for(int i=0;i<=y;i++) {
    			for(int j=14;j>=y;j--) {
    				List<R> rList = new ArrayList<>();
    				for(int k=i;k<=j;k++) {
    					rList.add(new R(x, k));
    				}
    				boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
    				if(has && rList.size() >= 5) {
    					if(scoring) {
    						checkBox[x][y] = 0;
    					}
    					return true;
    				}
    			}
    		}
    
    		// 竖向判断
    		for(int i=0;i<=x;i++) {
    			for(int j=14;j>=x;j--) {
    				List<R> rList = new ArrayList<>();
    				for(int k=i;k<=j;k++) {
    					rList.add(new R(k, y));
    				}
    				boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
    				if(has && rList.size() >= 5) {
    					if(scoring) {
    						checkBox[x][y] = 0;
    					}
    					return true;
    				}
    			}
    		}
    
    		// 右斜判断
    		{
    			// 取出起始点的坐标
    			R start = new R(x, y);
    			while(start.getX() != 0 && start.getY() != 0) {
    				start.increase(-1, -1);
    			}
    			// 取出终点坐标
    			R stop = new R(x, y);
    			while(stop.getX() != 14 && stop.getY() != 14) {
    				stop.increase(1, 1);
    			}
    			// 开始判断
    			for(int i1=start.getX(),i2=start.getY();i1<=x;i1++,i2++) {
    				for(int j1=stop.getX(),j2=stop.getY();j1>=x;j1--,j2--) {
    					List<R> rList = new ArrayList<>();
    					for(int k1=i1,k2=i2;k1<=j1;k1++,k2++) {
    						rList.add(new R(k1, k2));
    					}
    					boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
    					//System.out.println(new R(i1, i2) + "-" + new R(j1, j2) + ": " + has);
    					if(has && rList.size() >= 5) {
    						if(scoring) {
    							checkBox[x][y] = 0;
    						}
    						return true;
    					}
    				}
    			}
    		}
    
    		// 左斜判断
    		{
    			// 取出起点的坐标
    			R start = new R(x, y);
    			while(start.getX() != 0 && start.getY() != 14) {
    				start.increase(-1, 1);
    			}
    			// 取出终点坐标
    			R stop = new R(x, y);
    			while(stop.getX() != 14 && stop.getY() != 0) {
    				stop.increase(1, -1);
    			}
    			for(int i1=start.getX(),i2=start.getY();i1<=x;i1++,i2--) {
    				for(int j1=stop.getX(),j2=stop.getY();j1>=x;j1--,j2++) {
    					List<R> rList = new ArrayList<>();
    					for(int k1=i1,k2=i2;k1<=j1;k1++,k2--) {
    						rList.add(new R(k1, k2));
    					}
    					boolean has = piecesIsExists(user, rList.toArray(new R[]{}));
    					//System.out.println(new R(i1, i2) + "-" + new R(j1, j2) + ": " + has);
    					if(has && rList.size() >= 5) {
    						if(scoring) {
    							checkBox[x][y] = 0;
    						}
    						return true;
    					}
    				}
    			}
    		}
    
    		if(scoring) {
    			checkBox[x][y] = 0;
    		}
    		return false;
    	}
    
    	// 输出
    	private void output() {
    		// 输出棋盘
    		for(int i=0;i<15;i++) {
    			System.out.print("{");
    			for(int j=0;j<15;j++) {
    				System.out.print(checkBox[j][i]);
    				if(j != 14) {
    					System.out.print(", ");
    				}
    			}
    			System.out.println("},");
    		}
    		System.out.println();
    
    		// 输出坐标
    		for(int i=0;i<15;i++) {
    			for (int j = 0; j < 15; j++) {
    				System.out.print(scores[j][i] + "\t");
    			}
    			System.out.println("\n");
    		}
    	}
    
    	/**
    	 * 是否有true
    	 * @param resultList 要统计的集合
    	 * @return 如果有true返回1,如果没有则返回0
    	 */
    	private int hasTrue(List<Boolean> resultList) {
    		for(Boolean b: resultList) {
    			if(b) {
    				return 1;
    			}
    		}
    		return 0;
    	}
    
    	/**
    	 * 统计数量
    	 * @return
    	 */
    	private int getCount() {
    		int count = 0;
    		count += hasTrue(factory.getRowResult());
    		count += hasTrue(factory.getColumnResult());
    		count += hasTrue(factory.getLeftResult());
    		count += hasTrue(factory.getRightResult());
    		return count;
    	}
    
    	/**
    	 * 判断这些棋子是否都存在
    	 * @param user 用户,用来比较
    	 * @param pieces 棋子的坐标
    	 * @return 是否都存在
    	 */
    	private boolean piecesIsExists(int user, R... pieces) {
    		for(R piece: pieces) {
    			// 如果是边缘或者越界
    			if(piece.getX() < 0 || piece.getX() > 14 || piece.getY() < 0 || piece.getY() > 14) {
    				return false;
    			}
    			// 如果和用户的棋子不一致
    			if(!checkBox[piece.getX()][piece.getY()].equals(user)) {
    				return false;
    			}
    		}
    		return true;
    	}
    }
    
    class RegularFactory {
    	private Integer[][] checkBox;
    	private int user, x, y;
    
    	// 横向判断
    	private List<R[]> rowsIsExists = new ArrayList<>();
    	private List<R[]> rowsIsEmpty = new ArrayList<>();
    	private List<R> rowsIsPlayerOrBounds = new ArrayList<>();
    
    	// 竖向判断
    	private List<R[]> columnsIsExists = new ArrayList<>();
    	private List<R[]> columnsIsEmpty = new ArrayList<>();
    	private List<R> columnsIsPlayerOrBounds = new ArrayList<>();
    
    	// 左斜判断
    	private List<R[]> leftIsExists = new ArrayList<>();
    	private List<R[]> leftIsEmpty = new ArrayList<>();
    	private List<R> leftIsPlayerOrBounds = new ArrayList<>();
    
    	// 右斜判断
    	private List<R[]> rightIsExists = new ArrayList<>();
    	private List<R[]> rightIsEmpty = new ArrayList<>();
    	private List<R> rightIsPlayOrBounds = new ArrayList<>();
    
    	// 初始化工厂
    	public RegularFactory() {
    	}
    	public RegularFactory(Integer[][] checkBox, int user, int x, int y) {
    		this.checkBox = checkBox;
    		this.user = user;
    		this.x = x;
    		this.y = y;
    	}
    	// 设置棋盘
    	public void setCheckBox(Integer[][] checkBox) {
    		this.checkBox = checkBox;
    	}
    	// 设置坐标
    	public void set(int user, int x, int y) {
    		this.user = user;
    		this.x = x;
    		this.y = y;
    		clearList();
    	}
    
    	// 清除所有集合
    	public void clearList() {
    		rowsIsExists.clear();
    		columnsIsExists.clear();
    		leftIsExists.clear();
    		rightIsExists.clear();
    
    		rowsIsEmpty.clear();
    		columnsIsEmpty.clear();
    		leftIsEmpty.clear();
    		rightIsEmpty.clear();
    
    		rowsIsPlayerOrBounds.clear();
    		columnsIsPlayerOrBounds.clear();
    		leftIsPlayerOrBounds.clear();
    		rightIsPlayOrBounds.clear();
    	}
    
    	// 添加规则
    	public void addRegular(Integer[] yExists, Integer[] yEmpty) {
    		R[] rowsExists = new R[yExists.length];
    		R[] rowsEmpty = new R[yEmpty.length];
    		R[] columnsExists = new R[yExists.length];
    		R[] columnsEmpty = new R[yEmpty.length];
    		R[] leftExists = new R[yExists.length];
    		R[] leftEmpty = new R[yEmpty.length];
    		R[] rightExists = new R[yExists.length];
    		R[] rightEmpty = new R[yEmpty.length];
    
    		for(int i=0;i<yExists.length;i++) {
    			rowsExists[i] = new R(x, y + yExists[i]);
    			columnsExists[i] = new R(x + yExists[i], y);
    			leftExists[i] = new R(x + yExists[i], y - yExists[i]);
    			rightExists[i] = new R(x + yExists[i], y + yExists[i]);
    		}
    		rowsIsExists.add(rowsExists);
    		columnsIsExists.add(columnsExists);
    		leftIsExists.add(leftExists);
    		rightIsExists.add(rightExists);
    
    		for(int i=0;i<yEmpty.length;i++) {
    			rowsEmpty[i] = new R(x, y + yEmpty[i]);
    			columnsEmpty[i] = new R(x + yEmpty[i], y);
    			leftEmpty[i] = new R(x + yEmpty[i], y - yEmpty[i]);
    			rightEmpty[i] = new R(x + yEmpty[i], y + yEmpty[i]);
    		}
    		rowsIsEmpty.add(rowsEmpty);
    		columnsIsEmpty.add(columnsEmpty);
    		leftIsEmpty.add(leftEmpty);
    		rightIsEmpty.add(rightEmpty);
    	}
    
    	// 添加规则
    	public void addRegular(Integer[] yExists, Integer[] yEmpty, Integer yPlayerOrBounds) {
    		addRegular(yExists, yEmpty);
    		addRegular(yPlayerOrBounds);
    
    		// 相反方向再添加一次规则
    		for(int i=0;i<yExists.length;i++) {
    			yExists[i] = -yExists[i];
    		}
    		for(int i=0;i<yEmpty.length;i++) {
    			yEmpty[i] = -yEmpty[i];
    		}
    		yPlayerOrBounds = -yPlayerOrBounds;
    
    		// 第二次添加规则
    		addRegular(yExists, yEmpty);
    		addRegular(yPlayerOrBounds);
    	}
    
    	// 只添加玩家或边界规则
    	private void addRegular(Integer yPlayerOrBounds) {
    		rowsIsPlayerOrBounds.add(new R(x, y + yPlayerOrBounds));
    		columnsIsPlayerOrBounds.add(new R(x + yPlayerOrBounds, y));
    		leftIsPlayerOrBounds.add(new R(x + yPlayerOrBounds, y - yPlayerOrBounds));
    		rightIsPlayOrBounds.add(new R(x + yPlayerOrBounds, y + yPlayerOrBounds));
    	}
    
    	// 获取行结果
    	public List<Boolean> getRowResult() {
    		List<Boolean> rowResult = new ArrayList<>();
    		for(int i=0;i<rowsIsExists.size();i++) {
    			boolean row;
    			if(!rowsIsPlayerOrBounds.isEmpty()) {
    				row = piecesIsExists(user, rowsIsExists.get(i)) && piecesIsEmpty(rowsIsEmpty.get(i)) && pieceIsPlayerOrBounds(user, rowsIsPlayerOrBounds.get(i));
    			} else {
    				row = piecesIsExists(user, rowsIsExists.get(i)) && piecesIsEmpty(rowsIsEmpty.get(i));
    			}
    			rowResult.add(row);
    		}
    		return rowResult;
    	}
    
    	// 获取列结果
    	public List<Boolean> getColumnResult() {
    		List<Boolean> columnResult = new ArrayList<>();
    		for(int i=0;i<columnsIsExists.size();i++) {
    			boolean column;
    			if(!columnsIsPlayerOrBounds.isEmpty()) {
    				column = piecesIsExists(user, columnsIsExists.get(i)) && piecesIsEmpty(columnsIsEmpty.get(i)) && pieceIsPlayerOrBounds(user, columnsIsPlayerOrBounds.get(i));
    			} else {
    				column = piecesIsExists(user, columnsIsExists.get(i)) && piecesIsEmpty(columnsIsEmpty.get(i));
    			}
    			columnResult.add(column);
    		}
    		return columnResult;
    	}
    
    	// 获取左斜结果
    	public List<Boolean> getLeftResult() {
    		List<Boolean> leftResult = new ArrayList<>();
    		for(int i=0;i<leftIsExists.size();i++) {
    			boolean left;
    			if(!leftIsPlayerOrBounds.isEmpty()) {
    				left = piecesIsExists(user, leftIsExists.get(i)) && piecesIsEmpty(leftIsEmpty.get(i)) && pieceIsPlayerOrBounds(user, leftIsPlayerOrBounds.get(i));
    			} else {
    				left = piecesIsExists(user, leftIsExists.get(i)) && piecesIsEmpty(leftIsEmpty.get(i));
    			}
    			leftResult.add(left);
    		}
    		return leftResult;
    	}
    
    	// 获取右斜结果
    	public List<Boolean> getRightResult() {
    		List<Boolean> rightResult = new ArrayList<>();
    		for(int i=0;i<rightIsExists.size();i++) {
    			boolean right;
    			if(!rightIsPlayOrBounds.isEmpty()) {
    				right = piecesIsExists(user, rightIsExists.get(i)) && piecesIsEmpty(rightIsEmpty.get(i)) && pieceIsPlayerOrBounds(user, rightIsPlayOrBounds.get(i));
    			} else {
    				right = piecesIsExists(user, rightIsExists.get(i)) && piecesIsEmpty(rightIsEmpty.get(i));
    			}
    			rightResult.add(right);
    		}
    		return rightResult;
    	}
    
    	/**
    	 * 判断这些棋子是否都存在
    	 * @param user 用户,用来比较
    	 * @param pieces 棋子的坐标
    	 * @return 是否都存在
    	 */
    	private boolean piecesIsExists(int user, R... pieces) {
    		for(R piece: pieces) {
    			// 如果是边缘或者越界
    			if(piece.getX() < 0 || piece.getX() > 14 || piece.getY() < 0 || piece.getY() > 14) {
    				return false;
    			}
    			// 如果和用户的棋子不一致
    			if(!checkBox[piece.getX()][piece.getY()].equals(user)) {
    				return false;
    			}
    		}
    		return true;
    	}
    
    	/**
    	 * 判断这些棋子是否都为空
    	 * @param pieces 棋子的坐标
    	 * @return 是否都为空
    	 */
    	private boolean piecesIsEmpty(R... pieces) {
    		for(R piece: pieces) {
    			// 如果是边缘或者越界
    			if(piece.getX() < 0 || piece.getX() > 14 || piece.getY() < 0 || piece.getY() > 14) {
    				return false;
    			}
    			// 如果不为空
    			if(!checkBox[piece.getX()][piece.getY()].equals(0)) {
    				return false;
    			}
    		}
    		return true;
    	}
    
    
    	/**
    	 * 判断棋子是否是对方或者边界
    	 * @param piece 棋子的坐标
    	 * @retur 结果
    	 */
    	private boolean pieceIsPlayerOrBounds(int user, R piece) {
    		int contraryUser = user == 1 ? 2 : 1; // 对手的颜色
    		// 如果越界
    		if(piece.getX() < 0 || piece.getX() > 14 || piece.getY() < 0 || piece.getY() > 14) {
    			return true;
    		}
    		// 如果是玩家
    		return checkBox[piece.getX()][piece.getY()].equals(contraryUser);
    	}
    }
    
    //机器人返回的数据
    class R {
    	private String state = "返回成功";
    	private Integer x;
    	private Integer y;
    
    	public R() {
    	}
    
    	public R(String state, Integer x, Integer y) {
    		this.state = state;
    		this.x = x;
    		this.y = y;
    	}
    
    	public R(Integer x, Integer y) {
    		this.x = x;
    		this.y = y;
    	}
    
    	public String getState() {
    		return state;
    	}
    
    	public void setState(String state) {
    		this.state = state;
    	}
    
    	public Integer getX() {
    		return x;
    	}
    
    	public void setX(Integer x) {
    		this.x = x;
    	}
    
    	public Integer getY() {
    		return y;
    	}
    
    	public void setY(Integer y) {
    		this.y = y;
    	}
    
    	// 增加
    	public void increase(int x, int y) {
    		if(this.x != null) {
    			this.x += x;
    		}
    		if(this.y != null) {
    			this.y += y;
    		}
    	}
    
    	@Override
    	public String toString() {
    		return "R{" +
    			"x=" + x +
    			", y=" + y +
    			'}';
    	}
    }

     

    展开全文
  • 本人用贪心做的五子棋算法...呵呵... 很简单的初级五子棋..... 下不过的要自己打PP了哦....吼吼.... 算法啊...好要进一步研究啊..本以为用贪心写的不会差到哪去...哪知道......有空的研究研究五子棋的...要有java平台地...
  • JAVA五子棋人机对战

    2008-10-26 23:30:50
    这个可是好个东西给大家看看,也是给网络上的一点点小贡献,大家如果有更好的资源的话,也可以跟我说.我的QQ:66472309
  • 本人写的一个五子棋游戏,主要实现人机对战,感兴趣的开发人员可以看看,有不足的地方望多多指教。
  • Java制作五子棋人机对战

    千次阅读 2020-07-20 22:38:58
    人机对战 游戏思路 五子棋相比其他棋牌游戏比较简单 我们判断胜负只需要在玩家/人机下棋处循环遍历 上下是不是连起来到了指定点数 左右... 左斜 右斜 源码 github:https://github.com/1711680493/Applica..

    目录

     

    简介

    游戏思路

    源码

    效果视频

    代码片段


    简介

    Hello,I'm Shendi

    继上次写完象棋后又花了一天时间写了个五子棋,比较简单

    拥有大概如下功能

    1. 五子棋的基本功能
    2. 棋盘大小的自由设置
    3. 胜利棋数的自由设置
    4. 人机对战

    游戏思路

    五子棋相比其他棋牌游戏比较简单

    我们判断胜负只需要在玩家/人机下棋处循环遍历

    • 上下是不是连起来到了指定点数
    • 左右...
    • 左斜
    • 右斜

    源码

    github: https://github.com/1711680493/Application#gobang


    效果视频

    Java制作五子棋/连珠棋+简单AI


    代码片段

    棋盘,棋子绘制代码

    /**
     * 重新绘制棋盘.
     * @author Shendi <a href='tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=1711680493'>QQ</a>
     * @param chessBoardNum 棋盘大小
     */
    public void repaint(int chessBoardNum) {
    	BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    	Graphics g = img.getGraphics();
    	
    	size = 380 / chessBoardNum;
    	first = size >> 1;
    	
    	// 棋子大小初始化
    	Game.getGame().getChesses().forEach((chess) -> chess.setSize(size,size));
    	
    	// 将棋盘上所有位置初始化
    	positions.clear();
    	for (int i = 0;i < chessBoardNum;i++) {
    		positions.put(i, first + size * i);
    	}
    	
    	// 绘制棋盘
    	for (int i = 1;i < chessBoardNum;i++) {
    		// 横线
    		g.drawLine(size, size * i, 380, size * i);
    		// 竖线
    		g.drawLine(size * i, size, size * i, 380);
    	}
    	g.drawLine(size, 380 , 380, 380);
    	g.drawLine(380, size, 380, 380);
    	g.dispose();
    	background.setIcon(new ImageIcon(img));
    	invalidate();
    	repaint();
    	
    	// 白色黑色棋子图
    	int imgPos = (size - size / 2) >> 1;
    	int imgSize = size >> 1;
    	
    	BufferedImage whiteChessImg = new BufferedImage(size,size,BufferedImage.TYPE_INT_ARGB);
    	g = whiteChessImg.getGraphics();
    	g.setColor(Color.WHITE);
    	g.fillRoundRect(imgPos, imgPos, imgSize, imgSize, 180, 180);
    	g.dispose();
    	
    	BufferedImage blackChessImg = new BufferedImage(size,size,BufferedImage.TYPE_INT_ARGB);
    	g = blackChessImg.getGraphics();
    	g.setColor(Color.BLACK);
    	g.fillRoundRect(imgPos, imgPos, imgSize, imgSize, 180, 180);
    	g.dispose();
    	
    	whiteChessIcon = new ImageIcon(whiteChessImg);
    	blackChessIcon = new ImageIcon(blackChessImg);
    }

     

    判断游戏逻辑代码

     

    /**
     * 执行下棋操作.
     * @author Shendi <a href='tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=1711680493'>QQ</a>
     * @param chess 棋子
     * @param whiteBlack 白棋还是黑棋.
     */
    public void exec(JLabel chess,boolean whiteBlack) {
    	// 指定位置是否有棋子,有不做操作
    	int x = chessBoard.getScenePos(chess.getX());
    	int y = chessBoard.getScenePos(chess.getY());
    	if (scene[y][x] != 0) { return; }
    	// 下棋
    	int team = whiteBlack ? 1 : 2;
    	scene[y][x] = team;
    	
    //		for (int[] xScene : scene) System.out.println(Arrays.toString(xScene));
    	
    	if (whiteBlack) chess.setIcon(chessBoard.whiteChessIcon);
    	else chess.setIcon(chessBoard.blackChessIcon);
    	// 判断胜负,横竖斜有指定数量棋子则胜利,对方失败
    	int h = 1,v = 1,ld = 1,rd = 1;
    	
    	// 横,左右
    	int posX,posY;
    	for (int i = 1;i < chessBoardNum;i++) {
    		posX = x - i;
    		if (posX < 0) break;
    		if (scene[y][posX] == team) h++; else break;
    	}
    	for (int i = 1;i < chessBoardNum;i++) {
    		posX = x + i;
    		if (posX >= chessBoardNum) break;
    		if (scene[y][posX] == team) h++; else break;
    	}
    	
    	// 竖,上下
    	for (int i = 1;i < chessBoardNum;i++) {
    		posY = y - i;
    		if (posY < 0) break;
    		if (scene[posY][x] == team) v++; else break;
    	}
    	for (int i = 1;i < chessBoardNum;i++) {
    		posY = y + i;
    		if (posY >= chessBoardNum) break;
    		if (scene[posY][x] == team) v++; else break;
    	}
    	
    	// 左斜,上左下右 \
    	for (int i = 1;i < chessBoardNum;i++) {
    		posX = x - i;
    		posY = y - i;
    		if (posX < 0 || posY < 0) break;
    		if (scene[posY][posX] == team) ld++; else break;
    	}
    	for (int i = 1;i < chessBoardNum;i++) {
    		posX = x + i;
    		posY = y + i;
    		if (posX >=  chessBoardNum || posY >= chessBoardNum) break;
    		if (scene[posY][posX] == team) ld++; else break;
    	}
    	
    	// 右斜,上右下左 /
    	for (int i = 1;i < chessBoardNum;i++) {
    		posX = x + i;
    		posY = y - i;
    		if (posX >= chessBoardNum || posY < 0) break;
    		if (scene[posY][posX] == team) rd++; else break;
    	}
    	for (int i = 1;i < chessBoardNum;i++) {
    		posX = x - i;
    		posY = y + i;
    		if (posX < 0 || posY >= chessBoardNum) break;
    		if (scene[posY][posX] == team) rd++; else break;
    	}
    	
    	if (h >= vectoryNum || v >= vectoryNum || ld >= vectoryNum || rd >= vectoryNum) {
    		stop(whiteBlack);
    		return;
    	}
    	
    	// 当前角色下棋完成,换一边
    	currentTeam = !currentTeam;
    	if (!currentTeam) AI.getAI().play();
    }

     

    展开全文
  • java五子棋简易人机对战,含eclipse项目文件+可执行的jar(在linux上完美运行,在windows上棋子可能错位)
  • 五子棋人机对战

    2013-05-15 13:52:32
    五子棋Java人机对战源代码。包括代码和架构。
  • Java实现具有界面的人机对战五子棋【非常强悍】 使用Java开发的人机对战五子棋,具有界面效果,总代码量500多行。界面使用awt,swing简单技术,机器落子算法是评分法,对572个五元组评分,思路简单,效果很好,机器...
  • java五子棋人机

    2020-01-13 09:57:15
    初学Java,做个简单的五子棋人机对战(比较烂)锻炼一下逻辑思维,,, 暂时保存,以后完善。。。` package 五子棋; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; ...

    初学Java,做个简单的五子棋人机对战(比较烂)锻炼一下逻辑思维,,, 暂时保存,以后完善。。。`

    package 五子棋;

    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.BufferedImage;

    import javax.swing.*;
    public class 人机 extends JFrame implements MouseListener {

    int x, y, X, Y, h, s, p, l, m, n, k, b = 10, c, max = 1, x1, y1;
    int[][] chess = new int[25][25];// 1黑2白
    int[][] bai = new int[25][25];// 判断白棋下的位置
    boolean canplay = true;
    
    public 人机() {
    	this.setTitle("五子棋0.1");
    
    	this.setSize(800, 800);
    	this.setLocationRelativeTo(null);
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	this.setResizable(false);
    	this.setVisible(true);
    	this.repaint();
    	this.addMouseListener(this);
    }
    
    public void paint(Graphics g) {
    	// 创建一个图片缓冲区,先将图片放到缓冲区上
    	BufferedImage buf = new BufferedImage(800, 800, BufferedImage.TYPE_INT_RGB);
    
    	Graphics g1 = buf.createGraphics(); // 为该画布创建画笔
    	g1.setColor(Color.gray);
    	g1.fill3DRect(75, 75, 625, 625, true);// 画棋盘
    	for (int i = 0; i <= 25; i++) {
    		g1.setColor(Color.white);
    		g1.drawLine(75, 75 + 25 * i, 700, 75 + 25 * i);
    		g1.drawLine(75 + 25 * i, 75, 75 + 25 * i, 700);
    	}
    	for (int i = 0; i < 25; i++) {
    		for (int j = 0; j < 25; j++) {
    			if (chess[i][j] == 1) {
    				X = i * 25 + 50;
    				Y = j * 25 + 50;
    				g1.setColor(Color.black);
    				g1.fillOval(X, Y, 22, 22);
    			}
    			if (chess[i][j] == 2) {
    				X = i * 25 + 50;
    				Y = j * 25 + 50;
    				g1.setColor(Color.white);
    				g1.fillOval(X, Y, 22, 22);
    			}
    		}
    	}
    	g.drawImage(buf, 0, 0, this);
    
    }
    
    public void mousePressed(MouseEvent e) {
    	x = e.getX();
    	y = e.getY();
    	int k = 0, t = 0;
    	if (x >= 75 && x <= 700 && y >= 75 && y <= 700) {
    		if (canplay) {
    			x = (x - 50) / 25;
    			y = (y - 50) / 25;
    			if (chess[x][y] == 0) {
    				chess[x][y] = 1;
    				if (check() <= 5) {
    					this.repaint();
    					if (this.win()) {
    						JOptionPane.showMessageDialog(this, "黑  方  赢");
    						this.canplay = false;
    					}
    					k = 1;
    					while (chess[x][y] != 0 && canplay) {
    
    						if (h >= 3) {
    							x += k;
    
    							if (chess[x][y] == 2) {
    								while (chess[x][y] != 0 && canplay) {
    									k = -1;
    									x += k;
    								}
    							}
    						} else if (s >= 3) {
    							y += k;
    
    							if (chess[x][y] == 2) {
    								while (chess[x][y] != 0 && canplay) {
    									k = -1;
    									y += k;
    								}
    							}
    						} else if (p >= 3) {
    							x += k;
    							y -= k;
    
    							if (chess[x][y] == 2) {
    								while (chess[x][y] != 0 && canplay) {
    									k = -1;
    									x += k;
    									y -= k;
    								}
    							}
    						} else if (l >= 3) {
    							x += k;
    							y += k;
    
    							if (chess[x][y] == 2) {
    								while (chess[x][y] != 0 && canplay) {
    									k = -1;
    									x += k;
    									y += k;
    								}
    							}
    						} else {
    							for (int i = 1; i < 25; i++) {
    								for (int j = 1; j < 25; j++) {
    									if (chess[i][j] == 0)
    
    									{
    										b = 1;
    										c = 1;
    										if (i + c < 25) {
    											while (chess[i + c][j] == 2) {
    												c++;
    												b = b * 10;
    												if (i + c >= 25)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (i + c < 25) {
    											if (chess[i + c][j] == 1)
    												bai[i][j] -= 10;
    										}
    										b = 1;
    										c = 1;
    										if (i - c >= 0) {
    											while (chess[i - c][j] == 2) {
    												c++;
    												b = b * 10;
    												if (i - c < 0)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (i - c >= 0) {
    											if (chess[i - c][j] == 1)
    												bai[i][j] -= 10;
    										}
    										b = 1;
    										c = 1;
    										if (j + c < 25) {
    											while (chess[i][j + c] == 2) {
    												c++;
    
    												b = b * 10;
    												if (j + c >= 25)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (j + c < 25) {
    											if (chess[i][j + c] == 1)
    												bai[i][j] -= 10;
    										}
    										b = 1;
    										c = 1;
    										if (j - c >= 0) {
    											while (chess[i][j - c] == 2) {
    												c++;
    												b = b * 10;
    												if (j - c < 0)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (j - c >= 0) {
    											if (chess[i][j - c] == 1)
    												bai[i][j] -= 10;
    										}
    										b = 1;
    										c = 1;
    										if (i + c < 25 && j + c < 25) {
    											while (chess[i + c][j + c] == 2) {
    												c++;
    												b = b * 10;
    												if (j + c >= 25 || i + c >= 25)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (i + c < 25 && j + c < 25) {
    											if (chess[i + c][j + c] == 1)
    												bai[i][j] -= 10;
    										}
    										b = 1;
    										c = 1;
    										if (i - c >= 0 && j - c >= 0) {
    											while (chess[i - c][j - c] == 2) {
    												c++;
    												b = b * 10;
    												if (j - c < 0 || i - c < 0)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (i - c >= 0 && j - c >= 0) {
    											if (chess[i - c][j - c] == 1)
    												bai[i][j] -= 10;
    										}
    										b = 1;
    										c = 1;
    										if (i - c >= 0 && j + c < 25) {
    											while (chess[i - c][j + c] == 2) {
    												c++;
    												b = b * 10;
    												if (i - c < 0 || j + c >= 25)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (i - c >= 0 && j + c < 25) {
    											if (chess[i - c][j + c] == 1)
    												bai[i][j] -= 10;
    										}
    
    										b = 1;
    										c = 1;
    										if (i + c < 25 && j - c >= 0) {
    											while (chess[i + c][j - c] == 2) {
    												c++;
    												b = b * 10;
    												if (i + c >= 25 || j - c <= 0)
    													break;
    											}
    										}
    										bai[i][j] += b;
    										if (i + c < 25 && j - c >= 0) {
    											if (chess[i + c][j - c] == 1)
    												bai[i][j] -= 10;
    										}
    
    										if (bai[i][j] >= max) {
    											max = bai[i][j];
    
    											if (max >= 11) {
    												x = i;
    
    												y = j;
    											}
    										}
    									}
    
    								}
    							}
    
    						}
    						if (max <= 10)
    							x += 1;
    
    					}
    					if (chess[x][y] == 0) {
    						chess[x][y] = 2;
    						this.repaint();
    						if (this.win()) {
    							JOptionPane.showMessageDialog(this, "白  方  赢");
    							this.canplay = false;
    						}
    					}
    				} else
    					chess[x][y] = 0;
    			}
    		}
    	}
    
    }
    
    public boolean win() {
    	boolean la = false;
    	int po;
    	po = check();
    	if (po == 5)
    		la = true;
    	else
    		la = false;
    	return la;
    }
    
    public int check() {
    	int po = 0, a;
    	a = chess[x][y];
    	m = x;
    	n = y;
    	h = 0;
    	s = 0;
    	p = 0;
    	l = 0;
    	while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    		x++;
    		po++;
    		h++;
    	}
    	x = m;
    	po = po - 1;
    	h = h - 1;
    	while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    		x--;
    		po++;
    		h++;
    	}
    	if (po > 5) {
    		x = m;
    		h = 0;
    		JOptionPane.showMessageDialog(this, "禁手");
    
    	}
    	x = m;
    	if (po < 5) {
    		po = 0;
    		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    			y++;
    			po++;
    			s++;
    		}
    		y = n;
    		po = po - 1;
    		s = s - 1;
    		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    			y--;
    			po++;
    			s++;
    		}
    		if (po > 5) {
    			y = n;
    			s = 0;
    			JOptionPane.showMessageDialog(this, "禁手");
    
    		}
    		y = n;
    
    	}
    	if (po < 5) {
    		po = 0;
    		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    			x++;
    			y++;
    			po++;
    			l++;
    		}
    		x = m;
    		y = n;
    		po = po - 1;
    		l = l - 1;
    		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    			x--;
    			y--;
    			po++;
    			l++;
    		}
    		if (po > 5) {
    			x = m;
    			y = n;
    			l = 0;
    			JOptionPane.showMessageDialog(this, "禁手");
    
    		}
    		x = m;
    		y = n;
    	}
    	if (po < 5) {
    		po = 0;
    		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    			x++;
    			y--;
    			po++;
    			p++;
    		}
    		x = m;
    		y = n;
    		po = po - 1;
    		p = p - 1;
    		while (x >= 0 && x < 25 && y >= 0 && y < 25 && chess[x][y] == a) {
    			x--;
    			y++;
    			po++;
    			p++;
    		}
    		if (po > 5) {
    			x = m;
    			y = n;
    			p = 0;
    			JOptionPane.showMessageDialog(this, "禁手");
    
    		}
    		x = m;
    		y = n;
    	}
    	return po;
    }
    
    public void mouseClicked(MouseEvent e) {
    }
    
    public void mouseEntered(MouseEvent e) {
    }
    
    public void mouseExited(MouseEvent e) {
    }
    
    public void mouseReleased(MouseEvent e) {
    }
    
    public static void main(String[] args) {
    	new 人机();
    }
    

    }
    `
    游戏简陋,内容复杂。。。

    展开全文
  • 五子棋人机对战设计

    2017-12-05 17:31:58
    人机对战属于一种弱人工智能算法,其核心是:当玩家落下一枚棋子后,计算出这枚棋子构成的所有棋型,找出威胁程度最大的棋型,并破解其产生的威胁。

    一、人机对战算法概述

    人机对战属于一种弱人工智能算法,其核心是:当玩家落下一枚棋子后,计算出这枚棋子构成的所有棋型,找出威胁程度最大的棋型,并破解其产生的威胁。

    五子棋中所能产生的棋型有很多,如果棋子至少有一边被封死,这种棋型被称为“死”棋型,反之被称为“活”棋型。根据棋子的数量,棋型又可以细化为“活三”、“死三”、“活四”等不同的棋型。棋型的不同,产生的威胁程度不同,破解的方式也不同。以四枚棋子为例,列出所有棋型及其威胁值和破解方法,如图4.16所示。


    图4.16  四个棋子所产生的棋型及其威胁值和破解方法

    说明:棋型就是棋子在棋盘上组合成的形状。


    二、电脑自动处理用户请求

    玩家联机对战的时候,是在各自电脑中处理对方的指令。但是玩家与电脑对战,则是在一台电脑中同时处理玩家和电脑的指令,这种情况下就不能再使用联机对战的命令处理逻辑了。在AI(电脑智能对战算法)类中,编写oprationHandler()方法,来专门处理玩家向电脑发来的指令。由于本程序取消了人机对战中的“认输”、“和棋”、“悔棋”命令,所以只处理玩家的“开始”命令即可。具体代码如下。

    01	publicvoid oprationHandler(Object messageObj) {
    02		intcode = (Integer) messageObj; // 获取命令代码
    03		switch (code) {										// 判断命令
    04		case ChessPanel.OPRATION_START_MACHINE: 				// 如果是玩家请求开始游戏
    05			frame.getChessPanel1().setTowardsStart(true); 		// 设置AI的游戏开始状态为true
    06			break;
    07	}
    


    三、电脑判断落棋点

    电脑判断落棋点总共分四个步骤:第一步,遍历整个棋盘,获取每个棋子坐标;第二步,以一个棋子为中心,获取其四个直线方向排列的所有棋子;第三步,判断此直线排列的棋子中是否出现了有威胁的棋型;第四部,根据棋型库给出的棋型和破解方法,记录其威胁值和破解方案。下面将详细介绍这四个步骤是如何实现的。

    1. 遍历整个棋盘,寻找落棋点

    执行AI类中forEach()方法,此方法可以获取电脑最终给定的下棋位置。遍历整个棋盘,查看每一个棋子所能形成的棋型,找出其中威胁最大的,根据棋型库给出的破解位置找出落棋点。具体代码如下:


    01	privateint[] forEach() {
    02		intx = -1, y = -1;										// 将要下的棋子坐标
    03		intthreat = 0;											// 棋盘上出现的最大威胁值
    04		byte[][] chessmanArray = gobangModel1.getChessmanArray();	// 获得棋盘
    05		for (inti = 0; i< 15; i++) {							// 遍历棋盘行
    06			for (intj = 0; j< 15; j++) {						// 遍历棋盘列
    07				if (chessmanArray[i][j] > 0) {					// 如果此处有白棋子
    08					// 捕捉每个棋子形成的棋型
    09					inttmp[] = catchChessModle(i, j, chessmanArray);
    10					// 如果存在比当前最大威胁值还要大的威胁值,则记录此处落子坐标
    11					if (tmp[0] >threat) {
    12						threat = tmp[0];							// 更新最大威胁值
    13						x = tmp[1];								// 更新落子横坐标
    14						y = tmp[2];								// 更新落子纵坐标
    15					}
    16				}
    17			}
    18		}
    19		returnnewint[] { x, y };								// 返回横纵坐标组成的一维数组
    20	}
    

    2. 判断棋子构成的棋型

    forEach()方法中调用了catchChessModle()方法,这个方法以一枚棋子为原点向四种方位延伸,分别判断这四条线上可能产生具有威胁的棋型,并根据棋型库给出的破解位置,换算成棋盘坐标,并返回成结果数组。具体代码如下:

    01	privateint[] catchChessModle(intx, inty, byte[][] chessmanArray) {
    02		// 索引0:记录此位置的棋子可产生的最大威胁值或优势值
    03		// 索引1:对应下棋横坐标
    04		// 索引2:对应下棋横坐标
    05		intposition[] = newint[3];
    06		// 创建以被捕捉棋子为中心的四个方向形成的棋型
    07		// 以参数点为中心点,保存四个方向的棋型,方向分别为 — | \ /
    08		intmodel[][] = newint[4][11];
    09		for (inttmp[] : model) {								// 遍历数组
    10			Arrays.fill(tmp, boundary);						// 将数组填充为边界常量
    11		}
    12		// 把参数点放入每行的中心部位
    13		model[0][5] = model[1][5] = model[2][5] = model[3][5] = chessmanArray[x][y];
    14		// 以该棋子为中心,向两边走5步
    15		for (inti = 1; i<= 5; i++) {
    16			// 水平方向棋型
    17			if (x - i>= 0) {								// 如果没有走出边界
    18				model[0][5 - i] = chessmanArray[x - i][y];		// 将左侧棋子记录到水平棋型当中
    19			}
    20			if (x + i<= 14) {								// 如果没有走出边界
    21				model[0][5 + i] = chessmanArray[x + i][y];		// 将右侧棋子记录到水平棋型当中
    22			}
    23	
    24			// 垂直方向棋型
    25			if (y - i>= 0) {								// 如果没有走出边界
    26				model[1][5 - i] = chessmanArray[x][y - i];		// 将上方棋子记录到垂直棋型当中
    27			}
    28			if (y + i<= 14) {								// 如果没有走出边界
    29				model[1][5 + i] = chessmanArray[x][y + i];		// 将下方棋子记录到垂直棋型当中
    30			}
    31	
    32			// 反斜杠方向棋型
    33			if (x - i>= 0 &&y + i<= 14) {					// 如果没有走出边界
    34				// 将左下方棋子记录到反斜棋型当中
    35				model[2][5 - i] = chessmanArray[x - i][y + i];
    36			}
    37			if (x + i<= 14 &&y - i>= 0) {					// 如果没有走出边界
    38				// 将右上方棋子记录到反斜棋型当中
    39				model[2][5 + i] = chessmanArray[x + i][y - i];
    40			}
    41	
    42			// 正斜杠方向棋型
    43			if (x - i>= 0 &&y - i>= 0) {					// 如果没有走出边界
    44				// 将左上方棋子记录到正斜棋型当中
    45				model[3][5 - i] = chessmanArray[x - i][y - i];
    46			}
    47			if (x + i<= 14 &&y + i<= 14) {					// 如果没有走出边界
    48				// 将右下方棋子记录到正斜棋型当中
    49				model[3][5 + i] = chessmanArray[x + i][y + i];
    50			}
    51		}
    52		intscore = 0;						// 记录最大评分(威胁值)
    53		intdirection = -1;					// 记录最大评分的方向(model数组一维下标)
    54		intindex = 0;						// 记录坐标偏移量(judgeModle()方法给予的破解位置)
    55		for (inti = 0; i<model.length; i++) {	// 遍历棋型数组
    56			intgetResult[] = judgeModle(model[i]);	// 针对此方向棋型,给予破解方案
    57			if (score<getResult[1]) {		// 如果出现比当前最大威胁值还要大的威胁
    58				score = getResult[1];			// 更新最大分(威胁值)
    59				// 被捕捉的棋子在模型中的索引为5,getResult[0]为破解方案中的下棋索引位置
    60				// getResult[0] - 5 = 破解位置距离被捕捉的棋子的索引位置
    61				index = getResult[0] - 5;
    62				direction = i;				// 记录此棋型的方向
    63			}
    64		}
    65		switch (direction) {					// 判断最大威胁值所在的方向
    66		case 0:								// 如果是水平方向
    67			x += index;						// 下棋的位置是原位置向右(或向左)偏移index的值
    68			break;
    69		case 1:								// 如果是垂直方向
    70			y += index;						// 下棋的位置是原位置向下(或向上)偏移index的值
    71			break;
    72		case 2:								// 如果是反斜方向
    73			x += index;						// 下棋的位置是原位置向右(或向左)偏移index的值
    74			y -= index;						// 下棋的位置是原位置向上(或向下)偏移index的值
    75			break;
    76		case 3:								// 如果是正斜方向
    77			x += index;						// 下棋的位置是原位置向右(或向左)偏移index的值
    78			y += index;						// 下棋的位置是原位置向下(或向上)偏移index的值
    79			break;
    80		}
    81		position[0] = score;					// 记录此棋子的最大评分(威胁值)
    82		position[1] = x;						// 记录对应下棋横坐标
    83		position[2] = y;						// 记录对应下棋纵坐标
    84	
    85		returnposition;						// 返回结果数组
    86	}
    

    3. 获取棋型威胁值及其破解方法

    catchChessModle()中调用了judgeModle()方法,这个方法可以给出具体棋型的威胁值和相应破解位置。首先将一行棋子转换为字符串,然后查找字符串中是否出现与棋型库中相匹配的棋型,找出其中威胁值最大棋型,记录其的威胁值和落棋点并返回成结果数组。具体代码如下:


    01	publicint[] judgeModle(intmodel[]) {
    02		intpiont[] = newint[2];						// 初始化返回结果数组
    03		intscore = 0;								// 记录最大评分
    04		StringBuffer sb = new StringBuffer();		// 准备将棋型数组保存为字符串的StringBuffer
    05		for (intnum : model) {						// 遍历数组,将数组变成字符串
    06			if (num == GobangModel.BLACK_CHESSMAN) {	// 如果是黑子
    07				num = 4;								// 改为其他数字,以免负号会占字符
    08			}
    09			sb.append(num);							// 字符串添加此数字
    10		}
    11		Object library[][] = getModelLibrary();		// 获取棋型库中所有棋型及其解决方案
    12		for (inti = 0; i<library.length; i++) {		// 遍历棋型库
    13			String chessModel = (String) library[i][0];// 获取库中棋型
    14			intmodelIndex = -1;		// 临时变量,用于保存某棋型在字符串中出现的索引位置
    15			// 如果存在此棋型,则将棋型出现的位置付给modelIndex
    16			if ((modelIndex = sb.indexOf(chessModel)) != -1) {
    17				intscoreInLib = (int) library[i][1];	// 获取棋型评分
    18				intstepIndex = (int) library[i][2];	// 获取对应的下棋位置
    19				if (score<scoreInLib) {				// 如果出现更高的评分
    20					score = scoreInLib;				// 更新最大分
    21					// 记录应该(在这一行中)下棋的实际索引位置。
    22					// 棋型在字符串中的索引位置 + 棋型给出的解决位置 = 字符串中的解决位置
    23					piont[0] = modelIndex + stepIndex;
    24					piont[1] = score;				// 记录最大分数
    25				}
    26			}
    27		}
    28		returnpiont;								// 返回结果数组
    

    4. 棋型库

    judgeModle ()方法中调用了getModelLibrary()方法,该方法用于获取棋型库中所有棋型及其解决方案,这个棋型库可以自己搭建,也可以《Java项目实战入门》中的源码。


    本文摘自明日科技出版的《Java项目开发实战入门》,转载请注明出处!!!

    展开全文
  • java五子棋,具有人机对战,计时等功能
  • java实现的五子棋人机对战程序,运用了game tree, min-max search, alpha-beta pruning, heuristic evaluation等人工智能算法
  • 五子棋 人机对战版 ,你能容易地赢,说明你是五子棋高手了 c# 编写,但vc和java用户也可以看,没有用到特殊语法。 只有五子棋算法源码,界面是别人的,这是一个比赛中写的五子棋策略接口,你也可以写自己的策略接口...
  • 主要是一个五子棋游戏,利用java swing技术编写,总共只有一个类,两个内部类。人机对战
  • Java实现的五子棋的对战小游戏,实现了人人网络对战,人机对战,拥有界面,在使用的时候记得将src下面的所有素材放到bin目录下方可正常运行,导入Eclipse即可运行。
  • 原文:... package fir; import java.awt.*; import javax.swing.JPanel; /** * 有背景图片的Panel类 * @author tntxia */ public class BackgroundPanel extends JPa...
  • 五子棋人机对战思路: 1,棋盘与棋子的实现 2,玩家下完棋后,计算机要根据当前玩家所下的棋子找出最佳下棋点 3,输赢的判断 之前已经把人人对战的大概方法实现了,接下要实现的是人机的对战。我的...
  • 程序没有发现bug,试玩了一下感觉电脑智商一般般,缺点是棋盘有点小,还没怎么下就满了,javaapk之前也介绍过很多其他类型的五子棋游戏,需要更多可以在javaapk搜索“五子棋”就可以找到下载。
  • 这个小程序是我1年多前自己写着玩的,当时不懂这么多,两个个文件就搞定了,写的也不规范。 ai不是很智能,只是会搜索当前局面的最优解,评分规则是我从网上找的一个。大概介绍一下这个规则: 能成死二(当前位置下...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 720
精华内容 288
关键字:

java五子棋人机对战

java 订阅