小弟做毕业设计
网上当的五子棋判断输赢的算法太长,
看MLDN魔乐JAVA培训课程游戏五子棋-有一个挺好的算法
加上就是不能判断输赢
请路过的大侠看看帮帮忙!!!!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;public class ChessPad extends Frame implements MouseListener,
ActionListener { // 其中数据的内容 0:表示这个点并没有棋子, 1: 表示这个点是黑子, 2: 表示这个点是白子
int[][] allChess = new int[19][19]; int chessPoint_x = -1, chessPoint_y = -1;
int chessColor = 1; // 分别保存黑白棋子数目的变量: chessBlackCount chessWhiteCount
int chessBlackCount = 0, chessWhiteCount = 0;
int chessBlackWin = 0, chessWhiteWin = 0; boolean isWin = false; TextField statusText = new TextField(" "); public ChessPad() {
super("五子棋");
setSize(440, 440);
setLayout(null);
setBackground(Color.green);
addMouseListener(this);
add(statusText);
statusText.setBounds(40, 5, 360, 24);
statusText.setEditable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
} public void clear() {
this.removeAll();
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] != 0) {
allChess[i][j] = 0;
}
}
}
chessBlackCount = 0;
chessWhiteCount = 0;
add(statusText);
statusText.setBounds(40, 5, 360, 24);
System.out.println("我 完成了 clear()");
} public void chessVictory(int chessColorWin) {
this.removeAll();
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] != 0) {
allChess[i][j] = 0;
}
}
} chessBlackCount = 0;
chessWhiteCount = 0;
add(statusText);
statusText.setBounds(40, 5, 360, 24); if (chessColorWin == 1) {
chessBlackWin++;
statusText.setText("黑棋胜,黑:白为" + chessBlackWin + ":" + chessWhiteWin
+ ",重新开局,等待白棋下子...");
} else if (chessColorWin == -1) {
chessWhiteWin++;
statusText.setText("白棋胜,黑:白为" + chessBlackWin + ":" + chessWhiteWin
+ ",重新开局,等待黑棋下子...");
}
} public void getLocation(int a, int b, int color) {
if (color == 1) {
if (allChess[a][b] == 0) {
allChess[a][b] = 1;
}
chessBlackCount++;
} else if (color == -1) {
if (allChess[a][b] == 0) {
allChess[a][b] = -1;
}
chessWhiteCount++;
}
} private boolean checkWin(int a, int b) {
boolean flag = false;
int count = 1;
int color = allChess[a][b];
count = this.checkCount(a, b, 1, 0, color);
if (count >= 5) {
flag = true;
} else {
count = this.checkCount(a, b, 0, 1, color);
if (count >= 5) {
flag = true;
} else {
count = this.checkCount(a, b, 1, -1, color);
if (count >= 5) {
flag = true;
} else {
count = this.checkCount(a, b, 1, 1, color);
if (count >= 5)
flag = true;
}
}
} return flag;
} // 判断棋子相连的数量
private int checkCount(int x, int y, int xChange, int yChange, int color) {
int count = 1;
int tempX = xChange;
int tempY = yChange;
while (((x + xChange) < 19) && ((y + yChange) < 19)
&& ((x + xChange) >= 0) && ((y + yChange) >= 0)
&& color == allChess[x + xChange][y + yChange]) {
count++;
if (xChange != 0) {
xChange++;
}
if (yChange != 0) {
if (yChange > 0) {
yChange++;
} else {
yChange--;
}
}
}
xChange = tempX;
yChange = tempY; while (((x - xChange) < 19) && ((y - yChange) < 19)
&& ((x - xChange) >= 0) && ((y - yChange) >= 0)
&& color == allChess[x - xChange][y - yChange]) {
count++;
if (xChange != 0) {
xChange++;
}
if (yChange != 0) {
if (yChange > 0) {
yChange++;
} else {
yChange--;
}
} }
return count;
} public void paint(Graphics g) {
for (int i = 40; i <= 380; i = i + 20) {
g.drawLine(40, i, 400, i);
}
g.drawLine(40, 400, 400, 400);
for (int j = 40; j <= 380; j = j + 20) {
g.drawLine(j, 40, j, 400);
}
g.drawLine(400, 40, 400, 400);
g.fillOval(97, 97, 6, 6);
g.fillOval(337, 97, 6, 6);
g.fillOval(97, 337, 6, 6);
g.fillOval(337, 337, 6, 6);
g.fillOval(217, 217, 6, 6);
} public void chessPaint(int chessPoint_a, int chessPoint_b, int x, int y,
int color) { chessPoint_black chesspoint_black = new chessPoint_black(this);
chessPoint_white chesspoint_white = new chessPoint_white(this); if (color == 1) {
isWin = checkWin(chessPoint_a, chessPoint_b);
if (isWin == false) {
this.add(chesspoint_black);
chesspoint_black.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
statusText.setText("黑(第" + chessBlackCount + "步)" + x + " " + y
+ ",请白棋下子"); } else {
this.add(chesspoint_black);
chesspoint_black.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
chessVictory(1); }
} else if (color == -1) { isWin = checkWin(chessPoint_a, chessPoint_b);
if (isWin == false) {
this.add(chesspoint_white);
chesspoint_white.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
statusText.setText("白(第" + chessWhiteCount + "步)" + x + " " + y
+ ",请黑棋下子"); } else {
this.add(chesspoint_white);
chesspoint_white.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
chessVictory(-1); }
}
} public void mousePressed(MouseEvent e) { chessPoint_x = (int) e.getX();
chessPoint_y = (int) e.getY(); int x = (chessPoint_x + 10) / 20, y = (chessPoint_y + 10) / 20; int a = chessPoint_x / 20 - 2;
int b = chessPoint_y / 20 - 2; // System.out.println(" x= "+x+" y= "+y+" a= "+a+" b= "+b); if (a < 0 || b < 0 || a > 18 || b > 18) {
} else {
if (allChess[a][b] == 0) {
if (chessColor == 1) {
allChess[a][b] = 1;
chessBlackCount++;
}
if (chessColor == -1) {
allChess[a][b] = -1;
chessWhiteCount++;
}
}
chessPaint(a, b, x, y, chessColor);
if(chessColor==1){
chessColor = -1;
}else{
chessColor = 1;
}
}
}
public static void main(String[] args){
new ChessPad();
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseClicked(MouseEvent e) {
} public void actionPerformed(ActionEvent e) { }
}// Canvas ---- 组件表示屏幕上一个空白矩形区域,应用程序可以在该区域内绘图
class chessPoint_black extends Canvas implements MouseListener {
// class chessPoint_black extends Canvas {
ChessPad chesspad = null;
Image image;
Toolkit tool; chessPoint_black(ChessPad p) {
setSize(20, 20);
chesspad = p;
// 获取此组件的工具包
tool = getToolkit();
// 返回一幅图像,该图像从指定文件中获取像素数据,图像格式可以是 GIF、JPEG 或 PNG。
image = tool.getImage("heiqi.gif");
addMouseListener(this);
} public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval(0, 0, 14, 14);
// g.drawImage(image,0,0,48,54,this);
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseClicked(MouseEvent e) {
}
}class chessPoint_white extends Canvas implements MouseListener {
ChessPad chesspad = null;
Image image;
Toolkit tool; chessPoint_white(ChessPad p) { setSize(20, 20);
tool = getToolkit();
image = tool.getImage("baiqi.gif");
addMouseListener(this);
chesspad = p;
} public void paint(Graphics g) {
g.setColor(Color.white);
g.fillOval(0, 0, 14, 14);
// g.drawImage(image,0,0,48,54,this);
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseClicked(MouseEvent e) {
}
}
网上当的五子棋判断输赢的算法太长,
看MLDN魔乐JAVA培训课程游戏五子棋-有一个挺好的算法
加上就是不能判断输赢
请路过的大侠看看帮帮忙!!!!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;public class ChessPad extends Frame implements MouseListener,
ActionListener { // 其中数据的内容 0:表示这个点并没有棋子, 1: 表示这个点是黑子, 2: 表示这个点是白子
int[][] allChess = new int[19][19]; int chessPoint_x = -1, chessPoint_y = -1;
int chessColor = 1; // 分别保存黑白棋子数目的变量: chessBlackCount chessWhiteCount
int chessBlackCount = 0, chessWhiteCount = 0;
int chessBlackWin = 0, chessWhiteWin = 0; boolean isWin = false; TextField statusText = new TextField(" "); public ChessPad() {
super("五子棋");
setSize(440, 440);
setLayout(null);
setBackground(Color.green);
addMouseListener(this);
add(statusText);
statusText.setBounds(40, 5, 360, 24);
statusText.setEditable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
} public void clear() {
this.removeAll();
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] != 0) {
allChess[i][j] = 0;
}
}
}
chessBlackCount = 0;
chessWhiteCount = 0;
add(statusText);
statusText.setBounds(40, 5, 360, 24);
System.out.println("我 完成了 clear()");
} public void chessVictory(int chessColorWin) {
this.removeAll();
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] != 0) {
allChess[i][j] = 0;
}
}
} chessBlackCount = 0;
chessWhiteCount = 0;
add(statusText);
statusText.setBounds(40, 5, 360, 24); if (chessColorWin == 1) {
chessBlackWin++;
statusText.setText("黑棋胜,黑:白为" + chessBlackWin + ":" + chessWhiteWin
+ ",重新开局,等待白棋下子...");
} else if (chessColorWin == -1) {
chessWhiteWin++;
statusText.setText("白棋胜,黑:白为" + chessBlackWin + ":" + chessWhiteWin
+ ",重新开局,等待黑棋下子...");
}
} public void getLocation(int a, int b, int color) {
if (color == 1) {
if (allChess[a][b] == 0) {
allChess[a][b] = 1;
}
chessBlackCount++;
} else if (color == -1) {
if (allChess[a][b] == 0) {
allChess[a][b] = -1;
}
chessWhiteCount++;
}
} private boolean checkWin(int a, int b) {
boolean flag = false;
int count = 1;
int color = allChess[a][b];
count = this.checkCount(a, b, 1, 0, color);
if (count >= 5) {
flag = true;
} else {
count = this.checkCount(a, b, 0, 1, color);
if (count >= 5) {
flag = true;
} else {
count = this.checkCount(a, b, 1, -1, color);
if (count >= 5) {
flag = true;
} else {
count = this.checkCount(a, b, 1, 1, color);
if (count >= 5)
flag = true;
}
}
} return flag;
} // 判断棋子相连的数量
private int checkCount(int x, int y, int xChange, int yChange, int color) {
int count = 1;
int tempX = xChange;
int tempY = yChange;
while (((x + xChange) < 19) && ((y + yChange) < 19)
&& ((x + xChange) >= 0) && ((y + yChange) >= 0)
&& color == allChess[x + xChange][y + yChange]) {
count++;
if (xChange != 0) {
xChange++;
}
if (yChange != 0) {
if (yChange > 0) {
yChange++;
} else {
yChange--;
}
}
}
xChange = tempX;
yChange = tempY; while (((x - xChange) < 19) && ((y - yChange) < 19)
&& ((x - xChange) >= 0) && ((y - yChange) >= 0)
&& color == allChess[x - xChange][y - yChange]) {
count++;
if (xChange != 0) {
xChange++;
}
if (yChange != 0) {
if (yChange > 0) {
yChange++;
} else {
yChange--;
}
} }
return count;
} public void paint(Graphics g) {
for (int i = 40; i <= 380; i = i + 20) {
g.drawLine(40, i, 400, i);
}
g.drawLine(40, 400, 400, 400);
for (int j = 40; j <= 380; j = j + 20) {
g.drawLine(j, 40, j, 400);
}
g.drawLine(400, 40, 400, 400);
g.fillOval(97, 97, 6, 6);
g.fillOval(337, 97, 6, 6);
g.fillOval(97, 337, 6, 6);
g.fillOval(337, 337, 6, 6);
g.fillOval(217, 217, 6, 6);
} public void chessPaint(int chessPoint_a, int chessPoint_b, int x, int y,
int color) { chessPoint_black chesspoint_black = new chessPoint_black(this);
chessPoint_white chesspoint_white = new chessPoint_white(this); if (color == 1) {
isWin = checkWin(chessPoint_a, chessPoint_b);
if (isWin == false) {
this.add(chesspoint_black);
chesspoint_black.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
statusText.setText("黑(第" + chessBlackCount + "步)" + x + " " + y
+ ",请白棋下子"); } else {
this.add(chesspoint_black);
chesspoint_black.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
chessVictory(1); }
} else if (color == -1) { isWin = checkWin(chessPoint_a, chessPoint_b);
if (isWin == false) {
this.add(chesspoint_white);
chesspoint_white.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
statusText.setText("白(第" + chessWhiteCount + "步)" + x + " " + y
+ ",请黑棋下子"); } else {
this.add(chesspoint_white);
chesspoint_white.setBounds(x * 20 - 7, y * 20 - 7, 16, 16);
chessVictory(-1); }
}
} public void mousePressed(MouseEvent e) { chessPoint_x = (int) e.getX();
chessPoint_y = (int) e.getY(); int x = (chessPoint_x + 10) / 20, y = (chessPoint_y + 10) / 20; int a = chessPoint_x / 20 - 2;
int b = chessPoint_y / 20 - 2; // System.out.println(" x= "+x+" y= "+y+" a= "+a+" b= "+b); if (a < 0 || b < 0 || a > 18 || b > 18) {
} else {
if (allChess[a][b] == 0) {
if (chessColor == 1) {
allChess[a][b] = 1;
chessBlackCount++;
}
if (chessColor == -1) {
allChess[a][b] = -1;
chessWhiteCount++;
}
}
chessPaint(a, b, x, y, chessColor);
if(chessColor==1){
chessColor = -1;
}else{
chessColor = 1;
}
}
}
public static void main(String[] args){
new ChessPad();
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseClicked(MouseEvent e) {
} public void actionPerformed(ActionEvent e) { }
}// Canvas ---- 组件表示屏幕上一个空白矩形区域,应用程序可以在该区域内绘图
class chessPoint_black extends Canvas implements MouseListener {
// class chessPoint_black extends Canvas {
ChessPad chesspad = null;
Image image;
Toolkit tool; chessPoint_black(ChessPad p) {
setSize(20, 20);
chesspad = p;
// 获取此组件的工具包
tool = getToolkit();
// 返回一幅图像,该图像从指定文件中获取像素数据,图像格式可以是 GIF、JPEG 或 PNG。
image = tool.getImage("heiqi.gif");
addMouseListener(this);
} public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval(0, 0, 14, 14);
// g.drawImage(image,0,0,48,54,this);
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseClicked(MouseEvent e) {
}
}class chessPoint_white extends Canvas implements MouseListener {
ChessPad chesspad = null;
Image image;
Toolkit tool; chessPoint_white(ChessPad p) { setSize(20, 20);
tool = getToolkit();
image = tool.getImage("baiqi.gif");
addMouseListener(this);
chesspad = p;
} public void paint(Graphics g) {
g.setColor(Color.white);
g.fillOval(0, 0, 14, 14);
// g.drawImage(image,0,0,48,54,this);
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mouseClicked(MouseEvent e) {
}
}
static boolean judge(int a[][],int color){
int i,j,flag;
for(i=0;i<19;i++){
flag=0;
for(j=0;j<19;j++)
if(a[i][j]==color){
flag++;
if (flag==5)
return true;}
else flag=0;
}
for(j=0;j<19;j++){
flag=0;
for(i=0;i<19;i++)
if(a[i][j]==color)
{flag++;
if(flag==5)
return true;}
else flag=0;
}
for(j=4;j<19;j++){
flag=0; int m=j;
for(i=0;i<=j;i++){
if(a[i][m--]==color){
flag++;
if(flag==5)
return true;}
else flag=0;}
}
for(j=14;j>=0;j--){
flag=0; int m=j;
for(i=0;i<=18-j;i++){
if(a[i][m++]==color){
flag++;
if(flag==5)
return true;}
else flag=0;}
}
for(i=14;i>=0;i--){
flag=0; int n=i;
for(j=0;j<19-i;j++){
if(a[n++][j]==color){
flag++;
if(flag==5)
return true;}
else flag=0;}
}
for(j=14;j>=0;j--){
flag=0; int m=j;
for(i=18;i>=j;i--){
if(a[i][m++]==color){
flag++;
if(flag==5)
return true;}
else flag=0;}
} return false;}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;import javax.swing.*;/**
* <p>
* Title : 五子棋联网版(Server端)
* </p>
*
* <p>
* Description : 运用Swing,Graphics,MouseListener实现
* </p>
*
* @author Julian
*/public class FiveServer extends JPanel implements MouseListener, Runnable { private JFrame frame; // 主界面
private int amount = 16; // 设置棋盘划分网格数
private int distance = 30; // 设置棋盘网格大小
private int boardSize = amount * distance; // 设置棋盘大小
private int boardX = 30, boardY = 30; // 设置棋盘原点X,Y坐标
private int locationX; // 设置frame原点X坐标
private int locationY; // 设置frame原点Y坐标
private int border[][] = new int[amount][amount];// 网格数组,保存棋子状态,无棋子:0,黑棋子:1,白棋子:-1
private Color color = Color.BLACK;// 控制棋子颜色
private int pointX, pointY;// border数组X,Y下标
private boolean flag = true; private BufferedReader br = null;
private Writer w = null; // 构造函数
FiveServer() {
frame = new JFrame("五子棋服务端");
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
border[x][y] = 0;
}
}
frame.setSize(768, 576);
locationX = (Toolkit.getDefaultToolkit().getScreenSize().width - frame
.getWidth()) / 2;
locationY = (Toolkit.getDefaultToolkit().getScreenSize().height - frame
.getHeight()) / 2;
frame.setLocation(locationX, locationY);
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(this);
frame.setVisible(true);
} // 棋盘界面实现方法
public void paintComponent(Graphics g) { // 画棋盘网格
for (int x = boardX; x <= boardSize + boardX; x += distance) {
g.drawLine(x, boardY, x, boardSize + boardY);
}
for (int y = boardY; y <= boardSize + boardY; y += distance) {
g.drawLine(boardX, y, boardSize + boardX, y);
} // 画棋子
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
if (border[x][y] == 1) {
g.drawOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
g.setColor(Color.BLACK);
g.fillOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
}
if (border[x][y] == -1) {
g.drawOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
g.setColor(Color.WHITE);
g.fillOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
}
}
}
} private void init() {
try {
ServerSocket server = new ServerSocket(7777);
Socket socket = server.accept(); InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream(); Reader r = new InputStreamReader(is);
w = new OutputStreamWriter(os);
br = new BufferedReader(r);
} catch (IOException e) {
e.printStackTrace();
}
} private String get() {
String str = null;
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
} private void put(String str) {
try {
w.write(str + "\n");
w.flush();
} catch (IOException e) {
e.printStackTrace();
}
} public void run() {
String str = null;
while (!"end".equals(str = this.get())) {
String temp[] = str.split(",");
pointX = Integer.valueOf(temp[0]);
pointY = Integer.valueOf(temp[1]);
border[pointX][pointY] = -1;
this.repaint();
this.gameover(pointX, pointY);
flag = true;
}
} // 五子连线判定方法
public void gameover(int pointX, int pointY) {
int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
boolean l1 = true, l2 = true, l3 = true, l4 = true, l5 = true, l6 = true, l7 = true, l8 = true;
for (int i = 1; i < 5; i++) {
// 竖直向上
if (pointY - i >= 0
&& border[pointX][pointY - i] == border[pointX][pointY]
&& l1) {
c1++;
} else {
l1 = false;
}
// 竖直向下
if (pointY + i <= 15
&& border[pointX][pointY + i] == border[pointX][pointY]
&& l2) {
c1++;
} else {
l2 = false;
}
// 水平向左
if (pointX - i >= 0
&& border[pointX - i][pointY] == border[pointX][pointY]
&& l3) {
c2++;
} else {
l3 = false;
}
// 水平向右
if (pointX + i <= 15
&& border[pointX + i][pointY] == border[pointX][pointY]
&& l4) {
c2++;
} else {
l4 = false;
} // 斜向左上
if (pointX - i >= 0 && pointY - i >= 0
&& border[pointX - i][pointY - i] == border[pointX][pointY]
&& l5) {
c3++;
} else {
l5 = false;
}
// 斜向右下
if (pointX + i <= 15 && pointY + i <= 15
&& border[pointX + i][pointY + i] == border[pointX][pointY]
&& l6) {
c3++;
} else {
l6 = false;
} // 斜向左下
if (pointX - i >= 0 && pointY + i <= 15
&& border[pointX - i][pointY + i] == border[pointX][pointY]
&& l7) {
c4++;
} else {
l7 = false;
}
// 斜向右上
if (pointX + i <= 15 && pointY - i >= 0
&& border[pointX + i][pointY - i] == border[pointX][pointY]
&& l8) {
c4++;
} else {
l8 = false;
}
} if (c1 >= 4 || c2 >= 4 || c3 >= 4 || c4 >= 4) {
if (border[pointX][pointY] == 1) {
JOptionPane.showMessageDialog(frame, "黑棋获胜!");
} else {
JOptionPane.showMessageDialog(frame, "白棋获胜!");
}
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
border[x][y] = 0;
}
}
}
frame.repaint();
} // 鼠标监听方法(Start)
public void mouseClicked(MouseEvent e) {
pointX = (e.getX() - boardX - frame.getInsets().left) / distance;
pointY = (e.getY() - boardY - frame.getInsets().top) / distance;
if (flag) {
if (e.getX() <= boardSize + boardX + frame.getInsets().left
&& e.getX() >= boardX + frame.getInsets().left
&& e.getY() <= boardSize + boardY + frame.getInsets().top
&& e.getY() >= boardY + frame.getInsets().top) {
if (border[pointX][pointY] == 0) {
if (color.equals(Color.BLACK)) {
border[pointX][pointY] = 1;
// color = Color.WHITE;
} else {
border[pointX][pointY] = -1;
// color = Color.BLACK;
}
} else {
JOptionPane.showMessageDialog(frame, "这里已经下过棋子了!");
}
frame.repaint();
String pustr = pointX + "," + pointY;
this.put(pustr);
this.gameover(pointX, pointY);
flag = false;
}
} else {
JOptionPane.showMessageDialog(frame, "轮到对方下棋!");
}
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} // 鼠标监听方法(End) // 单元测试方法
public static void main(String[] args) {
FiveServer fs = new FiveServer();
fs.init();
Thread t = new Thread(fs);
t.start();
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;import javax.swing.*;/**
* <p>
* Title : 五子棋联网版(Client端)
* </p>
*
* <p>
* Description : 运用Swing,Graphics,MouseListener实现
* </p>
*
* @author Julian
*/public class FiveClient extends JPanel implements MouseListener, Runnable { private JFrame frame; // 主界面
private int amount = 16; // 设置棋盘划分网格数
private int distance = 30; // 设置棋盘网格大小
private int boardSize = amount * distance; // 设置棋盘大小
private int boardX = 30, boardY = 30; // 设置棋盘原点X,Y坐标
private int locationX; // 设置frame原点X坐标
private int locationY; // 设置frame原点Y坐标
private int border[][] = new int[amount][amount];// 网格数组,保存棋子状态,无棋子:0,黑棋子:1,白棋子:-1
private Color color = Color.WHITE;// 控制棋子颜色
private int pointX, pointY;// border数组X,Y下标
private boolean flag = true; private BufferedReader br = null;
private Writer w = null; // 构造函数
FiveClient() {
frame = new JFrame("五子棋客户端");
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
border[x][y] = 0;
}
}
frame.setSize(768, 576);
locationX = (Toolkit.getDefaultToolkit().getScreenSize().width - frame
.getWidth()) / 2;
locationY = (Toolkit.getDefaultToolkit().getScreenSize().height - frame
.getHeight()) / 2;
frame.setLocation(locationX, locationY);
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(this);
frame.setVisible(true);
} // 棋盘界面实现方法
public void paintComponent(Graphics g) { // 画棋盘网格
for (int x = boardX; x <= boardSize + boardX; x += distance) {
g.drawLine(x, boardY, x, boardSize + boardY);
}
for (int y = boardY; y <= boardSize + boardY; y += distance) {
g.drawLine(boardX, y, boardSize + boardX, y);
} // 画棋子
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
if (border[x][y] == 1) {
g.drawOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
g.setColor(Color.BLACK);
g.fillOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
}
if (border[x][y] == -1) {
g.drawOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
g.setColor(Color.WHITE);
g.fillOval((x + 1) * distance + 1, (y + 1) * distance + 1,
distance - 2, distance - 2);
}
}
}
} private void init() {
try {
Socket socket = new Socket("127.0.0.1", 7777); InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream(); Reader r = new InputStreamReader(is);
w = new OutputStreamWriter(os);
br = new BufferedReader(r);
} catch (IOException e) {
e.printStackTrace();
}
} private String get() {
String str = null;
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
} private void put(String str) {
try {
w.write(str + "\n");
w.flush();
} catch (IOException e) {
e.printStackTrace();
}
} public void run() {
String str = null;
while (!"end".equals(str = this.get())) {
String temp[] = str.split(",");
pointX = Integer.valueOf(temp[0]);
pointY = Integer.valueOf(temp[1]);
border[pointX][pointY] = 1;
this.repaint();
this.gameover(pointX, pointY);
flag = true;
}
} // 五子连线判定方法
public void gameover(int pointX, int pointY) {
int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
boolean l1 = true, l2 = true, l3 = true, l4 = true, l5 = true, l6 = true, l7 = true, l8 = true;
for (int i = 1; i < 5; i++) {
// 竖直向上
if (pointY - i >= 0
&& border[pointX][pointY - i] == border[pointX][pointY]
&& l1) {
c1++;
} else {
l1 = false;
}
// 竖直向下
if (pointY + i <= 15
&& border[pointX][pointY + i] == border[pointX][pointY]
&& l2) {
c1++;
} else {
l2 = false;
}
// 水平向左
if (pointX - i >= 0
&& border[pointX - i][pointY] == border[pointX][pointY]
&& l3) {
c2++;
} else {
l3 = false;
}
// 水平向右
if (pointX + i <= 15
&& border[pointX + i][pointY] == border[pointX][pointY]
&& l4) {
c2++;
} else {
l4 = false;
} // 斜向左上
if (pointX - i >= 0 && pointY - i >= 0
&& border[pointX - i][pointY - i] == border[pointX][pointY]
&& l5) {
c3++;
} else {
l5 = false;
}
// 斜向右下
if (pointX + i <= 15 && pointY + i <= 15
&& border[pointX + i][pointY + i] == border[pointX][pointY]
&& l6) {
c3++;
} else {
l6 = false;
} // 斜向左下
if (pointX - i >= 0 && pointY + i <= 15
&& border[pointX - i][pointY + i] == border[pointX][pointY]
&& l7) {
c4++;
} else {
l7 = false;
}
// 斜向右上
if (pointX + i <= 15 && pointY - i >= 0
&& border[pointX + i][pointY - i] == border[pointX][pointY]
&& l8) {
c4++;
} else {
l8 = false;
}
} if (c1 >= 4 || c2 >= 4 || c3 >= 4 || c4 >= 4) {
if (border[pointX][pointY] == 1) {
JOptionPane.showMessageDialog(frame, "黑棋获胜!");
} else {
JOptionPane.showMessageDialog(frame, "白棋获胜!");
}
for (int x = 0; x < amount; x++) {
for (int y = 0; y < amount; y++) {
border[x][y] = 0;
}
}
}
frame.repaint();
} // 鼠标监听方法(Start)
public void mouseClicked(MouseEvent e) {
pointX = (e.getX() - boardX - frame.getInsets().left) / distance;
pointY = (e.getY() - boardY - frame.getInsets().top) / distance;
if (flag) {
if (e.getX() <= boardSize + boardX + frame.getInsets().left
&& e.getX() >= boardX + frame.getInsets().left
&& e.getY() <= boardSize + boardY + frame.getInsets().top
&& e.getY() >= boardY + frame.getInsets().top) {
if (border[pointX][pointY] == 0) {
if (color.equals(Color.BLACK)) {
border[pointX][pointY] = 1;
// color = Color.WHITE;
} else {
border[pointX][pointY] = -1;
// color = Color.BLACK;
}
} else {
JOptionPane.showMessageDialog(frame, "这里已经下过棋子了!");
}
frame.repaint();
String pustr = pointX + "," + pointY;
this.put(pustr);
this.gameover(pointX, pointY);
flag = false;
}
} else {
JOptionPane.showMessageDialog(frame, "轮到对方下棋!");
}
} public void mouseEntered(MouseEvent e) {
} public void mouseExited(MouseEvent e) {
} public void mousePressed(MouseEvent e) {
} public void mouseReleased(MouseEvent e) {
} // 鼠标监听方法(End) // 单元测试方法
public static void main(String[] args) {
FiveClient fc = new FiveClient();
fc.init();
Thread t = new Thread(fc);
t.start();
}}
private int checkCount(int x, int y, int xChange, int yChange, int color)
问题在这个东西里面
错误我已找到:请观察对否! public void mousePressed(MouseEvent e) { chessPoint_x = (int) e.getX();
chessPoint_y = (int) e.getY(); int x = (chessPoint_x + 10) / 20, y = (chessPoint_y + 10) / 20; // int a = chessPoint_x / 20 - 2;
// int b = chessPoint_y / 20 - 2;
//以上删掉 if ( x< 0 || y < 0 || x > 18 || y > 18) {
} else {
if (allChess[x][y] == 0) {
if (chessColor == 1) {
allChess[x][y] = 1;
chessBlackCount++;
}
if (chessColor == -1) {
allChess[x][y] = -1;
chessWhiteCount++;
}
}
chessPaint( x, y, chessColor);//这里做了更改
if(chessColor==1){
chessColor = -1;
}else{
chessColor = 1;
}
}
}
// 其他相关函数改变形参个数适应这里的改动