mirror of
https://github.com/lov3b/Schack.git
synced 2025-01-18 12:50:10 +01:00
Snyggare validMoves
This commit is contained in:
parent
fcdf8fff48
commit
dfcbaa9525
@ -4,7 +4,7 @@ import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
public class Bishop extends Piece {
|
||||
public class Bishop extends LongWalkers {
|
||||
|
||||
public Bishop(boolean isWhite, Point startingPosition) throws IOException {
|
||||
super(isWhite, startingPosition);
|
||||
@ -13,42 +13,11 @@ public class Bishop extends Piece {
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
// Upp vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Upp höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
// Ner höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
// Ner vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return movable;
|
||||
return getMoves(
|
||||
new int[][]{{-1,-1},{1, 1}, {-1, 1}, {1, -1}},
|
||||
pieces,
|
||||
isSelected
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
32
src/schack/LongWalkers.java
Normal file
32
src/schack/LongWalkers.java
Normal file
@ -0,0 +1,32 @@
|
||||
package schack;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
public abstract class LongWalkers extends PieceKnownIfMoved {
|
||||
|
||||
public LongWalkers(boolean isWhite, Point startingPosition) throws IOException {
|
||||
super(isWhite, startingPosition);
|
||||
}
|
||||
|
||||
LinkedHashSet<Point> getMoves(int[][] directions, Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
for (int[] xy : directions) {
|
||||
int loopX = this.position.x, loopY = this.position.y;
|
||||
while (loopX + xy[0] >= 0 && loopX + xy[0] <= 7 && loopY + xy[1] >= 0 && loopY + xy[1] <= 7) {
|
||||
loopX += xy[0];
|
||||
loopY += xy[1];
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return movable;
|
||||
}
|
||||
|
||||
}
|
@ -4,7 +4,7 @@ import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
public class Queen extends Piece {
|
||||
public class Queen extends LongWalkers {
|
||||
|
||||
public Queen(boolean isWhite, Point point) throws IOException {
|
||||
super(isWhite, point);
|
||||
@ -13,73 +13,10 @@ public class Queen extends Piece {
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
|
||||
// Vänster
|
||||
for (int loopX = this.position.x - 1; loopX >= 0; loopX--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Höger
|
||||
for (int loopX = this.position.x + 1; loopX <= 7; loopX++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Ner
|
||||
for (int loopY = this.position.y + 1; loopY <= 7; loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Upp
|
||||
for (int loopY = this.position.y - 1; loopY >= 0; loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Upp vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y - 1; loopX >= 0 && loopY >= 0; loopX--, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Upp höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y - 1; loopX <= 7 && loopY >= 0; loopX++, loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
// Ner höger
|
||||
for (int loopX = this.position.x + 1, loopY = this.position.y + 1; loopX <= 7 && loopY <= 7; loopX++, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
// Ner vänster
|
||||
for (int loopX = this.position.x - 1, loopY = this.position.y + 1; loopX >= 0 && loopY <= 7; loopX--, loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
return movable;
|
||||
return getMoves(
|
||||
new int[][]{{1, 0}, {-1, 0}, {0, 1}, {-1, -1}, {0, -1}, {1, 1}, {-1, 1}, {1, -1}},
|
||||
pieces,
|
||||
isSelected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
public class Rook extends PieceKnownIfMoved {
|
||||
public class Rook extends LongWalkers {
|
||||
|
||||
public Rook(boolean isWhite, Point startingPosition) throws IOException {
|
||||
super(isWhite, startingPosition);
|
||||
@ -13,43 +13,11 @@ public class Rook extends PieceKnownIfMoved {
|
||||
|
||||
@Override
|
||||
public LinkedHashSet<Point> validMoves(Piece[][] pieces, boolean isSelected) {
|
||||
LinkedHashSet<Point> movable = new LinkedHashSet<>();
|
||||
//Behöver skriva att om rookX = this.position.x så ska vi istället loopa igenom
|
||||
//int rookY = 0-this.position.y; rookY < 8-this.position.Y; rookY++
|
||||
//men jag är trög och har spenderat alldles förmycket tid på att vara trög :^)
|
||||
|
||||
// Vänster
|
||||
for (int loopX = this.position.x - 1; loopX >= 0; loopX--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Höger
|
||||
for (int loopX = this.position.x + 1; loopX <= 7; loopX++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(loopX, this.position.y), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Ner
|
||||
for (int loopY = this.position.y + 1; loopY <= 7; loopY++) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Upp
|
||||
for (int loopY = this.position.y - 1; loopY >= 0; loopY--) {
|
||||
boolean shouldBreak = addMovesIfCan(new Point(this.position.x, loopY), movable, pieces, isSelected);
|
||||
if (shouldBreak) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return movable;
|
||||
return getMoves(
|
||||
new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}},
|
||||
pieces,
|
||||
isSelected
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user