Skip to content

Commit bd78864

Browse files
committed
Final Version
0 parents  commit bd78864

16 files changed

+840
-0
lines changed

.gitignore

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/checkstyle-idea.xml

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/material_theme_project_new.xml

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data/games.csv

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
zsl,test,2,1,0
2+
zsl,test,3,4,0
3+
zsl,test,4,7,0
4+
zsl,test,5,10,0
5+
admin,zsl,6,13,0
6+
admin,zsl,7,16,0
7+
admin,zsl,8,19,0
8+
admin,zsl,9,22,0
9+
admin,root,10,25,0
10+
admin,root,11,28,0
11+
zsl,root,12,31,0
12+
admin,root,13,34,0
13+
zsl,root,14,36,1
14+
2,1,14,20,1
15+
2,1,4,2,0
16+
6,5,2,4,1
17+
2,1,2,2,0
18+
B,A,0,2,0
19+
D,C,2,2,0
20+
1,2,-4,8,0
21+
1,2,8,22,1
22+
4,3,0,6,0

data/players.csv

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
zsl,10,24
2+
test,12,30
3+
root,60,66
4+
admin,23,90
5+
1,0,0
6+
2,0,0
7+
3,0,0
8+
4,0,0
9+
5,0,0
10+
6,0,0
11+
e,0,0
12+
f,0,0
13+
A,0,0
14+
B,0,0
15+
C,0,0
16+
D,0,0
17+
E,0,0
18+
F,0,0
19+
G,0,0
20+
H,0,0
21+
P,0,0
22+
Q,0,0
23+
aefeef,0,0

pom.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>Linking-Game-Server</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>23</maven.compiler.source>
13+
<maven.compiler.target>23</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>

