java五子棋代码解析 java五子棋的简单思路( 四 )


* count4++; i4++; } i4 = 1; while (color == allChess[x - i4][y - i4]) {
* count4++; i4++; } if (count4 = 5) { flag = true ; }
*/
// 判断横向
count = this.checkCount(1, 0, color);
if (count = 5) {
flag = true;
} else {
// 判断纵向
count = this.checkCount(0, 1, color);
if (count = 5) {
flag = true;
} else {
// 判断右上、左下
count = this.checkCount(1, -1, color);
if (count = 5) {
flag = true;
} else {
// 判断右下、左上
count = this.checkCount(1, 1, color);
if (count = 5) {
flag = true;
}
}
}
}
return flag;
}
// 判断棋子连接的数量
private int checkCount(int xChange, int yChange, int color) {
int count = 1;
int tempX = xChange;
int tempY = yChange;
while (x + xChange = 0x + xChange = 18y + yChange = 0
y + yChange = 18
color == allChess[x + xChange][y + yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange0)
yChange++;
else {
yChange--;
}
}
}
xChange = tempX;
yChange = tempY;
while (x - xChange = 0x - xChange = 18y - yChange = 0
y - yChange = 18
color == allChess[x - xChange][y - yChange]) {
count++;
if (xChange != 0)
xChange++;
if (yChange != 0) {
if (yChange0)
yChange++;
else {
yChange--;
}
}
}
return count;
}
public void run() {
// TODO Auto-generated method stub
// 判断是否有时间限制
if (maxTime0) {
while (true) {
if (isBlack) {
blackTime--;
if (blackTime == 0) {
JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!");
}
} else {
whiteTime--;
if (whiteTime == 0) {
JOptionPane.showMessageDialog(this, "白方超时,游戏结束!");
}
}
blackMessage = blackTime / 3600 + ":"
+ (blackTime / 60 - blackTime / 3600 * 60) + ":"
+ (blackTime - blackTime / 60 * 60);
whiteMessage = whiteTime / 3600 + ":"
+ (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
+ (whiteTime - whiteTime / 60 * 60);
this.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(blackTime + " -- " + whiteTime);
}
}
}
}
java五子棋代码带详细解释浩大的工程 你有五子棋程序 如果你水平还行的话你参照这个聊天室程序应该也比较容易写出人人对战的
package Chat;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.StringTokenizer;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
/**
* 聊天室的客户端程序,GUI界面 。
*/
public class ChatClient extends JFrame implements ActionListener{
// 登陆聊天室的名字标签和输入框
JLabel nameLabel = new JLabel();
JTextField nameTextField = new JTextField(15);
// 连接和断开连接的按钮

推荐阅读