Skip to content

Commit

Permalink
finish all work
Browse files Browse the repository at this point in the history
  • Loading branch information
drkying committed May 23, 2020
1 parent ed73b71 commit d31babe
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 139 deletions.
24 changes: 18 additions & 6 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified out/production/baduk/com/company/baduk/Client/Begin$1.class
Binary file not shown.
Binary file modified out/production/baduk/com/company/baduk/Client/Begin.class
Binary file not shown.
Binary file modified out/production/baduk/com/company/baduk/Client/ChessPad.class
Binary file not shown.
21 changes: 12 additions & 9 deletions src/com/company/baduk/Client/Begin.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public Begin() {
String ip, roomName;
ip = "127.0.0.1";
roomName = "233";
// ip = JOptionPane.showInputDialog("请输入服务器ip:");
// while (!Tool.ipCheck(ip)){
// JOptionPane.showMessageDialog(null, "输入的ip不合法,请重试");
// ip = JOptionPane.showInputDialog("请输入服务器ip:");
// }
// roomName = JOptionPane.showInputDialog("请输入房间号:");

ip = JOptionPane.showInputDialog("请输入服务器ip:");
while (!Tool.ipCheck(ip)) {
JOptionPane.showMessageDialog(null, "输入的ip不合法,请重试");
ip = JOptionPane.showInputDialog("请输入服务器ip:");
}
roomName = JOptionPane.showInputDialog("请输入房间号:");


request(roomName, ip);
Expand All @@ -45,12 +46,12 @@ public void request(String roomName, String ip) {
int x = Tool.encode(roomName);
System.out.println("after encode:\n" + x);
try {
playerSocket = new PlayerSocket(new Socket(ip, 8000));
playerSocket = new PlayerSocket(new Socket(ip, 8001));
playerSocket.writeInt(x);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "连接失败!");
System.exit(0);
}
System.out.println("request finish");
}
Expand Down Expand Up @@ -140,6 +141,8 @@ public void windowClosing(WindowEvent e) {
pack();
setSize(600, 550);

JOptionPane.showMessageDialog(null, "连接成功!");

}

