Java实现五子棋小游戏(附源码)_java五子棋登录和注册

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;
}

img
img
img

既有适合小白学习的零基础资料,也有适合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%以上大数据知识点,真正体系化!

简单 五子棋的客户端部分程序 //chessClient.java:客户端主程序。 //userPad.java:客户端的界面。 //chessPad.java:棋盘的绘制。 //chessServer.java:服务器端。 //可同时容纳50个人同时在线下棋,聊天。 import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; class clientThread extends Thread{ chessClient chessclient; clientThread(chessClient chessclient){ this.chessclient=chessclient; } public void acceptMessage(String recMessage){ if(recMessage.startsWith("/userlist ")){ StringTokenizer userToken=new StringTokenizer(recMessage," "); int userNumber=0; chessclient.userpad.userList.removeAll(); chessclient.inputpad.userChoice.removeAll(); chessclient.inputpad.userChoice.addItem("所有人"); while(userToken.hasMoreTokens()){ String user=(String)userToken.nextToken(" "); if(userNumber>0 && !user.startsWith("[inchess]")){ chessclient.userpad.userList.add(user); chessclient.inputpad.userChoice.addItem(user); } userNumber++; } chessclient.inputpad.userChoice.select("所有人"); } else if(recMessage.startsWith("/yourname ")){ chessclient.chessClientName=recMessage.substring(10); chessclient.setTitle("Java五子棋客户端 "+"用户名:"+chessclient.chessClientName); } else if(recMessage.equals("/reject")){ try{ chessclient.chesspad.statusText.setText("不能加入游戏"); chessclient.controlpad.cancelGameButton.setEnabled(false); chessclient.controlpad.joinGameButton.setEnabled(true); chessclient.controlpad.creatGameButton.setEnabled(true); } catch(Exception ef){ chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close无法关闭"); } chessclient.controlpad.joinGameButton.setEnabled(true); } else if(recMessage.startsWith("/peer ")){ chessclient.chesspad.chessPeerName=recMessage.substring(6); if(chessclient.isServer){ chessclient.chesspad.chessColor=1; chessclient.chesspad.isMouseEnabled=true; chessclient.chesspad.statusText.setText("请黑棋下子"); } else if(chessclient.isClient){ chessclient.chesspad.chessColor=-1; chessclient.chesspad.statusText.setText("已加入游戏,等待对方下子..."); } } else if(recMessage.equals("/youwin")){ chessclient.isOnChess=false; chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor); chessclient.chesspad.statusText.setText("对方退出,请点放弃游戏退出连接"); chessclient.chesspad.isMouseEnabled=false; } else if(recMessage.equals("/OK")){ chessclient.chesspad.statusText.setText("创建游戏成功,等待别人加入..."); } else if(recMessage.equals("/error")){ chessclient.chatpad.chatLineArea.append("传输错误:请退出程序,重新加入 \n"); } else{ chessclient.chatpad.chatLineArea.append(recMessage+"\n"); chessclient.chatpad.chatLineArea.setCaretPosition( chessclient.chatpad.chatLineArea.getText().length()); } } public void run(){ String message=""; try{ while(true){ message=chessclient.in.readUTF(); acceptMessage(message); } } catch(IOException es){ } } } public class chessClient extends Frame implements ActionListener,KeyListener{ userPad userpad=new userPad(); chatPad chatpad=new chatPad(); controlPad controlpad=new controlPad(); chessPad chesspad=new chessPad(); inputPad inputpad=new inputPad(); Socket chatSocket; DataInputStream in; DataOutputStream out; String chessClientName=null; String host=null; int port=4331; boolean isOnChat=false; //在聊天? boolean isOnChess=false; //在下棋? boolean isGameConnected=false; //下棋的客户端连接? boolean isServer=false; //如果是下棋的主机 boolean isClient=false; //如果是下棋的客户端 Panel southPanel=new Panel(); Panel northPanel=new Panel(); Panel centerPanel=new Panel(); Panel westPanel=new Panel(); Panel eastPanel=new Panel(); chessClient( ){ super("Java五子棋客户端"); setLayout(new BorderLayout()); host=controlpad.inputIP.getText(); westPanel.setLayout(new BorderLayout()); westPanel.add(userpad,BorderLayout.NORTH); westPanel.add(chatpad,BorderLayout.CENTER); westPanel.setBackground(Color.pink); inputpad.inputwords.addKeyListener(this); chesspad.host=controlpad.inputIP.getText(); centerPanel.add(chesspad,BorderLayout.CENTER); centerPanel.add(inputpad,BorderLayout.SOUTH); centerPanel.setBackground(Color.pink); controlpad.connectButton.addActionListener(this); controlpad.creatGameButton.addActionListener(this); controlpad.joinGameButton.addActionListener(this); controlpad.cancelGameButton.addActionListener(this); controlpad.exitGameButton.addActionListener(this); controlpad.creatGameButton.setEnabled(false); controlpad.joinGameButton.setEnabled(false); controlpad.cancelGameButton.setEnabled(false); southPanel.add(controlpad,BorderLayout.CENTER); southPanel.setBackground(Color.pink); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ if(isOnChat){ try{ chatSocket.close(); }catch(Exception ed){ } } if(isOnChess || isGameConnected){ try{ chesspad.chessSocket.close(); }catch(Exception ee){ } } System.exit(0); } public void windowActivated(WindowEvent ea){ } }); add(westPanel,BorderLayout.WEST); add(centerPanel,BorderLayout.CENTER); add(southPanel,BorderLayout.SOUTH); pack(); setSize(670,548); setVisible(true); setResizable(false); validate(); } public boolean connectServer(String serverIP,int serverPort) throws Exception{ try{ chatSocket=new Socket(serverIP,serverPort); in=new DataInputStream(chatSocket.getInputStream()); out=new DataOutputStream(chatSocket.getOutputStream()); clientThread clientthread=new clientThread(this); clientthread.start(); isOnChat=true; return true; } catch(IOException ex){ chatpad.chatLineArea.setText("chessClient:connectServer:无法连接,建议重新启动程序 \n"); } return false; } public void actionPerformed(ActionEvent e){ if(e.getSource()==controlpad.connectButton){ host=chesspad.host=controlpad.inputIP.getText(); try{ if(connectServer(host,port)){ chatpad.chatLineArea.setText(""); controlpad.connectButton.setEnabled(false); controlpad.creatGameButton.setEnabled(true); controlpad.joinGameButton.setEnabled(true); chesspad.statusText.setText("连接成功,请创建游戏或加入游戏"); } } catch(Exception ei){ chatpad.chatLineArea.setText("controlpad.connectButton:无法连接,建议重新启动程序 \n"); } } if(e.getSource()==controlpad.exitGameButton){ if(isOnChat){ try{ chatSocket.close(); } catch(Exception ed){ } } if(isOnChess || isGameConnected){ try{ chesspad.chessSocket.close(); } catch(Exception ee){ } } System.exit(0); } if(e.getSource()==controlpad.joinGameButton){ String selectedUser=userpad.userList.getSelectedItem(); if(selectedUser==null || selectedUser.startsWith("[inchess]") || selectedUser.equals(chessClientName)){ chesspad.statusText.setText("必须先选定一个有效用户"); } else{ try{ if(!isGameConnected){ if(chesspad.connectServer(chesspad.host,chesspad.port)){ isGameConnected=true; isOnChess=true; isClient=true; controlpad.creatGameButton.setEnabled(false); controlpad.joinGameButton.setEnabled(false); controlpad.cancelGameButton.setEnabled(true); chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName); } }else{ isOnChess=true; isClient=true; controlpad.creatGameButton.setEnabled(false); controlpad.joinGameButton.setEnabled(false); controlpad.cancelGameButton.setEnabled(true); chesspad.chessthread.sendMessage("/joingame "+userpad.userList.getSelectedItem()+" "+chessClientName); } } catch(Exception ee){ isGameConnected=false; isOnChess=false; isClient=false; controlpad.creatGameButton.setEnabled(true); controlpad.joinGameButton.setEnabled(true); controlpad.cancelGameButton.setEnabled(false); chatpad.chatLineArea.setText("chesspad.connectServer无法连接 \n"+ee); } } } if(e.getSource()==controlpad.creatGameButton){ try{ if(!isGameConnected){ if(chesspad.connectServer(chesspad.host,chesspad.port)){ isGameConnected=true; isOnChess=true; isServer=true; controlpad.creatGameButton.setEnabled(false); controlpad.joinGameButton.setEnabled(false); controlpad.cancelGameButton.setEnabled(true); chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName); } }else{ isOnChess=true; isServer=true; controlpad.creatGameButton.setEnabled(false); controlpad.joinGameButton.setEnabled(false); controlpad.cancelGameButton.setEnabled(true); chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName); } } catch(Exception ec){ isGameConnected=false; isOnChess=false; isServer=false; controlpad.creatGameButton.setEnabled(true); controlpad.joinGameButton.setEnabled(true); controlpad.cancelGameButton.setEnabled(false); ec.printStackTrace(); chatpad.chatLineArea.setText("chesspad.connectServer无法连接 \n"+ec); } } if(e.getSource()==controlpad.cancelGameButton){ if(isOnChess){ chesspad.chessthread.sendMessage("/giveup "+chessClientName); chesspad.chessVictory(-1*chesspad.chessColor); controlpad.creatGameButton.setEnabled(true); controlpad.joinGameButton.setEnabled(true); controlpad.cancelGameButton.setEnabled(false); chesspad.statusText.setText("请建立游戏或者加入游戏"); } if(!isOnChess){ controlpad.creatGameButton.setEnabled(true); controlpad.joinGameButton.setEnabled(true); controlpad.cancelGameButton.setEnabled(false); chesspad.statusText.setText("请建立游戏或者加入游戏"); } isClient=isServer=false; } } public void keyPressed(KeyEvent e){ TextField inputwords=(TextField)e.getSource(); if(e.getKeyCode()==KeyEvent.VK_ENTER){ if(inputpad.userChoice.getSelectedItem().equals("所有人")){ try{ out.writeUTF(inputwords.getText()); inputwords.setText(""); } catch(Exception ea){ chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接 \n"); userpad.userList.removeAll(); inputpad.userChoice.removeAll(); inputwords.setText(""); controlpad.connectButton.setEnabled(true); } } else{ try{ out.writeUTF("/"+inputpad.userChoice.getSelectedItem()+" "+inputwords.getText()); inputwords.setText(""); } catch(Exception ea){ chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接 \n"); userpad.userList.removeAll(); inputpad.userChoice.removeAll(); inputwords.setText(""); controlpad.connectButton.setEnabled(true); } } } } public void keyTyped(KeyEvent e){} public void keyReleased(KeyEvent e){} public static void main(String args[]){ chessClient chessClient=new chessClient(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值