Skip to content

Commit

Permalink
V1 , Promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
Slavrick committed Jan 23, 2021
1 parent c063eff commit f4f1991
Show file tree
Hide file tree
Showing 25 changed files with 272 additions and 99 deletions.
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/Board.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/engine/Move.class
Binary file not shown.
Binary file modified bin/engine/MoveNotation.class
Binary file not shown.
Binary file modified bin/engine/Timeline.class
Binary file not shown.
20 changes: 20 additions & 0 deletions res/PromotionPrompt.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.canvas.Canvas?>


<AnchorPane fx:id="innerLayout" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI.PromotionPrompt">
<Button layoutX="10" layoutY="10" prefWidth="75" text="Queen" onAction="#handleSubmitButton" />
<Button layoutX="90" layoutY="10" prefWidth="75" text="Rook" onAction="#handleSubmitButton" />
<Button layoutX="10" layoutY="40" prefWidth="75" text="Bishop" onAction="#handleSubmitButton" />
<Button layoutX="90" layoutY="40" prefWidth="75" text="Knight" onAction="#handleSubmitButton" />
<Button layoutX="10" layoutY="70" prefWidth="75" text="Unicorn" onAction="#handleSubmitButton" />
<Button layoutX="90" layoutY="70" prefWidth="75" text="Dragon" onAction="#handleSubmitButton" />
</AnchorPane>


91 changes: 68 additions & 23 deletions src/GUI/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import fileIO.FENParser;


