Skip to content

Commit

Permalink
added game saving/ even timelines. game saving only somewhat functional
Browse files Browse the repository at this point in the history
  • Loading branch information
Slavrick committed Jan 3, 2021
1 parent 0f66982 commit cdabc9d
Show file tree
Hide file tree
Showing 29 changed files with 264 additions and 64 deletions.
Binary file modified bin/GUI/ChessDrawer.class
Binary file not shown.
Binary file modified bin/GUI/Controller$1.class
Binary file not shown.
Binary file modified bin/GUI/Controller$2.class
Binary file not shown.
Binary file modified bin/GUI/Controller$3.class
Binary file not shown.
Binary file modified bin/GUI/Controller.class
Binary file not shown.
Binary file modified bin/engine/CoordFour.class
Binary file not shown.
Binary file modified bin/engine/GameState.class
Binary file not shown.
Binary file modified bin/engine/GameStateManager.class
Binary file not shown.
Binary file modified bin/fileIO/FENExporter.class
Binary file not shown.
Binary file modified bin/fileIO/FENParser.class
Binary file not shown.
Binary file modified bin/main/Main.class
Binary file not shown.
5 changes: 3 additions & 2 deletions bin/sample.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<MenuBar fx:id="menuBar">
<Menu text="File">
<MenuItem text=" Open file " fx:id="open" onAction="#loadGame"/>
<MenuItem text=" Save Game " onAction="#saveGameState"/>
</Menu>
<Menu text="Edit" onAction="#handleMenu">
</Menu>
Expand All @@ -27,14 +28,14 @@
</ScrollPane>
</center>
<right>
<ListView fx:id="notationList" minWidth="200" maxWidth="300" prefWidth="200">
</ListView>
<ListView fx:id="notationList" minWidth="200" maxWidth="300" prefWidth="200" onMouseClicked="#handleListEvent" />
</right>
<bottom>
<VBox>
<HBox Alignment="CENTER_LEFT" spacing="50" >
<Button text="Submit Turn" onAction="#handleSubmitButton" />
<Button text="Undo Turn" onAction="#handleUndoButton" />
<Button text="~~" onAction="#handlePanButton" />
</HBox>
<Label fx:id="statusLabel"/>
</VBox>
Expand Down
Binary file modified bin/test/FENParserTest.class
Binary file not shown.
3 changes: 3 additions & 0 deletions res/Variations/Standard-TwoTimeline.FEN.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
8;8;1;2;0;w
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR;KQkq;-;b1
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR;KQkq;-;w1
3 changes: 3 additions & 0 deletions res/exampleGame.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
8;8;0;1;0;w
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR;KQkq;-;w1
(6,0,1,0)(5,2,1,0);(6,7,1,0)(5,5,1,0);(4,1,2,0)(4,3,2,0);(4,6,2,0)(4,4,2,0);(5,2,3,0)(5,4,2,0);(4,6,2,1)(4,5,2,1);(4,1,3,1)(4,2,3,1);(5,7,3,1)(2,4,3,1);(1,7,3,0)(2,5,3,0);
5 changes: 3 additions & 2 deletions res/sample.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<MenuBar fx:id="menuBar">
<Menu text="File">
<MenuItem text=" Open file " fx:id="open" onAction="#loadGame"/>
<MenuItem text=" Save Game " onAction="#saveGameState"/>
</Menu>
<Menu text="Edit" onAction="#handleMenu">
</Menu>
Expand All @@ -27,14 +28,14 @@
</ScrollPane>
</center>
<right>
<ListView fx:id="notationList" minWidth="200" maxWidth="300" prefWidth="200">
</ListView>
<ListView fx:id="notationList" minWidth="200" maxWidth="300" prefWidth="200" onMouseClicked="#handleListEvent" />
</right>
<bottom>
<VBox>
<HBox Alignment="CENTER_LEFT" spacing="50" >
<Button text="Submit Turn" onAction="#handleSubmitButton" />
<Button text="Undo Turn" onAction="#handleUndoButton" />
<Button text="~~" onAction="#handlePanButton" />
</HBox>
<Label fx:id="statusLabel"/>
</VBox>
Expand Down
1 change: 0 additions & 1 deletion src/GUI/ChessDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public static void drawTimelineV(GraphicsContext gc, int x, int y, int screenx,
}
}

