Nickals fix

Löste bugg där man inte kan flytta de yttersta bönderna
This commit is contained in:
loveb 2022-03-24 08:43:23 +01:00
parent 1696c1519b
commit 96ee14f128
2 changed files with 11 additions and 8 deletions

View File

@ -25,12 +25,13 @@ public class Pawn extends Piece {
// Om bonden har gått en gång, får 1 steg, annars 2
final int upTo = hasMoved ? 1 : 2;
// Kolla om man kan rakt frak
for (int pawnX = 1; pawnX <= upTo; pawnX++) {
for (int pawnDY = 1; pawnDY <= upTo; pawnDY++) {
System.out.println("this.position.x: " + this.position.x);
System.out.println("calced y: " + (this.position.y + (this.isWhite ? -pawnX : pawnX)));
Point pos = new Point(this.position.x, this.position.y + (this.isWhite ? -pawnX : pawnX));
System.out.println("calced y: " + (this.position.y + (this.isWhite ? -pawnDY : pawnDY)));
Point pos = new Point(this.position.x, this.position.y + (this.isWhite ? -pawnDY : pawnDY));
boolean shouldBreak = checkMove(pos, movable, pieces);
if (shouldBreak) {
System.out.println("should brkje!");
break;
}
}
@ -47,10 +48,10 @@ public class Pawn extends Piece {
// Känns som det här skulle kunnat vara i validMoves, men nu är det såhär
private void checkAttack(Point pos, LinkedHashSet movable, Piece[][] pieces) {
Piece p = pieces[pos.x][pos.y];
// Ifall det är en pjäs som står här och den inte är samma färg som oss, lägg till
try {
Piece p = pieces[pos.x][pos.y];
if (p.isWhite != this.isWhite) {
movable.add(pos);
}
@ -60,6 +61,9 @@ public class Pawn extends Piece {
@Override
protected boolean checkMove(Point pos, LinkedHashSet movable, Piece[][] pieces) {
if (pos.x < 0 || pos.x > 7 || pos.y < 0 || pos.y > 7) {
return false;
}
// Instead of checking index and null, try-catch
try {
// Ifall vi kollar utanför brädet kommer detta att faila
@ -82,6 +86,7 @@ public class Pawn extends Piece {
movable.add(pos);
} catch (IndexOutOfBoundsException ioobe) {
// This means that the player is at the edge
System.out.println(pos);
} catch (Exception e) {
// For good meassure
}

View File

@ -1,16 +1,14 @@
package schack;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashSet;
import javax.imageio.ImageIO;
public abstract class Piece extends Component {
public abstract class Piece {
public Point position;
public boolean isWhite;
@ -44,7 +42,7 @@ public abstract class Piece extends Component {
icon,
position.x * Board.SIZE_OF_TILE,
position.y * Board.SIZE_OF_TILE,
(ImageObserver) this
null
);
}