src/main/java/Board.java

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import java.io.*;
2+
import java.util.ArrayList;
3+
import java.util.Base64;
4+
import java.util.Optional;
5+
6+
public class Board implements Serializable {
7+
@Serial
8+
private static final long serialVersionUID = 1L;
9+
10+
public static final int EMPTY = -1;
11+
12+
public int row;
13+
public int col;
14+
public int[][] content;
15+
16+
public Board(int row, int col, int[][] content) {
17+
this.row = row;
18+
this.col = col;
19+
this.content = content;
20+
}
21+
22+
public void setBoard(int row, int col, int val) {
23+
content[row][col] = val;
24+
}
25+
26+
public void setBoard(int row, int col) {
27+
setBoard(row, col, EMPTY);
28+
}
29+
30+
public Optional<ArrayList<Position>> judge(int row1, int col1, int row2, int col2) {
31+
if ((content[row1][col1] != content[row2][col2]) || (row1 == row2 && col1 == col2)) {
32+
return Optional.empty();
33+
}
34+
35+
// one line
36+
if (isDirectlyConnected(row1, col1, row2, col2, content)) {
37+
ArrayList<Position> res = new ArrayList<>();
38+
res.add(new Position(row1, col1));
39+
res.add(new Position(row2, col2));
40+
return Optional.of(res);
41+
}
42+
43+
// two lines
44+
if ((row1 != row2) && (col1 != col2)) {
45+
if (content[row1][col2] == EMPTY && isDirectlyConnected(row1, col1, row1, col2, content)
46+
&& isDirectlyConnected(row1, col2, row2, col2, content)) {
47+
ArrayList<Position> res = new ArrayList<>();
48+
res.add(new Position(row1, col1));
49+
res.add(new Position(row1, col2));
50+
res.add(new Position(row2, col2));
51+
return Optional.of(res);
52+
}
53+
54+
if (content[row2][col1] == EMPTY && isDirectlyConnected(row2, col2, row2, col1, content)
55+
&& isDirectlyConnected(row2, col1, row1, col1, content)) {
56+
ArrayList<Position> res = new ArrayList<>();
57+
res.add(new Position(row1, col1));
58+
res.add(new Position(row2, col1));
59+
res.add(new Position(row2, col2));
60+
return Optional.of(res);
61+
}
62+
}
63+
64+
// three lines
65+
if(row1 != row2) {
66+
for (int i = 0; i < content[0].length; i++) {
67+
if (content[row1][i] == EMPTY && content[row2][i] == EMPTY &&
68+
isDirectlyConnected(row1, col1, row1, i, content) &&
69+
isDirectlyConnected(row1, i, row2, i, content) &&
70+
isDirectlyConnected(row2, col2, row2, i, content)) {
71+
ArrayList<Position> res = new ArrayList<>();
72+
res.add(new Position(row1, col1));
73+
res.add(new Position(row1, i));
74+
res.add(new Position(row2, i));
75+
res.add(new Position(row2, col2));
76+
return Optional.of(res);
77+
}
78+
}
79+
}
80+
if(col1 != col2) {
81+
for (int j = 0; j < content.length; j++) {
82+
if (content[j][col1] == EMPTY && content[j][col2] == EMPTY &&
83+
isDirectlyConnected(row1, col1, j, col1, content) &&
84+
isDirectlyConnected(j, col1, j, col2, content) &&
85+
isDirectlyConnected(row2, col2, j, col2, content)) {
86+
ArrayList<Position> res = new ArrayList<>();
87+
res.add(new Position(row1, col1));
88+
res.add(new Position(j, col1));
89+
res.add(new Position(j, col2));
90+
res.add(new Position(row2, col2));
91+
return Optional.of(res);
92+
}
93+
}
94+
}
95+
96+
return Optional.empty();
97+
}
98+
99+
// judge whether
100+
private boolean isDirectlyConnected(int row1, int col1, int row2, int col2, int[][] board) {
101+
if (row1 == row2) {
102+
int minCol = Math.min(col1, col2);
103+
int maxCol = Math.max(col1, col2);
104+
for (int col = minCol + 1; col < maxCol; col++) {
105+
if (board[row1][col] != EMPTY) {
106+
return false;
107+
}
108+
}
109+
return true;
110+
} else if (col1 == col2) {
111+
int minRow = Math.min(row1, row2);
112+
int maxRow = Math.max(row1, row2);
113+
for (int row = minRow + 1; row < maxRow; row++) {
114+
if (board[row][col1] != EMPTY) {
115+
return false;
116+
}
117+
}
118+
return true;
119+
}
120+
return false;
121+
}
122+
123+
public boolean hasMoreMoves() {
124+
for (int i = 0; i < row; i++) {
125+
for (int j = 0; j < col; j++) {
126+
if (content[i][j] != EMPTY) {
127+
for (int k = 0; k < row; k++) {
128+
for (int l = 0; l < col; l++) {
129+
if (i == k && j == l || content[k][l] == EMPTY) {
130+
continue;
131+
}
132+
if (content[i][j] == content[k][l] && judge(i, j, k, l).isPresent()) {
133+
return true;
134+
}
135+
}
136+
}
137+
}
138+
}
139+
}
140+
return false;
141+
}
142+
}

src/main/java/Game.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
5+
public class Game {
6+
public Player player0, player1;
7+
public Board board;
8+
public final int NUM_ICONS = 12;
9+
10+
public Game(Player player0, Player player1, int row, int col) {
11+
this.player0 = player0;
12+
this.player1 = player1;
13+
List<Integer> numbers = new ArrayList<>();
14+
15+
int avg = (row * col + NUM_ICONS - 1) / NUM_ICONS;
16+
avg += (avg & 1);
17+
for (int i = 0; i < NUM_ICONS; i++) {
18+
for (int j = 0; j < avg; j++) {
19+
numbers.add(i);
20+
}
21+
}
22+
while (numbers.size() > row * col) {
23+
numbers.removeLast();
24+
}
25+
26+
do {
27+
Collections.shuffle(numbers);
28+
int[][] boardContent = new int[row][col];
29+
int index = 0;
30+
for (int i = 0; i < row; i++) {
31+
for (int j = 0; j < col; j++) {
32+
boardContent[i][j] = numbers.get(index++);
33+
}
34+
}
35+
board = new Board(row, col, boardContent);
36+
} while (!board.hasMoreMoves());
37+
}
38+
}

0 commit comments

Comments
 (0)