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;
}
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
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;
}
[外链图片转存中…(img-4YzvBcLn-4701986797063)]
[外链图片转存中…(img-yvzAqRot-4701986797063)]
[外链图片转存中…(img-0mgdNcXt-4701986797064)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!