//TODO fix loading of file.
public static void drawFullBoardV(GraphicsContext gc, Board b, boolean color, boolean playable, int x, int y, int screenx, int screeny) {
if (piecesprites == null) {
try {
Expand Down
39 changes: 33 additions & 6 deletions src/GUI/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import engine.GameStateManager;
import engine.Move;
import engine.MoveGenerator;
import fileIO.FENExporter;
import fileIO.FENParser;

public class Controller {
Expand Down Expand Up @@ -78,7 +79,7 @@ public class Controller {

// This func is called in the very start of initialization of this class
public Controller() {
g = new GameStateManager(FENParser.FENtoGSNew("res/Standard.FEN.txt"));
g = FENParser.FENtoGSM("res/Standard.FEN.txt");
}

// This func is called after all initializations from the FXML parser.
Expand Down Expand Up @@ -174,10 +175,23 @@ public void handleSubmitButton(ActionEvent e) {
boolean submitted = g.submitMoves();
setStatusLabel();
if(submitted) {
notationStringArray.add(g.turns.get(g.turns.size() - 1).toString());//Sometimes one move will show a phantom move TODO fix that.
notationStringArray.add(g.turns.get(g.turns.size() - 1).toString());
}
}


@FXML
private void handlePanButton(ActionEvent event) {
System.out.println("Pan");
}

@FXML
private void handleListEvent(MouseEvent event) {
System.out.println("Chagne");
//TODO set this -- test this
int turnIndex = 0;
//g.setTurn(turnIndex);
}

@FXML
private void handleMenu(ActionEvent event) {
if (event.getSource() instanceof Menu)
Expand All @@ -197,14 +211,24 @@ private void handleEventList(ActionEvent event) {
private void loadGame(ActionEvent event) {
File selectedFile = getFile();
if (selectedFile != null) {
g = new GameStateManager(FENParser.FENtoGSNew(selectedFile));
g = FENParser.FENtoGSM(selectedFile);
drawStage();
setStatusLabel();
screenX = 0;
screenY = 0;
}
}

@FXML
private void saveGameState(ActionEvent event) {
Popup p = new Popup();
FileChooser fileChooser = new FileChooser();
fileChooser.setInitialDirectory(new File(new File("").getAbsolutePath()));
fileChooser.setTitle("Open Resource File");
File selectedFile = fileChooser.showSaveDialog(p.getOwnerWindow());
FENExporter.exportString(selectedFile, FENExporter.GameStateToFEN(this.g));
}

@FXML
private void setProperties(ActionEvent e) {

Expand Down Expand Up @@ -297,8 +321,11 @@ public static boolean alContains(ArrayList<CoordFour> al, CoordFour target) {
return false;
}

private static void panToBoard(int T, int L) {
//TODO finish this function.
private void panToBoard(int T, int L) {
int pany = L * (ChessDrawer.padding + (ChessDrawer.squarewidth * g.height)) + 100;
int panx = T * (ChessDrawer.padding + (ChessDrawer.squarewidth * g.width)) + 100;
screenX = panx;
screenY = pany;
}

}
2 changes: 1 addition & 1 deletion src/Protocol/EngineListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class EngineListener implements Runnable{

@Override
public void run() {
// TODO Auto-generated method stub
//XXX Auto-generated method stub
//figure out how to get info
}

Expand Down
91 changes: 61 additions & 30 deletions src/engine/CoordFour.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

public class CoordFour {
/*
* X represents file ie. a,b,c... files
* y represents rank
* T/L represent their raw coordinates as per 5d chess rules
* X represents file ie. a,b,c... files y represents rank T/L represent their
* raw coordinates as per 5d chess rules
*/
public int x;
public int y;
Expand All @@ -25,7 +24,7 @@ public CoordFour(int x, int y) {
public CoordFour clone() {
return new CoordFour(this.x, this.y, this.T, this.L);
}

/**
* A comparison function to compare this coordinate and another.
*
Expand Down Expand Up @@ -56,45 +55,62 @@ public void add(CoordFour c) {
T += c.T;
L += c.L;
}

//turns a coord into a vector, or a coord with only 1/0's

public void sub(CoordFour c) {
x -= c.x;
y -= c.y;
T -= c.T;
L -= c.L;
}

// turns a coord into a vector, or a coord with only 1/0's
public void makeVector() {
if(this.x != 0) {
if (this.x != 0) {
this.x = 1;
}
if(this.y != 0) {
if (this.y != 0) {
this.y = 1;
}
if(this.T != 0) {
if (this.T != 0) {
this.T = 1;
}
if(this.L != 0) {
if (this.L != 0) {
this.L = 1;
}
}

//TODO make a func that flattents the vector ie. 2,2,2,2 would be 1,1,1,1 but something like 2,4,0,0 would be 1,2,0,0 not 1,1,0,0

//gets the n-diagonal that a vector is
public int getNagonal(){

public void flatten() {
int gcd = GCD(GCD(Math.abs(x),Math.abs(y)), GCD(Math.abs(T),Math.abs(L)));
if(gcd == 0) {
return;
}
this.x = this.x / gcd;
this.y = this.y / gcd;
this.T = this.T / gcd;
this.L = this.L / gcd;
}

// gets the n-diagonal that a vector is
public int getNagonal() {
int nagonal = 0;
if(this.x != 0)
if (this.x != 0)
nagonal++;
if(this.y != 0)
if (this.y != 0)
nagonal++;
if(this.T != 0)
if (this.T != 0)
nagonal++;
if(this.L != 0)
if (this.L != 0)
nagonal++;
return nagonal;
}

public String toString() {
return "(" + L + "L." + "T" + T + "." + intToFile(x) + "" + (y + 1) + ")";
}

/**
* gets a string raw representation of this.
*
* @return raw coord string
*/
public String rawCoordString() {
Expand All @@ -103,25 +119,40 @@ public String rawCoordString() {

/**
* get a SAN 2d coord of the given object such as a1 e4 ....
*
* @return String SAN representation
*/
public String SANString() {
return intToFile(this.x) + "" + (this.y +1);
return intToFile(this.x) + "" + (this.y + 1);
}

//add
public static CoordFour add(CoordFour c1, CoordFour c2) {
CoordFour sum = new CoordFour( c2.x + c1.x, c2.y +c1.y, c2.T + c1.T, c2.L + c1.L);
return sum;

public static int GCD(int num1, int num2) {
while(num1 > 0 && num2 > 0) {
if(num1 > num2) {
num1 -= num2;
}
else {
num2 -= num1;
}
}
return num1 + num2;
}

//sub
// sub
public static CoordFour sub(CoordFour c1, CoordFour c2) {
return new CoordFour( c2.x - c1.x, c2.y - c1.y, c2.T - c1.T, c2.L - c1.L);
return new CoordFour(c2.x - c1.x, c2.y - c1.y, c2.T - c1.T, c2.L - c1.L);
}

// add
public static CoordFour add(CoordFour c1, CoordFour c2) {
CoordFour sum = new CoordFour(c2.x + c1.x, c2.y + c1.y, c2.T + c1.T, c2.L + c1.L);
return sum;
}

/**
* returns the corrisponding file from the int file sent, 0 indexed so a is 0 b is 1 and so on.
* returns the corrisponding file from the int file sent, 0 indexed so a is 0 b
* is 1 and so on.
*
* @param file file to get char for
* @return char corrisponding to sent file.
*/
Expand Down
7 changes: 4 additions & 3 deletions src/engine/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,20 +530,21 @@ public void undoTempMoves() {
turnMoves.clear();
}

// TODO add handicap to this.

/**
* this function changes the object to reflect which TL are 'active'
* Uses 'handicap' for even tl things -- May be wrong but we will see.
*/
protected void determineActiveTLS() {
// case 1 -- black has branched more.
if (maxTL < Math.abs(minTL)) {
maxActiveTL = maxTL;
minActiveTL = -1 - maxActiveTL;
minActiveTL = Math.max(-1 - maxActiveTL + tlHandicap, minTL);
}
// case 2 -- white has branched more.
else if (Math.abs(minTL) < maxTL) {
minActiveTL = minTL;
maxActiveTL = 1 + Math.abs(minActiveTL);
maxActiveTL = Math.min(1 + Math.abs(minActiveTL) + tlHandicap, maxTL);
}
// case 3 -- they have branched the same # of times
else {
Expand Down
31 changes: 22 additions & 9 deletions src/engine/GameStateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class GameStateManager extends GameState{
public GameStateManager(Timeline[] origins, int width, int height, boolean evenStart, boolean color, int minTL, Move[] moves) {
super(origins, width, height, evenStart, color, minTL, moves);
this.preMoves = moves;
originBoards = new Board[origins.length];
this.turns = new ArrayList<Turn>();
for(int i = 0; i < origins.length ; i++){
if(origins[i].colorStart) {
originBoards[i] = origins[i].wboards.get(0);
Expand All @@ -26,17 +28,12 @@ public GameStateManager(Timeline[] origins, int width, int height, boolean evenS
}
}

public GameStateManager(GameState game) {//TODO fix this
super(game);
this.turns = new ArrayList<Turn>();
currTurn = 1;
}

public boolean submitMoves() {
determineActiveTLS();
boolean presColor = calcPresent();
if(!opponentCanCaptureKing() && !(presColor == color)) {
turns.add(new Turn( turnMoves, turnTLs));
currTurn++;
turnTLs.clear();
turnMoves.clear();
color =! color;
Expand All @@ -47,8 +44,24 @@ public boolean submitMoves() {
}

//Set the gamestate to a previous or future turn depending on the int given.
public boolean setTurn(int turnNum) {//TODO finish this func

return false;
public boolean setTurn(int turnNum) {//TODO test this
if(turnNum < 0 || turnNum > turns.size()) {
return false;
}
if(currTurn == turnNum) {
return true;
}
if(turnNum > currTurn) {
while(currTurn < turnNum) {
this.makeTurn(turns.get(currTurn).moves);
currTurn++;
}
}else {
while(currTurn > turnNum) {
undoTurn(turns.get(currTurn).tls);
currTurn--;
}
}
return true;
}
}
Loading

0 comments on commit cdabc9d

Please sign in to comment.