public class Controller {
public class Controller implements MessageListener{
@FXML
BorderPane innerLayout;
@FXML
Expand All @@ -57,13 +57,8 @@ public class Controller {
TextField movefield;
@FXML
Label statusLabel;

ObservableList<String> notationStringArray;



GameStateManager g;

static final double MAX_FONT_SIZE = 20.0;

// Canvas Constants
Expand All @@ -73,17 +68,17 @@ public class Controller {
double screenY = 0;
double canvasWidth = 8000;
double canvasHeight = 8000;

double startDragx;
double startDragy;
double xchange;
double ychange;

boolean dragging = false;
boolean release = false;

GameStateManager g;
CoordFive selectedSquare;
ArrayList<CoordFour> destinations;
Move promotionMoveBuffer;

boolean prompt = false;

Expand Down Expand Up @@ -136,10 +131,28 @@ public void handle(MouseEvent e) {
destinations = null;
drawStage();
} else if (e.getButton() == MouseButton.PRIMARY) {
if(promotionMoveBuffer != null) {
return;
}
CoordFive clickedCoord = getCoordClicked((int) e.getX(), (int) e.getY(), g.width, g.height);
if (destinations != null && clickedCoord != null && clickedCoord.color == selectedSquare.color && alContains(destinations, clickedCoord)) {
Move selectedMove = new Move(selectedSquare, clickedCoord);
g.makeMove(selectedMove);
//If it would be a promotion, do something special.
if(selectedMove.type == Move.SPATIALMOVE && ( selectedMove.dest.y == 0 || selectedMove.dest.y == g.height - 1 )) {
int pieceMoved = g.getSquare(selectedMove.origin, g.color);
pieceMoved = pieceMoved < 0 ? pieceMoved * -1 : pieceMoved;
if(pieceMoved == 1 || pieceMoved == 11) {
System.out.println("Promote");
promotionMoveBuffer = selectedMove;
showPromotionPrompt();
selectedSquare = null;
destinations = null;
return;
}
}
if(g.makeMove(selectedMove)) {
screenX += ChessDrawer.squarewidth * g.height + ChessDrawer.padding;
}
selectedSquare = null;
destinations = null;
drawStage();
Expand Down Expand Up @@ -172,7 +185,10 @@ public void handle(KeyEvent event) {
drawStage();
statusLabel.setFont(new Font(MAX_FONT_SIZE));
setStatusLabel();

if(Globals.es == null) {
Globals.es = new EventSource();
Globals.es.addListener(this);
}
}

//===========================Event Functions=========================================================================
Expand All @@ -190,6 +206,7 @@ public void handleUndoButton(ActionEvent e) {
g.undoTempMoves();
selectedSquare = null;
destinations = null;
promotionMoveBuffer = null;
drawStage();
}

Expand All @@ -200,7 +217,7 @@ public void handleSubmitButton(ActionEvent e) {
if(submitted) {
notationStringArray.add(g.turns.get(g.turns.size() - 1).toString());
boolean mated = g.bruteForceMateDetection();
System.out.println(mated);
//System.out.println(mated);
if(mated) {
String color;
if(g.color) {
Expand All @@ -224,8 +241,7 @@ private void handlePanButton(ActionEvent event) {
}

@FXML
private void handleListEvent(MouseEvent event) {
System.out.println("Chagne");
private void handleListEvent(MouseEvent event) throws IOException {
//XXX set this -- test this
int turnIndex = 0;
//g.setTurn(turnIndex);
Expand All @@ -242,8 +258,13 @@ private void handleMenu(ActionEvent event) {
}

@FXML
private void handleEventList(ActionEvent event) {

private void handleEventList(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/PromotionPrompt.fxml"));
Scene properties = new Scene(root, 200, 200);
Stage stage = new Stage();
stage.setScene(properties);
stage.setResizable(false);
stage.show();
}

@FXML
Expand Down Expand Up @@ -281,14 +302,6 @@ private void setProperties(ActionEvent e) throws IOException {
stage.setScene(properties);
stage.setResizable(false);
stage.show();
/*
p.getContent().add(root);
p.setX(500);
p.setY(500);
System.out.println("Got Resource, Will Show Now");
Window w = new Window();
*/
//XXX finish this later
}

//================================================================================================================
Expand Down Expand Up @@ -362,6 +375,22 @@ public void drawStage(double changex, double changey) {

}

private void showPromotionPrompt(){
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("/PromotionPrompt.fxml"));
}catch(Exception e) {
System.out.println("Could Not load Promotion Prompt");
return;
}
Scene properties = new Scene(root, 175, 100);
Stage stage = new Stage();
stage.setTitle("Select a promotion Type!");
stage.setScene(properties);
stage.setResizable(false);
stage.show();
}

/**
* This was created because I couldn't understand why the arraylist.contains was not working properly
* It is supposed to call the .equals function, but i think that it checking if two objects were the same memory, not the same contents.
Expand All @@ -384,4 +413,20 @@ private void panToBoard(int T, int L) {
screenY = pany;
}

@Override
public void handleMessage(MessageEvent m) {
if(m.type == MessageEvent.Promotion) {
promotionMoveBuffer.specialType = m.imess;
if(!g.color) {
promotionMoveBuffer.specialType += Board.numTypes;
}
if(g.makeMove(promotionMoveBuffer)) {
screenX += ChessDrawer.squarewidth * g.width + ChessDrawer.padding;
}
drawStage();
promotionMoveBuffer = null;
}

}

}
22 changes: 22 additions & 0 deletions src/GUI/EventSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package GUI;

import java.util.ArrayList;

public class EventSource {
ArrayList<MessageListener> subscribers;

public EventSource() {
subscribers = new ArrayList<MessageListener>();
}

public void addListener(MessageListener sub) {
subscribers.add(sub);
}

public void broadcastEvent(MessageEvent e) {
for(MessageListener sub: subscribers) {
sub.handleMessage(e);
}
}
}

5 changes: 5 additions & 0 deletions src/GUI/Globals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package GUI;

public class Globals {
public static EventSource es;
}
11 changes: 11 additions & 0 deletions src/GUI/MessageEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package GUI;

public class MessageEvent {
public static final int Promotion = 1;

public int type;
public int imess;
public String Message;
public Object Source;

}
5 changes: 5 additions & 0 deletions src/GUI/MessageListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package GUI;

public interface MessageListener {
public void handleMessage(MessageEvent m);
}
49 changes: 49 additions & 0 deletions src/GUI/PromotionPrompt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package GUI;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class PromotionPrompt {
@FXML
AnchorPane innerLayout;

@FXML
void initialize() {
innerLayout.setPrefWidth(200);
innerLayout.setPrefHeight(300);
}

public PromotionPrompt() {

}

@FXML
private void handleSubmitButton(ActionEvent e) {
MessageEvent m = new MessageEvent();
m.type = MessageEvent.Promotion;
Button b = (Button) (e.getSource());
String text = b.getText();
int promotionType = 0;
if(text.equals("Queen")) {
promotionType = 6;
}else if(text.equals("Rook")) {
promotionType = 4;
}else if(text.equals("Bishop")) {
promotionType = 3;
}else if(text.equals("Knight")) {
promotionType = 2;
}else if(text.equals("Dragon")) {
promotionType = 9;
}else if(text.equals("Unicorn")) {
promotionType = 8;
}
m.imess = promotionType;
Globals.es.broadcastEvent(m);
Stage stage = (Stage) innerLayout.getScene().getWindow();
stage.close();
}
}
36 changes: 0 additions & 36 deletions src/engine/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,38 +109,6 @@ public boolean isInBounds(CoordFour cf) {
return isInBounds(cf.x, cf.y);
}

/** TODO maybe this should be in the Timeline class?
* Casltes a king on the present board by checking if it is possible and then swapping the pieces.
* @param color the color to swap
* @param side the side to swap to( queen or king -- false or true respectively)
* @return whether or not the caslte happened.
*/
public boolean CastleKing(boolean color, boolean side) {
if(color) {
if(side && wkingSideCastle) {
wkingSideCastle = false;
wqueenSideCastle = false;
//TODO add code swapping pieces.
return true;
}
else if(!side && wqueenSideCastle) {
bkingSideCastle = false;
bqueenSideCastle = false;
return false;
}else {
return false;
}
}else if (!color) {
return false;
}else {
return false;
}
}

public boolean canCastle() {
return false;
}

/**
* Gets a string representation of the board. this string starts with the 1st
* rank, and each rank is a line of text.
Expand Down Expand Up @@ -175,8 +143,4 @@ public static boolean getColorBool(int pieceCode) {
}
return GameState.WHITE;
}




}
Loading

0 comments on commit f4f1991

Please sign in to comment.