今天给大家分享一个用java写的小游戏——《五子棋》 (完整代码可在【资源下载】目录查看) 。五子棋是一种两人对弈的纯策略型棋类游戏,棋具与围棋通用,是起源于中国古代的传统黑白棋种之一。发展于日本,流行于欧美。容易上手,老少皆宜,而且趣味横生,引人入胜;不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。
运行程序后需注册账号使用,也可使用我注册过的账号(账号: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();
}
该程序实现了对五子棋分数的计算,计算竖横斜黑子和白子数量,谁先达成五分(即五子)则胜利。
返回棋盘上某个空点的分数
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)) {//活1value[i] = 80;}if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||(upflag[i] == 3 && downflag[i] == 1)) {//眠1value[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)) {//活1value[i] = 50;}if((upflag[i] == 2 && downflag[i] == 2) || (upflag[i] == 1 && downflag[i] == 3) ||(upflag[i] == 3 && downflag[i] == 1)) {//眠1value[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){}
该算法按照五子棋规则,实现了最基本的打子,棋盘布局等功能。电脑可以计算玩家的棋子位置,严防死守(我完全没有机会赢😃,胜利的小伙伴可评论炫一波🧐),最终连成五子则结束。(此代码较多,展示部分代码,可下载完整版查看学习)
电脑下棋函数
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;//玩家下棋标志置0computer = 0;//电脑下棋标志为1judgeFlag = 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获取:(感谢点星的小伙伴)https://github.com/liangdianjun/game/tree/main/Temp
通过学习《五子棋》游戏的实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,又多了更深层次的领悟。