- 绘制窗体
- 画棋盘
- 画棋子
- 计算棋子位置
- 判断是否连成线
- 人机对战(权值算法)
针对一个项目来说,人机交互是必须的,那么先来绘制一个窗体吧
用到的类是Jframe,上代码
this.setSize(800, 800);
this.setTitle("五子棋游戏");
//设置居中显示
this.setLocationRelativeTo(null);
//退出进程
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
this.setVisible(true);
这时候就有问题了,不是说好Jframe对象吗,this又是啥?
public class GameUI extends Jframe implements Config
哦,原来继承了Jframe这个类,this其实就是下文的jf
Jframe jf = new Jframe();
jf.setSize(800, 800);
jf.setTitle("五子棋游戏");
//设置居中显示
jf.setLocationRelativeTo(null);
//退出进程
jf.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
jf.setVisible(true);
画棋盘
for(int i=0;i你现在随便调整一下窗体 咦 奇怪 怎么没了
这就需要重写一个paint函数 每次都重绘框体 自然棋子也保存了//重写绘制组件的方法 public void paint(Graphics g) { //1.绘制组件 super.paint(g); System.out.println("paint"); //2.绘制棋盘/棋子 //硬编码 for(int i=0;i画棋子 画完窗口画棋子 画画画
图形内容显示在哪个组件上,画笔就从该组件上获取
从窗体上获取画笔对象,获取画笔对象
在这介绍一下画笔工具Graphics类的使用Graphics g = jf.getGraphics();画笔有了 那画在哪里了 就靠鼠标点击了
来吧监听器登场 用于获取鼠标点击位置public interface MouseListener extends EventListener { public void mouseClicked(MouseEvent e); public void mousePressed(MouseEvent e); public void mouseReleased(MouseEvent e); public void mouseEntered(MouseEvent e); public void mouseExited(MouseEvent e); }什么鬼 看不懂!!!
别急 慢慢解释 这是java提供的监听器的接口interface
所以我们需要干嘛了 创建一个类 将这些接口写好方法
我创建的就是GameMouse类GameMouse mouse = new GameMouse(); this.addMouseListener(mouse);监听器写好了,我们需要监听器去干什么了
自然是要把棋子绘制出来 绘制棋子的代码g.setColor(Color.WHITE); g.fillOval(x, y, CHESS, CHESS);CHESS是什么,这是我定义的常量,代表棋子大小,这样方便与之后的代码修改,避免了硬编码
计算棋子位置现在棋子是点哪画哪 那咋办了 就需要算法了
靠近那个棋盘放在哪int x = e.getX(); if ((x - X0) % SIZE > SIZE / 2) x = ((x - X0) / SIZE + 1) * SIZE + X0 - CHESS / 2; else x = (x - X0) / SIZE * SIZE + X0 - CHESS / 2; int y = e.getY(); if ((y - Y0) % SIZE > SIZE / 2) y = ((y - Y0) / SIZE + 1) * SIZE + Y0 - CHESS / 2; else y = (y - Y0) / SIZE * SIZE + Y0 - CHESS / 2;PS:SIZE是棋盘两条线直接的距离
判断是否连成线只有在下完棋子之后 判断这个棋子周边是否连成线就可以了
算法思想就是计算左边几个棋子右边几个棋子 加一起是5个吗
一共是四个方向哈public void JudgeRow(int x,int y){ int count = 0; for (int i = (x - X0 + CHESS / 2) / SIZE + 1; i < LINE; i++) { if (chessBoard[i][(y - Y0 + CHESS / 2) / SIZE] == chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE]) { count++; } else break; } for (int i = (x - X0 + CHESS / 2) / SIZE; i >= 0; i--) { if (chessBoard[i][(y - Y0 + CHESS / 2) / SIZE] == chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE]) { count++; } else break; } if (count == 5) { this.startFlag=0; if (chessBoard[(x - X0 + CHESS / 2) / SIZE][(y - Y0 + CHESS / 2) / SIZE] == 1) JOptionPane.showMessageDialog(null, "黑棋胜利"); else JOptionPane.showMessageDialog(null, "白棋胜利"); } }五子棋就写完了 但是很孤独啊 没有人陪你玩
人机对战(权值算法)其实五子棋的目的是阻止对方五个子连成线,所以我们可以判断棋局的情况,赋予不同的权值,在权值大的地方落子,保证输不了
权值:可选择落子的地点,计算8个方向的棋子情况,将权值累积
我的权值赋值如下public void InitHm(){ hm.put("1111112",9000); hm.put("111112",8000); hm.put("1111",6000); hm.put("11112",4000); hm.put("111",200); hm.put("1112",40); hm.put("11",40); hm.put("112",20); hm.put("1",20); hm.put("12",2); hm.put("2",1); hm.put("",0); }hm是什么了 哈哈 就是
private HashMaphm = new HashMap (); 给个算法示例就结束啦
public int CountRowZuo(int i,int j){ int color = 0; String chessString = new String(); for(int z = 1;(i-z) > 0 &&chessBoard[i-z][j]!=0;z++) { color = chessBoard[i - 1][j]; if (chessBoard[i - z][j] == color) { chessString = chessString + "1"; } else { chessString = chessString + "2"; break; } } return hm.get(chessString).intValue(); }git还没建好(懒) 就先不开源了 哈哈



