Skip to content

Commit c1d00bc

Browse files
committed
Start implementing ChessNotation
1 parent fa40138 commit c1d00bc

File tree

6 files changed

+178
-11
lines changed

6 files changed

+178
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
.idea
33
/build
44
.gradle
5-
gameData.dat
5+
boardData.dat
6+
chessNotationData.dat
67
JavaChess.jar

src/main/java/pl/nogacz/chess/Chess.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static void main(String[] args) {
2424
public void start(Stage primaryStage) {
2525
Scene scene = new Scene(design.getBorderPane(), 900, 790, Color.BLACK);
2626
design.getGridPane().setOnMouseClicked(event -> board.readMouseEvent(event));
27+
scene.setOnKeyReleased(event -> board.readKeyboard(event));
2728

2829
primaryStage.setTitle("Sloenthran :: Chess");
2930
primaryStage.setScene(scene);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package pl.nogacz.chess.application;
2+
3+
import pl.nogacz.chess.board.Coordinates;
4+
import pl.nogacz.chess.pawns.PawnClass;
5+
6+
import java.io.Serializable;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* @author Dawid Nogacz on 11.05.2019
12+
*/
13+
public class ChessNotation implements Serializable {
14+
private static List<String> movesList = new ArrayList<>();
15+
16+
private String playerMove = null;
17+
private String computerMove = null;
18+
19+
public static List<String> getMovesList() {
20+
return movesList;
21+
}
22+
23+
public static void setMovesList(List<String> movesList) {
24+
ChessNotation.movesList = movesList;
25+
updateTextArea();
26+
}
27+
28+
public void addMovement(Coordinates oldCoordinates, Coordinates newCoordinates, PawnClass pawn) {
29+
if(pawn.getColor().isWhite()) {
30+
playerMove = getPawnInNotation(pawn) + getCoordinatesInNotation(oldCoordinates) + getCoordinatesInNotation(newCoordinates);
31+
} else {
32+
computerMove = getPawnInNotation(pawn) + getCoordinatesInNotation(oldCoordinates) + getCoordinatesInNotation(newCoordinates);
33+
}
34+
}
35+
36+
public void saveRound() {
37+
movesList.add(playerMove + " " + computerMove);
38+
39+
playerMove = null;
40+
computerMove = null;
41+
42+
updateTextArea();
43+
}
44+
45+
private static void updateTextArea() {
46+
int round = 0;
47+
String text = "";
48+
49+
for(String move : movesList) {
50+
round++;
51+
text = text + round + ". " + move + "\n";
52+
}
53+
54+
Design.setTextInTextArea(text);
55+
}
56+
57+
private String getPawnInNotation(PawnClass pawn) {
58+
switch(pawn.getPawn()) {
59+
case BISHOP: return "G";
60+
case KING: return "K";
61+
case KNIGHT: return "S";
62+
case QUEEN: return "H";
63+
case ROOK: return "W";
64+
default: return "";
65+
}
66+
}
67+
68+
private String getCoordinatesInNotation(Coordinates coordinates) {
69+
return getXInNotation(coordinates.getX()) + getYInNotation(coordinates.getY());
70+
}
71+
72+
private String getXInNotation(int x) {
73+
switch (x) {
74+
case 0: return "a";
75+
case 1: return "b";
76+
case 2: return "c";
77+
case 3: return "d";
78+
case 4: return "e";
79+
case 5: return "f";
80+
case 6: return "g";
81+
default: return "h";
82+
}
83+
}
84+
85+
private String getYInNotation(int y) {
86+
switch (y) {
87+
case 0: return "8";
88+
case 1: return "7";
89+
case 2: return "6";
90+
case 3: return "5";
91+
case 4: return "4";
92+
case 5: return "3";
93+
case 6: return "2";
94+
default: return "1";
95+
}
96+
}
97+
}

src/main/java/pl/nogacz/chess/application/Design.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Design {
1818
private static BorderPane borderPane = new BorderPane();
1919
private static GridPane gridPane = new GridPane();
2020
private VBox vBox = new VBox();
21+
private static TextArea textArea = new TextArea();
2122
private HBox hBox = new HBox();
2223
private static Image lightMove = new Image(Resources.getPath("light.png"));
2324

@@ -71,12 +72,9 @@ private void generateEmptyBoard() {
7172
}
7273

7374
private void createFieldForChessNotation() {
74-
75-
TextArea textArea = new TextArea();
7675
textArea.setEditable(false);
7776
textArea.setMinHeight(750);
7877
textArea.setMaxHeight(750);
79-
textArea.setText("ADSAD\nASDASD\nA\nASD\n");
8078

8179
vBox.getChildren().add(textArea);
8280
}
@@ -112,4 +110,8 @@ public static void addLightMove(Coordinates coordinates) {
112110
public static void removePawn(Coordinates coordinates) {
113111
gridPane.getChildren().removeIf(node -> GridPane.getColumnIndex(node) == coordinates.getX() && GridPane.getRowIndex(node) == coordinates.getY());
114112
}
113+
114+
public static void setTextInTextArea(String text) {
115+
textArea.setText(text);
116+
}
115117
}

src/main/java/pl/nogacz/chess/application/SaveGame.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.*;
88
import java.util.HashMap;
9+
import java.util.List;
910
import java.util.zip.GZIPInputStream;
1011
import java.util.zip.GZIPOutputStream;
1112

@@ -14,13 +15,18 @@
1415
*/
1516
public class SaveGame {
1617
public boolean isSave() {
17-
File tempFile = new File("gameData.dat");
18+
File tempFile = new File("boardData.dat");
1819
return tempFile.exists();
1920
}
2021

2122
public void save() {
23+
saveBoard();
24+
saveChessNotation();
25+
}
26+
27+
private void saveBoard() {
2228
try {
23-
File file = new File("gameData.dat");
29+
File file = new File("boardData.dat");
2430
ObjectOutputStream output = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
2531
output.writeObject(Board.getBoard());
2632
output.flush();
@@ -30,13 +36,40 @@ public void save() {
3036
}
3137
}
3238

39+
private void saveChessNotation() {
40+
try {
41+
File file = new File("chessNotationData.dat");
42+
ObjectOutputStream output = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(file)));
43+
output.writeObject(ChessNotation.getMovesList());
44+
output.flush();
45+
output.close();
46+
} catch (Exception e) {
47+
System.out.println(e.getMessage());
48+
}
49+
}
50+
3351
public void load() {
52+
loadBoard();
53+
loadChessNotation();
54+
}
55+
56+
private void loadBoard() {
3457
try {
35-
File file = new File("gameData.dat");
36-
ObjectInputStream input = new ObjectInputStream(new GZIPInputStream(new FileInputStream(file)));
58+
Object readObject = readObject(new File("chessNotationData.dat"));
3759

38-
Object readObject = input.readObject();
39-
input.close();
60+
if(!(readObject instanceof List)) throw new Exception("Data is not a List");
61+
62+
List<String> cacheNotation = (List<String>) readObject;
63+
64+
ChessNotation.setMovesList(cacheNotation);
65+
} catch (Exception e) {
66+
System.out.println(e.getMessage());
67+
}
68+
}
69+
70+
private void loadChessNotation() {
71+
try {
72+
Object readObject = readObject(new File("boardData.dat"));
4073

4174
if(!(readObject instanceof HashMap)) throw new Exception("Data is not a HashMap");
4275

@@ -48,8 +81,23 @@ public void load() {
4881
}
4982
}
5083

84+
private Object readObject(File file) {
85+
try {
86+
ObjectInputStream input = new ObjectInputStream(new GZIPInputStream(new FileInputStream(file)));
87+
88+
Object readObject = input.readObject();
89+
input.close();
90+
91+
return readObject;
92+
} catch (Exception e) {
93+
System.out.println(e.getMessage());
94+
}
95+
96+
return null;
97+
}
98+
5199
public void remove() {
52-
File tempFile = new File("gameData.dat");
100+
File tempFile = new File("boardData.dat");
53101
tempFile.delete();
54102
}
55103
}

src/main/java/pl/nogacz/chess/board/Board.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javafx.concurrent.Task;
44
import javafx.concurrent.WorkerStateEvent;
55
import javafx.event.EventHandler;
6+
import javafx.scene.input.KeyCode;
7+
import javafx.scene.input.KeyEvent;
68
import javafx.scene.input.MouseEvent;
79
import pl.nogacz.chess.application.*;
810
import pl.nogacz.chess.pawns.Pawn;
@@ -21,6 +23,7 @@
2123
*/
2224
public class Board {
2325
private SaveGame saveGame = new SaveGame();
26+
private ChessNotation chessNotation = new ChessNotation();
2427

2528
private static HashMap<Coordinates, PawnClass> board = new HashMap<>();
2629
private GameLogic gameLogic = new GameLogic();
@@ -128,6 +131,8 @@ public void readMouseEvent(MouseEvent event) {
128131
unLightSelect(selectedCoordinates);
129132
movePawn(selectedCoordinates, eventCoordinates);
130133

134+
chessNotation.addMovement(selectedCoordinates, eventCoordinates, getPawn(eventCoordinates));
135+
131136
selectedCoordinates = null;
132137
isSelected = false;
133138
isKingChecked = false;
@@ -155,6 +160,12 @@ public void readMouseEvent(MouseEvent event) {
155160
}
156161
}
157162

163+
public void readKeyboard(KeyEvent event) {
164+
if(event.getCode().equals(KeyCode.R) || event.getCode().equals(KeyCode.N)) {
165+
new EndGame("").newGame();
166+
}
167+
}
168+
158169
private void computerMove() {
159170
Task<Void> computerSleep = new Task<Void>() {
160171
@Override
@@ -178,12 +189,16 @@ public void handle(WorkerStateEvent event) {
178189
unLightSelect(selectedCoordinates);
179190
movePawn(selectedCoordinates, moveCoordinates);
180191

192+
chessNotation.addMovement(selectedCoordinates, moveCoordinates, getPawn(moveCoordinates));
193+
181194
checkPromote(moveCoordinates, 1);
182195
} else if(possibleMoves.size() > 0) {
183196
Coordinates moveCoordinates = computer.selectRandom(possibleMoves);
184197
unLightSelect(selectedCoordinates);
185198
movePawn(selectedCoordinates, moveCoordinates);
186199

200+
chessNotation.addMovement(selectedCoordinates, moveCoordinates, getPawn(moveCoordinates));
201+
187202
checkPromote(moveCoordinates, 1);
188203
} else {
189204
endGame("You win! Congratulations :)");
@@ -195,11 +210,14 @@ public void handle(WorkerStateEvent event) {
195210
unLightSelect(selectedCoordinates);
196211
movePawn(selectedCoordinates, moveCoordinates);
197212

213+
chessNotation.addMovement(selectedCoordinates, moveCoordinates, getPawn(moveCoordinates));
214+
198215
checkPromote(moveCoordinates, 1);
199216
}
200217

201218
isComputerRound = false;
202219
selectedCoordinates = null;
220+
chessNotation.saveRound();
203221
saveGame.save();
204222
}
205223
});

0 commit comments

Comments
 (0)