局域网下的联机五子棋游戏
功能描述:
该游戏能够实现在局域网下双人对战的五子棋,游戏界面以及图形全以gui展示,使用socket编程实现联机对战,功能上以创建游戏,加入游戏为主,判断五子棋是否五个棋子连成线以及socket编程为主要编程内容。
实例图片:
步骤:
1.绘制一个五子棋窗口
2.创建游戏,开始游戏
3.落子
4.判断五子棋的胜利
5.两个客户端联机
主要功能流程图
四、UML图
项目运行截图
为了演示方便,该截图为一台电脑开启两个客户端进行联机
开启服务端,两个客户端连接:
创建游戏以及加入游戏
游戏进行中
赢下小局
项目关键代码
判断五子连线的情况
/**
* 五子棋核心算法
* */
public boolean checkVicStatus(int xPos, int yPos, int chessColor) {
//连接的棋子数
int chessLinkedCount;
//用于比较是否要继续遍历一个棋子的相邻网格
int chessLinkedCompare = 1;
//要比较的棋子在数组中的索引位置
int chessToCompareIndex;
//相邻网格的位置
int closeGrid;
//当落子为黑棋时
if(chessColor == 1) {
//将该棋子自身算入的话,初始连接数为1
chessLinkedCount = 1;
//以下每对for循环语句为一组,因为下棋的位置能位于中间而非两端
//遍历相邻4个网格
//判断当前下的棋子的右边4个棋子是否都为黑棋
for(closeGrid = 1; closeGrid <= 4; closeGrid++) {
//遍历棋盘上所有黑棋子
for(chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
if(((xPos + closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos * dis) == chessBlack_YPOS[chessToCompareIndex])) {
//连接数加1
chessLinkedCount = chessLinkedCount + 1;
//五子相连时,胜利
if (chessLinkedCount == 5) {
return true;
}
}
}
if(chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
//若中间有一个棋子非黑棋,则会进入此分支,此时无需再遍历
} else {
break;
}
}
// 判断当前下的棋子的左边4个棋子是否都为黑棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
if (((xPos - closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
&& (yPos * dis == chessBlack_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
// 进入新的一组for循环时要将连接数等重置
chessLinkedCount = 1;
chessLinkedCompare = 1;
// 判断当前下的棋子的上边4个棋子是否都为黑棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
if ((xPos * dis == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
// 判断当前下的棋子的下边4个棋子是否都为黑棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
if ((xPos * dis == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos - closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
chessLinkedCount = 1;
chessLinkedCompare = 1;
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
// 判断当前下的棋子的左上方向4个棋子是否都为黑棋
if (((xPos - closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
// 判断当前下的棋子的右下方向4个棋子是否都为黑棋
if (((xPos + closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos - closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
chessLinkedCount = 1;
chessLinkedCompare = 1;
// 判断当前下的棋子的右上方向4个棋子是否都为黑棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
if (((xPos + closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
// 判断当前下的棋子的左下方向4个棋子是否都为黑棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessBlackCount; chessToCompareIndex++) {
if (((xPos - closeGrid) * dis == chessBlack_XPOS[chessToCompareIndex])
&& ((yPos - closeGrid) * dis == chessBlack_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
//当落子为白棋时
} else if (chessColor == -1) {
chessLinkedCount = 1;
// 判断当前下的棋子的右边4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if (((xPos + closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
&& (yPos * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
// 判断当前下的棋子的左边4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if (((xPos - closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
&& (yPos * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
chessLinkedCount = 1;
chessLinkedCompare = 1;
// 判断当前下的棋子的上边4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if ((xPos * dis == chessWhite_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
// 判断当前下的棋子的下边4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if ((xPos * dis == chessWhite_XPOS[chessToCompareIndex])
&& ((yPos - closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
chessLinkedCount = 1;
chessLinkedCompare = 1;
// 判断当前下的棋子的左上方向4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if (((xPos - closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
// 判断当前下的棋子的右下方向4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if (((xPos + closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
&& ((yPos - closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
chessLinkedCount = 1;
chessLinkedCompare = 1;
// 判断当前下的棋子的右上方向4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if (((xPos + closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
&& ((yPos + closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
// 判断当前下的棋子的左下方向4个棋子是否都为白棋
for (closeGrid = 1; closeGrid <= 4; closeGrid++) {
for (chessToCompareIndex = 0; chessToCompareIndex <= chessWhiteCount; chessToCompareIndex++) {
if (((xPos - closeGrid) * dis == chessWhite_XPOS[chessToCompareIndex])
&& ((yPos - closeGrid) * dis == chessWhite_YPOS[chessToCompareIndex])) {
chessLinkedCount++;
if (chessLinkedCount == 5) {
return (true);
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
}
return false;
}
服务端多线程
public void dealWithMsg(String msgReceived){
String peerName;
if(msgReceived.startsWith("/")){
//更新用户列表
if(msgReceived.equals("/list")){
Feedback(getUserList());
//创建游戏
}else if (msgReceived.startsWith("/creatgame [inchess]")){
//获取服务器名
String gameCreaterName = msgReceived.substring(20);
//将用户端口放入用户列表中
clientNameHash.put(clientSocket, msgReceived.substring(11));
//将主机设置为等待状态
chessPeerHash.put(gameCreaterName, "wait");
Feedback("/yourname " + clientNameHash.get(clientSocket));
sendGamePeerMsg(gameCreaterName, "/OK");
sendPublicMsg(getUserList());
//收到的信息为加入游戏
}else if (msgReceived.startsWith("/joingame ")){
//StringTokenizer用于分隔字符串
StringTokenizer userTokens = new StringTokenizer(msgReceived," ");
String userToken;
String gameCreatorName;
String gamePaticipantName;
String[] playerNames = {"0","0"};
int nameIndex = 0;
while(userTokens.hasMoreTokens()){
userToken =userTokens.nextToken(" ");
if(nameIndex >= 1 && nameIndex <= 2){
playerNames[nameIndex - 1] = userToken;
}
nameIndex++;
}
gameCreatorName = playerNames[0];
gamePaticipantName = playerNames[1];
//游戏已经创建
if(chessPeerHash.containsKey(gameCreatorName)
&& chessPeerHash.get(gameCreatorName).equals("wait")){
//增加游戏加入者的接口与名称对应
clientNameHash.put(clientSocket, ("[inchess]" + gamePaticipantName));
// 增加或修改游戏创建者与游戏加入者的名称的对应
chessPeerHash.put(gameCreatorName, gamePaticipantName);
sendPublicMsg(getUserList());
//发送信息给游戏加入者
sendGamePeerMsg(gamePaticipantName,("/peer " + "[inchess]" + gameCreatorName));
//发送游戏给游戏创建者
sendGamePeerMsg(gameCreatorName,("/peer " + "[inchess]" + gamePaticipantName));
//游戏未创建 则拒绝加入游戏
}else{
sendGamePeerMsg(gamePaticipantName,"/reject");
try{
closeClient();
}catch (Exception e){
e.printStackTrace();
}
}
//游戏中
} else if (msgReceived.startsWith("/[inchess]")) {
int firstLocation = 0;
int lastLocation = msgReceived.indexOf(" ",0);
peerName = msgReceived.substring((firstLocation + 1),lastLocation);
msgReceived = msgReceived.substring((lastLocation + 1));
if(sendGamePeerMsg(peerName,msgReceived)){
Feedback("/error");
}
//放弃游戏
}else if(msgReceived.startsWith("/giveup ")){
String chessClientName = msgReceived.substring(8);
//胜利方为加入游戏者,发送胜利信息
if(chessPeerHash.containsKey(chessClientName)
&& !chessPeerHash.get(chessClientName).equals("wait")){
sendGamePeerMsg((String) chessPeerHash.get(chessClientName),"/youwin");
//删除退出游戏的用户
chessPeerHash.remove(chessClientName);
}
//胜利方为游戏创建者,发送胜利信息
if(chessPeerHash.containsValue(chessClientName)){
sendGamePeerMsg((String) getHashKey(chessPeerHash,chessClientName),"/youwin");
// 删除退出游戏的用户
chessPeerHash.remove(getHashKey(chessPeerHash, chessClientName));
}
//收到其他信息
}else{
int lastLocation = msgReceived.indexOf(" ",0);
if(lastLocation == -1){
Feedback("无效命令");
}
}
} else {
msgReceived = clientNameHash.get(clientSocket) + ">" +msgReceived;
serverMsgPanel.msgTextArea.append(msgReceived + "\n");
sendPublicMsg(msgReceived);
serverMsgPanel.msgTextArea.setCaretPosition(serverMsgPanel.msgTextArea.getText().length());
}
}