Commit 6f394138 authored by Jan Rossmeisl's avatar Jan Rossmeisl
Browse files

working movesets

parent ad84b15c
......@@ -10,8 +10,8 @@ Pawn,whitePawn1,6,0,white
Pawn,whitePawn2,6,1,white
Pawn,whitePawn3,6,2,white
Pawn,whitePawn4,5,3,white
Pawn,whitePawn5,5,5,white
Pawn,whitePawn6,4,5,white
Pawn,whitePawn5,6,4,white
Pawn,whitePawn6,6,5,white
Pawn,whitePawn7,6,6,white
Pawn,whitePawn8,6,7,white
Rook,blackRook1,0,0,black
......@@ -27,6 +27,6 @@ Bishop,blackBishop2,0,5,black
Bishop,whiteBishop1,7,2,white
Bishop,whiteBishop2,7,5,white
King,blackKing,0,4,black
King,whiteKing,4,3,white
King,whiteKing,7,4,white
Queen,blackQueen,0,3,black
Queen,whiteQueen,6,3,white
Queen,whiteQueen,7,3,white
......@@ -17,31 +17,42 @@ public class ActionHandler {
this.coordY = y;
if (fileHandler.loadMove()) {
// click 2
if (e.getSource() instanceof ChessPiece) {
if (e.getSource() instanceof ChessPiece) { // check if second field is a -chess piece- or an -empty field-
// chess piece
ChessPiece chessPiece = (ChessPiece) e.getSource();
System.out.println(chessPiece.getColor());
System.out.println(fileHandler.getColorMove());
if (chessPiece.getColor().equals(fileHandler.getColorMove())) {
if (chessPiece.getColor().equals(fileHandler.getColorMove())) { // check if they have the same color
// yes
fileHandler.clearFile();
deselectPiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move());
System.out.println("wird gemacht");
// no
} else if (field.movePieceValidated(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move(), x, y)) {
return;
} else {
field.movePiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move(), x, y);
fileHandler.clearFile();
deselectPiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move());
}
// empty field
} else if (field.movePieceValidated(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move(), x, y)) {
return;
} else {
field.movePiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move(), x, y);
fileHandler.clearFile();
deselectPiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move());
}
} else if (functions.checkType(e)){
//click 1
// click 1
} else if (functions.checkType(e)) { // check if button is a -chess piece- or an -empty field-
// chess piece
ChessPiece chessPiece = (ChessPiece) e.getSource();
selectPiece(e);
fileHandler.saveMove(coordX, coordY, chessPiece.getColor());
}
}
......@@ -107,7 +118,8 @@ public class ActionHandler {
((ChessPiece) field.recs[x][y]).setPieceIcon();
field.recs[x][y].repaint();
pieceSelected = false;
} else pieceSelected = true;
} else
pieceSelected = true;
}
}
\ No newline at end of file
......@@ -2,7 +2,7 @@ package application;
import javax.swing.ImageIcon;
public class Bishop extends ChessPiece{
public class Bishop extends ChessPiece {
private static final long serialVersionUID = 1L;
ImageLoader imageLoader = new ImageLoader();
......@@ -10,29 +10,56 @@ public class Bishop extends ChessPiece{
public Bishop(String name, int x, int y, String color) {
super(name, x, y, color);
setPieceIcon();
}
@Override
public ImageIcon getPieceIcon() {
if (this.getColor().equals("white")) {
if (isPieceSelected()) {
return imageLoader.getWhiteBishopSelected();
} else return imageLoader.getWhiteBishop();
} else
return imageLoader.getWhiteBishop();
} else if (this.getColor().equals("black")) {
if (isPieceSelected()) {
return imageLoader.getBlackBishopSelected();
} else return imageLoader.getBlackBishop();
} return null;
} else
return imageLoader.getBlackBishop();
}
return null;
}
public String toString() {
return "Bishop," + this.getName() + "," + this.getCoordX() + "," + this.getCoordY() + "," + this.getColor();
}
@Override
public boolean checkMoveset(Field field, int destX, int destY) {
for (int i = 1; i < 8; i++) {
// if (field.recs[this.getCoordX() - i][this.getCoordY() + i] instanceof ChessPiece ||
// field.recs[this.getCoordX() + i][this.getCoordY() - i] instanceof ChessPiece ||
// field.recs[this.getCoordX() + i][this.getCoordY() + i] instanceof ChessPiece ||
// field.recs[this.getCoordX() - i][this.getCoordY() - i] instanceof ChessPiece) {
// return false;
// } else
if ((destX == (this.getCoordX() - i) && destY == (this.getCoordY() + i))) {
return true;
} else if ((destX == (this.getCoordX() + i) && destY == (this.getCoordY() - i))) {
return true;
} else if ((destX == (this.getCoordX() + i) && destY == (this.getCoordY() + i))) {
return true;
} else if ((destX == (this.getCoordX() - i) && destY == (this.getCoordY() - i))) {
return true;
}
}
return false;
}
}
......@@ -2,7 +2,7 @@ package application;
import javax.swing.ImageIcon;
public abstract class ChessPiece extends ButtonsField{
public abstract class ChessPiece extends ButtonsField {
private static final long serialVersionUID = 1L;
......@@ -10,9 +10,9 @@ public abstract class ChessPiece extends ButtonsField{
private int state;
private String color;
private int selected;
public ChessPiece(String pieceName, int x, int y, String color) {
super("",x,y);
super("", x, y);
this.name = pieceName;
this.color = color;
}
......@@ -20,23 +20,23 @@ public abstract class ChessPiece extends ButtonsField{
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public void setState(int i) {
this.state = i;
}
public int getState() {
return this.state;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return this.color;
}
......@@ -48,17 +48,20 @@ public abstract class ChessPiece extends ButtonsField{
public void setSelected(int selected) {
this.selected = selected;
}
public boolean isPieceSelected() {
if(this.selected == 1) {
if (this.selected == 1) {
return true;
} else return false;
} else
return false;
}
public void setPieceIcon() {
setIcon(getPieceIcon());
}
public abstract ImageIcon getPieceIcon();
public abstract ImageIcon getPieceIcon();
public abstract boolean checkMoveset(Field field, int x, int y);
}
......@@ -33,16 +33,15 @@ public class Field {
private Queen queenBlack;
private Queen queenWhite;
FileHandler fileHandler = new FileHandler("StartField.txt");
String letters[] = { "A", "B", "C", "D", "E", "F", "G", "H" };
JPanel fieldPanel;
public Field(JPanel fieldPanel) {
this.recs = new ButtonsField[8][8];
this.fieldPanel = fieldPanel;
}
......@@ -53,19 +52,19 @@ public class Field {
pawnsBlack = fileHandler.getBlackPawns();
pawnsWhite = fileHandler.getWhitePawns();
rooksBlack = fileHandler.getBlackRooks();
rooksWhite = fileHandler.getWhiteRooks();
knightsBlack = fileHandler.getBlackKnights();
knightsWhite = fileHandler.getWhiteKnights();
bishopsBlack = fileHandler.getBlackBishops();
bishopsWhite = fileHandler.getWhiteBishops();
queenBlack = fileHandler.getBlackQueen();
queenWhite = fileHandler.getWhiteQueen();
kingBlack = fileHandler.getBlackKing();
kingWhite = fileHandler.getWhiteKing();
......@@ -135,13 +134,13 @@ public class Field {
recs[whiteKingCoordX][whiteKingCoordY] = whiteKing;
// Black Queen
Queen blackQueen= queenBlack;
Queen blackQueen = queenBlack;
int blackQueenCoordX = blackQueen.getCoordX();
int blackQueenCoordY = blackQueen.getCoordY();
recs[blackQueenCoordX][blackQueenCoordY] = blackQueen;
// White Queen
Queen whiteQueen= queenWhite;
Queen whiteQueen = queenWhite;
int whiteQueenCoordX = whiteQueen.getCoordX();
int whiteQueenCoordY = whiteQueen.getCoordY();
recs[whiteQueenCoordX][whiteQueenCoordY] = whiteQueen;
......@@ -157,7 +156,6 @@ public class Field {
recs[x][y].addActionListener(generateActionListener(coordX, coordY, field));
}
}
System.out.println(recs[1][0].getName());
System.out.println(pawnsBlack.get(0).getName() + ": " + pawnsBlack.get(0).getCoordX() + "|"
......@@ -172,9 +170,10 @@ public class Field {
@SuppressWarnings("unused")
ActionHandler actionHandler = new ActionHandler(e, field, coordX, coordY);
FileHandler fileHandler = new FileHandler("FieldCurrent.txt");
updateField();
fileHandler.saveCurrent(pawnsBlack, pawnsWhite, rooksBlack, rooksWhite, knightsBlack, knightsWhite, bishopsBlack, bishopsWhite, kingBlack, kingWhite, queenBlack, queenWhite);
fileHandler.saveCurrent(pawnsBlack, pawnsWhite, rooksBlack, rooksWhite, knightsBlack, knightsWhite,
bishopsBlack, bishopsWhite, kingBlack, kingWhite, queenBlack, queenWhite);
// fileHandler.saveCurrent(field);
}
};
......@@ -191,8 +190,24 @@ public class Field {
}
}
}
public boolean movePieceValidated(int x1, int y1, int x2, int y2) {
Field field = this;
if (recs[x1][y1] instanceof ChessPiece) {
if (((ChessPiece) recs[x1][y1]).checkMoveset(field, x2, y2)) {
movePiece(x1, y1, x2, y2);
return true;
}
}
return false;
}
public void movePiece(int x1, int y1, int x2, int y2) {
recs[x2][y2] = recs[x1][y1];
recs[x1][y1] = new EmptyField("", x1, y1);
// recs[x1][y1] = new EmptyField(x1 + " | " + y1, x1, y1);
......@@ -205,24 +220,23 @@ public class Field {
for (ActionListener actionListener : recs[x2][y2].getActionListeners()) {
recs[x2][y2].removeActionListener(actionListener);
}
recs[x2][y2].addActionListener(generateActionListener(x2,y2, this));
recs[x2][y2].addActionListener(generateActionListener(x2, y2, this));
((ChessPiece) recs[x2][y2]).setCoordX(x2);
((ChessPiece) recs[x2][y2]).setCoordY(y2);
}
System.out.println(recs[x2][y2]);
drawField();
FileHandler fileHandler = new FileHandler("Move.txt");
fileHandler.clearFile();
}
public void drawField() {
fieldPanel.removeAll();
setLetterLabels();
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
fieldPanel.add(recs[x][y]);
recs[x][y].setBorder(null);
......@@ -230,9 +244,10 @@ public class Field {
int sum = x + y;
int div = sum % 2;
if (div == 0) {
recs[x][y].setBackground(new Color(245,230,196));
} else recs[x][y].setBackground(new Color(249,249,249));
recs[x][y].setBackground(new Color(245, 230, 196));
} else
recs[x][y].setBackground(new Color(249, 249, 249));
}
}
}
......@@ -244,13 +259,13 @@ public class Field {
if (lettersLabel[i] == null) {
lettersLabel[i] = new JLabel(letters[i]);
lettersLabel[i].setPreferredSize(new Dimension(100, 100));
lettersLabel[i].setBorder(new MatteBorder(0, 2, 2, 0, Color.WHITE));
lettersLabel[i].setBorder(new MatteBorder(0, 2, 2, 0, Color.WHITE));
lettersLabel[i].setForeground(Color.WHITE);
lettersLabel[i].setHorizontalAlignment(JLabel.CENTER);
fieldPanel.add(lettersLabel[i]);
fieldPanel.setBackground(new Color(205,116,116));
fieldPanel.setBackground(new Color(205, 116, 116));
}
}
}
......
......@@ -15,32 +15,30 @@ public class FileHandler {
private String line;
private String[] lineParts = new String[5];
private String[] linePartsMove = new String[2];
private List<Pawn> pawnsBlack = new ArrayList<>();
private List<Pawn> pawnsWhite = new ArrayList<>();
private List<Rook> rooksBlack = new ArrayList<>();
private List<Rook> rooksWhite= new ArrayList<>();
private List<Rook> rooksWhite = new ArrayList<>();
private List<Knight> knightsBlack = new ArrayList<>();
private List<Knight> knightsWhite = new ArrayList<>();
private List<Bishop> bishopsBlack = new ArrayList<>();
private List<Bishop> bishopsWhite= new ArrayList<>();
private List<Bishop> bishopsWhite = new ArrayList<>();
private King kingBlack;
private King kingWhite;
private Queen queenBlack;
private Queen queenWhite;
public FileHandler(String file) {
this.fileName = Paths.get(file);
this.fileNameString = file;
}
public boolean loadMove() {
try {
@SuppressWarnings("resource")
......@@ -48,14 +46,12 @@ public class FileHandler {
if (!fileReader.hasNext()) {
return false;
}
while (fileReader.hasNextLine()) {
line = fileReader.nextLine();
}
return true;
} catch (Exception e) {
......@@ -63,7 +59,7 @@ public class FileHandler {
return false;
}
}
public boolean clearFile() {
try {
@SuppressWarnings("resource")
......@@ -74,72 +70,66 @@ public class FileHandler {
e.printStackTrace();
return false;
}
}
public boolean saveMove(int x, int y, String color) {
try (PrintWriter writer = new PrintWriter(fileNameString)){
try (PrintWriter writer = new PrintWriter(fileNameString)) {
writer.println(x + "," + y + "," + color);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public boolean saveCurrent(List<Pawn> pawnsBlack, List<Pawn> pawnsWhite,
List<Rook> rooksBlack, List<Rook> rooksWhite,
List<Knight> knightsBlack, List<Knight> knightsWhite,
List<Bishop> bishopsBlack, List<Bishop> bishopsWhite,
King kingBlack, King kingWhite,
Queen queenBlack, Queen queenWhite) {
try (PrintWriter writer = new PrintWriter("FieldCurrent.txt")){
public boolean saveCurrent(List<Pawn> pawnsBlack, List<Pawn> pawnsWhite, List<Rook> rooksBlack,
List<Rook> rooksWhite, List<Knight> knightsBlack, List<Knight> knightsWhite, List<Bishop> bishopsBlack,
List<Bishop> bishopsWhite, King kingBlack, King kingWhite, Queen queenBlack, Queen queenWhite) {
try (PrintWriter writer = new PrintWriter("FieldCurrent.txt")) {
for (Pawn pawnBlack : pawnsBlack) {
writer.println(pawnBlack);
}
for (Pawn pawnWhite : pawnsWhite) {
writer.println(pawnWhite);
}
for (Rook rookBlack : rooksBlack) {
writer.println(rookBlack);
}
for (Rook rookWhite: rooksWhite) {
for (Rook rookWhite : rooksWhite) {
writer.println(rookWhite);
}
for (Knight knightBlack : knightsBlack) {
writer.println(knightBlack);
}
for (Knight knightWhite: knightsWhite) {
for (Knight knightWhite : knightsWhite) {
writer.println(knightWhite);
}
for (Bishop bishopBlack : bishopsBlack) {
writer.println(bishopBlack);
}
for (Bishop bishopWhite : bishopsWhite) {
writer.println(bishopWhite);
}
writer.println(kingBlack);
writer.println(kingWhite);
writer.println(queenBlack);
writer.println(queenWhite);
return true;
} catch (Exception e) {
......@@ -147,98 +137,98 @@ public class FileHandler {
return false;
}
}
public boolean load() {
try {
@SuppressWarnings("resource")
Scanner fileReader = new Scanner(fileName);
while (fileReader.hasNextLine()) {
String line = fileReader.nextLine();
splitLine(line);
String type = getType();
String pieceName = getPieceName();
int coordX = getCoordX();
int coordY = getCoordY();
String color = getColor();
if (type.equals("Pawn")) {
if (color.equals("black")) {
pawnsBlack.add(new Pawn(pieceName, coordX, coordY, color));
}
if (color.equals("white")) {
pawnsWhite.add(new Pawn(pieceName, coordX, coordY, color));
}
}
if (type.equals("Rook")) {
if (color.equals("black")) {
rooksBlack.add(new Rook(pieceName, coordX, coordY, color));
}
if (color.equals("white")) {
rooksWhite.add(new Rook(pieceName, coordX, coordY, color));
}
}