「本文已參與好文召集令活動,點擊查看:後端、大前端雙賽道投稿,2萬元獎池等你挑戰!」前端
讓我先來從註冊到登錄再到玩一局演示一下(原本想錄制個GIF,可是錄製出來的動圖太大了,有幾百兆,想一想仍是錄視頻,可是掘金目前尚未開發出傳視頻的功能,我就傳在嗶哩嗶哩了)代碼能夠經過文章末尾方式獲取java
www.bilibili.com/video/BV1hU…git
這個也有點大,四十多兆,能不能加載出來隨緣github
類名就叫作 Register.java算法
這裏咱們用到了JFrame,窗口。JFrame 是一個能夠獨立顯示的組件,一個窗口一般包含有標題、圖標、操做按鈕(關閉、最小化、最大化),還能夠爲窗口添加菜單欄、工具欄等。一個進程中能夠建立多個窗口,並可在適當時候進行顯示、隱藏 或 銷燬。spring
這裏只放主要代碼,否則太長不利於閱讀後端
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("確認密碼:");
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("該帳號已經註冊!");
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("重複密碼正確!");
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();//將文字旋轉指定角度
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數組
包括了編號,用戶名,密碼,分數,等級,勝利場數,總場數,是否記住密碼,是否自動登陸,以及註冊時間markdown
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");//建立文件
}
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();//獲取內容
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);//建立文件對象
if(!file.exists()) {//若是文件不存在
creatExcel("user.xls");//建立文件
}
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)) {//定位到關鍵詞的那一行
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();
}
}
複製代碼
public String backData(String path, String userName, String keyword) {
File file = new File(path);
if(!file.exists()) {//若是文件不存在
creatExcel("user.xls");//建立文件
}
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.javaapp
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);
}
}
});
複製代碼
newRound.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < map.length; i++) {//將map數組置0
for(int j = 0; j < map[i].length; j++) {
map[i][j] = 0;
mapflag[i][j] = 0;
}
}
for(int j = 0; j < 225; j++) {//將悔棋標記數組置爲0
imapflag[j] = 0;
jmapflag[j] = 0;
}
flag = 0;//行列標記數組下表置0
winFLAG = 0;//輸贏標記置0
playerColor = -1;//玩家棋色標記置-1
computerColor = -1;//電腦棋色標記爲-1
chessboardEmpty = 0;//棋盤標記爲空
player = 1;//玩家先下棋
computer = 0;//電腦後下
newchessX = 0;//新棋子標記置0
newchessY = 0;
depth = 0;
repaint();
}
});
複製代碼
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;//將座標存放在悔棋標記數組中
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;//玩家和電腦棋子顏色置0
computerColor = -1;
depth = 0;
}
repaint();
}else {
;
}
}
});
複製代碼
returnback.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
new Main();
}
});
複製代碼
exit.addActionListener(new ActionListener() {//點擊退出按鈕退出程序
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
複製代碼
addMouseMotionListener(new MouseMotionListener() {
public void mouseMoved(MouseEvent e) {
// TODO 自動生成的方法存根
x = e.getX();
y = e.getY();
if(x >= 247 && x <= 951 && y >=77 && y <= 781) {//若是鼠標點擊的點在棋盤內或者邊上一點
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) {//識別到的區域爲棋盤以內
if(mapflag[i][j] == 0 && winFLAG == 0 && player == 1) {//表示這個地方沒有棋子,而且尚未贏
promptBoxFlag[i][j] = 1;
repaint();
}
}
}
}
public void mouseDragged(MouseEvent e) {}
});
複製代碼
private void tuntoComputer() {//電腦下棋
// TODO 自動生成的方法存根
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();
}
複製代碼
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;//標記位置表示這個地方已經有棋子
}
//黑棋
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;
}
複製代碼
if(chessboardEmpty != 0) {
g2.setColor(Color.red);
g2.fillOval(newchessX * 47 + 254, newchessY * 47 + 55, 15, 15);
}
複製代碼
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));//設置線條大小
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);
}
複製代碼
g2.drawImage(win, 0, 0, 1, 1, null);
g2.drawImage(lose, 0, 0, 1, 1, null);
if(winFLAG == 0 && player == 0) {//表示還未分出勝負而且玩家下了,調用電腦下棋
tuntoComputer();
}
複製代碼
設置棋子顏色單選框可用狀態
if(chessboardEmpty == 1) {//棋盤不爲空
difficulityClass.setEnabled(false);//設置下拉框不可用
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) {//行棋子數量計算
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;
}
}
}
}
複製代碼
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) {}
});
複製代碼
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;
}
}
複製代碼
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;
}
複製代碼
Java文件
其餘所需文件
圖片:
jar包:jxl.jar
關注做者同名公衆號【海擁】回覆【java五子棋小遊戲】
我已經寫了很長一段時間的技術博客,這是個人一篇技術文章/教程。但願大家會喜歡!這裏彙總了個人所有原創及做品源碼:GitHub、Gitee
若是你真的從這篇文章中學到了一些新東西,喜歡它,收藏它並與你的小夥伴分享。🤗最後,不要忘了❤或📑支持一下哦