Commit ad84b15c authored by Jan Rossmeisl's avatar Jan Rossmeisl
Browse files

stable state of chess (playable)

parent 70fe4970
......@@ -2,31 +2,31 @@ Pawn,blackPawn1,1,0,black
Pawn,blackPawn2,1,1,black
Pawn,blackPawn3,1,2,black
Pawn,blackPawn4,1,3,black
Pawn,blackPawn5,3,5,black
Pawn,blackPawn6,4,5,black
Pawn,blackPawn7,4,4,black
Pawn,blackPawn5,1,4,black
Pawn,blackPawn6,1,5,black
Pawn,blackPawn7,1,6,black
Pawn,blackPawn8,1,7,black
Pawn,whitePawn1,4,0,white
Pawn,whitePawn2,4,1,white
Pawn,whitePawn1,6,0,white
Pawn,whitePawn2,6,1,white
Pawn,whitePawn3,6,2,white
Pawn,whitePawn4,6,3,white
Pawn,whitePawn5,4,0,white
Pawn,whitePawn6,6,5,white
Pawn,whitePawn4,5,3,white
Pawn,whitePawn5,5,5,white
Pawn,whitePawn6,4,5,white
Pawn,whitePawn7,6,6,white
Pawn,whitePawn8,6,7,white
Rook,blackRook1,0,0,black
Rook,blackRook2,0,7,black
Rook,whiteRook1,7,0,white
Rook,whiteRook2,4,4,white
Rook,whiteRook2,7,7,white
Knight,blackKnight1,0,1,black
Knight,blackKnight2,0,6,black
Knight,whiteKnight1,7,1,white
Knight,whiteKnight2,7,4,white
Knight,whiteKnight2,7,6,white
Bishop,blackBishop1,0,2,black
Bishop,blackBishop2,0,5,black
Bishop,whiteBishop1,4,0,white
Bishop,whiteBishop2,4,3,white
Bishop,whiteBishop1,7,2,white
Bishop,whiteBishop2,7,5,white
King,blackKing,0,4,black
King,whiteKing,5,2,white
King,whiteKing,4,3,white
Queen,blackQueen,0,3,black
Queen,whiteQueen,5,3,white
Queen,whiteQueen,6,3,white
......@@ -29,4 +29,4 @@ Bishop,whiteBishop2,7,5,white
King,blackKing,0,4,black
King,whiteKing,7,4,white
Queen,blackQueen,0,3,black
Queen,whiteQueen,7,3,white
\ No newline at end of file
Queen,whiteQueen,7,3,white
......@@ -16,19 +16,33 @@ public class ActionHandler {
this.coordX = x;
this.coordY = y;
if (fileHandler.loadMove()) { // click 1 or click 2?
if (fileHandler.loadMove()) {
// click 2
deselectPiece(e);
field.movePiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move(), x, y);
// click 1
} else if (functions.checkType(e)) {
if (e.getSource() instanceof ChessPiece) {
ChessPiece chessPiece = (ChessPiece) e.getSource();
System.out.println(chessPiece.getColor());
System.out.println(fileHandler.getColorMove());
if (chessPiece.getColor().equals(fileHandler.getColorMove())) {
fileHandler.clearFile();
deselectPiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move());
System.out.println("wird gemacht");
} else {
field.movePiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move(), x, y);
}
} else {
field.movePiece(fileHandler.getCoordX1Move(), fileHandler.getCoordY1Move(), x, y);
}
} else if (functions.checkType(e)){
//click 1
ChessPiece chessPiece = (ChessPiece) e.getSource();
selectPiece(e);
System.out.println(e.getSource());
fileHandler.saveMove(coordX, coordY);
} else return;
fileHandler.saveMove(coordX, coordY, chessPiece.getColor());
}
}
public void selectPiece(ActionEvent e) {
......@@ -84,74 +98,16 @@ public class ActionHandler {
pieceSelected = true;
} else
this.pieceSelected = false;
// if (e.getSource() instanceof EmptyField) {
//
// }
}
public void deselectPiece(ActionEvent e) {
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
// field.recs[x][y].setse
//
// if (field.recs[x][y].getSelected()) {
//
// }
}
}
if (pieceSelected == true) {
if (e.getSource() instanceof ChessPiece) {
if (e.getSource() instanceof Pawn) {
Pawn button = (Pawn) e.getSource();
button.setSelected(0);
button.setPieceIcon();
button.repaint();
}
if (e.getSource() instanceof Rook) {
Rook button = (Rook) e.getSource();
button.setSelected(0);
button.setPieceIcon();
button.repaint();
}
if (e.getSource() instanceof Knight) {
Knight button = (Knight) e.getSource();
button.setSelected(0);
button.setPieceIcon();
button.repaint();
}
if (e.getSource() instanceof Bishop) {
Bishop button = (Bishop) e.getSource();
button.setSelected(0);
button.setPieceIcon();
button.repaint();
}
if (e.getSource() instanceof King) {
King button = (King) e.getSource();
button.setSelected(0);
button.setPieceIcon();
button.repaint();
}
public void deselectPiece(int x, int y) {
if (e.getSource() instanceof Queen) {
Queen button = (Queen) e.getSource();
button.setSelected(0);
button.setPieceIcon();
button.repaint();
}
System.out.println("piece deselected");
pieceSelected = false;
if (field.recs[x][y] instanceof ChessPiece) {
((ChessPiece) field.recs[x][y]).setSelected(0);
((ChessPiece) field.recs[x][y]).setPieceIcon();
field.recs[x][y].repaint();
pieceSelected = false;
} else pieceSelected = true;
} else
this.pieceSelected = false;
}
}
}
\ No newline at end of file
......@@ -7,13 +7,11 @@ public class ButtonsField extends JButton{
private static final long serialVersionUID = 1L;
private int coordX, coordY;
private boolean selected;
public ButtonsField(String name, int x, int y, boolean selected) {
public ButtonsField(String name, int x, int y) {
super(name);
this.coordX = x;
this.coordY = y;
this.selected = false;
}
......
......@@ -12,7 +12,7 @@ public abstract class ChessPiece extends ButtonsField{
private int selected;
public ChessPiece(String pieceName, int x, int y, String color) {
super("",x,y, false);
super("",x,y);
this.name = pieceName;
this.color = color;
}
......
......@@ -6,7 +6,7 @@ public class EmptyField extends ButtonsField {
public EmptyField(String name, int x, int y) {
super(name,x,y, false);
super(name,x,y);
}
}
......@@ -7,10 +7,9 @@ import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
public class Field {
......@@ -49,6 +48,8 @@ public class Field {
public void setField() {
fileHandler.load();
FileHandler fileHandlerMove = new FileHandler("Move.txt");
fileHandlerMove.clearFile();
pawnsBlack = fileHandler.getBlackPawns();
pawnsWhite = fileHandler.getWhitePawns();
......@@ -168,6 +169,7 @@ public class Field {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
@SuppressWarnings("unused")
ActionHandler actionHandler = new ActionHandler(e, field, coordX, coordY);
FileHandler fileHandler = new FileHandler("FieldCurrent.txt");
......@@ -182,7 +184,8 @@ public class Field {
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
if (recs[x][y] == null) {
recs[x][y] = new EmptyField(x + " | " + y, x, y);
recs[x][y] = new EmptyField("", x, y);
// recs[x][y] = new EmptyField(x + " | " + y, x, y);
recs[x][y].setPreferredSize(new Dimension(100, 100));
}
}
......@@ -191,7 +194,8 @@ public class Field {
public void movePiece(int x1, int y1, int x2, int y2) {
recs[x2][y2] = recs[x1][y1];
recs[x1][y1] = new EmptyField(x1 + " | " + y1, x1, y1);
recs[x1][y1] = new EmptyField("", x1, y1);
// recs[x1][y1] = new EmptyField(x1 + " | " + y1, x1, y1);
recs[x1][y1].addActionListener(generateActionListener(x1, y1, this));
if (recs[x2][y2] instanceof ChessPiece) {
......@@ -239,17 +243,15 @@ public class Field {
for (int i = 0; i < 8; i++) {
if (lettersLabel[i] == null) {
lettersLabel[i] = new JLabel(letters[i]);
fieldPanel.add(lettersLabel[i]);
lettersLabel[i].setPreferredSize(new Dimension(100, 100));
lettersLabel[i].setBorder(new LineBorder(Color.BLACK, 1));
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));
}
}
}
private void deselectPiece() {
}
}
......@@ -34,7 +34,7 @@ public class FileHandler {
private Queen queenBlack;
private Queen queenWhite;
private int coordX1, coordY1;
public FileHandler(String file) {
this.fileName = Paths.get(file);
......@@ -43,6 +43,7 @@ public class FileHandler {
public boolean loadMove() {
try {
@SuppressWarnings("resource")
Scanner fileReader = new Scanner(fileName);
if (!fileReader.hasNext()) {
return false;
......@@ -51,8 +52,7 @@ public class FileHandler {
while (fileReader.hasNextLine()) {
line = fileReader.nextLine();
this.coordX1 = getCoordX1Move();
this.coordY1 = getCoordY1Move();
}
......@@ -66,6 +66,7 @@ public class FileHandler {
public boolean clearFile() {
try {
@SuppressWarnings("resource")
PrintWriter writer = new PrintWriter(fileNameString);
writer.print("");
return true;
......@@ -76,10 +77,10 @@ public class FileHandler {
}
public boolean saveMove(int x, int y) {
public boolean saveMove(int x, int y, String color) {
try (PrintWriter writer = new PrintWriter(fileNameString)){
writer.println(x + "," + y);
writer.println(x + "," + y + "," + color);
return true;
} catch (Exception e) {
......@@ -265,6 +266,11 @@ public class FileHandler {
return Integer.valueOf(linePartsMove[1]);
}
public String getColorMove() {
linePartsMove = line.split(",");
return linePartsMove[2];
}
public String getColor() {
return lineParts[4];
}
......
......@@ -2,8 +2,6 @@ package application;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class Functions {
Pawn pawn;
......@@ -17,14 +15,9 @@ public class Functions {
EmptyField emptyField;
private int coordX1, coordY1, coordX2, coordY2;
public Functions(ActionEvent e) {
this.coordX1 = 99;
this.coordY1 = 99;
this.coordX2 = 99;
this.coordY2 = 99;
}
public void setPieceType(ActionEvent e, int x, int y) {
......
......@@ -8,7 +8,7 @@ import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
public class Gui {
......@@ -62,13 +62,17 @@ public class Gui {
cornerLabel.setPreferredSize(new Dimension(100,100));
numberIterationPanel.add(cornerLabel);
for (int i = 0; i < 8; i++) {
numbers = new JLabel[8];
numbers[i] = new JLabel(String.valueOf(i + 1));
numberIterationPanel.add(numbers[i]);
numbers[i].setPreferredSize(new Dimension(100, 100));
numbers[i].setBorder(new LineBorder(Color.BLACK, 1));
numbers[i].setBorder(new MatteBorder(2, 0, 0, 2, Color.WHITE));
numbers[i].setForeground(Color.WHITE);
numbers[i].setBackground(new Color(185,61,61));
numbers[i].setHorizontalAlignment(JLabel.CENTER);
numberIterationPanel.add(numbers[i]);
numberIterationPanel.setBackground(new Color(205,116,116));
}
mainPanel.add(numberIterationPanel, BorderLayout.WEST);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment