今天给大家分享一个用java写的小游戏——《五子棋》 (完整代码可在【资源下载】目录查看) 。
推荐学习专栏:
一、效果展示
二、游戏介绍
《五子棋》 是一种两人对弈的纯策略型棋类游戏,棋具与围棋通用,是起源于中国古代的传统黑白棋种之一。发展于日本,流行于欧美。容易上手,老少皆宜,而且趣味横生,引人入胜;不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。
比赛规则:
- 行棋顺序:黑先、白后,相互顺序落子。
- 判断胜负:最先在棋盘横向、竖向、斜向形成连续的相同色五个棋子的一方为胜。黑棋禁手判负,白棋无禁手。黑棋禁手包括三三禁手,四四禁手,长连禁手。如分不出胜负,则定为平局。
棋型解释:
- 五连:五颗同色棋子连在一起,即4个方向的11111这种形式的棋型。
- 活四:有2个成五点的四颗棋子,即4个方向的011110这种形式的棋型,注意两边一定要有空格。
- 冲四:有1个成五点的四颗棋子,棋型有点多。
- 活三:可以形成活四的三颗棋子
禁手规则:
- 三三禁手:由于黑方落一子,同时形成二个或二个以上黑方活三的局面
- 四四禁手:由于黑方落一子,同时形成二个或二个以上黑方四(活四或者冲四)的局面
- 长连禁手:由于黑方落一子,形成六个或者六个以上的同色连续棋子
三、代码展示
1、登录页面
运行程序后需注册账号使用,也可使用我注册过的账号(账号:liangdianjun,密码:123456),可在项目文档user.xls查看。
话不多说,直接上代码
用户登录
private static final long servialVersionUID = 1L;
final JLabel logoLabel = new JLabel("开心五子棋");
final JLabel logo = new JLabel();
final JButton loginButton = new JButton(" 登 陆 ");
final JLabel registerLabel = new JLabel("立即注册");
final JLabel userLabel = new JLabel("账号:");
final JLabel passwordLabel = new JLabel("密码:");
final static JTextField userjt = new JTextField(11);
final JPasswordField passwordjt = new JPasswordField(11);
final JCheckBox rememberPasswordjcb = new JCheckBox();
final JLabel rememberPasswordjl = new JLabel("记住密码");
final JCheckBox automaticLoginjcb = new JCheckBox();
final JLabel automaticLoginjl = new JLabel("自动登录");
final JLabel promptPasswordFalse = new JLabel("密码错误!");
final JLabel promptRegister = new JLabel("该账号还未注册!");
final JLabel promptUserNameEmpty = new JLabel("请输入账号!");
final JLabel prompPasswordEmpty = new JLabel("请输入密码!");
final Color color = new Color(255, 218, 185);
final FileOperation read = new FileOperation();//创建文件对象
final FileOperation f = new FileOperation();
public Main() {
setTitle("开心五子棋");
setBounds(200, 200, 500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
//基本布局设置
SpringLayout springLayout = new SpringLayout();//使用弹簧布局管理器
Container c = getContentPane();//创建容器
c.setBackground(new Color(255, 218, 185));
c.setLayout(springLayout);
userjt.setFont(new Font("微软雅黑", 0, 18 ));
userjt.setText(Register.userName);
passwordjt.setFont(new Font("微软雅黑", 0, 18));
passwordjt.setText(Register.password);
logoLabel.setFont(new Font("微软雅黑", 1, 48));
logoLabel.setForeground(Color.pink);
ImageIcon logoimage = new ImageIcon(Main.class.getResource("/image/logo5.jpg"));
logoimage.setImage(logoimage.getImage().getScaledInstance(260, 130, Image.SCALE_DEFAULT));
logo.setIcon(logoimage);
userLabel.setFont(new Font("微软雅黑", 1, 20));
passwordLabel.setFont(new Font("微软雅黑", 1, 20));
rememberPasswordjl.setFont(new Font("微软雅黑", 0, 14));
rememberPasswordjl.setForeground(Color.gray);
automaticLoginjl.setFont(new Font("微软雅黑", 0, 14));
automaticLoginjl.setForeground(Color.gray);
loginButton.setFont(new Font("微软雅黑", 1, 16));
registerLabel.setFont(new Font("微软雅黑", 1, 13));
registerLabel.setForeground(Color.gray);
promptPasswordFalse.setFont(new Font("微软雅黑", 0, 13));
promptPasswordFalse.setForeground(Color.red);
promptUserNameEmpty.setFont(new Font("微软雅黑", 0, 13));
promptUserNameEmpty.setForeground(Color.red);
prompPasswordEmpty.setFont(new Font("微软雅黑", 0, 13));
prompPasswordEmpty.setForeground(Color.red);
promptRegister.setFont(new Font("微软雅黑", 0, 13));
promptRegister.setForeground(Color.red);
rememberPasswordjcb.setBackground(new Color(255, 218, 185));
automaticLoginjcb.setBackground(new Color(255, 218, 185));
c.add(logo);//首页图标
springLayout.putConstraint(springLayout.NORTH, logo, 40, springLayout.NORTH, c);
springLayout.putConstraint(springLayout.WEST, logo, 115, springLayout.WEST, c);
c.add(logoLabel);//标题“开心五子棋”
springLayout.putConstraint(springLayout.NORTH, logoLabel, 100, springLayout.NORTH, c);
springLayout.putConstraint(springLayout.WEST, logoLabel, 120, springLayout.WEST, c);
logoLabel.setVisible(false);
c.add(userLabel);//用户名
springLayout.putConstraint(springLayout.NORTH, userLabel, 35, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, userLabel, 110, springLayout.WEST, c);
c.add(userjt);
springLayout.putConstraint(springLayout.NORTH, userjt, 35, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, userjt, 10, springLayout.EAST, userLabel);
c.add(passwordLabel);//密码
springLayout.putConstraint(springLayout.NORTH, passwordLabel, 10, springLayout.SOUTH, userLabel);
springLayout.putConstraint(springLayout.WEST, passwordLabel, 110, springLayout.WEST, c);
c.add(passwordjt);
springLayout.putConstraint(springLayout.NORTH, passwordjt, 10, springLayout.SOUTH, userjt);
springLayout.putConstraint(springLayout.WEST, passwordjt, 10, springLayout.EAST, passwordLabel);
c.add(rememberPasswordjcb);//复选框
springLayout.putConstraint(springLayout.NORTH, rememberPasswordjcb, 10, springLayout.SOUTH, passwordLabel);
springLayout.putConstraint(springLayout.WEST, rememberPasswordjcb, 175, springLayout.WEST, c);
c.add(rememberPasswordjl);
springLayout.putConstraint(springLayout.NORTH, rememberPasswordjl, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, rememberPasswordjl, 5, springLayout.EAST, rememberPasswordjcb);
c.add(automaticLoginjcb);
springLayout.putConstraint(springLayout.NORTH, automaticLoginjcb, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, automaticLoginjcb, 30, springLayout.EAST, rememberPasswordjl);
c.add(automaticLoginjl);
springLayout.putConstraint(springLayout.NORTH, automaticLoginjl, 10, springLayout.SOUTH, passwordjt);
springLayout.putConstraint(springLayout.WEST, automaticLoginjl, 5, springLayout.EAST, automaticLoginjcb);
c.add(loginButton);//登陆按钮
springLayout.putConstraint(springLayout.NORTH, loginButton, 20, springLayout.SOUTH, rememberPasswordjl);
springLayout.putConstraint(springLayout.WEST, loginButton, 110, springLayout.WEST, c);
c.add(registerLabel);//注册按钮
springLayout.putConstraint(springLayout.NORTH, registerLabel, 5, springLayout.SOUTH, loginButton);
springLayout.putConstraint(springLayout.WEST, registerLabel, 320, springLayout.WEST, c);
c.add(promptRegister);//账号未注册提示
promptRegister.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptRegister, 41, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, promptRegister, 5, springLayout.EAST, userjt);
c.add(promptUserNameEmpty);//请输入账号
promptUserNameEmpty.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptUserNameEmpty, 41, springLayout.SOUTH, logoLabel);
springLayout.putConstraint(springLayout.WEST, promptUserNameEmpty, 5, springLayout.EAST, userjt);
c.add(promptPasswordFalse);//密码错误提示
promptPasswordFalse.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, promptPasswordFalse, 20, springLayout.SOUTH, promptRegister);
springLayout.putConstraint(springLayout.WEST, promptPasswordFalse, 5, springLayout.EAST, passwordjt);
c.add(prompPasswordEmpty);//密码为空提示
prompPasswordEmpty.setVisible(false);
springLayout.putConstraint(springLayout.NORTH, prompPasswordEmpty, 20, springLayout.SOUTH, promptRegister);
springLayout.putConstraint(springLayout.WEST, prompPasswordEmpty, 5, springLayout.EAST, passwordjt);
//设置文本框鼠标点击事件
userjt.addMouseListener(new MouseAdapter() {
//文本框
public void mouseClicked(MouseEvent e) {
userjt.setText("");
}
});
passwordjt.addMouseListener(new MouseAdapter() {
//密码框
public void mouseClicked(MouseEvent e) {
passwordjt.setText("");
}
});
//设置登陆按钮单击事件
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String userName = userjt.getText().trim();//获取用户输入的账号和密码
String Password = new String(passwordjt.getPassword()).trim();
//判断账号和密码
if(userName.length() != 0) {
//用户名不为空
promptUserNameEmpty.setVisible(false);//关闭账号为空显示
if(Password.length() != 0) {
//密码不为空
if(f.readData("user.xls", userName) && Password.equals(f.backData("user.xls", userName, "password"))) {
//用户输入的账号和密码正确
promptRegister.setVisible(false);//隐藏提示信息
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
loginButton.setText(" 登 陆 中... ");
new Chessboard();//跳转到五子棋棋盘页面
dispose();//销毁当前页面
}
else if( f.readData("user.xls", userName) && !Password.equals(f.backData("user.xls", userName, "password"))) {
//用户输入密码错误
promptPasswordFalse.setVisible(true);//显示密码错误提示
promptRegister.setVisible(false);
prompPasswordEmpty.setVisible(false);
passwordjt.setText("");//密码框清空
passwordjt.requestFocus();//光标定位到密码框
}else {
//账号还未注册
promptRegister.setVisible(true);
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
}
}
else {
//密码为空
if(userName.equals("admin")) {
//用户名已经注册, 提示输入密码
prompPasswordEmpty.setVisible(true);
promptUserNameEmpty.setVisible(false);
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
}else {
//用户名未注册
prompPasswordEmpty.setVisible(false);
promptUserNameEmpty.setVisible(false);
promptRegister.setVisible(true);
promptPasswordFalse.setVisible(false);
}
}
}else {
//用户名为空
promptUserNameEmpty.setVisible(true);//提示输入账号
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
prompPasswordEmpty.setVisible(false);
passwordjt.setText("");//将密码框置为空
if(Password.length() == 0) {
//密码为空
prompPasswordEmpty.setVisible(true);
promptRegister.setVisible(false);
promptPasswordFalse.setVisible(false);
}
}
}
});
//注册标签监听器
registerLabel.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
dispose();
new Register();
}
public void mouseEntered(MouseEvent e) {
registerLabel.setForeground(Color.red);;
}
public void mouseExited(MouseEvent e) {
registerLabel.setForeground(Color.black);
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
new Main();
}
2、算法程序
该程序实现了对五子棋分数的计算,计算竖横斜黑子和白子数量,谁先达成五分(即五子)则胜利。
返回棋盘上某个空点的分数
public static int countScore(int map[][], int X, int Y, int computerColor) {
int sum = 0;
int count = 0;
int value[] = new int[] {
0, 0, 0, 0};
int upcount[] = new int[] {
0, 0, 0, 0};
int downcount[] = new int[] {
0, 0, 0, 0};
int upflag[] = new int[] {
0, 0, 0, 0};
int downflag[] = new int[] {
0, 0, 0, 0};
for(int color = 1; color <= 2; color++) {
//计算双方的分数
map[X][Y] = color;//先将该点放白子
/*******************************************计算横向棋子***********************/
for(int i = X - 1; i >=0; i--) {
//计算左边棋子数量
if(map[i][Y] == color) {
upcount[0]++;
}else if(map[i][Y] != 0 && map[i][Y] != color) {
//表示有对方棋子
upflag[0] = -1;
break;
}else {
//表示为空
upflag[0] = 1;
if(i - 1 >= 0 && map[i][Y] == 0) {
upflag[0] = 2;//表示两个空格
}else {
break;
}
if(i - 2 >= 0 && map[i][Y] == 0) {
upflag[0] = 3;//表示有三个空格
}else {
break;
}
break;
}
}
for(int j = X + 1; j <= 14; j++) {
//计算右边棋子数量
if(map[j][Y] == color) {
downcount[0]++;
}else if(map[j][Y] != 0 && map[j][Y] != color) {
downflag[0] = -1;
break;
}else {
//表示为空
downflag[0] = 1;
if(j + 1 <= 14 && map[j][Y] == 0) {
downflag[0] = 2;
}else {
break;
}
if(j + 2 <= 14 && map[j][Y] == 0) {
downflag[0] = 3;
}else {
break;
}
break;
}
}
/******************************************************计算列项棋子***************************************/
for(int i = Y - 1; i >= 0; i--) {
//计算方向向上
if(map[X][i] == color) {
upcount[1]++;
}else if(map[X][i] != 0 && map[X][i] != color) {
//表示该点是对方棋子
upflag[1] = -1;
break;
}else {
//表示为空
upflag[1] = 1;
if(i - 1 >= 0 && map[X][i] == 0) {
upflag[1] = 2;
}else {
break;
}
if(i - 2 >= 0 && map[X][i] == 0) {
upflag[1] = 3;
}else {
break;
}
break;
}
}
for(int j = Y + 1; j <= 14; j++) {
//计算方向向下
if(map[X][j] == color) {
downcount[1]++;
}else if(map[X][j] != 0 && map[X][j] != color) {
//表示该点是对方棋子
downflag[1] = -1;
break;
}else {
//表示为空
downflag[1] = 1;
if(j + 1 >= 0 && map[X][j] == 0) {
downflag[1] = 2;
}else {
break;
}
if(j + 2 >= 0 && map[X][j] == 0) {
downflag[1] = 3;
}else {
break;
}
break;
}
}
/****************************************************计算斜向下棋子*********************************************/
int i = 0;
int j = 0;
for(i = X - 1, j = Y - 1; i >= 0 && j >= 0; i--, j--) {
//计算斜向上
if(map[i][j] == color) {
upcount[2]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
upflag[2] = -1;
break;
}else {
//为空
upflag[2] = 1;
if(i - 1 >= 0 && j - 1 >= 0 && map[i][j] == 0) {
upflag[2] = 2;
}else {
break;
}
if(i - 2 >= 0 && j - 2 >= 0 && map[i][j] == 0) {
upflag[2] = 3;
}else {
break;
}
break;
}
}
for(i = X + 1, j = Y + 1; i <= 14 && j <= 14; i++, j++) {
//计算斜向下
if(map[i][j] == color) {
downcount[2]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
downflag[2] = -1;
break;
}else {
//为空
downflag[2] = 1;
if(i + 1 <= 14 && j + 1 <= 14 && map[i][j] == 0) {
downflag[2] = 2;
}else {
break;
}
if(i + 2 <= 14 && j + 2 <= 14 && map[i][j] == 0) {
downflag[2] = 3;
}else {
break;
}
break;
}
}
/****************************************************计算斜向上棋子*************************************************/
for(i = X + 1, j = Y - 1; i <= 14 && j >= 0; i++, j--) {
if(map[i][j] == color) {
upcount[3]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
upflag[3] = -1;
break;
}else {
upflag[3] = 1;
if(i + 1 <= 14 && j - 1 >= 0 && map[i][j] == 0) {
upflag[3] = 2;
}else {
break;
}
if(i + 2 <= 14 && j - 2 >= 0 && map[i][j] == 0) {
upflag[3] = 3;
}else {
break;
}
break;
}
}
for(i = X - 1, j = Y + 1; i >= 0 && j <= 14; i--, j++) {
//计算斜向下
if(map[i][j] == color) {
downcount[3]++;
}else if(map[i][j] != 0 && map[i][j] != color) {
downflag[3] = -1;
break;
}else {
//为空
downflag[3] = 1;
if(i - 1 >= 0 && j + 1 <= 14 && map[i][j] == 0) {
downflag[3] = 2;
}else {
break;
}
if(i - 2 >= 0 && j + 2 <= 14 && map[i][j] == 0) {
downflag[3] = 3;
}else {
break;
}
break;
}
}
//数据处理
if(map[X][Y] == computerColor) {
//如果是电脑方的话分数要高一点
for(i =0; i < 4; i++) {
count = upcount[i] + downcount[i] + 1;
if(count == 5) {
//成五
value[i] = 40000;
}else if(count == 4) {
if(upflag[i] >= 1 && downflag[i] >= 1) {
//活四
value[i] = 19000;
}
if((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {
//眠四
value[i] = 3000;
}
if(upflag[i] == -1 && downflag[i] == -1) {
//死四
value[i] = -50;
}
}else if(count == 3) {
if((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {
//活三
value[i] = 4000;
}
if((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) ||
(upflag[i] == 1 && downflag[i] == 1)){
//眠三
value[i] = 800;
}
if(upflag[i] == -1 && downflag[i] == -1) {
//死三
value[i] = -50;
}
}else if(count == 2) {
if((upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >=2 && downflag[i] >= 2) ||
(upflag[i] >= 3 && downflag[i] >= 1)) {
//活二
value[i] = 1050;
}
if((upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] == -1) ||
(upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {
//眠二
value[i] = 350;
}
if(upflag[i] == -1 && downflag[i] == -1) {
//死二
value[i] = -50;
}
}else {
if((upflag[i] >= 2 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] >= 2)) {
//活1
value[i] = 80;
}
if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||
(upflag[i] == 3 && downflag[i] == 1)) {
//眠1
value[i] = 20;
}
if((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1)) {
value[i] = -50;
}
}
}
}else {
for(i =0; i < 4; i++) {
count = upcount[i] + downcount[i] + 1;
if(count == 5) {
//成五
value[i] = 30000;
}else if(count == 4) {
if(upflag[i] >= 1 && downflag[i] >= 1) {
//活四
value[i] = 15000;
}
if((upflag[i] >= 1 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 1)) {
//眠四
value[i] = 2500;
}
if(upflag[i] == -1 && downflag[i] == -1) {
//死四
value[i] = -50;
}
}else if(count == 3) {
if((upflag[i] >= 2 && downflag[i] >= 1) || (upflag[i] >= 1 && downflag[i] >= 2)) {
//活三
value[i] = 3000;
}
if((upflag[i] >= 2 && downflag[i] == -1) || (upflag[i] == -1 && downflag[i] >= 2) ||
(upflag[i] == 1 && downflag[i] == 1)){
//眠三
value[i] = 500;
}
if(upflag[i] == -1 && downflag[i] == -1) {
//死三
value[i] = -50;
}
}else if(count == 2) {
if((upflag[i] >= 1 && downflag[i] >= 3) || (upflag[i] >=2 && downflag[i] >= 2) ||
(upflag[i] >= 3 && downflag[i] >= 1)) {
//活二
value[i] = 650;
}
if((upflag[i] == -1 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] == -1) ||
(upflag[i] == 2 && downflag[i] == 1) || (upflag[i] == 1 && downflag[i] == 2)) {
//眠二
value[i] = 150;
}
if((upflag[i] == -1 && downflag[i] == -1) || (upflag[i] == 1 && downflag[i] == 1) ||
(upflag[i] == -1 && downflag[i] == 2) || (upflag[i] == 2 && downflag[i] == -1)) {
//死二
value[i] = -50;
}
}else {
if((upflag[i] >= 2 && downflag[i] >= 3) || (upflag[i] >= 3 && downflag[i] >= 2)) {
//活1
value[i] = 50;
}
if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||
(upflag[i] == 3 && downflag[i] == 1)) {
//眠1
value[i] = 10;
}
if((upflag[i] <= 1 && downflag[i] <= 2) || (upflag[i] <= 2 && downflag[i] <= 1)||
(upflag[i] <= 3 && downflag[i] == -1)|| (upflag[i] == -1 && downflag[i] <= 3)) {
value[i] = -50;
}
}
}
}
for(i = 0; i < 4; i++) {
sum += value[i];
value[i] = 0;
upcount[i] = 0;
downcount[i] = 0;
upflag[i] = 0;
downflag[i] = 0;
}
}
map[X][Y] = 0;
return sum;
}
估值算法,返回一个数组,用于记录坐标
public static int[] evalute(int map[][], int depth, int computerColor) {
int maxscore = 0;
Random r = new Random();
int pos[][] = new int[10][2];{
for(int i = 0; i < pos.length; i++) {
for(int j = 0; j < pos[i].length; j++) {
pos[i][j] = 0;
}
}
}
int FLAG = 0;
int score[][] = new int[15][15];{
//初始化计分数组
for(int i = 0; i < 15; i++) {
for(int j = 0; j < 15; j++) {
score[i][j] = 0;
}
}
}
int position[] = new int[]{
0, 0};//初始化位置坐标数组
for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {
//搜索横坐标
for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {
//搜索纵坐标
if(map[i][j] == 0) {
//表示该点在棋盘上面为空
score[i][j] = countScore(map, i, j, computerColor);
if(maxscore < score[i][j]) {
maxscore = score[i][j];//记录当前棋盘分数的最大值
}
}
}
}
for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {
for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {
if(score[i][j] == maxscore) {
pos[FLAG][0] = i;
pos[FLAG++][1] = j;
}
}
}
int m = r.nextInt(FLAG);
position[0] = pos[m][0];
position[1] = pos[m][1];
return position;
}
//极大极小值算法
public int minimax(int map[][], int chessColor) {
return chessColor;
}
//alpha beta剪枝
public void alphaBetaCutting(int map[][], int chessColor){
}
3、棋盘实现
该算法按照五子棋规则,实现了最基本的打子,棋盘布局等功能。电脑可以计算玩家的棋子位置,严防死守(我完全没有机会赢😃,胜利的小伙伴可评论炫一波🧐),最终连成五子则结束。(此代码较多,展示部分代码,可下载完整版查看学习)
电脑下棋函数
private void tuntoComputer() {
//电脑下棋
if(depth >= 7) {
depth = 6;
}
position = Algorithm.evalute(map, depth, computerColor);//调用估值函数
map[position[0]][position[1]] = computerColor;
imapflag[flag] = position[0];//将坐标存放在悔棋标记数组中
jmapflag[flag] = position[1];
newchessX = position[0];//新棋子标记记录坐标
newchessY = position[1];
int a = Math.max(Math.abs(position[0] - 7), Math.abs(position[1] - 7));//计算该点到中心的最大的距离
depth = Math.max(depth, a);//不断更新depth的值
flag ++;
chessboardEmpty = 1;//棋盘标记为有棋子
player = 1;//玩家下棋标志置0
computer = 0;//电脑下棋标志为1
judgeFlag = 1;
repaint();
}
判断棋子是否连成五个
public void judge() {
for(t = newchessX,s = newchessY,count = 0; t >=0 && s >= 0 && count <= 4; t--,s--,count++) {
comeX = t;
comeY = s;
}
for(t = newchessX, s = newchessY, count = 0; t <=14 && s >= 0 && count <= 4; t++, s--, count++) {
toX = t;
toY = s;
}
if(winFLAG == 0) {
for(int ch = 1; ch <=2; ch++) {
CHESSCOLOR = ch;
//判断横向棋子
for(s = (newchessX - 4) >=0 ? (newchessX - 4) : 0 ; s <= newchessX; s++) {
//表示玩家获胜
t = newchessY;
if(map[s][t] == CHESSCOLOR && s < 11) {
//行棋子数量计算
if(map[s + 1][t] == CHESSCOLOR) {
if(map[s + 2][t] == CHESSCOLOR) {
if(map[s + 3][t] == CHESSCOLOR) {
if(map[s + 4][t] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 1;
if(CHESSCOLOR == 1) {
//白棋
winFLAG = 1;
}else {
//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {
//如果某一方赢了就直接退出
break;
}
//判断列项棋子
for(t = (newchessY - 4) >=0 ? (newchessY - 4) : 0 ; t <= newchessY; t ++) {
s = newchessX;
if(map[s][t] == CHESSCOLOR && t < 11) {
//列棋子数量计算
if(map[s][t + 1] == CHESSCOLOR) {
if(map[s][t + 2] == CHESSCOLOR) {
if(map[s][t + 3] == CHESSCOLOR) {
if(map[s][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 2;
if(CHESSCOLOR == 1) {
//白棋
winFLAG = 1;
}else {
//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {
//如果某一方赢了就直接退出
break;
}
//判断左上到右下棋子
for(s = comeX, t = comeY; s <= newchessX && t <= newchessY; s ++, t++) {
if(map[s][t] == CHESSCOLOR && s < 11 && t < 11) {
//斜下棋子数量计算
if(map[s + 1][t + 1] == CHESSCOLOR) {
if(map[s + 2][t + 2] == CHESSCOLOR) {
if(map[s + 3][t + 3] == CHESSCOLOR) {
if(map[s + 4][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 3;
if(CHESSCOLOR == 1) {
//白棋
winFLAG = 1;
}else {
//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {
//如果某一方赢了就直接退出
break;
}
//判断右上到左下棋子
for(s = toX, t = toY; s >= newchessX && t <= newchessY; s --, t++) {
if(map[s][t] == CHESSCOLOR && s >= 4 && t < 11) {
//斜上棋子数量计算
if(map[s - 1][t + 1] == CHESSCOLOR) {
if(map[s - 2][t + 2] == CHESSCOLOR) {
if(map[s - 3][t + 3] == CHESSCOLOR) {
if(map[s - 4][t + 4] == CHESSCOLOR) {
winX = s;
winY = t;
winWay = 4;
if(CHESSCOLOR == 1) {
//白棋
winFLAG = 1;
}else {
//黑棋
winFLAG = 2;
}
break;
}
}
}
}
}
}
if(winFLAG != 0) {
//如果某一方赢了就直接退出
break;
}
}
}
}
四、资源下载
更多源码下载链接
-
百度网盘:链接:
https://pan.baidu.com/s/19F19h1RlWhdp_7qzqpOotg?pwd=gzxn
提取码:gzxn -
CSDN下载:需要VIP
https://download.csdn.net/download/CSDN_anhl/86967552 -
GitHub获取:附其他小游戏源码,感谢您的star
https://github.com/liangdianjun/game/tree/main/Temp
五、文末总结
通过学习《五子棋》游戏的实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,又多了更深层次的领悟。
转载:https://blog.csdn.net/CSDN_anhl/article/details/127853028