public static void main(String args[]) {
Expand Down
53 changes: 24 additions & 29 deletions src/com/company/baduk/Client/ChessPad.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,29 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class ChessPad extends JPanel implements MouseListener, ActionListener {
public Player nowPlayer;
public boolean blackGroup;
public boolean whiteGroup;
public int currentGroup = 0;
public PlayerSocket playerSocket;
public boolean isYourTurn = false;
public boolean isYourTurn;
public Point[][] now = new Point[21][21];
public Point[][] previous = new Point[21][21];
public List<Point[][]> chessHistory = new ArrayList<>();
public List<String> chessHash = new ArrayList<>();
public int dim = 19; //19X19的棋盘
public int[] block;
public int blockLength = 0, score_B = 0, score_W = 0;
public int score_B = 0, score_W = 0;
public static int W_eat_B = 0, B_eat_W = 0;
public GoRules goRules;
public myHashTable chessPadHistory;

//初始化棋盘
public ChessPad(Player nowPlayer, PlayerSocket playerSocket) {
initBorder();
this.nowPlayer = nowPlayer;
this.playerSocket = playerSocket;
this.goRules = new GoRules(this);
this.chessPadHistory = new myHashTable();

if (nowPlayer == Player.BLACK) {
isYourTurn = true;
Expand Down Expand Up @@ -57,23 +55,21 @@ public void run() {
public void backupBorder() {
previous = new Point[21][21];
previous = Tool.clone(now);
chessHistory.add(previous);
chessHash.add(Tool.sumHashCode(previous));
chessPadHistory.add(previous);
updateScore();
}

//恢复棋局,用于悔棋操作
public void restoreBorder() {
Point[][] lastChessPad = chessHistory.get(chessHistory.size() - 2);
chessHistory.remove(chessHistory.size() - 1);
Point[][] lastChessPad = chessPadHistory.pop();

this.removeAll();
for (int i = 2; i <= 20; i++)
for (int j = 2; j <= 20; j++) {
if (lastChessPad[i][j].getPlayer() == Player.NONE) continue;
this.add(lastChessPad[i][j]);
lastChessPad[i][j].setBounds(lastChessPad[i][j].getX() * 20 - 10, lastChessPad[i][j].getY() * 20 - 10, 20, 20);
}
chessHash.remove(chessHash.size() - 1);
}

public boolean isYourTurn() {
Expand All @@ -100,6 +96,7 @@ public void addPoint(Point point) {
goRules.checkDelete(point.getX(), point.getY(), now);
backupBorder();
}

public void addPoint(int x, int y) {
if (isYourTurn) {
int a = (x + 10) / 20, b = (y + 10) / 20;
Expand All @@ -120,7 +117,7 @@ public void addPoint(int x, int y) {
}

Point[][] future = Tool.clone(now);
goRules.checkDelete(future[a][b].getX(), future[a][b].getY(), future);
goRules.checkDelete(future[a][b].getX(), future[a][b].getY(), future, false);

if (!check(future)) {
JOptionPane.showMessageDialog(null, "当前下棋位置导致重局,请重新下棋");
Expand All @@ -142,27 +139,25 @@ public void addPoint(int x, int y) {
//更新计分板
public void updateScore() {
calcTerritory();
int score_B = this.score_B + B_eat_W, score_W = this.score_W + W_eat_B;
System.out.println("1:" + this.score_B + ";2:" + B_eat_W + ";3:" + this.score_W + ";4:" + W_eat_B);
Begin.score.setText("计分板:" + "黑:" + score_B + " " + "白: " + score_W);
}

//判断是否出现重局的情况
public boolean check(Point[][] future) {
String temp = Tool.sumHashCode(future);
System.out.println("hash History:");
for (String s : chessHash)
System.out.println(s);
System.out.println("future hash:" + temp);
for (int i = 0; i < chessHash.size(); i++) {
if (chessHash.get(i).equals(temp)) {
che:
for (int j = 2; j <= 20; j++) {
for (int k = 2; k <= 20; k++) {
if (!chessHistory.get(i)[j][k].equals(future[j][k]))
break che;
if (j == 20 && k == 20) {
return false;
}
}
String temp = myHashTable.hashFun(future);
int id;
Point[][] past;
if ((id = chessPadHistory.find(temp)) != -1)
past = chessPadHistory.get(id);
else return true;
for (int j = 2; j <= 20; j++) {
for (int k = 2; k <= 20; k++) {
if (!past[j][k].equals(future[j][k]))
return true;
if (j == 20 && k == 20) {
return false;
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/com/company/baduk/DataStruct/GoRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public void checkDelete(int x, int y, Point[][] temp) {
doStep(x, y, temp);
}

public void checkDelete(int x, int y, Point[][] temp, boolean noScore) {
int W = ChessPad.B_eat_W;
int B = ChessPad.W_eat_B;
if (x - 1 >= 2) doStep(x - 1, y, temp);
if (y - 1 >= 2) doStep(x, y - 1, temp);
if (x + 1 <= 20) doStep(x + 1, y, temp);
if (y + 1 <= 20) doStep(x, y + 1, temp);
doStep(x, y, temp);
ChessPad.B_eat_W = W;
ChessPad.W_eat_B = B;
}

public void doStep(int x, int y, Point[][] temp) {
if (temp[x][y].getPlayer() == Player.NONE) {
} else {
Expand All @@ -69,12 +81,17 @@ public void doStep(int x, int y, Point[][] temp) {
if (hasQi(temp)) {
} else {
//进行吃子
for (int t = 0; t < blockLength; t++)
if (temp[block[0] / 100][block[0] % 100].getPlayer() == Player.WHITE) {
ChessPad.B_eat_W += blockLength;
} else if (temp[block[0] / 100][block[0] % 100].getPlayer() == Player.BLACK) {
ChessPad.W_eat_B += blockLength;
}
for (int t = 0; t < blockLength; t++) {
chessPad.removePoint(temp[block[t] / 100][block[t] % 100], temp);
}
}
}
}

//判断当前棋子块是否有气,即未被完全包围
public boolean hasQi(Point[][] temp) {
int i, j;
Expand Down
16 changes: 0 additions & 16 deletions src/com/company/baduk/DataStruct/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,6 @@ public static int encode(String roomName) {
return s;
}

//为当前棋局生成一个哈希值
public static String sumHashCode(Point[][] points) {
int hash = 0;
for (int i = 2; i <= 20; i++) {
for (int j = 2; j <= 20; j++) {
Point point = points[i][j];
if (point.getPlayer().equals(Player.BLACK))
hash += (point.getX() * 3 + point.getY() * 5) * 3;
else if (point.getPlayer().equals(Player.WHITE))
hash += (point.getX() * 3 + point.getY() * 5) * 5;
else if (point.getPlayer().equals(Player.NONE))
hash--;
}
}
return String.valueOf(hash);
}

//java 对象的深拷贝
public static <T extends Serializable> T clone(T obj) {
Expand Down
53 changes: 53 additions & 0 deletions src/com/company/baduk/DataStruct/myHashTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.company.baduk.DataStruct;

import com.company.baduk.Client.Point;

import java.util.ArrayList;
import java.util.List;

public class myHashTable {
public List<String> chessHash;
public List<Point[][]> chessHistory;

public myHashTable() {
this.chessHash = new ArrayList<>();
this.chessHistory = new ArrayList<>();
}

public void add(Point[][] temp) {
chessHistory.add(temp);
chessHash.add(hashFun(temp));
}

//为当前棋局生成一个哈希值
public static String hashFun(Point[][] points) {
int hash = 0;
for (int i = 2; i <= 20; i++) {
for (int j = 2; j <= 20; j++) {
Point point = points[i][j];
if (point.getPlayer().equals(Player.BLACK))
hash += (point.getX() * 3 + point.getY() * 5) * 3;
else if (point.getPlayer().equals(Player.WHITE))
hash += (point.getX() * 3 + point.getY() * 5) * 5;
else if (point.getPlayer().equals(Player.NONE))
hash--;
}
}
return String.valueOf(hash);
}

public int find(String key) {
return chessHash.indexOf(key);
}

public Point[][] get(int i) {
return chessHistory.get(i);
}

public Point[][] pop() {
chessHistory.remove(chessHistory.size() - 1);
chessHash.remove(chessHash.size() - 1);
return chessHistory.get(chessHistory.size() - 1);
}

}
Loading

0 comments on commit d31babe

Please sign in to comment.