java五子棋小游戏含免费源码
讓我先來從注冊到登陸再到玩一局演示一下(本來想錄制個GIF,但是錄制出來的動圖太大了,有幾百兆,想想還是錄視頻,但是掘金目前還沒有開發(fā)出傳視頻的功能,我就傳在嗶哩嗶哩了)代碼可以通過文章末尾方式獲取
https://www.bilibili.com/video/BV1hU4y137es/
這個也有點大,四十多兆,能不能加載出來隨緣
🎯一、用戶注冊類
類名就叫做 Register.java
這里我們用到了JFrame,窗口。JFrame 是一個可以獨立顯示的組件,一個窗口通常包含有標(biāo)題、圖標(biāo)、操作按鈕(關(guān)閉、最小化、最大化),還可以為窗口添加菜單欄、工具欄等。一個進程中可以創(chuàng)建多個窗口,并可在適當(dāng)時候進行顯示、隱藏 或 銷毀。
控制用戶注冊的類主要代碼
這里只放主要代碼,不然太長不利于閱讀
public class Register extends JFrame{/** 用戶注冊*/ final JLabel welcomeRegisterLabel = new JLabel("歡迎注冊開心五子棋!"); final JLabel userLabel = new JLabel("用戶名:"); final JTextField userjt = new JTextField(14); final JLabel passwordLabel = new JLabel("密碼:"); final JPasswordField passwordjp = new JPasswordField(14); final JLabel confirmPasswordLabel = new JLabel("確認(rèn)密碼:"); final JPasswordField confirmPasswordjp = new JPasswordField(14); final JLabel yanzhengmajl = new JLabel("驗證碼:"); final JTextField yanzhengmajt = new JTextField("請輸入驗證碼", 7); final JLabel yanzhengmaUpdate = new JLabel("看不清?換一張"); final JButton register = new JButton(" 注 冊 "); final JLabel back = new JLabel("返回"); final JLabel tipUserAlreadyRegistered = new JLabel("該賬號已經(jīng)注冊!"); final JLabel tipUserNameEmpty = new JLabel("用戶名為空!"); final JLabel tipUserNameLessThan5Char = new JLabel("賬號少于5個字符!"); final JLabel tipPasswordEmpty = new JLabel("密碼不能為空!"); final JLabel tipPasswordLessThan6Char = new JLabel("密碼少于6個字符!"); final JLabel tipPasswordInconfirmity = new JLabel("兩次密碼不一致!"); final JLabel tipConfirmPasswordqualified = new JLabel("重復(fù)密碼正確!"); final JLabel tipyanzhengmaerror = new JLabel("驗證碼輸入不正確!"); final DrawYZM drawyzm = new DrawYZM(); final Random r = new Random(); static String userName = new String(); static String password = new String(); final FileOperation read = new FileOperation(); String repeatPassword = new String(); String yanzhengma = new String(); String yzm = new String(); int flagUserName = 0; int flagPassword = 0; int flagConfirmedPassword = 0; int flagyanzhengma = 0; char[] YZM = new char[62];{for(int i = 0; i < 26; i++) {YZM[i] = (char) ('A' + i);}for(int i = 26; i< 52; i++) {YZM[i] = (char) ('a' + i - 26);}for(char i = 52; i < 62; i++) {YZM[i] = (char) (i - 4);} } SpringLayout springLayout = new SpringLayout();//使用彈簧布局管理器}注冊時需要的驗證碼類
class DrawYZM extends JPanel{/**驗證碼類*/public void paint(Graphics g) {setBounds(269, 269, 100, 60);int R = r.nextInt(255);int G = r.nextInt(255);int B = r.nextInt(255);super.paintComponent(g);Graphics2D g2 = (Graphics2D)g;int n = 0;yzm = "";for(int i = 0; i < 4; i++) {n = r.nextInt(62);yzm += YZM[n];int flag = r.nextInt(2);Color color = new Color(r.nextInt(200) + 20, r.nextInt(200) + 20, r.nextInt(200) + 20);g2.setColor(color);Graphics2D g2d = (Graphics2D)g;AffineTransform trans = new AffineTransform();//將文字旋轉(zhuǎn)指定角度if(flag == 0) {trans.rotate(- r.nextInt(45) * 3.14 / 180, 275 + i * 25, 307);}else {trans.rotate(r.nextInt(45) * 3.14 / 180, 275 + i * 25, 307);}g2d.setTransform(trans);g2.setFont(new Font("微軟雅黑", 1, 24));g2.drawString(YZM[n] + "", 277 + i * 22, 307);}} }🍺二、文件操作類
類名就叫做 FileOperation.java
首先我們需要創(chuàng)建excel文件
包括了編號,用戶名,密碼,分?jǐn)?shù),等級,勝利場數(shù),總場數(shù),是否記住密碼,是否自動登錄,以及注冊時間
public void creatExcel(String path) {String data[] = { "Num","userName", "password", "points", "class", "winNum", "totalNum", "RememberPassword", "AutoLogin", "date"};try { File file = new File(path);WritableWorkbook book = Workbook.createWorkbook(file); WritableSheet sheet = book.createSheet("用戶信息", 0); for(int i = 0; i < data.length; i++) {//將信息寫入xls文件中Label label = new Label(i, 0, data[i]);sheet.addCell(label);}book.write(); book.close(); } catch (Exception e) { System.out.println(e); } }下面的方法用于判斷賬號是否存在
public boolean readData(String path, String userName) {File file = new File(path);if(!file.exists()) {//如果文件不存在creatExcel("user.xls");//創(chuàng)建文件}Workbook workbook = null;Sheet sheet = null;Cell cell = null;boolean flag = false;try {workbook = Workbook.getWorkbook(file);sheet = workbook.getSheet(0);for(int i = 0; i < sheet.getRows(); i ++) {cell = sheet.getCell(1, i);String content = cell.getContents();//獲取內(nèi)容if(content.equals(userName)) {flag = true;break;}}workbook.close();}catch(Exception e) {e.printStackTrace();}return flag; }將用戶將信息寫入文件中
public void writeData(String path, String userName, String keyword, String data) {File file = new File(path);//創(chuàng)建文件對象if(!file.exists()) {//如果文件不存在creatExcel("user.xls");//創(chuàng)建文件}Workbook workbook = null; WritableWorkbook wtbook = null; WritableSheet wtsheet = null; WritableCell wtcell = null; int flagx = 0; int flagy = 0;try {workbook = Workbook.getWorkbook(file); wtbook = Workbook.createWorkbook(file, workbook); wtsheet = wtbook.getSheet(0);//獲取第一張表格 for(int i = 0; i < wtsheet.getColumns(); i++) {//查找列wtcell = wtsheet.getWritableCell(i, 0);if(wtcell.getType() == CellType.LABEL) {String cell = ((Label)wtcell).getContents();if(cell.equals(keyword)) {//定位到關(guān)鍵詞的那一行flagx = i;break;}} }if(keyword.equals("userName")) {//如果寫入用戶名flagy = wtsheet.getRows();}else {//寫入其他for(int i = 0; i < wtsheet.getRows(); i++) {//查找行wtcell = wtsheet.getWritableCell(1, i);String cell = ((Label)wtcell).getContents();if(cell.equals(userName)) {flagy = i;break;}}}Label label = new Label(flagx, flagy, data);wtsheet.addCell(label);wtsheet.addCell(new Label(0, flagy, String.valueOf((wtsheet.getRows() - 1))));} catch (Exception e) { e.printStackTrace(); } finally { try { wtbook.write(); } catch (IOException e) { e.printStackTrace(); } try { wtbook.close(); } catch (WriteException | IOException e) { e.printStackTrace(); } workbook.close(); } }返回對應(yīng)用戶的對應(yīng)信息
public String backData(String path, String userName, String keyword) {File file = new File(path);if(!file.exists()) {//如果文件不存在creatExcel("user.xls");//創(chuàng)建文件}Workbook workbook = null;Sheet sheet = null;Cell cell = null;int flagx = 0; int flagy = 0;try {workbook = Workbook.getWorkbook(file);sheet = workbook.getSheet(0);for(int i = 0; i < sheet.getColumns(); i++) {//找到用戶名所在行cell = sheet.getCell(i, 0);String s = cell.getContents();if(s.equals(keyword)) {flagx = i;break;}}for(int i = 0; i < sheet.getRows(); i++) {cell = sheet.getCell(1, i);String s = cell.getContents();if(s.equals(userName)) {flagy = i;break;}}workbook.close();}catch(Exception e) {e.printStackTrace();}try {cell = Workbook.getWorkbook(file).getSheet(0).getCell(flagx, flagy);}catch(Exception e) {e.printStackTrace();}String s = cell.getContents();return s; }?三、棋盤頁面類
棋盤頁面類名 Chessboard.java
為棋色選擇添加事件監(jiān)聽器
whiteChessLabel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(chessboardEmpty == 0) {//只有棋盤為空的時候才能選擇棋子顏色whiteChessjr.setSelected(true);}} }); blackChessLabel.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {if(chessboardEmpty == 0) {//只有棋盤為空的時候才能選擇棋子顏色blackChessjr.setSelected(true);}} });為新局按鈕添加事件監(jiān)聽器
newRound.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {for(int i = 0; i < map.length; i++) {//將map數(shù)組置0for(int j = 0; j < map[i].length; j++) {map[i][j] = 0;mapflag[i][j] = 0;}}for(int j = 0; j < 225; j++) {//將悔棋標(biāo)記數(shù)組置為0imapflag[j] = 0;jmapflag[j] = 0;}flag = 0;//行列標(biāo)記數(shù)組下表置0winFLAG = 0;//輸贏標(biāo)記置0playerColor = -1;//玩家棋色標(biāo)記置-1computerColor = -1;//電腦棋色標(biāo)記為-1chessboardEmpty = 0;//棋盤標(biāo)記為空player = 1;//玩家先下棋computer = 0;//電腦后下newchessX = 0;//新棋子標(biāo)記置0newchessY = 0;depth = 0;repaint();} });為悔棋添加事件監(jiān)聽器
back.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(flag - 1 >= 0) {//棋盤上面有棋子的時候, 點擊一次棋盤上面少兩顆棋子,一顆是自己的,另一顆是電腦的for(int i = flag - 1; i > flag - 3; i--) {map[imapflag[i]][jmapflag[i]] = 0;mapflag[imapflag[i]][jmapflag[i]] = 0;imapflag[i] = 0;//將坐標(biāo)存放在悔棋標(biāo)記數(shù)組中jmapflag[i] = 0;}flag = flag - 2;//表示每次悔棋棋盤上雙方均少一顆子winFLAG = 0;if(flag - 1 >= 0) {newchessX = imapflag[flag - 1];newchessY = jmapflag[flag - 1];}if(flag == 0) {//表示棋盤為空chessboardEmpty = 0;//棋盤為空playerColor = -1;//玩家和電腦棋子顏色置0computerColor = -1;depth = 0;}repaint();}else {;}} });返回按鈕添加事件監(jiān)聽器
returnback.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {dispose();new Main();} });退出按鈕事件監(jiān)聽器
exit.addActionListener(new ActionListener() {//點擊退出按鈕退出程序public void actionPerformed(ActionEvent e) {System.exit(0);} });鼠標(biāo)進入棋盤區(qū)域內(nèi),用于顯示提示方框,表示點擊之后可以在哪個區(qū)域內(nèi)下棋
addMouseMotionListener(new MouseMotionListener() {public void mouseMoved(MouseEvent e) {// TODO 自動生成的方法存根x = e.getX();y = e.getY();if(x >= 247 && x <= 951 && y >=77 && y <= 781) {//如果鼠標(biāo)點擊的點在棋盤內(nèi)或者邊上一點double m = (x - 270.0)/47.0;//判斷點擊位置最靠近的哪個交叉點double n = (y - 100.0)/47.0;int p = (x - 270)/47;int q = (y - 100)/47;int i, j;if(m - p >= 0.5 || m - p <= -0.5) {i = p + 1;}else {i = p;}if(n - q >= 0.5 || n - q <= -0.5) {j = q + 1;}else {j = q;}if(i >=0 && i <= 15 && j >= 0 && j <= 15) {//識別到的區(qū)域為棋盤之內(nèi)if(mapflag[i][j] == 0 && winFLAG == 0 && player == 1) {//表示這個地方?jīng)]有棋子,并且還沒有贏promptBoxFlag[i][j] = 1;repaint();}}} }public void mouseDragged(MouseEvent e) {} });電腦下棋函數(shù)
private void tuntoComputer() {//電腦下棋// TODO 自動生成的方法存根if(depth >= 7) {depth = 6;}position = Algorithm.evalute(map, depth, computerColor);//調(diào)用估值函數(shù)map[position[0]][position[1]] = computerColor;imapflag[flag] = position[0];//將坐標(biāo)存放在悔棋標(biāo)記數(shù)組中jmapflag[flag] = position[1];newchessX = position[0];//新棋子標(biāo)記記錄坐標(biāo)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;//棋盤標(biāo)記為有棋子player = 1;//玩家下棋標(biāo)志置0computer = 0;//電腦下棋標(biāo)志為1judgeFlag = 1;repaint();}繪制棋盤
for(int i = 0; i < 15; i++) {//橫線g2.drawLine(261, i * 47 + 63, 919, i * 47 + 63);}for(int j = 0; j < 15; j++) {//豎線g2.drawLine(j * 47 + 261, 64, j * 47 + 261, 721);}繪制棋子
for(int i = 0; i < map.length; i++) {for(int j = 0; j < map[i].length; j++) {//白棋if(map[i][j] == 1) {g2.drawImage(white, i * 47 + 241, j * 47 + 43, 40, 40, this);mapflag[i][j] = 1;//標(biāo)記位置表示這個地方已經(jīng)有棋子}//黑棋if(map[i][j] == 2) {g2.drawImage(black, i * 47 + 241, j * 47 + 43, 40, 40, this);mapflag[i][j] = 1;} } }判斷棋子是否連成五個
if(judgeFlag == 1) {judge();judgeFlag = 0; }繪制新棋子紅點標(biāo)記
if(chessboardEmpty != 0) {g2.setColor(Color.red);g2.fillOval(newchessX * 47 + 254, newchessY * 47 + 55, 15, 15); }繪制鼠標(biāo)移動方框
for(int i = 0; i < 15; i++) {for(int j = 0; j < 15; j++) {if(promptBoxFlag[i][j] == 1) {g2.setColor(Color.RED);g2.setStroke(new BasicStroke(2.5f));//設(shè)置線條大小g2.drawLine(238 + i * 47, 40 + j * 47, 248 + i * 47, 40 + j * 47);//上左橫線g2.drawLine(275 + i * 47, 40 + j * 47, 285 + i * 47, 40 + j * 47);//上右橫線g2.drawLine(238 + i * 47, 40 + j * 47, 238 + i * 47, 50 + j * 47);//左上豎線g2.drawLine(285 + i * 47, 40 + j * 47, 285 + i * 47, 50 + j * 47);//右上豎線g2.drawLine(238 + i * 47, 77 + j * 47, 238 + i * 47, 87 + j * 47);//左下豎線g2.drawLine(285 + i * 47, 77 + j * 47, 285 + i * 47, 87 + j * 47);//右下豎線g2.drawLine(238 + i * 47, 87 + j * 47, 248 + i * 47, 87 + j * 47);//下左橫線g2.drawLine(275 + i * 47, 87 + j * 47, 285 + i * 47, 87 + j * 47);//下右橫線promptBoxFlag[i][j] = 0;}} } for(int i = 0; i < 5; i++) {//右上角星星g2.drawImage(happy, 711 + i * 47, 20, 40, 40, null); }解決第一次不能顯示輸贏標(biāo)志的問題
g2.drawImage(win, 0, 0, 1, 1, null); g2.drawImage(lose, 0, 0, 1, 1, null);if(winFLAG == 0 && player == 0) {//表示還未分出勝負并且玩家下了,調(diào)用電腦下棋tuntoComputer(); }設(shè)置下拉框和單選按鈕在棋盤不為空的是否不能進行選擇
設(shè)置棋子顏色單選框可用狀態(tài)
if(chessboardEmpty == 1) {//棋盤不為空difficulityClass.setEnabled(false);//設(shè)置下拉框不可用if(whiteChessjr.isSelected()) {//白棋子單選框被選中blackChessjr.setEnabled(false);whiteChessjr.setEnabled(true);}else {//黑棋子單選框被選中blackChessjr.setEnabled(true);whiteChessjr.setEnabled(false);} }else {//棋盤為空blackChessjr.setEnabled(true);//釋放兩個單選框whiteChessjr.setEnabled(true);difficulityClass.setEnabled(true);//釋放下拉框 }重繪積分,等級,勝率
classNum = (int)((int)(pointsNum /100 * 0.4 + gamewinNum * 0.4 + gameNum * 0.2) * 0.8); sunNum = (int) (classNum / 100); moonNum = (int)(classNum - sunNum * 100) / 50; starNum = (int)(classNum - sunNum * 100 - moonNum * 50) / 10; for(t = 0; t < sunNum; t++) {//繪畫太陽g2.drawImage(sun, 75 + t * 30, 538, 30, 30, null); } for(t = sunNum ; t < moonNum + sunNum; t++) {//繪畫月亮g2.drawImage(moon, 75 + t * 30, 540, 25, 25, null); } if(moonNum > 0 || sunNum > 0) {//繪畫星星for(t = moonNum + sunNum ; t < starNum + moonNum + sunNum; t ++) {g2.drawImage(star, 75 + t * 30, 540, 25, 25, null);} }else {for(t = moonNum ; t < starNum + 1; t ++) {g2.drawImage(star, 75 + t * 30, 538, 30, 30, null);} }判斷棋子是否連成五個
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) {//行棋子數(shù)量計算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) {//列棋子數(shù)量計算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) {//斜下棋子數(shù)量計算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) {//斜上棋子數(shù)量計算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;}}} }💌四、主函數(shù) Main.java
基本布局設(shè)置
setTitle("開心五子棋"); setBounds(200, 200, 500, 500); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); SpringLayout springLayout = new SpringLayout();//使用彈簧布局管理器 Container c = getContentPane();//創(chuàng)建容器 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));首頁圖標(biāo)
c.add(logo);//首頁圖標(biāo) springLayout.putConstraint(springLayout.NORTH, logo, 40, springLayout.NORTH, c); springLayout.putConstraint(springLayout.WEST, logo, 115, springLayout.WEST, c);c.add(logoLabel);//標(biāo)題“開心五子棋”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);復(fù)選框
c.add(rememberPasswordjcb);//復(fù)選框 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);設(shè)置文本框鼠標(biāo)點擊事件
userjt.addMouseListener(new MouseAdapter() {//文本框public void mouseClicked(MouseEvent e) {userjt.setText("");} }); passwordjt.addMouseListener(new MouseAdapter() {//密碼框public void mouseClicked(MouseEvent e) {passwordjt.setText("");} });設(shè)置登陸按鈕單擊事件
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);//關(guān)閉賬號為空顯示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();//跳轉(zhuǎn)到五子棋棋盤頁面dispose();//銷毀當(dāng)前頁面}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();//光標(biāo)定位到密碼框}else {//賬號還未注冊promptRegister.setVisible(true);promptPasswordFalse.setVisible(false);prompPasswordEmpty.setVisible(false);}}else {//密碼為空if(userName.equals("admin")) {//用戶名已經(jīng)注冊, 提示輸入密碼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);}}} });注冊標(biāo)簽監(jiān)聽器
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) {} });🎖五、算法類 Algorithm.java
計算左邊棋子數(shù)量
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;} }計算右邊棋子數(shù)量
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;} }估值算法,返回一個數(shù)組,用于記錄坐標(biāo)
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];{//初始化計分?jǐn)?shù)組for(int i = 0; i < 15; i++) {for(int j = 0; j < 15; j++) {score[i][j] = 0;}}}int position[] = new int[]{0, 0};//初始化位置坐標(biāo)數(shù)組for(int i = 6 - depth; i <= 8 + depth && i <= 14; i++) {//搜索橫坐標(biāo)for(int j = 6 - depth; j <= 8 + depth && j <= 14; j++) {//搜索縱坐標(biāo)if(map[i][j] == 0) {//表示該點在棋盤上面為空score[i][j] = countScore(map, i, j, computerColor);if(maxscore < score[i][j]) {maxscore = score[i][j];//記錄當(dāng)前棋盤分?jǐn)?shù)的最大值}}}}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; }項目目錄
Java文件
其他所需文件
圖片:
jar包:jxl.jar
代碼下載:
關(guān)注作者同名公眾號【海擁】回復(fù)【java五子棋小游戲】
源碼獲取
1.CSDN積分下載地址:
https://download.csdn.net/download/qq_44273429/15210290
2.關(guān)注作者公眾號海擁回復(fù)java五子棋小游戲免費獲取
長按識別二維碼關(guān)注微信公眾號
最后,不要忘了?或📑支持一下哦
總結(jié)
以上是生活随笔為你收集整理的java五子棋小游戏含免费源码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot开发登录、退出功能
- 下一篇: NODE 微信签名