Shifted coordinate system to fix bug

This commit is contained in:
Loveb 2022-03-06 11:10:59 +01:00
parent 17fcfbc571
commit 795bcf1345
6 changed files with 88 additions and 80 deletions

View File

@ -17,38 +17,23 @@ public class Bishop extends Piece {
LinkedHashSet<Point> unmovable = new LinkedHashSet<>(); LinkedHashSet<Point> unmovable = new LinkedHashSet<>();
LinkedHashSet<Point> perhapsMovable = new LinkedHashSet<>(); LinkedHashSet<Point> perhapsMovable = new LinkedHashSet<>();
try { // // Ner höger
Piece[][] p = new Piece[][]{ // for (int x = 0, y = 0; x < 8 && x > -8; x++, y++) {
{new King(false)}, // System.out.println("x: " + x + ", y: " + y);
{}, // }
{}, // // Upp höger
{}, // for (int x = 0, y = 0; x < 8 && x > -8; x++, y--) {
{}, // System.out.println("x: " + x + ", y: " + y);
{}, // }
{}, // // Ner vänster
{new King(true),null } // for (int x = 0, y = 0; x < 8 && x > -8; x--, y--) {
}; // System.out.println("x: " + x + ", y: " + y);
} catch (IOException ex) { // }
Logger.getLogger(Bishop.class.getName()).log(Level.SEVERE, null, ex); // // Upp vänster
} // for (int x = 0, y = 0; x < 8 && x > -8; x--, y++) {
// System.out.println("x: " + x + ", y: " + y);
// Ner höger // }
for (int x = 0, y = 0; x < 8 && x > -8; x++, y++) { return new LinkedHashSet<>();
System.out.println("x: " + x + ", y: " + y);
}
// Upp höger
for (int x = 0, y = 0; x < 8 && x > -8; x++, y--) {
System.out.println("x: " + x + ", y: " + y);
}
// Ner vänster
for (int x = 0, y = 0; x < 8 && x > -8; x--, y--) {
System.out.println("x: " + x + ", y: " + y);
}
// Upp vänster
for (int x = 0, y = 0; x < 8 && x > -8; x--, y++) {
System.out.println("x: " + x + ", y: " + y);
}
return null;
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -25,22 +25,46 @@ public class Board extends JPanel {
private Piece[][] initPieces() throws IOException { private Piece[][] initPieces() throws IOException {
Piece[][] piecesRet = { Piece[][] piecesRet = {
{null, null, null, null, new King(false), null, null, null},
{null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null},
{new King(false), null, null, null, null, null, null, new King(true)},
{null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null},
{null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null},
{null, null, null, null, new King(true), null, null, null} {null, null, null, null, null, null, null, null}
}; };
// Pawns // Test
for (int i = 0; i < piecesRet[1].length; i++) { piecesRet[2][2] = null;
piecesRet[1][i] = new Pawn(false, new Point(i, 1)); piecesRet[2][3] = null;
piecesRet[6][i] = new Pawn(true, new Point(i, 6));
// Sätt ut bönder
for (int i = 0; i < pieces.length; i++) {
piecesRet[i][1] = new Pawn(false, new Point(i, 1));
piecesRet[i][6] = new Pawn(true, new Point(i, 6));
} }
// // Sätt ut bönder no point
// for (int i = 0; i < pieces.length; i++) {
// piecesRet[i][1] = new Pawn(false);
// piecesRet[i][6] = new Pawn(true);
// }
// // Ställ in varje Point i varje pjäs
// for (int x = 0; x < piecesRet.length; x++) {
// for (int y = 0; y < piecesRet.length; y++) {
// try {
// Point before = new Point(piecesRet[x][y].position);
// piecesRet[x][y].position = new Point(x, y);
// System.out.println("Efter "+piecesRet[x][y].position + " Innan " + before);
// } catch (NullPointerException e) {
// }
// }
// }
printPieces(piecesRet);
return piecesRet; return piecesRet;
} }
@ -49,13 +73,6 @@ public class Board extends JPanel {
Graphics2D g2 = (Graphics2D) g; Graphics2D g2 = (Graphics2D) g;
drawSquares(g2); drawSquares(g2);
// for (Piece[] pieceArr : pieces) {
// for (Piece p : pieceArr) {
// if (p != null) {
// p.draw(g2);
// }
// }
// }
// Draw piece // Draw piece
Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> { Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
if (piece != null) { if (piece != null) {
@ -64,42 +81,40 @@ public class Board extends JPanel {
})); }));
// Check valid moves method // Check valid moves method
Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> { // Arrays.stream(pieces).forEach(pieceArr -> Arrays.stream(pieceArr).forEach(piece -> {
if (piece != null) { // if (piece != null) {
// Draw eglible moves // // Draw eglible moves
LinkedHashSet<Point> validMoves = piece.validMoves(pieces); // LinkedHashSet<Point> validMoves = piece.validMoves(pieces);
Color c = new Color((int) (230 * Math.random()), (int) (230 * Math.random()), (int) (230 * Math.random())); // Color c = new Color((int) (230 * Math.random()), (int) (230 * Math.random()), (int) (230 * Math.random()));
g2.setColor(c);
validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
System.out.println("x:" + piece.position.x + ", y:" + piece.position.y + ": " + validMoves.size());
}
}));
printPieces();
// // Check valid moves method
// for (Piece[] piecesOne : pieces) {
// for (Piece p : piecesOne) {
// if (p == null) {
// continue;
// }
// LinkedHashSet<Point> validMoves = p.validMoves(pieces);
// Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
// g2.setColor(c); // g2.setColor(c);
// validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE)); // validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
// System.out.println("x:" + p.position.x + ", y:" + p.position.y + ": " + validMoves.size()); // System.out.println("x:" + piece.position.x + ", y:" + piece.position.y + ": " + validMoves.size());
// }
// } // }
// }));
// Check valid moves method
for (int y = 0; y < pieces.length; y++) {
for (int x = 0; x < pieces.length; x++) {
Piece p = pieces[y][x];
if (p == null) {
continue;
} }
private void printPieces() { LinkedHashSet<Point> validMoves = p.validMoves(pieces);
Color c = new Color((int) (255 * Math.random()), (int) (255 * Math.random()), (int) (255 * Math.random()));
g2.setColor(c);
validMoves.forEach(point -> g2.fillOval(point.x * SIZE_OF_TILE, point.y * SIZE_OF_TILE, SIZE_OF_TILE, SIZE_OF_TILE));
}
}
}
private void printPieces(Piece[][] pieces) {
System.out.println(""); System.out.println("");
for (int i = 0; i < pieces.length; i++) { for (int loopX = 0; loopX < pieces.length; loopX++) {
Piece[] pieceArr = pieces[i]; for (int loopY = 0; loopY < pieces.length; loopY++) {
for (int j = 0; j < pieceArr.length; j++) { if (loopY == 0) {
if (j == 0) {
System.out.print("|"); System.out.print("|");
} }
Piece piece = pieceArr[j]; Piece piece = pieces[loopY][loopX];
// Titta inte Nicklas. Det är bara debug, jag ska ta bort det sedan lovar :P // Titta inte Nicklas. Det är bara debug, jag ska ta bort det sedan lovar :P
String type; String type;
@ -116,9 +131,7 @@ public class Board extends JPanel {
} }
System.out.println(""); System.out.println("");
} }
} }
private void drawSquares(Graphics2D g2) { private void drawSquares(Graphics2D g2) {

View File

@ -45,14 +45,12 @@ public final class King extends Piece {
System.out.println(p); System.out.println(p);
// If this piece is the same team as ours, skip // If this piece is the same team as ours, skip
if (p.isWhite == this.isWhite) { if (p.isWhite == this.isWhite) {
System.out.println("equals");
continue; continue;
} }
movable.add(pos); movable.add(pos);
} catch (NullPointerException npe) { } catch (NullPointerException npe) {
// This is an empty spot // This is an empty spot
System.out.println("null: " + pos);
movable.add(pos); movable.add(pos);
} catch (Exception e) { } catch (Exception e) {
// This means that the player is at the edge // This means that the player is at the edge

View File

@ -11,6 +11,10 @@ public class Pawn extends Piece {
setPieceIcon("Pawn"); setPieceIcon("Pawn");
} }
Pawn(boolean isWhite) {
super(isWhite);
}
@Override @Override
public LinkedHashSet<Point> validMoves(Piece[][] pieces) { public LinkedHashSet<Point> validMoves(Piece[][] pieces) {

View File

@ -21,6 +21,14 @@ public abstract class Piece extends Component {
this.position = startingPosition; this.position = startingPosition;
} }
public Piece(boolean isWhite) {
this.isWhite = isWhite;
}
public void setPosition(Point p) {
this.position = p;
}
protected void setPieceIcon(String className) throws IOException { protected void setPieceIcon(String className) throws IOException {
String colorName = isWhite ? "White" : "Black"; String colorName = isWhite ? "White" : "Black";
String fileName = colorName + className + ".png"; String fileName = colorName + className + ".png";

View File

@ -19,7 +19,7 @@ public class Schack extends JFrame {
public Schack() throws IOException { public Schack() throws IOException {
setTitle("Schack"); setTitle("Schack");
setAlwaysOnTop(true); setAlwaysOnTop(false);
setResizable(false); setResizable(false);
setContentPane(new Board()); setContentPane(new Board());