前言
五子棋是世界智力运动会竞技项目之一,是一种两人对弈的纯策略型棋类游戏,是世界智力运动会竞技项目之一,通常双方分别使用黑白两色的棋子,下在棋盘直线与横线的交叉点上,先形成5子连线者获胜。
棋具与围棋通用,起源于中国上古时代的传统黑白棋种之一。主要流行于华人和汉字文化圈的国家以及欧美一些地区,是世界上最古老的棋。
容易上手,老少皆宜,而且趣味横生,引人入胜;不仅能增强思维能力,提高智力,而且富含哲理,有助于修身养性。
用java语言实现,采用了swing技术进行了界面化处理,设计思路用了面向对象思想。
主要需求
1、对局双方各执一色棋子。
2、空棋盘开局。
3、黑先、白后,交替下子,每次只能下一子。
4、棋子下在棋盘的空白点上,棋子下定后,不得向其它点移动,不得从棋盘上拿掉或拿起另落别处。
5、黑方的第一枚棋子可下在棋盘任意交叉点上。
6、轮流下子是双方的权利,但允许任何一方放弃下子权,先形成5子连线者获胜。
主要设计
1、由于是两人的游戏,非单机版,所以要有多个客户端相互通信,这时就要用到socket 技术
2、设计socket服务端,用来维护socket客户端连接
3、设计socket客户端,用来实现五子棋逻辑和效果
4、客户端要能设置连接服务端的IP,用来连接服务端
5、客户端1创建游戏后,客户端2可以选择客户端1进行联机对战
6、游戏规则:
- 对局双方各执一色棋子。
- 空棋盘开局。
- 黑先、白后,交替下子,每次只能下一子。
- 棋子下在棋盘的空白点上,棋子下定后,不得向其它点移动,不得从棋盘上拿掉或拿起另落别处。
- 黑方的第一枚棋子可下在棋盘任意交叉点上。
- 轮流下子是双方的权利,但允许任何一方放弃下子权,先形成5子连线者获胜。
功能截图
服务端启动
客户端1启动
客户端2启动
对战效果
代码实现
服务端启动类
public class ServerRunner extends Frame implements ActionListener {
JButton clearMsgButton = new JButton("清空");
JButton serverStatusButton = new JButton("状态");
JButton closeServerButton = new JButton("关闭");
Panel buttonPanel = new Panel();
ServerMsgPanel serverMsgPanel = new ServerMsgPanel();
ServerSocket serverSocket;
int clientAccessNumber = 1;
Hashtable clientDataHash = new Hashtable(50);
Hashtable clientNameHash = new Hashtable(50);
Hashtable chessPeerHash = new Hashtable(50);
public ServerRunner() {
super("网络游戏对战平台服务器控制平台");
setBackground(Color.PINK);
buttonPanel.setLayout(new FlowLayout());
clearMsgButton.setSize(50, 30);
buttonPanel.add(clearMsgButton);
clearMsgButton.addActionListener(this);
serverStatusButton.setSize(50, 30);
buttonPanel.add(serverStatusButton);
serverStatusButton.addActionListener(this);
closeServerButton.setSize(50, 30);
buttonPanel.add(closeServerButton);
closeServerButton.addActionListener(this);
add(serverMsgPanel, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
setVisible(true);
setSize(600, 440);
setResizable(false);
validate();
try {
createServer(1234, serverMsgPanel);
} catch (Exception e) {
e.printStackTrace();
}
}
public void createServer(int port, ServerMsgPanel serverMsgPanel) throws IOException {
Socket clientSocket;
this.serverMsgPanel = serverMsgPanel;
try {
serverSocket = new ServerSocket(port);
serverMsgPanel.msgTextArea.setText("服务器启动:"
+ InetAddress.getLocalHost() + ":"
+ serverSocket.getLocalPort() + "\n");
while (true) {
clientSocket = serverSocket.accept();
serverMsgPanel.msgTextArea.append("已连接用户:" +
"毅慎" + clientAccessNumber +"\n" + clientSocket + "\n");
DataOutputStream outputData = new DataOutputStream(clientSocket.getOutputStream());
clientDataHash.put(clientSocket, outputData);
clientNameHash.put(clientSocket, ("毅慎" + clientAccessNumber++));
ServerThread thread = new ServerThread(clientSocket,
clientDataHash, clientNameHash, chessPeerHash, serverMsgPanel);
thread.start();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearMsgButton) {
serverMsgPanel.msgTextArea.setText("");
}
if (e.getSource() == serverStatusButton) {
try {
serverMsgPanel.msgTextArea.append("用户信息:" + "毅慎"
+ (clientAccessNumber - 1) + "\n服务器信息:"
+ InetAddress.getLocalHost() + ":"
+ serverSocket.getLocalPort() + "\n");
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
public static void main(String[] args) {
new ServerRunner();
}
}
客户端启动类
public class ClientChess extends Frame implements ActionListener, KeyListener {
public Socket clientSocket;
public DataInputStream inputStream;
public DataOutputStream outputStream;
public String chessClientName = null;
public String host = null;
public int port = 1234;
public boolean isOnChat = false;
public boolean isOnChess = false;
public boolean isGameConnected = false;
public boolean isCreator = false;
public boolean isParticipant = false;
public UserList userListPad = new UserList();
protected UserChat userChatPad = new UserChat();
public UserController userControlPad = new UserController();
protected UserInput userInputPad = new UserInput();
ChessBoard chessBoard = new ChessBoard();
private Panel southPanel = new Panel();
private Panel centerPanel = new Panel();
private Panel eastPanel = new Panel();
public ClientChess() {
super("五子棋客户端");
setLayout(new BorderLayout());
host = userControlPad.ipInputted.getText();
eastPanel.setLayout(new BorderLayout());
eastPanel.add(userListPad, BorderLayout.NORTH);
eastPanel.add(userChatPad, BorderLayout.CENTER);
eastPanel.setBackground(new Color(238, 154, 73));
userInputPad.contentInputted.addKeyListener(this);
chessBoard.host = (userControlPad.ipInputted.getText());
centerPanel.add(chessBoard, BorderLayout.CENTER);
centerPanel.add(userInputPad, BorderLayout.SOUTH);
centerPanel.setBackground(new Color(238, 154, 73));
userControlPad.connectButton.addActionListener(this);
userControlPad.createButton.addActionListener(this);
userControlPad.joinButton.addActionListener(this);
userControlPad.cancelButton.addActionListener(this);
userControlPad.exitButton.addActionListener(this);
userControlPad.createButton.setEnabled(false);
userControlPad.joinButton.setEnabled(false);
userControlPad.cancelButton.setEnabled(false);
southPanel.add(userControlPad, BorderLayout.CENTER);
southPanel.setBackground(PINK);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (isOnChat) {
try {
clientSocket.close();
}
catch (Exception ed){}
}
if (isOnChess || isGameConnected) {
try {
chessBoard.chessSocket.close();
}
catch (Exception ee){}
}
System.exit(0);
}
});
add(eastPanel, BorderLayout.EAST);
add(centerPanel, BorderLayout.CENTER);
add(southPanel, BorderLayout.SOUTH);
pack();
setSize(1000, 700);
setVisible(true);
setResizable(false);
this.validate();
}
public boolean connectToServer(String serverIP, int serverPort) throws Exception {
try {
clientSocket = new Socket(serverIP, serverPort);
inputStream = new DataInputStream(clientSocket.getInputStream());
outputStream = new DataOutputStream(clientSocket.getOutputStream());
ClientThread clientThread = new ClientThread(this);
clientThread.start();
isOnChat = true;
return true;
} catch (IOException ex) {
userChatPad.chatTextArea.setText("Sorry,无法连接!!!\n");
}
return false;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == userControlPad.connectButton) {
host = chessBoard.host = userControlPad.ipInputted.getText();
try {
if (connectToServer(host, port)) {
userChatPad.chatTextArea.setText("");
userControlPad.connectButton.setEnabled(false);
userControlPad.createButton.setEnabled(true);
userControlPad.joinButton.setEnabled(true);
chessBoard.statusText.setText("连接成功,请等待!!!");
}
} catch (Exception ei) {
userChatPad.chatTextArea.setText("Sorry,不能连接!!!\n");
}
}
if (e.getSource() == userControlPad.exitButton) {
if (isOnChat) {
try {
clientSocket.close();
}
catch (Exception ed){}
}
if (isOnChess || isGameConnected) {
try {
chessBoard.chessSocket.close();
}
catch (Exception ee){}
}
System.exit(0);
}
if (e.getSource() == userControlPad.joinButton) {
String selectedUser = userListPad.userList.getSelectedItem();
if (selectedUser == null || selectedUser.startsWith("[inchess]") ||
selectedUser.equals(chessClientName)) {
chessBoard.statusText.setText("必须选择一个用户!");
} else {
try {
if (!isGameConnected) {
if (chessBoard.connectServer(chessBoard.host, chessBoard.port)) {
isGameConnected = true;
isOnChess = true;
isParticipant = true;
userControlPad.createButton.setEnabled(false);
userControlPad.joinButton.setEnabled(false);
userControlPad.cancelButton.setEnabled(true);
chessBoard.chessThread.sendMessage("/joingame "
+ userListPad.userList.getSelectedItem() + " "
+ chessClientName);
}
} else {
isOnChess = true;
isParticipant = true;
userControlPad.createButton.setEnabled(false);
userControlPad.joinButton.setEnabled(false);
userControlPad.cancelButton.setEnabled(true);
chessBoard.chessThread.sendMessage("/joingame "
+ userListPad.userList.getSelectedItem() + " "
+ chessClientName);
}
} catch (Exception ee) {
isGameConnected = false;
isOnChess = false;
isParticipant = false;
userControlPad.createButton.setEnabled(true);
userControlPad.joinButton.setEnabled(true);
userControlPad.cancelButton.setEnabled(false);
userChatPad.chatTextArea.setText("不能连接: \n" + ee);
}
}
}
if (e.getSource() == userControlPad.createButton) {
try {
if (!isGameConnected) {
if (chessBoard.connectServer(chessBoard.host, chessBoard.port)) {
isGameConnected = true;
isOnChess = true;
isCreator = true;
userControlPad.createButton.setEnabled(false);
userControlPad.joinButton.setEnabled(false);
userControlPad.cancelButton.setEnabled(true);
chessBoard.chessThread.sendMessage("/creatgame " + "[inchess]" + chessClientName);
}
} else {
isOnChess = true;
isCreator = true;
userControlPad.createButton.setEnabled(false);
userControlPad.joinButton.setEnabled(false);
userControlPad.cancelButton.setEnabled(true);
chessBoard.chessThread.sendMessage("/creatgame "
+ "[inchess]" + chessClientName);
}
} catch (Exception ec) {
isGameConnected = false;
isOnChess = false;
isCreator = false;
userControlPad.createButton.setEnabled(true);
userControlPad.joinButton.setEnabled(true);
userControlPad.cancelButton.setEnabled(false);
ec.printStackTrace();
userChatPad.chatTextArea.setText("Sorry,不能连接: \n" + ec);
}
}
if (e.getSource() == userControlPad.cancelButton) {
if (isOnChess) {
chessBoard.chessThread.sendMessage("/giveup " + chessClientName);
chessBoard.setVicStatus(-1 * chessBoard.chessColor);
userControlPad.createButton.setEnabled(true);
userControlPad.joinButton.setEnabled(true);
userControlPad.cancelButton.setEnabled(false);
chessBoard.statusText.setText("请选择创建房间或加入游戏!!!");
} if (!isOnChess) {
userControlPad.createButton.setEnabled(true);
userControlPad.joinButton.setEnabled(true);
userControlPad.cancelButton.setEnabled(false);
chessBoard.statusText.setText("请选择创建房间或加入游戏!!!");
}
isParticipant = isCreator = false;
}
}
@Override
public void keyPressed(KeyEvent e) {
TextField inputwords = (TextField) e.getSource();
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
if (userInputPad.userChoice.getSelectedItem().equals("所有用户")) {
try {
outputStream.writeUTF(inputwords.getText());
inputwords.setText("");
} catch (Exception ea) {
userChatPad.chatTextArea.setText("Sorry,不能连接到服务器!\n");
userListPad.userList.removeAll();
userInputPad.userChoice.removeAll();
inputwords.setText("");
userControlPad.connectButton.setEnabled(true);
}
} else {
try {
outputStream.writeUTF("/" + userInputPad.userChoice.getSelectedItem()
+ " " + inputwords.getText());
inputwords.setText("");
} catch (Exception ea) {
userChatPad.chatTextArea.setText("Sorry,不能连接到服务器!\n");
userListPad.userList.removeAll();
userInputPad.userChoice.removeAll();
inputwords.setText("");
userControlPad.connectButton.setEnabled(true);
}
}
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
public static void main(String[] args) {
new ClientChess();
}
}
棋盘五子棋核心算法类
public class ChessBoard extends Panel implements MouseListener, ActionListener {
public int dis = 30;
public boolean isMouseEnabled = false;
public boolean isWon = false;
public int chessX_POS = -1;
public int chessY_POS = -1;
public int chessColor = 1;
public int chessBlack_XPOS[] = new int[200];
public int chessBlack_YPOS[] = new int[200];
public int chessWhite_XPOS[] = new int[200];
public int chessWhite_YPOS[] = new int[200];
public int chessBlackCount = 0;
public int chessWhiteCount = 0;
public int chessBlackVicTimes = 0;
public int chessWhiteVicTimes = 0;
public Socket chessSocket;
protected DataInputStream inputData;
protected DataOutputStream outputData;
public String chessSelfName = null;
public String chessPeerName = null;
public String host = null;
public int port = 1234;
public TextField statusText = new TextField("请先连接服务器!!!");
public ChessThread chessThread = new ChessThread(this);
public ChessBoard() {
setSize(600, 600);
setLayout(null);
setBackground(new Color(205, 133, 63));
addMouseListener(this);
add(statusText);
statusText.setBounds(new Rectangle(80, 5, 440, 24));
statusText.setEditable(false);
}
public boolean connectServer(String ServerIP, int ServerPort) throws Exception {
try {
chessSocket = new Socket(ServerIP, ServerPort);
inputData = new DataInputStream(chessSocket.getInputStream());
outputData = new DataOutputStream(chessSocket.getOutputStream());
chessThread.start();
return true;
} catch (IOException ex) {
statusText.setText("sorry,连接失败!!! \n");
}
return false;
}
public void setVicStatus(int vicChessColor) {
this.removeAll();
for (int i = 0; i <= chessBlackCount; i++) {
chessBlack_XPOS[i] = 0;
chessBlack_YPOS[i] = 0;
}
for (int i = 0; i <= chessWhiteCount; i++) {
chessWhite_XPOS[i] = 0;
chessWhite_YPOS[i] = 0;
}
chessBlackCount = 0;
chessWhiteCount = 0;
add(statusText);
statusText.setBounds(40, 5, 200, 24);
if (vicChessColor == 1) {
chessBlackVicTimes++;
statusText.setText("恭喜黑方胜!!!黑VS白 " + chessBlackVicTimes + ":" + chessWhiteVicTimes
+ ".游戏重启,等待白方...");
} else if (vicChessColor == -1) {
chessWhiteVicTimes++;
statusText.setText("恭喜白方胜!!!黑VS白 " + chessBlackVicTimes + ":" + chessWhiteVicTimes
+ ".游戏重启,等待黑方...");
}
}
public void setLocation(int xPos, int yPos, int chessColor) {
if (chessColor == 1) {
chessBlack_XPOS[chessBlackCount] = xPos * dis;
chessBlack_YPOS[chessBlackCount] = yPos * dis;
chessBlackCount++;
} else if (chessColor == -1) {
chessWhite_XPOS[chessWhiteCount] = xPos * dis;
chessWhite_YPOS[chessWhiteCount] = yPos * dis;
chessWhiteCount++;
}
}
public boolean checkVicStatus(int xPos, int yPos, int chessColor) {
int chessLinkedCount = 1;
int chessLinkedCompare = 1;
int chessToCompareIndex = 0;
int closeGrid = 1;
if (chessColor == 1) {
chessLinkedCount = 1;
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 = chessLinkedCount + 1;
if (chessLinkedCount == 5) {
return true;
}
}
}
if (chessLinkedCount == (chessLinkedCompare + 1)) {
chessLinkedCompare++;
} else {
break;
}
}
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;
}
}
chessLinkedCount = 1;
chessLinkedCompare = 1;
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;
}
}
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++) {
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++) {
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;
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;
}
}
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;
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;
}
}
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;
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;
}
}
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;
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;
}
}
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;
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;
}
}
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;
}
@Override
public void paint(Graphics g) {
for (int i = 40; i <= 580; i = i + 30) {
g.drawLine(40, i, 580, i);
}
for (int j = 40; j <= 580; j = j + 30) {
g.drawLine(j, 40, j, 580);
}
g.fillOval(127, 127, 8, 8);
g.fillOval(487, 127, 8, 8);
g.fillOval(127, 487, 8, 8);
g.fillOval(487, 487, 8, 8);
g.fillOval(307, 307, 8, 8);
}
public void paintChessPoint(int xPos, int yPos, int chessColor) {
ChessBlack chessBlack = new ChessBlack(this);
ChessWhite chessWhite = new ChessWhite(this);
if (chessColor == 1 && isMouseEnabled) {
setLocation(xPos, yPos, chessColor);
isWon = checkVicStatus(xPos, yPos, chessColor);
if (isWon == false) {
chessThread.sendMessage("/" + chessPeerName + " /chess "
+ xPos + " " + yPos + " " + chessColor);
this.add(chessBlack);
chessBlack.setBounds(xPos * dis -4, yPos * dis -3, 30, 30);
statusText.setText("黑(第" + chessBlackCount + "步)" + xPos + " " + yPos + ",轮到白方.");
isMouseEnabled = false;
} else {
chessThread.sendMessage("/" + chessPeerName + " /chess "
+ xPos + " " + yPos + " " + chessColor);
this.add(chessBlack);
chessBlack.setBounds(xPos * dis -4, yPos * dis -3, 30, 30);
setVicStatus(1);
isMouseEnabled = false;
}
} else if (chessColor == -1 && isMouseEnabled) {
setLocation(xPos, yPos, chessColor);
isWon = checkVicStatus(xPos, yPos, chessColor);
if (isWon == false) {
chessThread.sendMessage("/" + chessPeerName + " /chess "
+ xPos + " " + yPos + " " + chessColor);
this.add(chessWhite);
chessWhite.setBounds(xPos * dis -4, yPos * dis-3 , 30, 30);
statusText.setText("白(第" + chessWhiteCount + "步)" + xPos + " " + yPos + ",轮到黑方.");
isMouseEnabled = false;
} else {
chessThread.sendMessage("/" + chessPeerName + " /chess "
+ xPos + " " + yPos + " " + chessColor);
this.add(chessWhite);
chessWhite.setBounds(xPos * dis-4 , yPos * dis -3, 30, 30);
setVicStatus(-1);
isMouseEnabled = false;
}
}
}
public void paintNetChessPoint(int xPos, int yPos, int chessColor) {
ChessBlack chessBlack = new ChessBlack(this);
ChessWhite chessWhite = new ChessWhite(this);
setLocation(xPos, yPos, chessColor);
if (chessColor == 1) {
isWon = checkVicStatus(xPos, yPos, chessColor);
if (isWon == false) {
this.add(chessBlack);
chessBlack.setBounds(xPos * dis-4 , yPos * dis -3, 30, 30);
statusText.setText("黑(第" + chessBlackCount + "步)" + xPos + " " + yPos + ",轮到白方.");
isMouseEnabled = true;
} else {
chessThread.sendMessage("/" + chessPeerName + " /victory " + chessColor);
this.add(chessBlack);
chessBlack.setBounds(xPos * dis -4, yPos * dis-3, 30, 30);
setVicStatus(1);
isMouseEnabled = true;
}
} else if (chessColor == -1) {
isWon = checkVicStatus(xPos, yPos, chessColor);
if (isWon == false) {
this.add(chessWhite);
chessWhite.setBounds(xPos * dis-4, yPos * dis-3, 30, 30);
statusText.setText("白(第" + chessWhiteCount + "步)" + xPos + " " + yPos + ",轮到黑方.");
isMouseEnabled = true;
} else {
chessThread.sendMessage("/" + chessPeerName + " /victory " + chessColor);
this.add(chessWhite);
chessWhite.setBounds(xPos * dis-4, yPos * dis-3, 30, 30);
setVicStatus(-1);
isMouseEnabled = true;
}
}
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
chessX_POS = e.getX();
System.out.println(chessX_POS);
chessY_POS = e.getY();
System.out.println(chessY_POS);
int a = (chessX_POS) / dis, b = (chessY_POS) / dis;
System.out.println("a="+a);
System.out.println("b="+b);
if (chessX_POS / dis < 2 || chessY_POS / dis < 2
|| chessX_POS / dis > 19 || chessY_POS / dis > 19) {
} else {
paintChessPoint(a, b, chessColor);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
}
总结
通过此次的《五子棋》游戏实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。
java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。
源码获取
可关注博主后,私聊博主获取
今天是持续写作的第 3 / 100 天。 可以关注我,点赞我、评论我、收藏我啦。
|